1 # BEGIN BPS TAGGED BLOCK {{{
5 # This software is Copyright (c) 1996-2015 Best Practical Solutions, LLC
6 # <sales@bestpractical.com>
8 # (Except where explicitly superseded by other copyright notices)
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
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.
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.
30 # CONTRIBUTION SUBMISSION POLICY:
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.)
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.
47 # END BPS TAGGED BLOCK }}}
54 use base 'RT::SearchBuilder';
56 sub Table {'Articles'}
61 { FIELD => 'SortOrder', ORDER => 'ASC' },
62 { FIELD => 'Name', ORDER => 'ASC' },
64 return $self->SUPER::_Init( @_ );
69 Overrides the collection to ensure that only Articles the user can see
78 return unless $record->CurrentUserHasRight('ShowArticle');
79 return $self->SUPER::AddRecord( $record );
82 =head2 Limit { FIELD => undef, OPERATOR => '=', VALUE => 'undef'}
84 Limit the result set. See DBIx::SearchBuilder docs
85 In addition to the "normal" stuff, value can be an array.
96 if ( ref( $ARGS{'VALUE'} ) ) {
97 my @values = $ARGS{'VALUE'};
98 delete $ARGS{'VALUE'};
99 foreach my $v (@values) {
100 $self->SUPER::Limit( %ARGS, VALUE => $v );
104 $self->SUPER::Limit(%ARGS);
108 =head2 LimitName { OPERATOR => 'LIKE', VALUE => undef }
110 Find all articles with Name fields which satisfy OPERATOR for VALUE
129 =head2 LimitSummary { OPERATOR => 'LIKE', VALUE => undef }
131 Find all articles with summary fields which satisfy OPERATOR for VALUE
166 FIELD => 'CreatedBy',
193 FIELD => 'UpdatedBy',
203 # {{{ LimitToParent ID
205 =head2 LimitToParent ID
207 Limit the returned set of articles to articles which are children
209 This does not recurse.
225 # {{{ LimitCustomField
227 =head2 LimitCustomField HASH
229 Limit the result set to articles which have or do not have the custom field
230 value listed, using a left join to catch things where no rows match.
232 HASH needs the following fields:
233 FIELD (A custom field id) or undef for any custom field
234 ENTRYAGGREGATOR => (AND, OR)
235 OPERATOR ('=', 'LIKE', '!=', 'NOT LIKE')
236 VALUE ( a single scalar value or a list of possible values to be concatenated with ENTRYAGGREGATOR)
238 The subclause that the LIMIT statement(s) should be done in can also
239 be passed in with a SUBCLAUSE parameter.
243 sub LimitCustomField {
247 ENTRYAGGREGATOR => 'OR',
255 my $value = $args{'VALUE'};
256 # XXX: this work in a different way than RT
257 return unless $value; #strip out total blank wildcards
259 my $ObjectValuesAlias = $self->Join(
263 TABLE2 => 'ObjectCustomFieldValues',
264 FIELD2 => 'ObjectId',
265 EXPRESSION => 'main.id'
269 LEFTJOIN => $ObjectValuesAlias,
274 if ( $args{'FIELD'} ) {
277 if (UNIVERSAL::isa($args{'FIELD'} ,'RT::CustomField')) {
278 $field_id = $args{'FIELD'}->id;
279 } elsif($args{'FIELD'} =~ /^\d+$/) {
280 $field_id = $args{'FIELD'};
283 $self->Limit( LEFTJOIN => $ObjectValuesAlias,
284 FIELD => 'CustomField',
286 ENTRYAGGREGATOR => 'AND');
287 # Could convert the above to a non-left join and also enable the thing below
288 # $self->SUPER::Limit( ALIAS => $ObjectValuesAlias,
289 # FIELD => 'CustomField',
293 # ENTRYAGGREGATOR => 'OR',);
295 # Search for things by name if the cf was specced by name.
296 my $fields = $self->NewAlias('CustomFields');
297 $self->Join( TYPE => 'left',
298 ALIAS1 => $ObjectValuesAlias , FIELD1 => 'CustomField',
299 ALIAS2 => $fields, FIELD2=> 'id');
300 $self->Limit( ALIAS => $fields,
302 VALUE => $args{'FIELD'},
303 ENTRYAGGREGATOR => 'OR',
307 FIELD => 'LookupType',
309 RT::Article->new($RT::SystemUser)->CustomFieldLookupType()
314 # If we're trying to find articles where a custom field value
315 # doesn't match something, be sure to find things where it's null
317 # basically, we do a left join on the value being applicable to
318 # the article and then we turn around and make sure that it's
319 # actually null in practise
321 # TODO this should deal with starts with and ends with
325 return $op unless RT->Config->Get('DatabaseType') eq 'Oracle';
326 return 'MATCHES' if $op eq '=';
327 return 'NOT MATCHES' if $op eq '!=';
331 my $clause = $args{'SUBCLAUSE'} || $ObjectValuesAlias;
333 if ( $args{'OPERATOR'} eq '!=' || $args{'OPERATOR'} =~ /^not like$/i ) {
335 if ( $args{'OPERATOR'} eq '!=' ) {
338 elsif ( $args{'OPERATOR'} =~ /^not like$/i ) {
343 LEFTJOIN => $ObjectValuesAlias,
347 QUOTEVALUE => $args{'QUOTEVALUE'},
348 ENTRYAGGREGATOR => 'AND', #$args{'ENTRYAGGREGATOR'},
349 SUBCLAUSE => $clause,
353 ALIAS => $ObjectValuesAlias,
358 ENTRYAGGREGATOR => 'AND',
359 SUBCLAUSE => $clause,
364 ALIAS => $ObjectValuesAlias,
365 FIELD => 'LargeContent',
366 OPERATOR => $fix_op->($args{'OPERATOR'}),
368 QUOTEVALUE => $args{'QUOTEVALUE'},
369 ENTRYAGGREGATOR => $args{'ENTRYAGGREGATOR'},
370 SUBCLAUSE => $clause,
374 ALIAS => $ObjectValuesAlias,
376 OPERATOR => $args{'OPERATOR'},
378 QUOTEVALUE => $args{'QUOTEVALUE'},
379 ENTRYAGGREGATOR => $args{'ENTRYAGGREGATOR'},
380 SUBCLAUSE => $clause,
392 return unless @topics;
394 my $topics = $self->NewAlias('ObjectTopics');
399 VALUE => [ @topics ],
404 FIELD => 'ObjectType',
405 VALUE => 'RT::Article',
411 FIELD2 => 'ObjectId',
417 # {{{ LimitRefersTo URI
419 =head2 LimitRefersTo URI
421 Limit the result set to only articles which are referred to by the URI passed in.
429 my $uri_obj = RT::URI->new($self->CurrentUser);
430 $uri_obj->FromURI($uri);
431 my $links = $self->NewAlias('Links');
435 VALUE => $uri_obj->URI
449 # {{{ LimitReferredToBy URI
451 =head2 LimitReferredToBy URI
453 Limit the result set to only articles which are referred to by the URI passed in.
457 sub LimitReferredToBy {
461 my $uri_obj = RT::URI->new($self->CurrentUser);
462 $uri_obj->FromURI($uri);
463 my $links = $self->NewAlias('Links');
467 VALUE => $uri_obj->URI
481 =head2 LimitHostlistClasses
483 Only fetch Articles from classes where Hotlist is true.
487 sub LimitHotlistClasses {
490 my $classes = $self->Join(
496 $self->Limit( ALIAS => $classes, FIELD => 'HotList', VALUE => 1 );
499 =head2 LimitAppliedClasses Queue => QueueObj
501 Takes a Queue and limits articles returned to classes which are applied to that Queue
503 Accepts either a Queue obj or a Queue id
507 sub LimitAppliedClasses {
511 unless (ref $args{Queue} || $args{Queue} =~/^[0-9]+$/) {
512 $RT::Logger->error("Not a valid Queue: $args{Queue}");
516 my $queue = ( ref $args{Queue} ? $args{Queue}->Id : $args{Queue} );
518 my $oc_alias = $self->Join(
521 TABLE2 => 'ObjectClasses',
525 my $subclause = "possibleobjectclasses";
526 $self->_OpenParen($subclause);
527 $self->Limit( ALIAS => $oc_alias,
530 SUBCLAUSE => $subclause,
531 ENTRYAGGREGATOR => 'OR' );
532 $self->Limit( ALIAS => $oc_alias,
533 FIELD => 'ObjectType',
534 VALUE => 'RT::Queue',
535 SUBCLAUSE => $subclause,
536 ENTRYAGGREGATOR => 'AND' );
537 $self->_CloseParen($subclause);
539 $self->_OpenParen($subclause);
540 $self->Limit( ALIAS => $oc_alias,
543 SUBCLAUSE => $subclause,
544 ENTRYAGGREGATOR => 'OR' );
545 $self->Limit( ALIAS => $oc_alias,
546 FIELD => 'ObjectType',
547 VALUE => 'RT::System',
548 SUBCLAUSE => $subclause,
549 ENTRYAGGREGATOR => 'AND' );
550 $self->_CloseParen($subclause);
559 my $customfields = $args{CustomFields}
560 || RT::CustomFields->new( $self->CurrentUser );
561 my $dates = $args{Dates} || {};
562 my $order_by = $args{OrderBy};
563 my $order = $args{Order};
567 SUBCLAUSE => 'NameOrSummary',
569 ENTRYAGGREGATOR => 'OR',
575 SUBCLAUSE => 'NameOrSummary',
577 ENTRYAGGREGATOR => 'OR',
584 foreach my $date (qw(Created< Created> LastUpdated< LastUpdated>)) {
585 next unless ( $args{$date} );
586 my $date_obj = RT::Date->new( $self->CurrentUser );
587 $date_obj->Set( Format => 'unknown', Value => $args{$date} );
588 $dates->{$date} = $date_obj;
590 if ( $date =~ /^(.*?)<$/i ) {
594 ENTRYAGGREGATOR => "AND",
595 VALUE => $date_obj->ISO
599 if ( $date =~ /^(.*?)>$/i ) {
603 ENTRYAGGREGATOR => "AND",
604 VALUE => $date_obj->ISO
610 if ($args{'RefersTo'}) {
611 foreach my $link ( split( /\s+/, $args{'RefersTo'} ) ) {
613 $self->LimitRefersTo($link);
617 if ($args{'ReferredToBy'}) {
618 foreach my $link ( split( /\s+/, $args{'ReferredToBy'} ) ) {
620 $self->LimitReferredToBy($link);
624 if ( $args{'Topics'} ) {
626 ( ref $args{'Topics'} eq 'ARRAY' )
627 ? @{ $args{'Topics'} }
628 : ( $args{'Topics'} );
629 @Topics = map { split } @Topics;
630 if ( $args{'ExpandTopics'} ) {
633 my $id = shift @Topics;
634 next if $topics{$id};
636 RT::Topics->new( $self->CurrentUser );
637 $Topics->Limit( FIELD => 'Parent', VALUE => $id );
638 push @Topics, $_->Id while $_ = $Topics->Next;
641 @Topics = keys %topics;
642 $args{'Topics'} = \@Topics;
644 $self->LimitTopics(@Topics);
648 $customfields->LimitToLookupType(
649 RT::Article->new( $self->CurrentUser )
650 ->CustomFieldLookupType );
651 if ( $args{'Class'} ) {
653 ( ref $args{'Class'} eq 'ARRAY' )
654 ? @{ $args{'Class'} }
655 : ( $args{'Class'} );
656 foreach my $class (@Classes) {
657 $customfields->LimitToGlobalOrObjectId($class);
661 $customfields->LimitToGlobalOrObjectId();
663 while ( my $cf = $customfields->Next ) {
664 $cfs{ $cf->Name } = $cf->Id;
667 # reset the iterator because we use this to build the UI
668 $customfields->GotoFirstItem;
670 foreach my $field ( keys %cfs ) {
673 ( ref $args{ $field . "~" } eq 'ARRAY' )
674 ? @{ $args{ $field . "~" } }
675 : ( $args{ $field . "~" } );
677 ( ref $args{ $field . "!~" } eq 'ARRAY' )
678 ? @{ $args{ $field . "!~" } }
679 : ( $args{ $field . "!~" } );
682 ( ref $args{$field} eq 'ARRAY' )
686 ( ref $args{ $field . "!" } eq 'ARRAY' )
687 ? @{ $args{ $field . "!" } }
688 : ( $args{ $field . "!" } );
690 foreach my $val (@MatchLike) {
692 push @Match, "~" . $val;
695 foreach my $val (@NoMatchLike) {
697 push @NoMatch, "~" . $val;
700 foreach my $value (@Match) {
703 if ( $value =~ /^~(.*)$/ ) {
710 $self->LimitCustomField(
711 FIELD => $cfs{$field},
714 ENTRYAGGREGATOR => 'OR',
718 foreach my $value (@NoMatch) {
721 if ( $value =~ /^~(.*)$/ ) {
728 $self->LimitCustomField(
729 FIELD => $cfs{$field},
732 ENTRYAGGREGATOR => 'OR',
738 ### Searches for any field
740 if ( $args{'Article~'} ) {
741 $self->LimitCustomField(
742 VALUE => $args{'Article~'},
743 ENTRYAGGREGATOR => 'OR',
746 SUBCLAUSE => 'SearchAll'
749 SUBCLAUSE => 'SearchAll',
751 VALUE => $args{'Article~'},
752 ENTRYAGGREGATOR => 'OR',
757 SUBCLAUSE => 'SearchAll',
759 VALUE => $args{'Article~'},
760 ENTRYAGGREGATOR => 'OR',
766 if ( $args{'Article!~'} ) {
767 $self->LimitCustomField(
768 VALUE => $args{'Article!~'},
769 OPERATOR => 'NOT LIKE',
771 SUBCLAUSE => 'SearchAll'
774 SUBCLAUSE => 'SearchAll',
776 VALUE => $args{'Article!~'},
777 ENTRYAGGREGATOR => 'AND',
779 OPERATOR => 'NOT LIKE'
782 SUBCLAUSE => 'SearchAll',
784 VALUE => $args{'Article!~'},
785 ENTRYAGGREGATOR => 'AND',
787 OPERATOR => 'NOT LIKE'
791 foreach my $field (qw(Name Summary Class)) {
794 ( ref $args{ $field . "~" } eq 'ARRAY' )
795 ? @{ $args{ $field . "~" } }
796 : ( $args{ $field . "~" } );
798 ( ref $args{ $field . "!~" } eq 'ARRAY' )
799 ? @{ $args{ $field . "!~" } }
800 : ( $args{ $field . "!~" } );
803 ( ref $args{$field} eq 'ARRAY' )
807 ( ref $args{ $field . "!" } eq 'ARRAY' )
808 ? @{ $args{ $field . "!" } }
809 : ( $args{ $field . "!" } );
811 foreach my $val (@MatchLike) {
813 push @Match, "~" . $val;
816 foreach my $val (@NoMatchLike) {
818 push @NoMatch, "~" . $val;
822 foreach my $value (@Match) {
823 if ( $value && $value =~ /^~(.*)$/ ) {
831 # preprocess Classes, so we can search on class
832 if ( $field eq 'Class' && $value ) {
833 my $class = RT::Class->new($RT::SystemUser);
834 $class->Load($value);
838 # now that we've pruned the value, get out if it's different.
842 SUBCLAUSE => $field . 'Match',
847 ENTRYAGGREGATOR => 'OR'
851 foreach my $value (@NoMatch) {
853 # preprocess Classes, so we can search on class
854 if ( $value && $value =~ /^~(.*)/ ) {
861 if ( $field eq 'Class' ) {
862 my $class = RT::Class->new($RT::SystemUser);
863 $class->Load($value);
867 # now that we've pruned the value, get out if it's different.
871 SUBCLAUSE => $field . 'NoMatch',
876 ENTRYAGGREGATOR => 'AND'
882 if ($order_by && @$order_by) {
883 if ( $order_by->[0] && $order_by->[0] =~ /\|/ ) {
884 @$order_by = split '|', $order_by->[0];
885 @$order = split '|', $order->[0];
888 map { { FIELD => $order_by->[$_], ORDER => $order->[$_] } } 0 .. $#{$order_by};
889 $self->OrderByCols(@tmp);
895 RT::Base->_ImportOverlays();