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