rt 4.0.23
[freeside.git] / rt / lib / RT / Articles.pm
1 # BEGIN BPS TAGGED BLOCK {{{
2 #
3 # COPYRIGHT:
4 #
5 # This software is Copyright (c) 1996-2015 Best Practical Solutions, LLC
6 #                                          <sales@bestpractical.com>
7 #
8 # (Except where explicitly superseded by other copyright notices)
9 #
10 #
11 # LICENSE:
12 #
13 # This work is made available to you under the terms of Version 2 of
14 # the GNU General Public License. A copy of that license should have
15 # been provided with this software, but in any event can be snarfed
16 # from www.gnu.org.
17 #
18 # This work is distributed in the hope that it will be useful, but
19 # WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21 # General Public License for more details.
22 #
23 # You should have received a copy of the GNU General Public License
24 # along with this program; if not, write to the Free Software
25 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
26 # 02110-1301 or visit their web page on the internet at
27 # http://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
28 #
29 #
30 # CONTRIBUTION SUBMISSION POLICY:
31 #
32 # (The following paragraph is not intended to limit the rights granted
33 # to you to modify and distribute this software under the terms of
34 # the GNU General Public License and is only of importance to you if
35 # you choose to contribute your changes and enhancements to the
36 # community by submitting them to Best Practical Solutions, LLC.)
37 #
38 # By intentionally submitting any modifications, corrections or
39 # derivatives to this work, or any other work intended for use with
40 # Request Tracker, to Best Practical Solutions, LLC, you confirm that
41 # you are the copyright holder for those contributions and you grant
42 # Best Practical Solutions,  LLC a nonexclusive, worldwide, irrevocable,
43 # royalty-free, perpetual, license to use, copy, create derivative
44 # works based on those contributions, and sublicense and distribute
45 # those contributions and any derivatives thereof.
46 #
47 # END BPS TAGGED BLOCK }}}
48
49 use strict;
50 use warnings;
51
52 package RT::Articles;
53
54 use base 'RT::SearchBuilder';
55
56 sub Table {'Articles'}
57
58 sub _Init {
59     my $self = shift;
60     $self->OrderByCols(
61         { FIELD => 'SortOrder', ORDER => 'ASC' },
62         { FIELD => 'Name',      ORDER => 'ASC' },
63     );
64     return $self->SUPER::_Init( @_ );
65 }
66
67 =head2 AddRecord
68
69 Overrides the collection to ensure that only Articles the user can see
70 are returned.
71
72 =cut
73
74 sub AddRecord {
75     my $self = shift;
76     my ($record) = @_;
77
78     return unless $record->CurrentUserHasRight('ShowArticle');
79     return $self->SUPER::AddRecord( $record );
80 }
81
82 =head2 Limit { FIELD  => undef, OPERATOR => '=', VALUE => 'undef'} 
83
84 Limit the result set. See DBIx::SearchBuilder docs
85 In addition to the "normal" stuff, value can be an array.
86
87 =cut
88
89 sub Limit {
90     my $self = shift;
91     my %ARGS = (
92         OPERATOR => '=',
93         @_
94     );
95
96     if ( ref( $ARGS{'VALUE'} ) ) {
97         my @values = $ARGS{'VALUE'};
98         delete $ARGS{'VALUE'};
99         foreach my $v (@values) {
100             $self->SUPER::Limit( %ARGS, VALUE => $v );
101         }
102     }
103     else {
104         $self->SUPER::Limit(%ARGS);
105     }
106 }
107
108 =head2 LimitName  { OPERATOR => 'LIKE', VALUE => undef } 
109
110 Find all articles with Name fields which satisfy OPERATOR for VALUE
111
112 =cut
113
114 sub LimitName {
115     my $self = shift;
116
117     my %args = (
118         FIELD         => 'Name',
119         OPERATOR      => 'LIKE',
120         CASESENSITIVE => 0,
121         VALUE         => undef,
122         @_
123     );
124
125     $self->Limit(%args);
126
127 }
128
129 =head2 LimitSummary  { OPERATOR => 'LIKE', VALUE => undef } 
130
131 Find all articles with summary fields which satisfy OPERATOR for VALUE
132
133 =cut
134
135 sub LimitSummary {
136     my $self = shift;
137
138     my %args = (
139         FIELD         => 'Summary',
140         OPERATOR      => 'LIKE',
141         CASESENSITIVE => 0,
142         VALUE         => undef,
143         @_
144     );
145
146     $self->Limit(%args);
147
148 }
149
150 sub LimitCreated {
151     my $self = shift;
152     my %args = (
153         FIELD    => 'Created',
154         OPERATOR => undef,
155         VALUE    => undef,
156         @_
157     );
158
159     $self->Limit(%args);
160
161 }
162
163 sub LimitCreatedBy {
164     my $self = shift;
165     my %args = (
166         FIELD    => 'CreatedBy',
167         OPERATOR => '=',
168         VALUE    => undef,
169         @_
170     );
171
172     $self->Limit(%args);
173
174 }
175
176 sub LimitUpdated {
177
178     my $self = shift;
179     my %args = (
180         FIELD    => 'Updated',
181         OPERATOR => undef,
182         VALUE    => undef,
183         @_
184     );
185
186     $self->Limit(%args);
187
188 }
189
190 sub LimitUpdatedBy {
191     my $self = shift;
192     my %args = (
193         FIELD    => 'UpdatedBy',
194         OPERATOR => '=',
195         VALUE    => undef,
196         @_
197     );
198
199     $self->Limit(%args);
200
201 }
202
203 # {{{ LimitToParent ID
204
205 =head2 LimitToParent ID
206
207 Limit the returned set of articles to articles which are children
208 of article ID.
209 This does not recurse.
210
211 =cut
212
213 sub LimitToParent {
214     my $self   = shift;
215     my $parent = shift;
216     $self->Limit(
217         FIELD    => 'Parent',
218         OPERATOR => '=',
219         VALUE    => $parent
220     );
221
222 }
223
224 # }}}
225 # {{{ LimitCustomField
226
227 =head2 LimitCustomField HASH
228
229 Limit the result set to articles which have or do not have the custom field 
230 value listed, using a left join to catch things where no rows match.
231
232 HASH needs the following fields: 
233    FIELD (A custom field id) or undef for any custom field
234    ENTRYAGGREGATOR => (AND, OR)
235    OPERATOR ('=', 'LIKE', '!=', 'NOT LIKE')
236    VALUE ( a single scalar value or a list of possible values to be concatenated with ENTRYAGGREGATOR)
237
238 The subclause that the LIMIT statement(s) should be done in can also
239 be passed in with a SUBCLAUSE parameter.
240
241 =cut
242
243 sub LimitCustomField {
244     my $self = shift;
245     my %args = (
246         FIELD           => undef,
247         ENTRYAGGREGATOR => 'OR',
248         OPERATOR        => '=',
249         QUOTEVALUE      => 1,
250         VALUE           => undef,
251         SUBCLAUSE       => undef,
252         @_
253     );
254
255     my $value = $args{'VALUE'};
256     # XXX: this work in a different way than RT
257     return unless $value;    #strip out total blank wildcards
258
259     my $ObjectValuesAlias = $self->Join(
260         TYPE   => 'left',
261         ALIAS1 => 'main',
262         FIELD1 => 'id',
263         TABLE2 => 'ObjectCustomFieldValues',
264         FIELD2 => 'ObjectId',
265         EXPRESSION => 'main.id'
266     );
267
268     $self->Limit(
269         LEFTJOIN => $ObjectValuesAlias,
270         FIELD    => 'Disabled',
271         VALUE    => '0'
272     );
273
274     if ( $args{'FIELD'} ) {
275
276         my $field_id;
277         if (UNIVERSAL::isa($args{'FIELD'} ,'RT::CustomField')) {
278             $field_id = $args{'FIELD'}->id;
279         } elsif($args{'FIELD'} =~ /^\d+$/) {
280             $field_id = $args{'FIELD'};
281         }
282         if ($field_id) {
283             $self->Limit( LEFTJOIN        => $ObjectValuesAlias,
284                           FIELD           => 'CustomField',
285                           VALUE           => $field_id,
286                           ENTRYAGGREGATOR => 'AND');
287             # Could convert the above to a non-left join and also enable the thing below
288             # $self->SUPER::Limit( ALIAS           => $ObjectValuesAlias,
289             #                      FIELD           => 'CustomField',
290             #                      OPERATOR        => 'IS',
291             #                      VALUE           => 'NULL',
292             #                      QUOTEVALUE      => 0,
293             #                      ENTRYAGGREGATOR => 'OR',);
294         } else {
295             # Search for things by name if the cf was specced by name.
296             my $fields = $self->NewAlias('CustomFields');
297             $self->Join( TYPE => 'left',
298                          ALIAS1 => $ObjectValuesAlias , FIELD1 => 'CustomField',
299                          ALIAS2 => $fields, FIELD2=> 'id');
300             $self->Limit( ALIAS => $fields,
301                           FIELD => 'Name',
302                           VALUE => $args{'FIELD'},
303                           ENTRYAGGREGATOR  => 'OR');
304             $self->Limit(
305                 ALIAS => $fields,
306                 FIELD => 'LookupType',
307                 VALUE =>
308                   RT::Article->new($RT::SystemUser)->CustomFieldLookupType()
309             );
310
311         }
312     }
313     # If we're trying to find articles where a custom field value
314     # doesn't match something, be sure to find things where it's null
315
316     # basically, we do a left join on the value being applicable to
317     # the article and then we turn around and make sure that it's
318     # actually null in practise
319
320     # TODO this should deal with starts with and ends with
321
322     my $fix_op = sub {
323         my $op = shift;
324         return $op unless RT->Config->Get('DatabaseType') eq 'Oracle';
325         return 'MATCHES' if $op eq '=';
326         return 'NOT MATCHES' if $op eq '!=';
327         return $op;
328     };
329
330     my $clause = $args{'SUBCLAUSE'} || $ObjectValuesAlias;
331     
332     if ( $args{'OPERATOR'} eq '!=' || $args{'OPERATOR'} =~ /^not like$/i ) {
333         my $op;
334         if ( $args{'OPERATOR'} eq '!=' ) {
335             $op = "=";
336         }
337         elsif ( $args{'OPERATOR'} =~ /^not like$/i ) {
338             $op = 'LIKE';
339         }
340
341         $self->SUPER::Limit(
342             LEFTJOIN        => $ObjectValuesAlias,
343             FIELD           => 'Content',
344             OPERATOR        => $op,
345             VALUE           => $value,
346             QUOTEVALUE      => $args{'QUOTEVALUE'},
347             ENTRYAGGREGATOR => 'AND', #$args{'ENTRYAGGREGATOR'},
348             SUBCLAUSE       => $clause,
349             CASESENSITIVE   => 0,
350         );
351         $self->SUPER::Limit(
352             ALIAS           => $ObjectValuesAlias,
353             FIELD           => 'Content',
354             OPERATOR        => 'IS',
355             VALUE           => 'NULL',
356             QUOTEVALUE      => 0,
357             ENTRYAGGREGATOR => 'AND',
358             SUBCLAUSE       => $clause,
359         );
360     }
361     else {
362         $self->SUPER::Limit(
363             ALIAS           => $ObjectValuesAlias,
364             FIELD           => 'LargeContent',
365             OPERATOR        => $fix_op->($args{'OPERATOR'}),
366             VALUE           => $value,
367             QUOTEVALUE      => $args{'QUOTEVALUE'},
368             ENTRYAGGREGATOR => $args{'ENTRYAGGREGATOR'},
369             SUBCLAUSE       => $clause,
370             CASESENSITIVE   => 0,
371         );
372         $self->SUPER::Limit(
373             ALIAS           => $ObjectValuesAlias,
374             FIELD           => 'Content',
375             OPERATOR        => $args{'OPERATOR'},
376             VALUE           => $value,
377             QUOTEVALUE      => $args{'QUOTEVALUE'},
378             ENTRYAGGREGATOR => $args{'ENTRYAGGREGATOR'},
379             SUBCLAUSE       => $clause,
380             CASESENSITIVE   => 0,
381         );
382     }
383 }
384
385 # }}}
386
387 # {{{ LimitTopics
388 sub LimitTopics {
389     my $self   = shift;
390     my @topics = @_;
391
392     my $topics = $self->NewAlias('ObjectTopics');
393     $self->Limit(
394         ALIAS           => $topics,
395         FIELD           => 'Topic',
396         VALUE           => $_,
397         ENTRYAGGREGATOR => 'OR'
398       )
399       for @topics;
400
401     $self->Limit(
402         ALIAS => $topics,
403         FIELD => 'ObjectType',
404         VALUE => 'RT::Article',
405     );
406     $self->Join(
407         ALIAS1 => 'main',
408         FIELD1 => 'id',
409         ALIAS2 => $topics,
410         FIELD2 => 'ObjectId',
411     );
412 }
413
414 # }}}
415
416 # {{{ LimitRefersTo URI
417
418 =head2 LimitRefersTo URI
419
420 Limit the result set to only articles which are referred to by the URI passed in.
421
422 =cut
423
424 sub LimitRefersTo {
425     my $self = shift;
426     my $uri  = shift;
427
428     my $uri_obj = RT::URI->new($self->CurrentUser);
429     $uri_obj->FromURI($uri);   
430     my $links = $self->NewAlias('Links');
431     $self->Limit(
432         ALIAS => $links,
433         FIELD => 'Target',
434         VALUE => $uri_obj->URI
435     );
436
437     $self->Join(
438         ALIAS1 => 'main',
439         FIELD1 => 'URI',
440         ALIAS2 => $links,
441         FIELD2 => 'Base'
442     );
443
444 }
445
446 # }}}
447
448 # {{{ LimitReferredToBy URI
449
450 =head2 LimitReferredToBy URI
451
452 Limit the result set to only articles which are referred to by the URI passed in.
453
454 =cut
455
456 sub LimitReferredToBy {
457     my $self = shift;
458     my $uri  = shift;
459
460     my $uri_obj = RT::URI->new($self->CurrentUser);
461     $uri_obj->FromURI($uri);    
462     my $links = $self->NewAlias('Links');
463     $self->Limit(
464         ALIAS => $links,
465         FIELD => 'Base',
466         VALUE => $uri_obj->URI
467     );
468
469     $self->Join(
470         ALIAS1 => 'main',
471         FIELD1 => 'URI',
472         ALIAS2 => $links,
473         FIELD2 => 'Target'
474     );
475
476 }
477
478 # }}}
479
480 =head2 LimitHostlistClasses
481
482 Only fetch Articles from classes where Hotlist is true.
483
484 =cut
485
486 sub LimitHotlistClasses {
487     my $self = shift;
488
489     my $classes = $self->Join(
490         ALIAS1 => 'main',
491         FIELD1 => 'Class',
492         TABLE2 => 'Classes',
493         FIELD2 => 'id',
494     );
495     $self->Limit( ALIAS => $classes, FIELD => 'HotList', VALUE => 1 );
496 }
497
498 =head2 LimitAppliedClasses Queue => QueueObj
499
500 Takes a Queue and limits articles returned to classes which are applied to that Queue
501
502 Accepts either a Queue obj or a Queue id
503
504 =cut
505
506 sub LimitAppliedClasses {
507     my $self = shift;
508     my %args = @_;
509
510     unless (ref $args{Queue} || $args{Queue} =~/^[0-9]+$/) {
511         $RT::Logger->error("Not a valid Queue: $args{Queue}");
512         return;
513     }
514
515     my $queue = ( ref $args{Queue} ? $args{Queue}->Id : $args{Queue} );
516
517     my $oc_alias = $self->Join(
518         ALIAS1 => 'main',
519         FIELD1 => 'Class',
520         TABLE2 => 'ObjectClasses',
521         FIELD2 => 'Class'
522     );
523
524     my $subclause = "possibleobjectclasses";
525     $self->_OpenParen($subclause);
526     $self->Limit( ALIAS => $oc_alias,
527                   FIELD => 'ObjectId',
528                   VALUE => $queue,
529                   SUBCLAUSE => $subclause,
530                   ENTRYAGGREGATOR => 'OR' );
531     $self->Limit( ALIAS => $oc_alias,
532                   FIELD => 'ObjectType',
533                   VALUE => 'RT::Queue',
534                   SUBCLAUSE => $subclause,
535                   ENTRYAGGREGATOR => 'AND' );
536     $self->_CloseParen($subclause);
537
538     $self->_OpenParen($subclause);
539     $self->Limit( ALIAS => $oc_alias,
540                   FIELD => 'ObjectId',
541                   VALUE => 0,
542                   SUBCLAUSE => $subclause,
543                   ENTRYAGGREGATOR => 'OR' );
544     $self->Limit( ALIAS => $oc_alias,
545                   FIELD => 'ObjectType',
546                   VALUE => 'RT::System',
547                   SUBCLAUSE => $subclause,
548                   ENTRYAGGREGATOR => 'AND' );
549     $self->_CloseParen($subclause);
550
551     return $self;
552
553 }
554
555 sub Search {
556     my $self     = shift;
557     my %args     = @_;
558     my $customfields = $args{CustomFields}
559       || RT::CustomFields->new( $self->CurrentUser );
560     my $dates = $args{Dates} || {};
561     my $order_by = $args{OrderBy};
562     my $order = $args{Order};
563     if ( $args{'q'} ) {
564         $self->Limit(
565             FIELD           => 'Name',
566             SUBCLAUSE       => 'NameOrSummary',
567             OPERATOR        => 'LIKE',
568             ENTRYAGGREGATOR => 'OR',
569             CASESENSITIVE   => 0,
570             VALUE           => $args{'q'}
571         );
572         $self->Limit(
573             FIELD           => 'Summary',
574             SUBCLAUSE       => 'NameOrSummary',
575             OPERATOR        => 'LIKE',
576             ENTRYAGGREGATOR => 'OR',
577             CASESENSITIVE   => 0,
578             VALUE           => $args{'q'}
579         );
580     }
581
582
583     require Time::ParseDate;
584     foreach my $date (qw(Created< Created> LastUpdated< LastUpdated>)) {
585         next unless ( $args{$date} );
586         my ($seconds, $error) = Time::ParseDate::parsedate( $args{$date}, FUZZY => 1, PREFER_PAST => 1 );
587         unless ( defined $seconds ) {
588             $RT::Logger->warning(
589                 "Couldn't parse date '$args{$date}' by Time::ParseDate" );
590         }
591         my $date_obj = RT::Date->new( $self->CurrentUser );
592         $date_obj->Set( Format => 'unix', Value => $seconds );
593         $dates->{$date} = $date_obj;
594
595         if ( $date =~ /^(.*?)<$/i ) {
596             $self->Limit(
597                 FIELD           => $1,
598                 OPERATOR        => "<=",
599                 ENTRYAGGREGATOR => "AND",
600                 VALUE           => $date_obj->ISO
601             );
602         }
603
604         if ( $date =~ /^(.*?)>$/i ) {
605             $self->Limit(
606                 FIELD           => $1,
607                 OPERATOR        => ">=",
608                 ENTRYAGGREGATOR => "AND",
609                 VALUE           => $date_obj->ISO
610             );
611         }
612
613     }
614
615     if ($args{'RefersTo'}) {
616         foreach my $link ( split( /\s+/, $args{'RefersTo'} ) ) {
617             next unless ($link);
618             $self->LimitRefersTo($link);
619         }
620     }
621
622     if ($args{'ReferredToBy'}) {
623         foreach my $link ( split( /\s+/, $args{'ReferredToBy'} ) ) {
624             next unless ($link);
625             $self->LimitReferredToBy($link);
626         }
627     }
628
629     if ( $args{'Topics'} ) {
630         my @Topics =
631           ( ref $args{'Topics'} eq 'ARRAY' )
632           ? @{ $args{'Topics'} }
633           : ( $args{'Topics'} );
634         @Topics = map { split } @Topics;
635         if ( $args{'ExpandTopics'} ) {
636             my %topics;
637             while (@Topics) {
638                 my $id = shift @Topics;
639                 next if $topics{$id};
640                 my $Topics =
641                   RT::Topics->new( $self->CurrentUser );
642                 $Topics->Limit( FIELD => 'Parent', VALUE => $id );
643                 push @Topics, $_->Id while $_ = $Topics->Next;
644                 $topics{$id}++;
645             }
646             @Topics = keys %topics;
647             $args{'Topics'} = \@Topics;
648         }
649         $self->LimitTopics(@Topics);
650     }
651
652     my %cfs;
653     $customfields->LimitToLookupType(
654         RT::Article->new( $self->CurrentUser )
655           ->CustomFieldLookupType );
656     if ( $args{'Class'} ) {
657         my @Classes =
658           ( ref $args{'Class'} eq 'ARRAY' )
659           ? @{ $args{'Class'} }
660           : ( $args{'Class'} );
661         foreach my $class (@Classes) {
662             $customfields->LimitToGlobalOrObjectId($class);
663         }
664     }
665     else {
666         $customfields->LimitToGlobalOrObjectId();
667     }
668     while ( my $cf = $customfields->Next ) {
669         $cfs{ $cf->Name } = $cf->Id;
670     }
671
672     # reset the iterator because we use this to build the UI
673     $customfields->GotoFirstItem;
674
675     foreach my $field ( keys %cfs ) {
676
677         my @MatchLike =
678           ( ref $args{ $field . "~" } eq 'ARRAY' )
679           ? @{ $args{ $field . "~" } }
680           : ( $args{ $field . "~" } );
681         my @NoMatchLike =
682           ( ref $args{ $field . "!~" } eq 'ARRAY' )
683           ? @{ $args{ $field . "!~" } }
684           : ( $args{ $field . "!~" } );
685
686         my @Match =
687           ( ref $args{$field} eq 'ARRAY' )
688           ? @{ $args{$field} }
689           : ( $args{$field} );
690         my @NoMatch =
691           ( ref $args{ $field . "!" } eq 'ARRAY' )
692           ? @{ $args{ $field . "!" } }
693           : ( $args{ $field . "!" } );
694
695         foreach my $val (@MatchLike) {
696             next unless $val;
697             push @Match, "~" . $val;
698         }
699
700         foreach my $val (@NoMatchLike) {
701             next unless $val;
702             push @NoMatch, "~" . $val;
703         }
704
705         foreach my $value (@Match) {
706             next unless $value;
707             my $op;
708             if ( $value =~ /^~(.*)$/ ) {
709                 $value = "%$1%";
710                 $op    = 'LIKE';
711             }
712             else {
713                 $op = '=';
714             }
715             $self->LimitCustomField(
716                 FIELD           => $cfs{$field},
717                 VALUE           => $value,
718                 CASESENSITIVE   => 0,
719                 ENTRYAGGREGATOR => 'OR',
720                 OPERATOR        => $op
721             );
722         }
723         foreach my $value (@NoMatch) {
724             next unless $value;
725             my $op;
726             if ( $value =~ /^~(.*)$/ ) {
727                 $value = "%$1%";
728                 $op    = 'NOT LIKE';
729             }
730             else {
731                 $op = '!=';
732             }
733             $self->LimitCustomField(
734                 FIELD           => $cfs{$field},
735                 VALUE           => $value,
736                 CASESENSITIVE   => 0,
737                 ENTRYAGGREGATOR => 'OR',
738                 OPERATOR        => $op
739             );
740         }
741     }
742
743 ### Searches for any field
744
745     if ( $args{'Article~'} ) {
746         $self->LimitCustomField(
747             VALUE           => $args{'Article~'},
748             ENTRYAGGREGATOR => 'OR',
749             OPERATOR        => 'LIKE',
750             CASESENSITIVE   => 0,
751             SUBCLAUSE       => 'SearchAll'
752         );
753         $self->Limit(
754             SUBCLAUSE       => 'SearchAll',
755             FIELD           => "Name",
756             VALUE           => $args{'Article~'},
757             ENTRYAGGREGATOR => 'OR',
758             CASESENSITIVE   => 0,
759             OPERATOR        => 'LIKE'
760         );
761         $self->Limit(
762             SUBCLAUSE       => 'SearchAll',
763             FIELD           => "Summary",
764             VALUE           => $args{'Article~'},
765             ENTRYAGGREGATOR => 'OR',
766             CASESENSITIVE   => 0,
767             OPERATOR        => 'LIKE'
768         );
769     }
770
771     if ( $args{'Article!~'} ) {
772         $self->LimitCustomField(
773             VALUE         => $args{'Article!~'},
774             OPERATOR      => 'NOT LIKE',
775             CASESENSITIVE => 0,
776             SUBCLAUSE     => 'SearchAll'
777         );
778         $self->Limit(
779             SUBCLAUSE       => 'SearchAll',
780             FIELD           => "Name",
781             VALUE           => $args{'Article!~'},
782             ENTRYAGGREGATOR => 'AND',
783             CASESENSITIVE   => 0,
784             OPERATOR        => 'NOT LIKE'
785         );
786         $self->Limit(
787             SUBCLAUSE       => 'SearchAll',
788             FIELD           => "Summary",
789             VALUE           => $args{'Article!~'},
790             ENTRYAGGREGATOR => 'AND',
791             CASESENSITIVE   => 0,
792             OPERATOR        => 'NOT LIKE'
793         );
794     }
795
796     foreach my $field (qw(Name Summary Class)) {
797
798         my @MatchLike =
799           ( ref $args{ $field . "~" } eq 'ARRAY' )
800           ? @{ $args{ $field . "~" } }
801           : ( $args{ $field . "~" } );
802         my @NoMatchLike =
803           ( ref $args{ $field . "!~" } eq 'ARRAY' )
804           ? @{ $args{ $field . "!~" } }
805           : ( $args{ $field . "!~" } );
806
807         my @Match =
808           ( ref $args{$field} eq 'ARRAY' )
809           ? @{ $args{$field} }
810           : ( $args{$field} );
811         my @NoMatch =
812           ( ref $args{ $field . "!" } eq 'ARRAY' )
813           ? @{ $args{ $field . "!" } }
814           : ( $args{ $field . "!" } );
815
816         foreach my $val (@MatchLike) {
817             next unless $val;
818             push @Match, "~" . $val;
819         }
820
821         foreach my $val (@NoMatchLike) {
822             next unless $val;
823             push @NoMatch, "~" . $val;
824         }
825
826         my $op;
827         foreach my $value (@Match) {
828             if ( $value && $value =~ /^~(.*)$/ ) {
829                 $value = "%$1%";
830                 $op    = 'LIKE';
831             }
832             else {
833                 $op = '=';
834             }
835
836             # preprocess Classes, so we can search on class
837             if ( $field eq 'Class' && $value ) {
838                 my $class = RT::Class->new($RT::SystemUser);
839                 $class->Load($value);
840                 $value = $class->Id;
841             }
842
843             # now that we've pruned the value, get out if it's different.
844             next unless $value;
845
846             $self->Limit(
847                 SUBCLAUSE       => $field . 'Match',
848                 FIELD           => $field,
849                 OPERATOR        => $op,
850                 CASESENSITIVE   => 0,
851                 VALUE           => $value,
852                 ENTRYAGGREGATOR => 'OR'
853             );
854
855         }
856         foreach my $value (@NoMatch) {
857
858             # preprocess Classes, so we can search on class
859             if ( $value && $value =~ /^~(.*)/ ) {
860                 $value = "%$1%";
861                 $op    = 'NOT LIKE';
862             }
863             else {
864                 $op = '!=';
865             }
866             if ( $field eq 'Class' ) {
867                 my $class = RT::Class->new($RT::SystemUser);
868                 $class->Load($value);
869                 $value = $class->Id;
870             }
871
872             # now that we've pruned the value, get out if it's different.
873             next unless $value;
874
875             $self->Limit(
876                 SUBCLAUSE       => $field . 'NoMatch',
877                 OPERATOR        => $op,
878                 VALUE           => $value,
879                 CASESENSITIVE   => 0,
880                 FIELD           => $field,
881                 ENTRYAGGREGATOR => 'AND'
882             );
883
884         }
885     }
886
887     if ($order_by && @$order_by) {
888         if ( $order_by->[0] && $order_by->[0] =~ /\|/ ) {
889             @$order_by = split '|', $order_by->[0];
890             @$order   = split '|', $order->[0];
891         }
892         my @tmp =
893           map { { FIELD => $order_by->[$_], ORDER => $order->[$_] } } 0 .. $#{$order_by};
894         $self->OrderByCols(@tmp);
895     }
896
897     return 1;
898 }
899
900
901 =head2 NewItem
902
903 Returns an empty new RT::Article item
904
905 =cut
906
907 sub NewItem {
908     my $self = shift;
909     return(RT::Article->new($self->CurrentUser));
910 }
911
912
913
914 RT::Base->_ImportOverlays();
915
916 1;
917
918 1;