1 # BEGIN BPS TAGGED BLOCK {{{
5 # This software is Copyright (c) 1996-2014 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 Returns the next article that this user can see.
76 my $Object = $self->SUPER::Next();
77 if ( ( defined($Object) ) and ( ref($Object) ) ) {
79 if ( $Object->CurrentUserHasRight('ShowArticle') ) {
83 #If the user doesn't have the right to show this Object
85 return ( $self->Next() );
89 #if there never was any queue
96 =head2 Limit { FIELD => undef, OPERATOR => '=', VALUE => 'undef'}
98 Limit the result set. See DBIx::SearchBuilder docs
99 In addition to the "normal" stuff, value can be an array.
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 );
118 $self->SUPER::Limit(%ARGS);
122 =head2 LimitName { OPERATOR => 'LIKE', VALUE => undef }
124 Find all articles with Name fields which satisfy OPERATOR for VALUE
143 =head2 LimitSummary { OPERATOR => 'LIKE', VALUE => undef }
145 Find all articles with summary fields which satisfy OPERATOR for VALUE
180 FIELD => 'CreatedBy',
207 FIELD => 'UpdatedBy',
217 # {{{ LimitToParent ID
219 =head2 LimitToParent ID
221 Limit the returned set of articles to articles which are children
223 This does not recurse.
239 # {{{ LimitCustomField
241 =head2 LimitCustomField HASH
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.
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)
252 The subclause that the LIMIT statement(s) should be done in can also
253 be passed in with a SUBCLAUSE parameter.
257 sub LimitCustomField {
261 ENTRYAGGREGATOR => 'OR',
269 my $value = $args{'VALUE'};
270 # XXX: this work in a different way than RT
271 return unless $value; #strip out total blank wildcards
273 my $ObjectValuesAlias = $self->Join(
277 TABLE2 => 'ObjectCustomFieldValues',
278 FIELD2 => 'ObjectId',
279 EXPRESSION => 'main.id'
283 LEFTJOIN => $ObjectValuesAlias,
288 if ( $args{'FIELD'} ) {
291 if (UNIVERSAL::isa($args{'FIELD'} ,'RT::CustomField')) {
292 $field_id = $args{'FIELD'}->id;
293 } elsif($args{'FIELD'} =~ /^\d+$/) {
294 $field_id = $args{'FIELD'};
297 $self->Limit( LEFTJOIN => $ObjectValuesAlias,
298 FIELD => 'CustomField',
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',
307 # ENTRYAGGREGATOR => 'OR',);
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,
316 VALUE => $args{'FIELD'},
317 ENTRYAGGREGATOR => 'OR',
321 FIELD => 'LookupType',
323 RT::Article->new($RT::SystemUser)->CustomFieldLookupType()
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
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
335 # TODO this should deal with starts with and ends with
339 return $op unless RT->Config->Get('DatabaseType') eq 'Oracle';
340 return 'MATCHES' if $op eq '=';
341 return 'NOT MATCHES' if $op eq '!=';
345 my $clause = $args{'SUBCLAUSE'} || $ObjectValuesAlias;
347 if ( $args{'OPERATOR'} eq '!=' || $args{'OPERATOR'} =~ /^not like$/i ) {
349 if ( $args{'OPERATOR'} eq '!=' ) {
352 elsif ( $args{'OPERATOR'} =~ /^not like$/i ) {
357 LEFTJOIN => $ObjectValuesAlias,
361 QUOTEVALUE => $args{'QUOTEVALUE'},
362 ENTRYAGGREGATOR => 'AND', #$args{'ENTRYAGGREGATOR'},
363 SUBCLAUSE => $clause,
367 ALIAS => $ObjectValuesAlias,
372 ENTRYAGGREGATOR => 'AND',
373 SUBCLAUSE => $clause,
378 ALIAS => $ObjectValuesAlias,
379 FIELD => 'LargeContent',
380 OPERATOR => $fix_op->($args{'OPERATOR'}),
382 QUOTEVALUE => $args{'QUOTEVALUE'},
383 ENTRYAGGREGATOR => $args{'ENTRYAGGREGATOR'},
384 SUBCLAUSE => $clause,
388 ALIAS => $ObjectValuesAlias,
390 OPERATOR => $args{'OPERATOR'},
392 QUOTEVALUE => $args{'QUOTEVALUE'},
393 ENTRYAGGREGATOR => $args{'ENTRYAGGREGATOR'},
394 SUBCLAUSE => $clause,
407 my $topics = $self->NewAlias('ObjectTopics');
412 ENTRYAGGREGATOR => 'OR'
418 FIELD => 'ObjectType',
419 VALUE => 'RT::Article',
425 FIELD2 => 'ObjectId',
431 # {{{ LimitRefersTo URI
433 =head2 LimitRefersTo URI
435 Limit the result set to only articles which are referred to by the URI passed in.
443 my $uri_obj = RT::URI->new($self->CurrentUser);
444 $uri_obj->FromURI($uri);
445 my $links = $self->NewAlias('Links');
449 VALUE => $uri_obj->URI
463 # {{{ LimitReferredToBy URI
465 =head2 LimitReferredToBy URI
467 Limit the result set to only articles which are referred to by the URI passed in.
471 sub LimitReferredToBy {
475 my $uri_obj = RT::URI->new($self->CurrentUser);
476 $uri_obj->FromURI($uri);
477 my $links = $self->NewAlias('Links');
481 VALUE => $uri_obj->URI
495 =head2 LimitHostlistClasses
497 Only fetch Articles from classes where Hotlist is true.
501 sub LimitHotlistClasses {
504 my $classes = $self->Join(
510 $self->Limit( ALIAS => $classes, FIELD => 'HotList', VALUE => 1 );
513 =head2 LimitAppliedClasses Queue => QueueObj
515 Takes a Queue and limits articles returned to classes which are applied to that Queue
517 Accepts either a Queue obj or a Queue id
521 sub LimitAppliedClasses {
525 unless (ref $args{Queue} || $args{Queue} =~/^[0-9]+$/) {
526 $RT::Logger->error("Not a valid Queue: $args{Queue}");
530 my $queue = ( ref $args{Queue} ? $args{Queue}->Id : $args{Queue} );
532 my $oc_alias = $self->Join(
535 TABLE2 => 'ObjectClasses',
539 my $subclause = "possibleobjectclasses";
540 $self->_OpenParen($subclause);
541 $self->Limit( ALIAS => $oc_alias,
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);
553 $self->_OpenParen($subclause);
554 $self->Limit( ALIAS => $oc_alias,
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);
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};
581 SUBCLAUSE => 'NameOrSummary',
583 ENTRYAGGREGATOR => 'OR',
589 SUBCLAUSE => 'NameOrSummary',
591 ENTRYAGGREGATOR => 'OR',
598 foreach my $date (qw(Created< Created> LastUpdated< LastUpdated>)) {
599 next unless ( $args{$date} );
600 my $date_obj = RT::Date->new( $self->CurrentUser );
601 $date_obj->Set( Format => 'unknown', Value => $args{$date} );
602 $dates->{$date} = $date_obj;
604 if ( $date =~ /^(.*?)<$/i ) {
608 ENTRYAGGREGATOR => "AND",
609 VALUE => $date_obj->ISO
613 if ( $date =~ /^(.*?)>$/i ) {
617 ENTRYAGGREGATOR => "AND",
618 VALUE => $date_obj->ISO
624 if ($args{'RefersTo'}) {
625 foreach my $link ( split( /\s+/, $args{'RefersTo'} ) ) {
627 $self->LimitRefersTo($link);
631 if ($args{'ReferredToBy'}) {
632 foreach my $link ( split( /\s+/, $args{'ReferredToBy'} ) ) {
634 $self->LimitReferredToBy($link);
638 if ( $args{'Topics'} ) {
640 ( ref $args{'Topics'} eq 'ARRAY' )
641 ? @{ $args{'Topics'} }
642 : ( $args{'Topics'} );
643 @Topics = map { split } @Topics;
644 if ( $args{'ExpandTopics'} ) {
647 my $id = shift @Topics;
648 next if $topics{$id};
650 RT::Topics->new( $self->CurrentUser );
651 $Topics->Limit( FIELD => 'Parent', VALUE => $id );
652 push @Topics, $_->Id while $_ = $Topics->Next;
655 @Topics = keys %topics;
656 $args{'Topics'} = \@Topics;
658 $self->LimitTopics(@Topics);
662 $customfields->LimitToLookupType(
663 RT::Article->new( $self->CurrentUser )
664 ->CustomFieldLookupType );
665 if ( $args{'Class'} ) {
667 ( ref $args{'Class'} eq 'ARRAY' )
668 ? @{ $args{'Class'} }
669 : ( $args{'Class'} );
670 foreach my $class (@Classes) {
671 $customfields->LimitToGlobalOrObjectId($class);
675 $customfields->LimitToGlobalOrObjectId();
677 while ( my $cf = $customfields->Next ) {
678 $cfs{ $cf->Name } = $cf->Id;
681 # reset the iterator because we use this to build the UI
682 $customfields->GotoFirstItem;
684 foreach my $field ( keys %cfs ) {
687 ( ref $args{ $field . "~" } eq 'ARRAY' )
688 ? @{ $args{ $field . "~" } }
689 : ( $args{ $field . "~" } );
691 ( ref $args{ $field . "!~" } eq 'ARRAY' )
692 ? @{ $args{ $field . "!~" } }
693 : ( $args{ $field . "!~" } );
696 ( ref $args{$field} eq 'ARRAY' )
700 ( ref $args{ $field . "!" } eq 'ARRAY' )
701 ? @{ $args{ $field . "!" } }
702 : ( $args{ $field . "!" } );
704 foreach my $val (@MatchLike) {
706 push @Match, "~" . $val;
709 foreach my $val (@NoMatchLike) {
711 push @NoMatch, "~" . $val;
714 foreach my $value (@Match) {
717 if ( $value =~ /^~(.*)$/ ) {
724 $self->LimitCustomField(
725 FIELD => $cfs{$field},
728 ENTRYAGGREGATOR => 'OR',
732 foreach my $value (@NoMatch) {
735 if ( $value =~ /^~(.*)$/ ) {
742 $self->LimitCustomField(
743 FIELD => $cfs{$field},
746 ENTRYAGGREGATOR => 'OR',
752 ### Searches for any field
754 if ( $args{'Article~'} ) {
755 $self->LimitCustomField(
756 VALUE => $args{'Article~'},
757 ENTRYAGGREGATOR => 'OR',
760 SUBCLAUSE => 'SearchAll'
763 SUBCLAUSE => 'SearchAll',
765 VALUE => $args{'Article~'},
766 ENTRYAGGREGATOR => 'OR',
771 SUBCLAUSE => 'SearchAll',
773 VALUE => $args{'Article~'},
774 ENTRYAGGREGATOR => 'OR',
780 if ( $args{'Article!~'} ) {
781 $self->LimitCustomField(
782 VALUE => $args{'Article!~'},
783 OPERATOR => 'NOT LIKE',
785 SUBCLAUSE => 'SearchAll'
788 SUBCLAUSE => 'SearchAll',
790 VALUE => $args{'Article!~'},
791 ENTRYAGGREGATOR => 'AND',
793 OPERATOR => 'NOT LIKE'
796 SUBCLAUSE => 'SearchAll',
798 VALUE => $args{'Article!~'},
799 ENTRYAGGREGATOR => 'AND',
801 OPERATOR => 'NOT LIKE'
805 foreach my $field (qw(Name Summary Class)) {
808 ( ref $args{ $field . "~" } eq 'ARRAY' )
809 ? @{ $args{ $field . "~" } }
810 : ( $args{ $field . "~" } );
812 ( ref $args{ $field . "!~" } eq 'ARRAY' )
813 ? @{ $args{ $field . "!~" } }
814 : ( $args{ $field . "!~" } );
817 ( ref $args{$field} eq 'ARRAY' )
821 ( ref $args{ $field . "!" } eq 'ARRAY' )
822 ? @{ $args{ $field . "!" } }
823 : ( $args{ $field . "!" } );
825 foreach my $val (@MatchLike) {
827 push @Match, "~" . $val;
830 foreach my $val (@NoMatchLike) {
832 push @NoMatch, "~" . $val;
836 foreach my $value (@Match) {
837 if ( $value && $value =~ /^~(.*)$/ ) {
845 # preprocess Classes, so we can search on class
846 if ( $field eq 'Class' && $value ) {
847 my $class = RT::Class->new($RT::SystemUser);
848 $class->Load($value);
852 # now that we've pruned the value, get out if it's different.
856 SUBCLAUSE => $field . 'Match',
861 ENTRYAGGREGATOR => 'OR'
865 foreach my $value (@NoMatch) {
867 # preprocess Classes, so we can search on class
868 if ( $value && $value =~ /^~(.*)/ ) {
875 if ( $field eq 'Class' ) {
876 my $class = RT::Class->new($RT::SystemUser);
877 $class->Load($value);
881 # now that we've pruned the value, get out if it's different.
885 SUBCLAUSE => $field . 'NoMatch',
890 ENTRYAGGREGATOR => 'AND'
896 if ($order_by && @$order_by) {
897 if ( $order_by->[0] && $order_by->[0] =~ /\|/ ) {
898 @$order_by = split '|', $order_by->[0];
899 @$order = split '|', $order->[0];
902 map { { FIELD => $order_by->[$_], ORDER => $order->[$_] } } 0 .. $#{$order_by};
903 $self->OrderByCols(@tmp);
909 RT::Base->_ImportOverlays();