File: Article_Overlay.pm

package info (click to toggle)
rtfm 2.0.3-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 812 kB
  • ctags: 220
  • sloc: perl: 3,426; sh: 153; makefile: 144
file content (1286 lines) | stat: -rw-r--r-- 35,456 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
# BEGIN LICENSE BLOCK
#
#  Copyright (c) 2002-2003 Jesse Vincent <jesse@bestpractical.com>
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of version 2 of the GNU General Public License
#  as published by the Free Software Foundation.
#
#  A copy of that license should have arrived with this
#  software, but in any event can be snarfed from www.gnu.org.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
# END LICENSE BLOCK

use strict;

no warnings qw/redefine/;

use RT::FM;
use RT::FM::ArticleCollection;
use RT::FM::CustomFieldCollection;
use RT::FM::ClassCollection;
use RT::Links;
use RT::URI::fsck_com_rtfm;
use RT::FM::TransactionCollection;

# {{{ Create

=item Create PARAMHASH

Create takes a hash of values and creates a row in the database:

  varchar(200) 'Name'.
  varchar(200) 'Summary'.
  int(11) 'Content'.
  Class ID  'Class'

  A paramhash called  'CustomFields', which contains 
  arrays of values for each custom field you want to fill in.
  Arrays aRe ordered. 



=begin testing

use_ok(RT::FM::Article);
use_ok(RT::FM::Class);

my $user = RT::CurrentUser->new('root');

my $class = RT::FM::Class->new($user);


my ($id, $msg) = $class->Create(Name =>'ArticleTest');
ok ($id, $msg);



my $article = RT::FM::Article->new($user);
ok (UNIVERSAL::isa($article, 'RT::FM::Article'));
ok (UNIVERSAL::isa($article, 'RT::FM::Record'));
ok (UNIVERSAL::isa($article, 'RT::Record'));
ok (UNIVERSAL::isa($article, 'DBIx::SearchBuilder::Record') , "It's a searchbuilder record!");


($id, $msg) = $article->Create( Class => 'ArticleTest', Summary => "ArticleTest");
ok ($id, $msg);
$article->Load($id);
is ($article->Summary, 'ArticleTest', "The summary is set correct");
my $at = RT::FM::Article->new($RT::SystemUser);
$at->Load($id);
is ($at->id , $id);
is ($at->Summary, $article->Summary);


=end testing


=cut

sub Create {
    my $self = shift;
    my %args = ( Name         => '',
                 Summary      => '',
                 Class        => '0',
                 CustomFields => {},
                 Links        => {},
                 @_ );

    use Data::Dumper;
    $RT::Logger->crit(Dumper \%args);

    my $class = RT::FM::Class->new($RT::SystemUser);
    $class->Load( $args{'Class'} );
    unless ( $class->Id ) {
        return ( 0, $self->loc('Invalid Class') );
    }

    unless ( $class->CurrentUserHasRight('CreateArticle') ) {
        return ( 0, $self->loc("Permission Denied") );
    }

    return (undef,$self->loc('Name in use')) unless $self->ValidateName($args{'Name'});

    $RT::Handle->BeginTransaction();
    my ( $id, $msg ) = $self->SUPER::Create( Name    => $args{'Name'},
                                             Class   => $class->Id,
                                             Summary => $args{'Summary'}, );
    unless ($id) {
        $RT::Handle->Rollback();
        return ( undef, $msg );
    }

    # {{{ Add custom fields

    foreach my $key ( keys %args ) {
        next unless ( $key =~ /^CustomField-(.*)$/ );
        my $cf   = $1;
        my @vals =
          ref( $args{$key} ) eq 'ARRAY' ? @{ $args{$key} } : ( $args{$key} );
        foreach my $val (@vals) {

            my ( $cfid, $cfmsg ) = $self->_AddCustomFieldValue(
                                                          Field   => $1,
                                                          Content => $val,
                                                          RecordTransaction => 0
            );

            unless ($cfid) {
                $RT::Handle->Rollback();
                return ( undef, $cfmsg );
            }
        }

    }

    # }}}
    # {{{ Add relationships

    foreach my $type ( keys %args ) {
        next unless ( $type =~ /^(RefersTo-new|new-RefersTo)$/ );
        my @vals =
          ref( $args{$type} ) eq 'ARRAY' ? @{ $args{$type} } : ( $args{$type} );
        foreach my $val (@vals) {
            my ( $base, $target );
            if ( $type =~ /^new-(.*)$/ ) {
                $type   = $1;
                $base   = undef;
                $target = $val;
            }
            elsif ( $type =~ /^(.*)-new$/ ) {
                $type   = $1;
                $base   = $val;
                $target = undef;
            }

            my ( $linkid, $linkmsg ) = $self->AddLink( Type   => $type,
                                                       Target => $target,
                                                       Base   => $base,
                                                       RecordTransaction => 0 );

            unless ($linkid) {
                $RT::Handle->Rollback();
                return ( undef, $linkmsg );
            }
        }

    }

    # }}}

    # We override the URI lookup. the whole reason
    # we have a URI column is so that joins on the links table
    # aren't expensive and stupid
    $self->__Set( Field => 'URI', Value => $self->URI );

    $self->_NewTransaction( Type => 'Create' );

    $RT::Handle->Commit();

    return ( $id, $msg );
}

# }}}

# {{{ ValidateName

=head2 ValidateName NAME

Takes a string name. Returns true if that name isn't in use by another article

Empty names are permitted.


=begin testing

my  $a1 = RT::FM::Article->new($RT::SystemUser);
my ($id, $msg)  = $a1->Create(Class => 1, Name => 'ValidateNameTest');
ok ($id, $msg);



my  $a2 = RT::FM::Article->new($RT::SystemUser);
($id, $msg)  = $a2->Create(Class => 1, Name => 'ValidateNameTest');
ok (!$id, $msg);

my  $a3 = RT::FM::Article->new($RT::SystemUser);
($id, $msg)  = $a3->Create(Class => 1, Name => 'ValidateNameTest2');
ok ($id, $msg);
($id, $msg) =$a3->SetName('ValidateNameTest');

ok (!$id, $msg);

($id, $msg) =$a3->SetName('ValidateNametest2');

ok ($id, $msg);




=end testing


=cut

sub ValidateName {
    my $self = shift;
    my $name = shift;

    if (!$name) {
        return(1);
    }

    my $temp = RT::FM::Article->new($RT::SystemUser);
    $temp->LoadByCols(Name => $name);
    if ($temp->id && $temp->id != $self->id) {
        return(undef);
    }

    return(1);

}

# }}}

# {{{ Delete 

=head2 Delete

Delete all its transactions
Delete all its custom field values
Delete all its relationships
Delete this article.

=begin testing

my $newart = RT::FM::Article->new($RT::SystemUser);
$newart->Create(Name => 'DeleteTest', Class => '1');
my $id = $newart->Id;

ok($id, "New article has an id");


my $article = RT::FM::Article->new($RT::SystemUser);
$article->Load($id);
ok ($article->Id, "Found the article");
my ($val, $msg) = $article->Delete();
ok ($val, "Article Deleted: $msg");

my $a2 = RT::FM::Article->new($RT::SystemUser);
$a2->Load($id);
ok (!$a2->Id, "Did not find the article");


=end testing

=cut

sub Delete {
    my $self = shift;
    unless ( $self->CurrentUserHasRight('ModifyArticle') ) {
        return ( 0, $self->loc("Permission Denied") );
    }

    $RT::Handle->BeginTransaction();
    my $linksto   = $self->_Links( Field => 'Target' );
    my $linksfrom = $self->_Links( Field => 'Base' );
    my $cfvalues = $self->CustomFieldValues;
    my $txns     = $self->Transactions;

    while ( my $item = $linksto->Next ) {
        my ( $val, $msg ) = $item->Delete();
        unless ($val) {
            $RT::Logger->crit( ref($item) . ": $msg" );
            $RT::Handle->Rollback();
            return ( 0, $self->loc('Internal Error') );
        }
    }

    while ( my $item = $linksfrom->Next ) {
        my ( $val, $msg ) = $item->Delete();
        unless ($val) {
            $RT::Logger->crit( ref($item) . ": $msg" );
            $RT::Handle->Rollback();
            return ( 0, $self->loc('Internal Error') );
        }
    }
    while ( my $item = $txns->Next ) {
        my ( $val, $msg ) = $item->Delete();
        unless ($val) {
            $RT::Logger->crit( ref($item) . ": $msg" );
            $RT::Handle->Rollback();
            return ( 0, $self->loc('Internal Error') );
        }
    }

    while ( my $item = $cfvalues->Next ) {
        my ( $val, $msg ) = $item->Delete();
        unless ($val) {
            $RT::Logger->crit( ref($item) . ": $msg" );
            $RT::Handle->Rollback();
            return ( 0, $self->loc('Internal Error') );
        }
    }

    $self->SUPER::Delete();
    $RT::Handle->Commit();
    return ( 1, $self->loc('Article Deleted') );

}

# }}}

# {{{ Children

=item Children

Returns an RT::FM::ArticleCollection object which contains
all articles which have this article as their parent.  This 
routine will not recurse and will not find grandchildren, great-grandchildren, uncles, aunts, nephews or any other such thing.  

=cut

sub Children {
    my $self = shift;
    my $kids = new RT::FM::ArticleCollection( $self->CurrentUser );

    unless ( $self->CurrentUserHasRight('ShowArticle') ) {
        $kids->LimitToParent( $self->Id );
    }
    return ($kids);
}

# }}}

# {{{ sub AddLink

=head2 AddLink

Takes a paramhash of Type and one of Base or Target. Adds that link to this tick
et.

=begin testing

=end testing



=cut

sub AddLink {
    my $self = shift;
    my %args = ( Target            => '',
                 Base              => '',
                 Type              => 'RefersTo',
                 RecordTransaction => 1,
                 @_ );

    unless ( $self->CurrentUserHasRight('ModifyArticle') ) {
        return ( 0, $self->loc("Permission Denied") );
    }

    my ( $link_type, $link_pointer );

    if ( $args{'Base'} and $args{'Target'} ) {
        $RT::Logger->debug(
             "$self tried to delete a link. both base and target were specified"
        );
        return ( 0, $self->loc("Can't specifiy both base and target") );
    }
    elsif ( $args{'Base'} ) {
        $args{'Target'} = $self->URI();
        $link_type      = "ReferredToBy";
        $link_pointer   = $args{'Base'};
    }
    elsif ( $args{'Target'} ) {
        $args{'Base'} = $self->URI();
        $link_type    = "RefersTo";
        $link_pointer = $args{'Target'};
    }
    else {
        return ( 0, $self->loc('Either base or target must be specified') );
    }

    # {{{ We don't want references to ourself
    if ( $args{'Base'} eq $args{'Target'} ) {
        $RT::Logger->debug(
                 "Trying to link " . $args{'Base'} . " to " . $args{'Target'} );
        return ( 0, $self->loc("Can't link a ticket to itself") );
    }

    # }}}

    # If the base isn't a URI, make it a URI.
    # If the target isn't a URI, make it a URI.

    # {{{ Check if the link already exists - we don't want duplicates
    my $old_link = new RT::Link( $self->CurrentUser );
    $old_link->LoadByParams( Base   => $args{'Base'},
                             Type   => $args{'Type'},
                             Target => $args{'Target'} );
    if ( $old_link->Id ) {
        $RT::Logger->debug("$self Somebody tried to duplicate a link");
        return ( $old_link->id, $self->loc("Link already exists"), 0 );
    }

    # }}}

    # Storing the link in the DB.
    my $link = RT::Link->new( $self->CurrentUser );
    my ($linkid) = $link->Create( Target => $args{Target},
                                  Base   => $args{Base},
                                  Type   => $args{Type} );

    unless ($linkid) {
        return ( 0, $self->loc("Link could not be created") );
    }

    my $TransString = "$args{'Base'} $args{Type} $args{'Target'}";

    # Don't write the transaction if we're doing this on create
    if ( $args{'RecordTransaction'} ) {

        #Write the transaction
        my ( $Trans, $Msg, $TransObj ) = $self->_NewTransaction(
                                                     Type       => 'Link',
                                                     Field      => $link_type,
                                                     NewContent => $link_pointer
        );
        return ( $Trans, $self->loc( "Link created ([_1])", $TransString ) );
    }
    else {
        return ( 1, $self->loc( "Link created ([_1])", $TransString ) );
    }
}

# }}}

# {{{ Links

=head2 Links

The following routines deal with links and relationships between articles and
RT tickets.


=begin testing

my ($id, $msg);

$RT::Handle->SimpleQuery("DELETE FROM Links");

my $article_a = RT::FM::Article->new($RT::SystemUser);
($id, $msg) = $article_a->Create( Class => 'ArticleTest', Summary => "ArticleTestlink1");
ok($id,$msg);

my $article_b = RT::FM::Article->new($RT::SystemUser);
($id, $msg) = $article_b->Create( Class => 'ArticleTest', Summary => "ArticleTestlink2");
ok($id,$msg);

# Create a link between two articles
($id, $msg) = $article_a->AddLink( Type => 'RefersTo', Target => $article_b->URI);
ok($id,$msg);

# Make sure that Article B's "ReferredToBy" links object refers to to this article"
my $refers_to_b = $article_b->ReferredToBy;
ok($refers_to_b->Count == 1, "Found one thing referring to b");
my $first = $refers_to_b->First;
ok ($first->isa(RT::Link), "IT's an RT link - ref ".ref($first) );
ok ($first->TargetObj->Id == $article_b->Id, "Its target is B");

ok($refers_to_b->First->BaseObj->isa('RT::FM::Article'), "Yep. its an article");


# Make sure that Article A's "RefersTo" links object refers to this article"
my $referred_To_by_a = $article_a->RefersTo;
ok($referred_To_by_a->Count == 1, "Found one thing referring to b ".$referred_To_by_a->Count. "-".$referred_To_by_a->First->id . " - ".$referred_To_by_a->Last->id);
my $first = $referred_To_by_a->First;
ok ($first->isa(RT::Link), "IT's an RT link - ref ".ref($first) );
ok ($first->TargetObj->Id == $article_b->Id, "Its target is B - " . $first->TargetObj->Id);
ok ($first->BaseObj->Id == $article_a->Id, "Its base is A");

ok($referred_To_by_a->First->BaseObj->isa('RT::FM::Article'), "Yep. its an article");

# Delete the link
($id, $msg) = $article_a->DeleteLink(Type => 'RefersTo', Target => $article_b->URI);
ok($id,$msg);


# Create an Article A RefersTo Ticket 1 from the RTFM side
use RT::Ticket;


my $tick = RT::Ticket->new($RT::SystemUser);
$tick->Create(Subject => "Article link test ", Queue => 'General');
$tick->Load($tick->Id);
ok ($tick->Id, "Found ticket ".$tick->id);
($id, $msg) = $article_a->AddLink(Type => 'RefersTo', Target => $tick->URI);
ok($id,$msg);

# Find all tickets whhich refer to Article A

use RT::Tickets;
use RT::Links;

my $tix = RT::Tickets->new($RT::SystemUser);
ok ($tix, "Got an RT::Tickets object");
ok ($tix->LimitReferredToBy($article_a->URI)); 
ok ($tix->Count == 1, "Found one ticket linked to that article");
ok ($tix->First->Id == $tick->id, "It's even the right one");



# Find all articles which refer to Ticket 1
use RT::FM::ArticleCollection;

my $articles = RT::FM::ArticleCollection->new($RT::SystemUser);
ok($articles->isa('RT::FM::ArticleCollection'), "Created an article collection");
ok($articles->isa('RT::FM::SearchBuilder'), "Created an article collection");
ok($articles->isa('RT::SearchBuilder'), "Created an article collection");
ok($articles->isa('DBIx::SearchBuilder'), "Created an article collection");
ok($tick->URI, "The ticket does still have a URI");
$articles->LimitRefersTo($tick->URI);

is($articles->Count(), 1);
is ($articles->First->Id, $article_a->Id);
is ($articles->First->URI, $article_a->URI);



# Find all things which refer to ticket 1 using the RT API.

my $tix2 = RT::Links->new($RT::SystemUser);
ok ($tix2->isa('RT::Links'));
ok($tix2->LimitRefersTo($tick->URI));
ok ($tix2->Count == 1);
is ($tix2->First->BaseObj->URI ,$article_a->URI);



# Delete the link from the RT side.
my $t2 = RT::Ticket->new($RT::SystemUser);
$t2->Load($tick->Id);
($id, $msg)= $t2->DeleteLink( Base => $article_a->URI, Type => 'RefersTo');
ok ($id, $msg . " - $id - $msg");

# it's actually deleted
my $tix3 = RT::Links->new($RT::SystemUser);
$tix3->LimitReferredToBy($tick->URI);
ok ($tix3->Count == 0);

# Recreate the link from teh RT site
($id, $msg) = $t2->AddLink( Base => $article_a->URI, Type => 'RefersTo');
ok ($id, $msg);

# Find all tickets whhich refer to Article A

# Find all articles which refer to Ticket 1


=end testing



=cut

# {{{ sub DeleteLink

=head2 DeleteLink

Delete a link. takes a paramhash of Base, Target and Type.
Either Base or Target must be null. The null value will 
be replaced with this ticket\'s id

=cut 

sub DeleteLink {
    my $self = shift;
    my %args = ( Base   => undef,
                 Target => undef,
                 Type   => undef,
                 @_ );

    #check acls
    unless ( $self->CurrentUserHasRight('ModifyArticle') ) {
        return ( 0, $self->loc('Permission Denied') );
    }

    #we want one of base and target. we don't care which
    #but we only want _one_
    my ( $link_type, $link_pointer );
    if ( $args{'Base'} and $args{'Target'} ) {
        $RT::Logger->debug("$self ->_DeleteLink. got both Base and Target\n");
        return ( 0, $self->loc("Can't specifiy both base and target") );
    }
    elsif ( $args{'Base'} ) {
        $args{'Target'} = $self->URI();
        $link_type      = "ReferredToBy";
        $link_pointer   = $args{'Base'};
    }
    elsif ( $args{'Target'} ) {
        $args{'Base'} = $self->URI();
        $link_type    = "RefersTo";
        $link_pointer = $args{'Target'};
    }
    else {
        $RT::Logger->debug("$self: Base or Target must be specified\n");
        return ( 0, $self->loc('Either base or target must be specified') );
    }

    my $link = new RT::Link( $self->CurrentUser );
    $RT::Logger->debug( "Trying to load link: "
                        . $args{'Base'} . " "
                        . $args{'Type'} . " "
                        . $args{'Target'}
                        . "\n" );
    $link->LoadByParams( Base   => $args{'Base'},
                         Type   => $args{'Type'},
                         Target => $args{'Target'} );

    #it's a real link.
    if ( $link->id ) {
        my $linkid = $link->Id;
        $RT::Logger->debug( "We're going to delete link " . $link->id . "\n" );
        $link->Delete();

        my $TransString =
          "Ticket $args{'Base'} no longer $args{Type} ticket $args{'Target'}.";
        my ( $Trans, $Msg, $TransObj ) = $self->_NewTransaction(
                                                     Type       => 'Link',
                                                     Field      => $link_type,
                                                     OldContent => $link_pointer
        );

        return ( $linkid,
                 $self->loc( "Link deleted ([_1])",
                             $args{'Base'} . " "
                               . $args{'Type'} . " "
                               . $args{'Target'} ) );
    }

    #if it's not a link we can find
    else {
        $RT::Logger->debug("Couldn't find that link\n");
        return ( 0, $self->loc("Link not found") );
    }
}

# }}}

# {{{ sub RefersTo

=head2 RefersTo

Return an RT::Links object which contains pointers to all the things 
which this article refers to

=cut

sub RefersTo {
    my $self = shift;
    return $self->_Links( Field => 'Base', Type => 'RefersTo' );

}

# }}}

# {{{ sub ReferredToBy

=head2 ReferredToBy

Return an RT::Links object which contains pointers to all the things 
which refer to this article.

=cut

sub ReferredToBy {
    my $self = shift;
    return $self->_Links( Field => 'Target', Type => 'RefersTo' );

}

# }}}

# {{{ sub _Links

sub _Links {
    my $self = shift;
    my %args = ( Field => undef,
                 Type  => undef,
                 @_ );

    my $search = new RT::Links( $self->CurrentUser );
    if ( $self->CurrentUserHasRight('ShowArticle') ) {

        $search->Limit( FIELD => $args{'Field'}, VALUE => $self->URI );
        $search->Limit( FIELD => 'Type', VALUE => $args{'Type'} )
          if ( $args{'Type'} );
    }
    return ($search);
}

# }}}

# }}}

# {{{ sub URI

=head2 URI

Returns this article's URI


=begin testing

my ($id,$msg);
my $art = RT::FM::Article->new($RT::SystemUser);
($id, $msg) = $art->Create (Class => 'ArticleTest');
ok ($id,$msg);

ok($art->URI);
ok($art->__Value('URI') eq $art->URI, "The uri in the db is set correctly");


=end testing

         
=cut

sub URI {
    my $self = shift;

    unless ( $self->CurrentUserHasRight('ShowArticle') ) {
        return $self->loc("Permission Denied");
    }

    my $uri = RT::URI::fsck_com_rtfm->new( $self->CurrentUser );
    return ( $uri->URIForObject($self) );
}

# }}}

# {{{ sub URIObj

=head2 URIObj

Returns this article's URI


=begin testing

my ($id,$msg);
my $art = RT::FM::Article->new($RT::SystemUser);
($id, $msg) = $art->Create (Class => 'ArticleTest');
ok ($id,$msg);

ok($art->URIObj);
ok($art->__Value('URI') eq $art->URIObj->URI, "The uri in the db is set correctly");


=end testing

         
=cut

sub URIObj {
    my $self = shift;
    my $uri  = RT::URI->new( $self->CurrentUser );
    if ( $self->CurrentUserHasRight('ShowArticle') ) {
        $uri->FromObject($self);
    }

    return ($uri);
}

# }}}

# {{{ Custom Fields

=head2 Custom Fields

The following routines deal with custom fields and their values

=cut

# {{{ CustomFieldValues

=item CustomFieldValues [ CUSTOMFIELD_ID ]

Returns an RT::FM::CustomFieldObjectValueCollection object containing
the values of CustomField CUSTOMFIELD_ID for this Article. if no CUSTOMFIELD_ID is specified, returns ALL customfield values.


=cut

sub CustomFieldValues {
    my $self = shift;
    my $customfield;
    $customfield = shift if (@_);

    my $cfovc = new RT::FM::ArticleCFValueCollection( $self->CurrentUser );
    if ( $self->CurrentUserHasRight('ShowArticle') ) {
        $cfovc->LimitToArticle( $self->Id );
        $cfovc->LimitToCustomField($customfield) if ($customfield);
    }
    return ($cfovc);
}

# }}}

# {{{ AddCustomFieldValue

=item AddCustomFieldValue { Field => FIELD, Content => VALUE }

VALUE can either be a CustomFieldValue object or a string.
FIELD can be a CustomField object OR a CustomField ID.


Adds VALUE as a value of CustomField FIELD.  If this is a single-value custom field,
deletes the old value. 
If VALUE isn't a valid value for the custom field, returns 
(0, 'Error message' ) otherwise, returns (1, 'Success Message')


=begin testing


my $art = RT::FM::Article->new($RT::SystemUser);
my ($id,$msg) =$art->Create(Class => 'ArticleTest');
ok ($id, $msg);

my $cf = RT::FM::CustomField->new($RT::SystemUser);
($id, $msg) =$cf->Create(Name => "Test", Type => "SelectMultiple");
ok ($id, $msg);
($id, $msg) = $cf->AddValue(Name => "Test1");
ok ($id, $msg);

($id, $msg) = $cf->AddValue(Name => "Testy");
ok ($id, $msg);

($id, $msg )= $cf->AddToClass('ArticleTest');
ok ($id, $msg);

$id = $cf->ValidForClass('ArticleTest');
ok ($id, "This cf is good for the class 'ArticleTest'");
$id = $cf->ValidForClass($art->ClassObj->Name);
ok ($id, "This cf is good for the class ".$art->ClassObj->Name);

($id, $msg) = $art->AddCustomFieldValue( Field => "Test", Content => "Test1");
ok ($id, $msg);
($id, $msg) = $art->AddCustomFieldValue( Field => "Test", Content => "Test1");
ok (!$id, "Can add a duplicate value to a custom field that's a 'select multiple' - $msg");
($id, $msg) = $art->AddCustomFieldValue( Field => "Test", Content => "Testy");
ok ($id, $msg);


($id, $msg) = $art->AddCustomFieldValue( Field => "Test", Content => "TestFroboz");
ok (!$id, "Can't add a non-existent value to a custom field that's a 'select multiple' - $msg");

=end testing




=cut

sub AddCustomFieldValue {
    my $self = shift;
    unless ( $self->CurrentUserHasRight('ModifyArticle') ) {
        return ( 0, $self->loc("Permission Denied") );
    }
    $self->_AddCustomFieldValue(@_);
}

sub _AddCustomFieldValue {
    my $self = shift;
    my %args = ( Field             => undef,
                 Content           => undef,
                 RecordTransaction => 1,
                 @_ );

    # {{{ Get the custom field we're talking about

    my $cf = RT::FM::CustomField->new( $self->CurrentUser );
    if ( UNIVERSAL::isa( $args{'Field'}, "RT::FM::CustomField" ) ) {
        $cf->Load( $args{'Field'}->id );
    }
    else {
        $cf->Load( $args{'Field'} );
    }

    unless ( $cf->ValidForClass( $self->__Value('Class') ) ) {
        return ( 0,
                 $self->loc( "Custom field [_1] not valid for that article",
                             $args{'Field'} ) );
    }

    unless ( $cf->Id ) {
        return ( 0,
                 $self->loc( "Custom field [_1] not found", $args{'Field'} ) );
    }

    # }}}

    # Load up a ArticleCFValueCollection object for this custom field
    my $values = $self->CustomFieldValues( $cf->Id );

    # If the custom field only accepts a single value, delete the existing
    # value and record a "changed from foo to bar" transaction
    if ( $cf->SingleValue ) {

# {{{ We need to whack any old values here.  In most cases, the custom field should
# only have one value to delete.  In the pathalogical case, this custom field
# used to be a multiple and we have many values to whack....
        my $cf_values = $values->Count;

        if ( $cf_values > 1 ) {
            my $i = 0;   #We want to delete all but the last one, so we can then
                 # execute the same code to "change" the value from old to new
            while ( my $value = $values->Next ) {
                $i++;
                if ( $i < $cf_values ) {
                    my $old_value = $value->Content;
                    my ( $val, $msg ) = $cf->DeleteValueForArticle(
                                                      Article => $self->Id,
                                                      Content => $value->Content
                    );
                    unless ($val) {
                        return ( 0, $msg );
                    }
                    my ( $TransactionId, $Msg, $TransactionObj ) =
                      $self->_NewTransaction( Type       => 'Custom',
                                              Field      => $cf->Id,
                                              OldContent => $old_value );
                }
            }
        }

        # }}}

        # {{{ Add a new custom field value
        my $value = $cf->ValuesForArticle( $self->Id )->First;
        my $old_value;
        if ( $cf_values == 1 ) {
            $old_value = $value->Content();
        }

        my ( $new_value_id, $value_msg ) = $cf->AddValueForArticle(
                                                     Article => $self->Id,
                                                     Content => $args{'Content'}
        );

        unless ($new_value_id) {
            return (
                  0,
                  $self->loc(
                      "Could not add new custom field value for Article. [_1] ",
                      $value_msg ) );
        }

        # }}}

        # {{{ Kill the old value
        my $new_value = RT::FM::ArticleCFValue->new( $self->CurrentUser );
        $new_value->Load($new_value_id);

        # now that adding the new value was successful, delete the old one
        if ( defined $old_value ) {
            my ( $val, $msg ) = $cf->DeleteValueForArticle(Article => $self->Id,
                                                           Content => $old_value
            );
            unless ($val) {
                return ( 0, $msg );
            }

        }

        # }}}

        # {{{ Record the "Changed" transaction
        if ( $args{'RecordTransaction'} ) {
            my ( $TransactionId, $Msg, $TransactionObj ) =
              $self->_NewTransaction( Type       => 'Custom',
                                      Field      => $cf->Id,
                                      OldContent => $old_value,
                                      NewContent => $new_value->Content );
        }
        return ( 1,
                 $self->loc( "Custom field value changed from '[_1]' to '[_2]'",
                             $old_value, $new_value->Content ) );

        # }}}
    }

    # otherwise, just add a new value and record "new value added"
    else {

        # {{{ Add a custom field value
        my ( $new_value_id, $new_value_msg ) = $cf->AddValueForArticle(
                                                     Article => $self->Id,
                                                     Content => $args{'Content'}
        );

        unless ($new_value_id) {
            return (
                   0,
                   $self->loc(
                       "Could not add new custom field value for Article. [_1]",
                       $new_value_msg ) );
        }

        # }}}

        # {{{ Record a tranaction
        if ( $args{'RecordTransaction'} ) {
            my ( $TransactionId, $Msg, $TransactionObj ) =
              $self->_NewTransaction( Type       => 'Custom',
                                      Field      => $cf->Id,
                                      NewContent => $args{'Content'} );
            unless ($TransactionId) {
                return ( 0,
                         $self->loc( "Couldn't create a transaction: [_1]", $Msg
                         ) );
            }
        }
        return ( $new_value_id,
                 $self->loc( "[_1] added as a value for [_2]",
                             $args{'Content'},
                             $cf->Name ) );

        # }}}
    }

}

# }}}

# {{{ DeleteCustomFieldValue

=item DeleteCustomFieldValue { CustomField => undef, Content => undef } 
  
  Takes a paramhash. Deletes the Keyword denoted by the I<Keyword> parameter from this
  ticket's object keywords.

=cut

sub DeleteCustomFieldValue {
    my $self = shift;
    my %args = ( Content => undef,
                 Field   => undef,
                 @_ );

    unless ( $self->CurrentUserHasRight('ModifyArticle') ) {
        return ( 0, $self->loc("Permission Denied") );
    }

    my $cf = RT::FM::CustomField->new($RT::SystemUser);
    $cf->Load( $args{'Field'} );

    #Load up the ObjectKeyword we\'re talking about

    my $values = RT::FM::ArticleCFValueCollection($self->CurrentUser);
    $values->LimitToArticle($self->id);
    $values->LimitToCustomField($self->id);
    my $CFObjectValue = $values->HasEntryWithContent($args{'Content'}); 

    #if we can\'t find it, bail
    unless ($CFObjectValue ) {
        return (
            undef,
            $self->loc(
"Couldn't load custom field [_1] value [_2] while trying to delete it.",
                $cf->Id,
                $args{'Content'} ) );
    }

    #record transaction here.
    $RT::Handle->BeginTransaction();
    my ( $TransactionId, $Msg, $TransactionObj ) = $self->_NewTransaction(
                                           Type  => 'Custom',
                                           Field => $CFObjectValue->CustomField,
                                           OldContent => $args{'Content'} );
    unless ($TransactionId) {
        $RT::Handle->Rollback();
        $RT::Logger->crit("Somehow, couldn't add a transaction. aiee");
        return ( undef, $self->loc("Internal error") );
    }
    my $del = $CFObjectValue->Delete();
    unless ($del) {

        $RT::Handle->Rollback();
        return ( undef,
                 $self->loc( "Couldn't delete  custom field [_1] value [_2].",
                             $cf->Id, $args{'Content'} ) );
    }

    $RT::Handle->Commit();
    return ( 1,
             $self->loc( "Value [_1] deleted from custom field [_2].",
                         $CFObjectValue->Content,
                         $cf->Name ) );

}

# }}}

# }}}

# {{{ CurrentUserHasRight

=head2 CurrentUserHasRight

Returns true if the current user has the right for this article, for the whole system or for this article's class

=cut

sub CurrentUserHasRight {
    my $self  = shift;
    my $right = shift;

    return ( $self->CurrentUser->HasRight(
                            Right        => $right,
                            Object       => $self,
                            EquivObjects => [ $RT::FM::System, $self->ClassObj ]
             ) );

}

# }}}

# {{{ _NewTransaction

=head2 _NewTransaction PARAMHASH


Takes a hash of:

Type
Field
OldContent
NewContent
Data 


=cut

sub _NewTransaction {
    my $self = shift;
    my %args = ( Type       => undef,
                 Field      => '',
                 OldContent => '',
                 NewContent => '',
                 ChangeLog  => '',
                 @_ );

    my $trans = RT::FM::Transaction->new( $self->CurrentUser );
    $trans->Create( Article    => $self->Id,
                    Type       => $args{'Type'},
                    Field      => $args{'Field'},
                    OldContent => $args{'OldContent'},
                    NewContent => $args{'NewContent'},
                    ChangeLog  => $args{'ChangeLog'} );

    #something bad happened;
    unless ( $trans->Id ) {
        $RT::Logger->crit(
                       $self . " could not create a transaction for " . %args );
        return ( undef, $self->loc("Internal error"), $trans );
    }

    return ( $trans->id, $self->loc("Transaction recorded"), $trans );
}

# }}}

=head2 Transactions

Returns an RT::FM::TransactionCollection pre-loaded with all the transactions for tthis Article. If the current user doesn't have the right to 'ShowArticleHistory',
this object is an _empty_ TransactionCollection

=cut

sub Transactions {
    my $self         = shift;
    my $transactions = RT::FM::TransactionCollection->new( $self->CurrentUser );

    if ( $self->CurrentUserHasRight('ShowArticleHistory') ) {
        $transactions->Limit( FIELD    => 'Article',
                              OPERATOR => '=',
                              VALUE    => $self->Id );
    }

    return ($transactions);

}

# {{{ _Set

=head2 _Set { Field => undef, Value => undef

Internal helper method to record a transaction as we update some core field of the article


=begin testing

my $art = RT::FM::Article->new($RT::SystemUser);
$art->Load(1);
ok ($art->Id == 1, "Loaded article 1");
my $s =$art->Summary;
my ($val, $msg) = $art->SetSummary("testFoo");
ok ($val, $msg);
ok ($art->Summary eq 'testFoo', "The Summary was set to foo");
my $t = $art->Transactions();
my $trans = $t->Last;
ok ($trans->Type eq 'Core', "It's a core transaction");
ok ($trans->Field eq 'Summary', "it's about setting the Summary");
ok ($trans->NewContent eq 'testFoo', "The new content is 'foo'");
ok ($trans->OldContent, "There was some old value");


=end testing

=cut

sub _Set {
    my $self = shift;
    my %args = ( Field => undef,
                 Value => undef,
                 @_ );

    unless ( $self->CurrentUserHasRight('ModifyArticle') ) {
        return ( 0, $self->loc("Permission Denied") );
    }

    $self->_NewTransaction( Type       => 'Core',
                            Field      => $args{'Field'},
                            NewContent => $args{'Value'},
                            OldContent => $self->__Value( $args{'Field'} ) );

    return ( $self->SUPER::_Set(%args) );

}

=head2 _Value PARAM

Return "PARAM" for this object. if the current user doesn't have rights, returns undef

=cut

sub _Value {
    my $self = shift;
    my $arg  = shift;
    unless (    ( $arg eq 'Class' )
             || ( $self->CurrentUserHasRight('ShowArticle') ) ) {
        return (undef);
    }
    return $self->SUPER::_Value($arg);
}

# }}}

1;