Putting 4.2.0 on top of 4.0.17
[usit-rt.git] / lib / RT / Articles.pm
CommitLineData
84fb5b46
MKG
1# BEGIN BPS TAGGED BLOCK {{{
2#
3# COPYRIGHT:
4#
403d7b0b 5# This software is Copyright (c) 1996-2013 Best Practical Solutions, LLC
84fb5b46
MKG
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
49use strict;
50use warnings;
51
52package RT::Articles;
53
54use base 'RT::SearchBuilder';
55
56sub Table {'Articles'}
57
58sub _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
69Returns the next article that this user can see.
70
71=cut
72
73sub 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
98Limit the result set. See DBIx::SearchBuilder docs
99In addition to the "normal" stuff, value can be an array.
100
101=cut
102
103sub 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
124Find all articles with Name fields which satisfy OPERATOR for VALUE
125
126=cut
127
128sub 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
145Find all articles with summary fields which satisfy OPERATOR for VALUE
146
147=cut
148
149sub 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
164sub 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
177sub 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
190sub 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
204sub 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
221Limit the returned set of articles to articles which are children
222of article ID.
223This does not recurse.
224
225=cut
226
227sub 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
243Limit the result set to articles which have or do not have the custom field
244value listed, using a left join to catch things where no rows match.
245
246HASH 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
252The subclause that the LIMIT statement(s) should be done in can also
253be passed in with a SUBCLAUSE parameter.
254
255=cut
256
257sub 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'},
af59614d
MKG
317 ENTRYAGGREGATOR => 'OR',
318 CASESENSITIVE => 0);
84fb5b46
MKG
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,
dab09ea8 364 CASESENSITIVE => 0,
84fb5b46
MKG
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,
dab09ea8 385 CASESENSITIVE => 0,
84fb5b46
MKG
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,
dab09ea8 395 CASESENSITIVE => 0,
84fb5b46
MKG
396 );
397 }
398}
399
400# }}}
401
402# {{{ LimitTopics
403sub 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
435Limit the result set to only articles which are referred to by the URI passed in.
436
437=cut
438
439sub 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
467Limit the result set to only articles which are referred to by the URI passed in.
468
469=cut
470
471sub 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
497Only fetch Articles from classes where Hotlist is true.
498
499=cut
500
501sub 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
515Takes a Queue and limits articles returned to classes which are applied to that Queue
516
517Accepts either a Queue obj or a Queue id
518
519=cut
520
521sub 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
570sub 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 require Time::ParseDate;
599 foreach my $date (qw(Created< Created> LastUpdated< LastUpdated>)) {
600 next unless ( $args{$date} );
af59614d
MKG
601 my ($seconds, $error) = Time::ParseDate::parsedate( $args{$date}, FUZZY => 1, PREFER_PAST => 1 );
602 unless ( defined $seconds ) {
603 $RT::Logger->warning(
604 "Couldn't parse date '$args{$date}' by Time::ParseDate" );
605 }
84fb5b46
MKG
606 my $date_obj = RT::Date->new( $self->CurrentUser );
607 $date_obj->Set( Format => 'unix', Value => $seconds );
608 $dates->{$date} = $date_obj;
609
610 if ( $date =~ /^(.*?)<$/i ) {
611 $self->Limit(
612 FIELD => $1,
613 OPERATOR => "<=",
614 ENTRYAGGREGATOR => "AND",
615 VALUE => $date_obj->ISO
616 );
617 }
618
619 if ( $date =~ /^(.*?)>$/i ) {
620 $self->Limit(
621 FIELD => $1,
622 OPERATOR => ">=",
623 ENTRYAGGREGATOR => "AND",
624 VALUE => $date_obj->ISO
625 );
626 }
627
628 }
629
630 if ($args{'RefersTo'}) {
631 foreach my $link ( split( /\s+/, $args{'RefersTo'} ) ) {
632 next unless ($link);
633 $self->LimitRefersTo($link);
634 }
635 }
636
637 if ($args{'ReferredToBy'}) {
638 foreach my $link ( split( /\s+/, $args{'ReferredToBy'} ) ) {
639 next unless ($link);
640 $self->LimitReferredToBy($link);
641 }
642 }
643
644 if ( $args{'Topics'} ) {
645 my @Topics =
646 ( ref $args{'Topics'} eq 'ARRAY' )
647 ? @{ $args{'Topics'} }
648 : ( $args{'Topics'} );
649 @Topics = map { split } @Topics;
650 if ( $args{'ExpandTopics'} ) {
651 my %topics;
652 while (@Topics) {
653 my $id = shift @Topics;
654 next if $topics{$id};
655 my $Topics =
656 RT::Topics->new( $self->CurrentUser );
657 $Topics->Limit( FIELD => 'Parent', VALUE => $id );
658 push @Topics, $_->Id while $_ = $Topics->Next;
659 $topics{$id}++;
660 }
661 @Topics = keys %topics;
662 $args{'Topics'} = \@Topics;
663 }
664 $self->LimitTopics(@Topics);
665 }
666
667 my %cfs;
668 $customfields->LimitToLookupType(
669 RT::Article->new( $self->CurrentUser )
670 ->CustomFieldLookupType );
671 if ( $args{'Class'} ) {
672 my @Classes =
673 ( ref $args{'Class'} eq 'ARRAY' )
674 ? @{ $args{'Class'} }
675 : ( $args{'Class'} );
676 foreach my $class (@Classes) {
677 $customfields->LimitToGlobalOrObjectId($class);
678 }
679 }
680 else {
681 $customfields->LimitToGlobalOrObjectId();
682 }
683 while ( my $cf = $customfields->Next ) {
684 $cfs{ $cf->Name } = $cf->Id;
685 }
686
687 # reset the iterator because we use this to build the UI
688 $customfields->GotoFirstItem;
689
690 foreach my $field ( keys %cfs ) {
691
692 my @MatchLike =
693 ( ref $args{ $field . "~" } eq 'ARRAY' )
694 ? @{ $args{ $field . "~" } }
695 : ( $args{ $field . "~" } );
696 my @NoMatchLike =
697 ( ref $args{ $field . "!~" } eq 'ARRAY' )
698 ? @{ $args{ $field . "!~" } }
699 : ( $args{ $field . "!~" } );
700
701 my @Match =
702 ( ref $args{$field} eq 'ARRAY' )
703 ? @{ $args{$field} }
704 : ( $args{$field} );
705 my @NoMatch =
706 ( ref $args{ $field . "!" } eq 'ARRAY' )
707 ? @{ $args{ $field . "!" } }
708 : ( $args{ $field . "!" } );
709
710 foreach my $val (@MatchLike) {
711 next unless $val;
712 push @Match, "~" . $val;
713 }
714
715 foreach my $val (@NoMatchLike) {
716 next unless $val;
717 push @NoMatch, "~" . $val;
718 }
719
720 foreach my $value (@Match) {
721 next unless $value;
722 my $op;
723 if ( $value =~ /^~(.*)$/ ) {
724 $value = "%$1%";
725 $op = 'LIKE';
726 }
727 else {
728 $op = '=';
729 }
730 $self->LimitCustomField(
731 FIELD => $cfs{$field},
732 VALUE => $value,
733 CASESENSITIVE => 0,
734 ENTRYAGGREGATOR => 'OR',
735 OPERATOR => $op
736 );
737 }
738 foreach my $value (@NoMatch) {
739 next unless $value;
740 my $op;
741 if ( $value =~ /^~(.*)$/ ) {
742 $value = "%$1%";
743 $op = 'NOT LIKE';
744 }
745 else {
746 $op = '!=';
747 }
748 $self->LimitCustomField(
749 FIELD => $cfs{$field},
750 VALUE => $value,
751 CASESENSITIVE => 0,
752 ENTRYAGGREGATOR => 'OR',
753 OPERATOR => $op
754 );
755 }
756 }
757
758### Searches for any field
759
760 if ( $args{'Article~'} ) {
761 $self->LimitCustomField(
762 VALUE => $args{'Article~'},
763 ENTRYAGGREGATOR => 'OR',
764 OPERATOR => 'LIKE',
765 CASESENSITIVE => 0,
766 SUBCLAUSE => 'SearchAll'
767 );
768 $self->Limit(
769 SUBCLAUSE => 'SearchAll',
770 FIELD => "Name",
771 VALUE => $args{'Article~'},
772 ENTRYAGGREGATOR => 'OR',
773 CASESENSITIVE => 0,
774 OPERATOR => 'LIKE'
775 );
776 $self->Limit(
777 SUBCLAUSE => 'SearchAll',
778 FIELD => "Summary",
779 VALUE => $args{'Article~'},
780 ENTRYAGGREGATOR => 'OR',
781 CASESENSITIVE => 0,
782 OPERATOR => 'LIKE'
783 );
784 }
785
786 if ( $args{'Article!~'} ) {
787 $self->LimitCustomField(
788 VALUE => $args{'Article!~'},
789 OPERATOR => 'NOT LIKE',
790 CASESENSITIVE => 0,
791 SUBCLAUSE => 'SearchAll'
792 );
793 $self->Limit(
794 SUBCLAUSE => 'SearchAll',
795 FIELD => "Name",
796 VALUE => $args{'Article!~'},
797 ENTRYAGGREGATOR => 'AND',
798 CASESENSITIVE => 0,
799 OPERATOR => 'NOT LIKE'
800 );
801 $self->Limit(
802 SUBCLAUSE => 'SearchAll',
803 FIELD => "Summary",
804 VALUE => $args{'Article!~'},
805 ENTRYAGGREGATOR => 'AND',
806 CASESENSITIVE => 0,
807 OPERATOR => 'NOT LIKE'
808 );
809 }
810
811 foreach my $field (qw(Name Summary Class)) {
812
813 my @MatchLike =
814 ( ref $args{ $field . "~" } eq 'ARRAY' )
815 ? @{ $args{ $field . "~" } }
816 : ( $args{ $field . "~" } );
817 my @NoMatchLike =
818 ( ref $args{ $field . "!~" } eq 'ARRAY' )
819 ? @{ $args{ $field . "!~" } }
820 : ( $args{ $field . "!~" } );
821
822 my @Match =
823 ( ref $args{$field} eq 'ARRAY' )
824 ? @{ $args{$field} }
825 : ( $args{$field} );
826 my @NoMatch =
827 ( ref $args{ $field . "!" } eq 'ARRAY' )
828 ? @{ $args{ $field . "!" } }
829 : ( $args{ $field . "!" } );
830
831 foreach my $val (@MatchLike) {
832 next unless $val;
833 push @Match, "~" . $val;
834 }
835
836 foreach my $val (@NoMatchLike) {
837 next unless $val;
838 push @NoMatch, "~" . $val;
839 }
840
841 my $op;
842 foreach my $value (@Match) {
843 if ( $value && $value =~ /^~(.*)$/ ) {
844 $value = "%$1%";
845 $op = 'LIKE';
846 }
847 else {
848 $op = '=';
849 }
850
851 # preprocess Classes, so we can search on class
852 if ( $field eq 'Class' && $value ) {
853 my $class = RT::Class->new($RT::SystemUser);
854 $class->Load($value);
855 $value = $class->Id;
856 }
857
858 # now that we've pruned the value, get out if it's different.
859 next unless $value;
860
861 $self->Limit(
862 SUBCLAUSE => $field . 'Match',
863 FIELD => $field,
864 OPERATOR => $op,
865 CASESENSITIVE => 0,
866 VALUE => $value,
867 ENTRYAGGREGATOR => 'OR'
868 );
869
870 }
871 foreach my $value (@NoMatch) {
872
873 # preprocess Classes, so we can search on class
874 if ( $value && $value =~ /^~(.*)/ ) {
875 $value = "%$1%";
876 $op = 'NOT LIKE';
877 }
878 else {
879 $op = '!=';
880 }
881 if ( $field eq 'Class' ) {
882 my $class = RT::Class->new($RT::SystemUser);
883 $class->Load($value);
884 $value = $class->Id;
885 }
886
887 # now that we've pruned the value, get out if it's different.
888 next unless $value;
889
890 $self->Limit(
891 SUBCLAUSE => $field . 'NoMatch',
892 OPERATOR => $op,
893 VALUE => $value,
894 CASESENSITIVE => 0,
895 FIELD => $field,
896 ENTRYAGGREGATOR => 'AND'
897 );
898
899 }
900 }
901
902 if ($order_by && @$order_by) {
903 if ( $order_by->[0] && $order_by->[0] =~ /\|/ ) {
904 @$order_by = split '|', $order_by->[0];
905 @$order = split '|', $order->[0];
906 }
907 my @tmp =
908 map { { FIELD => $order_by->[$_], ORDER => $order->[$_] } } 0 .. $#{$order_by};
909 $self->OrderByCols(@tmp);
910 }
911
912 return 1;
913}
914
84fb5b46
MKG
915RT::Base->_ImportOverlays();
916
9171;