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