rt 4.2.16
[freeside.git] / rt / lib / RT / Articles.pm
1 # BEGIN BPS TAGGED BLOCK {{{
2 #
3 # COPYRIGHT:
4 #
5 # This software is Copyright (c) 1996-2019 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                           CASESENSITIVE => 0);
305             $self->Limit(
306                 ALIAS => $fields,
307                 FIELD => 'LookupType',
308                 VALUE =>
309                   RT::Article->new($RT::SystemUser)->CustomFieldLookupType()
310             );
311
312         }
313     }
314     # If we're trying to find articles where a custom field value
315     # doesn't match something, be sure to find things where it's null
316
317     # basically, we do a left join on the value being applicable to
318     # the article and then we turn around and make sure that it's
319     # actually null in practise
320
321     # TODO this should deal with starts with and ends with
322
323     my $fix_op = sub {
324         my $op = shift;
325         return $op unless RT->Config->Get('DatabaseType') eq 'Oracle';
326         return 'MATCHES' if $op eq '=';
327         return 'NOT MATCHES' if $op eq '!=';
328         return $op;
329     };
330
331     my $clause = $args{'SUBCLAUSE'} || $ObjectValuesAlias;
332     
333     if ( $args{'OPERATOR'} eq '!=' || $args{'OPERATOR'} =~ /^not like$/i ) {
334         my $op;
335         if ( $args{'OPERATOR'} eq '!=' ) {
336             $op = "=";
337         }
338         elsif ( $args{'OPERATOR'} =~ /^not like$/i ) {
339             $op = 'LIKE';
340         }
341
342         $self->SUPER::Limit(
343             LEFTJOIN        => $ObjectValuesAlias,
344             FIELD           => 'Content',
345             OPERATOR        => $op,
346             VALUE           => $value,
347             QUOTEVALUE      => $args{'QUOTEVALUE'},
348             ENTRYAGGREGATOR => 'AND', #$args{'ENTRYAGGREGATOR'},
349             SUBCLAUSE       => $clause,
350             CASESENSITIVE   => 0,
351         );
352         $self->SUPER::Limit(
353             ALIAS           => $ObjectValuesAlias,
354             FIELD           => 'Content',
355             OPERATOR        => 'IS',
356             VALUE           => 'NULL',
357             QUOTEVALUE      => 0,
358             ENTRYAGGREGATOR => 'AND',
359             SUBCLAUSE       => $clause,
360         );
361     }
362     else {
363         $self->SUPER::Limit(
364             ALIAS           => $ObjectValuesAlias,
365             FIELD           => 'LargeContent',
366             OPERATOR        => $fix_op->($args{'OPERATOR'}),
367             VALUE           => $value,
368             QUOTEVALUE      => $args{'QUOTEVALUE'},
369             ENTRYAGGREGATOR => $args{'ENTRYAGGREGATOR'},
370             SUBCLAUSE       => $clause,
371             CASESENSITIVE   => 0,
372         );
373         $self->SUPER::Limit(
374             ALIAS           => $ObjectValuesAlias,
375             FIELD           => 'Content',
376             OPERATOR        => $args{'OPERATOR'},
377             VALUE           => $value,
378             QUOTEVALUE      => $args{'QUOTEVALUE'},
379             ENTRYAGGREGATOR => $args{'ENTRYAGGREGATOR'},
380             SUBCLAUSE       => $clause,
381             CASESENSITIVE   => 0,
382         );
383     }
384 }
385
386 # }}}
387
388 # {{{ LimitTopics
389 sub LimitTopics {
390     my $self   = shift;
391     my @topics = @_;
392     return unless @topics;
393
394     my $topics = $self->NewAlias('ObjectTopics');
395     $self->Limit(
396         ALIAS    => $topics,
397         FIELD    => 'Topic',
398         OPERATOR => 'IN',
399         VALUE    => [ @topics ],
400     );
401
402     $self->Limit(
403         ALIAS => $topics,
404         FIELD => 'ObjectType',
405         VALUE => 'RT::Article',
406     );
407     $self->Join(
408         ALIAS1 => 'main',
409         FIELD1 => 'id',
410         ALIAS2 => $topics,
411         FIELD2 => 'ObjectId',
412     );
413 }
414
415 # }}}
416
417 # {{{ LimitRefersTo URI
418
419 =head2 LimitRefersTo URI
420
421 Limit the result set to only articles which are referred to by the URI passed in.
422
423 =cut
424
425 sub LimitRefersTo {
426     my $self = shift;
427     my $uri  = shift;
428
429     my $uri_obj = RT::URI->new($self->CurrentUser);
430     $uri_obj->FromURI($uri);   
431     my $links = $self->NewAlias('Links');
432     $self->Limit(
433         ALIAS => $links,
434         FIELD => 'Target',
435         VALUE => $uri_obj->URI
436     );
437
438     $self->Join(
439         ALIAS1 => 'main',
440         FIELD1 => 'URI',
441         ALIAS2 => $links,
442         FIELD2 => 'Base'
443     );
444
445 }
446
447 # }}}
448
449 # {{{ LimitReferredToBy URI
450
451 =head2 LimitReferredToBy URI
452
453 Limit the result set to only articles which are referred to by the URI passed in.
454
455 =cut
456
457 sub LimitReferredToBy {
458     my $self = shift;
459     my $uri  = shift;
460
461     my $uri_obj = RT::URI->new($self->CurrentUser);
462     $uri_obj->FromURI($uri);    
463     my $links = $self->NewAlias('Links');
464     $self->Limit(
465         ALIAS => $links,
466         FIELD => 'Base',
467         VALUE => $uri_obj->URI
468     );
469
470     $self->Join(
471         ALIAS1 => 'main',
472         FIELD1 => 'URI',
473         ALIAS2 => $links,
474         FIELD2 => 'Target'
475     );
476
477 }
478
479 # }}}
480
481 =head2 LimitHostlistClasses
482
483 Only fetch Articles from classes where Hotlist is true.
484
485 =cut
486
487 sub LimitHotlistClasses {
488     my $self = shift;
489
490     my $classes = $self->Join(
491         ALIAS1 => 'main',
492         FIELD1 => 'Class',
493         TABLE2 => 'Classes',
494         FIELD2 => 'id',
495     );
496     $self->Limit( ALIAS => $classes, FIELD => 'HotList', VALUE => 1 );
497 }
498
499 =head2 LimitAppliedClasses Queue => QueueObj
500
501 Takes a Queue and limits articles returned to classes which are applied to that Queue
502
503 Accepts either a Queue obj or a Queue id
504
505 =cut
506
507 sub LimitAppliedClasses {
508     my $self = shift;
509     my %args = @_;
510
511     unless (ref $args{Queue} || $args{Queue} =~/^[0-9]+$/) {
512         $RT::Logger->error("Not a valid Queue: $args{Queue}");
513         return;
514     }
515
516     my $queue = ( ref $args{Queue} ? $args{Queue}->Id : $args{Queue} );
517
518     my $oc_alias = $self->Join(
519         ALIAS1 => 'main',
520         FIELD1 => 'Class',
521         TABLE2 => 'ObjectClasses',
522         FIELD2 => 'Class'
523     );
524
525     my $subclause = "possibleobjectclasses";
526     $self->_OpenParen($subclause);
527     $self->Limit( ALIAS => $oc_alias,
528                   FIELD => 'ObjectId',
529                   VALUE => $queue,
530                   SUBCLAUSE => $subclause,
531                   ENTRYAGGREGATOR => 'OR' );
532     $self->Limit( ALIAS => $oc_alias,
533                   FIELD => 'ObjectType',
534                   VALUE => 'RT::Queue',
535                   SUBCLAUSE => $subclause,
536                   ENTRYAGGREGATOR => 'AND' );
537     $self->_CloseParen($subclause);
538
539     $self->_OpenParen($subclause);
540     $self->Limit( ALIAS => $oc_alias,
541                   FIELD => 'ObjectId',
542                   VALUE => 0,
543                   SUBCLAUSE => $subclause,
544                   ENTRYAGGREGATOR => 'OR' );
545     $self->Limit( ALIAS => $oc_alias,
546                   FIELD => 'ObjectType',
547                   VALUE => 'RT::System',
548                   SUBCLAUSE => $subclause,
549                   ENTRYAGGREGATOR => 'AND' );
550     $self->_CloseParen($subclause);
551
552     return $self;
553
554 }
555
556 sub Search {
557     my $self     = shift;
558     my %args     = @_;
559     my $customfields = $args{CustomFields}
560       || RT::CustomFields->new( $self->CurrentUser );
561     my $dates = $args{Dates} || {};
562     my $order_by = $args{OrderBy};
563     my $order = $args{Order};
564     if ( $args{'q'} ) {
565         $self->Limit(
566             FIELD           => 'Name',
567             SUBCLAUSE       => 'NameOrSummary',
568             OPERATOR        => 'LIKE',
569             ENTRYAGGREGATOR => 'OR',
570             CASESENSITIVE   => 0,
571             VALUE           => $args{'q'}
572         );
573         $self->Limit(
574             FIELD           => 'Summary',
575             SUBCLAUSE       => 'NameOrSummary',
576             OPERATOR        => 'LIKE',
577             ENTRYAGGREGATOR => 'OR',
578             CASESENSITIVE   => 0,
579             VALUE           => $args{'q'}
580         );
581     }
582
583
584     foreach my $date (qw(Created< Created> LastUpdated< LastUpdated>)) {
585         next unless ( $args{$date} );
586         my $date_obj = RT::Date->new( $self->CurrentUser );
587         $date_obj->Set( Format => 'unknown', Value => $args{$date} );
588         $dates->{$date} = $date_obj;
589
590         if ( $date =~ /^(.*?)<$/i ) {
591             $self->Limit(
592                 FIELD           => $1,
593                 OPERATOR        => "<=",
594                 ENTRYAGGREGATOR => "AND",
595                 VALUE           => $date_obj->ISO
596             );
597         }
598
599         if ( $date =~ /^(.*?)>$/i ) {
600             $self->Limit(
601                 FIELD           => $1,
602                 OPERATOR        => ">=",
603                 ENTRYAGGREGATOR => "AND",
604                 VALUE           => $date_obj->ISO
605             );
606         }
607
608     }
609
610     if ($args{'RefersTo'}) {
611         foreach my $link ( split( /\s+/, $args{'RefersTo'} ) ) {
612             next unless ($link);
613             $self->LimitRefersTo($link);
614         }
615     }
616
617     if ($args{'ReferredToBy'}) {
618         foreach my $link ( split( /\s+/, $args{'ReferredToBy'} ) ) {
619             next unless ($link);
620             $self->LimitReferredToBy($link);
621         }
622     }
623
624     if ( $args{'Topics'} ) {
625         my @Topics =
626           ( ref $args{'Topics'} eq 'ARRAY' )
627           ? @{ $args{'Topics'} }
628           : ( $args{'Topics'} );
629         @Topics = map { split } @Topics;
630         if ( $args{'ExpandTopics'} ) {
631             my %topics;
632             while (@Topics) {
633                 my $id = shift @Topics;
634                 next if $topics{$id};
635                 my $Topics =
636                   RT::Topics->new( $self->CurrentUser );
637                 $Topics->Limit( FIELD => 'Parent', VALUE => $id );
638                 push @Topics, $_->Id while $_ = $Topics->Next;
639                 $topics{$id}++;
640             }
641             @Topics = keys %topics;
642             $args{'Topics'} = \@Topics;
643         }
644         $self->LimitTopics(@Topics);
645     }
646
647     my %cfs;
648     $customfields->LimitToLookupType(
649         RT::Article->new( $self->CurrentUser )
650           ->CustomFieldLookupType );
651     if ( $args{'Class'} ) {
652         my @Classes =
653           ( ref $args{'Class'} eq 'ARRAY' )
654           ? @{ $args{'Class'} }
655           : ( $args{'Class'} );
656         foreach my $class (@Classes) {
657             $customfields->LimitToGlobalOrObjectId($class);
658         }
659     }
660     else {
661         $customfields->LimitToGlobalOrObjectId();
662     }
663     while ( my $cf = $customfields->Next ) {
664         $cfs{ $cf->Name } = $cf->Id;
665     }
666
667     # reset the iterator because we use this to build the UI
668     $customfields->GotoFirstItem;
669
670     foreach my $field ( keys %cfs ) {
671
672         my @MatchLike =
673           ( ref $args{ $field . "~" } eq 'ARRAY' )
674           ? @{ $args{ $field . "~" } }
675           : ( $args{ $field . "~" } );
676         my @NoMatchLike =
677           ( ref $args{ $field . "!~" } eq 'ARRAY' )
678           ? @{ $args{ $field . "!~" } }
679           : ( $args{ $field . "!~" } );
680
681         my @Match =
682           ( ref $args{$field} eq 'ARRAY' )
683           ? @{ $args{$field} }
684           : ( $args{$field} );
685         my @NoMatch =
686           ( ref $args{ $field . "!" } eq 'ARRAY' )
687           ? @{ $args{ $field . "!" } }
688           : ( $args{ $field . "!" } );
689
690         foreach my $val (@MatchLike) {
691             next unless $val;
692             push @Match, "~" . $val;
693         }
694
695         foreach my $val (@NoMatchLike) {
696             next unless $val;
697             push @NoMatch, "~" . $val;
698         }
699
700         foreach my $value (@Match) {
701             next unless $value;
702             my $op;
703             if ( $value =~ /^~(.*)$/ ) {
704                 $value = "%$1%";
705                 $op    = 'LIKE';
706             }
707             else {
708                 $op = '=';
709             }
710             $self->LimitCustomField(
711                 FIELD           => $cfs{$field},
712                 VALUE           => $value,
713                 CASESENSITIVE   => 0,
714                 ENTRYAGGREGATOR => 'OR',
715                 OPERATOR        => $op
716             );
717         }
718         foreach my $value (@NoMatch) {
719             next unless $value;
720             my $op;
721             if ( $value =~ /^~(.*)$/ ) {
722                 $value = "%$1%";
723                 $op    = 'NOT LIKE';
724             }
725             else {
726                 $op = '!=';
727             }
728             $self->LimitCustomField(
729                 FIELD           => $cfs{$field},
730                 VALUE           => $value,
731                 CASESENSITIVE   => 0,
732                 ENTRYAGGREGATOR => 'OR',
733                 OPERATOR        => $op
734             );
735         }
736     }
737
738 ### Searches for any field
739
740     if ( $args{'Article~'} ) {
741         $self->LimitCustomField(
742             VALUE           => $args{'Article~'},
743             ENTRYAGGREGATOR => 'OR',
744             OPERATOR        => 'LIKE',
745             CASESENSITIVE   => 0,
746             SUBCLAUSE       => 'SearchAll'
747         );
748         $self->Limit(
749             SUBCLAUSE       => 'SearchAll',
750             FIELD           => "Name",
751             VALUE           => $args{'Article~'},
752             ENTRYAGGREGATOR => 'OR',
753             CASESENSITIVE   => 0,
754             OPERATOR        => 'LIKE'
755         );
756         $self->Limit(
757             SUBCLAUSE       => 'SearchAll',
758             FIELD           => "Summary",
759             VALUE           => $args{'Article~'},
760             ENTRYAGGREGATOR => 'OR',
761             CASESENSITIVE   => 0,
762             OPERATOR        => 'LIKE'
763         );
764     }
765
766     if ( $args{'Article!~'} ) {
767         $self->LimitCustomField(
768             VALUE         => $args{'Article!~'},
769             OPERATOR      => 'NOT LIKE',
770             CASESENSITIVE => 0,
771             SUBCLAUSE     => 'SearchAll'
772         );
773         $self->Limit(
774             SUBCLAUSE       => 'SearchAll',
775             FIELD           => "Name",
776             VALUE           => $args{'Article!~'},
777             ENTRYAGGREGATOR => 'AND',
778             CASESENSITIVE   => 0,
779             OPERATOR        => 'NOT LIKE'
780         );
781         $self->Limit(
782             SUBCLAUSE       => 'SearchAll',
783             FIELD           => "Summary",
784             VALUE           => $args{'Article!~'},
785             ENTRYAGGREGATOR => 'AND',
786             CASESENSITIVE   => 0,
787             OPERATOR        => 'NOT LIKE'
788         );
789     }
790
791     foreach my $field (qw(Name Summary Class)) {
792
793         my @MatchLike =
794           ( ref $args{ $field . "~" } eq 'ARRAY' )
795           ? @{ $args{ $field . "~" } }
796           : ( $args{ $field . "~" } );
797         my @NoMatchLike =
798           ( ref $args{ $field . "!~" } eq 'ARRAY' )
799           ? @{ $args{ $field . "!~" } }
800           : ( $args{ $field . "!~" } );
801
802         my @Match =
803           ( ref $args{$field} eq 'ARRAY' )
804           ? @{ $args{$field} }
805           : ( $args{$field} );
806         my @NoMatch =
807           ( ref $args{ $field . "!" } eq 'ARRAY' )
808           ? @{ $args{ $field . "!" } }
809           : ( $args{ $field . "!" } );
810
811         foreach my $val (@MatchLike) {
812             next unless $val;
813             push @Match, "~" . $val;
814         }
815
816         foreach my $val (@NoMatchLike) {
817             next unless $val;
818             push @NoMatch, "~" . $val;
819         }
820
821         my $op;
822         foreach my $value (@Match) {
823             if ( $value && $value =~ /^~(.*)$/ ) {
824                 $value = "%$1%";
825                 $op    = 'LIKE';
826             }
827             else {
828                 $op = '=';
829             }
830
831             # preprocess Classes, so we can search on class
832             if ( $field eq 'Class' && $value ) {
833                 my $class = RT::Class->new($RT::SystemUser);
834                 $class->Load($value);
835                 $value = $class->Id;
836             }
837
838             # now that we've pruned the value, get out if it's different.
839             next unless $value;
840
841             $self->Limit(
842                 SUBCLAUSE       => $field . 'Match',
843                 FIELD           => $field,
844                 OPERATOR        => $op,
845                 CASESENSITIVE   => 0,
846                 VALUE           => $value,
847                 ENTRYAGGREGATOR => 'OR'
848             );
849
850         }
851         foreach my $value (@NoMatch) {
852
853             # preprocess Classes, so we can search on class
854             if ( $value && $value =~ /^~(.*)/ ) {
855                 $value = "%$1%";
856                 $op    = 'NOT LIKE';
857             }
858             else {
859                 $op = '!=';
860             }
861             if ( $field eq 'Class' ) {
862                 my $class = RT::Class->new($RT::SystemUser);
863                 $class->Load($value);
864                 $value = $class->Id;
865             }
866
867             # now that we've pruned the value, get out if it's different.
868             next unless $value;
869
870             $self->Limit(
871                 SUBCLAUSE       => $field . 'NoMatch',
872                 OPERATOR        => $op,
873                 VALUE           => $value,
874                 CASESENSITIVE   => 0,
875                 FIELD           => $field,
876                 ENTRYAGGREGATOR => 'AND'
877             );
878
879         }
880     }
881
882     if ($order_by && @$order_by) {
883         if ( $order_by->[0] && $order_by->[0] =~ /\|/ ) {
884             @$order_by = split '|', $order_by->[0];
885             @$order   = split '|', $order->[0];
886         }
887         my @tmp =
888           map { { FIELD => $order_by->[$_], ORDER => $order->[$_] } } 0 .. $#{$order_by};
889         $self->OrderByCols(@tmp);
890     }
891
892     return 1;
893 }
894
895 RT::Base->_ImportOverlays();
896
897 1;