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