1 # BEGIN BPS TAGGED BLOCK {{{
5 # This software is Copyright (c) 1996-2012 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');
320 FIELD => 'LookupType',
322 RT::Article->new($RT::SystemUser)->CustomFieldLookupType()
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
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
334 # TODO this should deal with starts with and ends with
338 return $op unless RT->Config->Get('DatabaseType') eq 'Oracle';
339 return 'MATCHES' if $op eq '=';
340 return 'NOT MATCHES' if $op eq '!=';
344 my $clause = $args{'SUBCLAUSE'} || $ObjectValuesAlias;
346 if ( $args{'OPERATOR'} eq '!=' || $args{'OPERATOR'} =~ /^not like$/i ) {
348 if ( $args{'OPERATOR'} eq '!=' ) {
351 elsif ( $args{'OPERATOR'} =~ /^not like$/i ) {
356 LEFTJOIN => $ObjectValuesAlias,
360 QUOTEVALUE => $args{'QUOTEVALUE'},
361 ENTRYAGGREGATOR => 'AND', #$args{'ENTRYAGGREGATOR'},
362 SUBCLAUSE => $clause,
366 ALIAS => $ObjectValuesAlias,
371 ENTRYAGGREGATOR => 'AND',
372 SUBCLAUSE => $clause,
377 ALIAS => $ObjectValuesAlias,
378 FIELD => 'LargeContent',
379 OPERATOR => $fix_op->($args{'OPERATOR'}),
381 QUOTEVALUE => $args{'QUOTEVALUE'},
382 ENTRYAGGREGATOR => $args{'ENTRYAGGREGATOR'},
383 SUBCLAUSE => $clause,
387 ALIAS => $ObjectValuesAlias,
389 OPERATOR => $args{'OPERATOR'},
391 QUOTEVALUE => $args{'QUOTEVALUE'},
392 ENTRYAGGREGATOR => $args{'ENTRYAGGREGATOR'},
393 SUBCLAUSE => $clause,
406 my $topics = $self->NewAlias('ObjectTopics');
411 ENTRYAGGREGATOR => 'OR'
417 FIELD => 'ObjectType',
418 VALUE => 'RT::Article',
424 FIELD2 => 'ObjectId',
430 # {{{ LimitRefersTo URI
432 =head2 LimitRefersTo URI
434 Limit the result set to only articles which are referred to by the URI passed in.
442 my $uri_obj = RT::URI->new($self->CurrentUser);
443 $uri_obj->FromURI($uri);
444 my $links = $self->NewAlias('Links');
448 VALUE => $uri_obj->URI
462 # {{{ LimitReferredToBy URI
464 =head2 LimitReferredToBy URI
466 Limit the result set to only articles which are referred to by the URI passed in.
470 sub LimitReferredToBy {
474 my $uri_obj = RT::URI->new($self->CurrentUser);
475 $uri_obj->FromURI($uri);
476 my $links = $self->NewAlias('Links');
480 VALUE => $uri_obj->URI
494 =head2 LimitHostlistClasses
496 Only fetch Articles from classes where Hotlist is true.
500 sub LimitHotlistClasses {
503 my $classes = $self->Join(
509 $self->Limit( ALIAS => $classes, FIELD => 'HotList', VALUE => 1 );
512 =head2 LimitAppliedClasses Queue => QueueObj
514 Takes a Queue and limits articles returned to classes which are applied to that Queue
516 Accepts either a Queue obj or a Queue id
520 sub LimitAppliedClasses {
524 unless (ref $args{Queue} || $args{Queue} =~/^[0-9]+$/) {
525 $RT::Logger->error("Not a valid Queue: $args{Queue}");
529 my $queue = ( ref $args{Queue} ? $args{Queue}->Id : $args{Queue} );
531 my $oc_alias = $self->Join(
534 TABLE2 => 'ObjectClasses',
538 my $subclause = "possibleobjectclasses";
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::Queue',
548 SUBCLAUSE => $subclause,
549 ENTRYAGGREGATOR => 'AND' );
550 $self->_CloseParen($subclause);
552 $self->_OpenParen($subclause);
553 $self->Limit( ALIAS => $oc_alias,
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);
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};
580 SUBCLAUSE => 'NameOrSummary',
582 ENTRYAGGREGATOR => 'OR',
588 SUBCLAUSE => 'NameOrSummary',
590 ENTRYAGGREGATOR => 'OR',
597 require Time::ParseDate;
598 foreach my $date (qw(Created< Created> LastUpdated< LastUpdated>)) {
599 next unless ( $args{$date} );
600 my $seconds = Time::ParseDate::parsedate( $args{$date}, FUZZY => 1, PREFER_PAST => 1 );
601 my $date_obj = RT::Date->new( $self->CurrentUser );
602 $date_obj->Set( Format => 'unix', Value => $seconds );
603 $dates->{$date} = $date_obj;
605 if ( $date =~ /^(.*?)<$/i ) {
609 ENTRYAGGREGATOR => "AND",
610 VALUE => $date_obj->ISO
614 if ( $date =~ /^(.*?)>$/i ) {
618 ENTRYAGGREGATOR => "AND",
619 VALUE => $date_obj->ISO
625 if ($args{'RefersTo'}) {
626 foreach my $link ( split( /\s+/, $args{'RefersTo'} ) ) {
628 $self->LimitRefersTo($link);
632 if ($args{'ReferredToBy'}) {
633 foreach my $link ( split( /\s+/, $args{'ReferredToBy'} ) ) {
635 $self->LimitReferredToBy($link);
639 if ( $args{'Topics'} ) {
641 ( ref $args{'Topics'} eq 'ARRAY' )
642 ? @{ $args{'Topics'} }
643 : ( $args{'Topics'} );
644 @Topics = map { split } @Topics;
645 if ( $args{'ExpandTopics'} ) {
648 my $id = shift @Topics;
649 next if $topics{$id};
651 RT::Topics->new( $self->CurrentUser );
652 $Topics->Limit( FIELD => 'Parent', VALUE => $id );
653 push @Topics, $_->Id while $_ = $Topics->Next;
656 @Topics = keys %topics;
657 $args{'Topics'} = \@Topics;
659 $self->LimitTopics(@Topics);
663 $customfields->LimitToLookupType(
664 RT::Article->new( $self->CurrentUser )
665 ->CustomFieldLookupType );
666 if ( $args{'Class'} ) {
668 ( ref $args{'Class'} eq 'ARRAY' )
669 ? @{ $args{'Class'} }
670 : ( $args{'Class'} );
671 foreach my $class (@Classes) {
672 $customfields->LimitToGlobalOrObjectId($class);
676 $customfields->LimitToGlobalOrObjectId();
678 while ( my $cf = $customfields->Next ) {
679 $cfs{ $cf->Name } = $cf->Id;
682 # reset the iterator because we use this to build the UI
683 $customfields->GotoFirstItem;
685 foreach my $field ( keys %cfs ) {
688 ( ref $args{ $field . "~" } eq 'ARRAY' )
689 ? @{ $args{ $field . "~" } }
690 : ( $args{ $field . "~" } );
692 ( ref $args{ $field . "!~" } eq 'ARRAY' )
693 ? @{ $args{ $field . "!~" } }
694 : ( $args{ $field . "!~" } );
697 ( ref $args{$field} eq 'ARRAY' )
701 ( ref $args{ $field . "!" } eq 'ARRAY' )
702 ? @{ $args{ $field . "!" } }
703 : ( $args{ $field . "!" } );
705 foreach my $val (@MatchLike) {
707 push @Match, "~" . $val;
710 foreach my $val (@NoMatchLike) {
712 push @NoMatch, "~" . $val;
715 foreach my $value (@Match) {
718 if ( $value =~ /^~(.*)$/ ) {
725 $self->LimitCustomField(
726 FIELD => $cfs{$field},
729 ENTRYAGGREGATOR => 'OR',
733 foreach my $value (@NoMatch) {
736 if ( $value =~ /^~(.*)$/ ) {
743 $self->LimitCustomField(
744 FIELD => $cfs{$field},
747 ENTRYAGGREGATOR => 'OR',
753 ### Searches for any field
755 if ( $args{'Article~'} ) {
756 $self->LimitCustomField(
757 VALUE => $args{'Article~'},
758 ENTRYAGGREGATOR => 'OR',
761 SUBCLAUSE => 'SearchAll'
764 SUBCLAUSE => 'SearchAll',
766 VALUE => $args{'Article~'},
767 ENTRYAGGREGATOR => 'OR',
772 SUBCLAUSE => 'SearchAll',
774 VALUE => $args{'Article~'},
775 ENTRYAGGREGATOR => 'OR',
781 if ( $args{'Article!~'} ) {
782 $self->LimitCustomField(
783 VALUE => $args{'Article!~'},
784 OPERATOR => 'NOT LIKE',
786 SUBCLAUSE => 'SearchAll'
789 SUBCLAUSE => 'SearchAll',
791 VALUE => $args{'Article!~'},
792 ENTRYAGGREGATOR => 'AND',
794 OPERATOR => 'NOT LIKE'
797 SUBCLAUSE => 'SearchAll',
799 VALUE => $args{'Article!~'},
800 ENTRYAGGREGATOR => 'AND',
802 OPERATOR => 'NOT LIKE'
806 foreach my $field (qw(Name Summary Class)) {
809 ( ref $args{ $field . "~" } eq 'ARRAY' )
810 ? @{ $args{ $field . "~" } }
811 : ( $args{ $field . "~" } );
813 ( ref $args{ $field . "!~" } eq 'ARRAY' )
814 ? @{ $args{ $field . "!~" } }
815 : ( $args{ $field . "!~" } );
818 ( ref $args{$field} eq 'ARRAY' )
822 ( ref $args{ $field . "!" } eq 'ARRAY' )
823 ? @{ $args{ $field . "!" } }
824 : ( $args{ $field . "!" } );
826 foreach my $val (@MatchLike) {
828 push @Match, "~" . $val;
831 foreach my $val (@NoMatchLike) {
833 push @NoMatch, "~" . $val;
837 foreach my $value (@Match) {
838 if ( $value && $value =~ /^~(.*)$/ ) {
846 # preprocess Classes, so we can search on class
847 if ( $field eq 'Class' && $value ) {
848 my $class = RT::Class->new($RT::SystemUser);
849 $class->Load($value);
853 # now that we've pruned the value, get out if it's different.
857 SUBCLAUSE => $field . 'Match',
862 ENTRYAGGREGATOR => 'OR'
866 foreach my $value (@NoMatch) {
868 # preprocess Classes, so we can search on class
869 if ( $value && $value =~ /^~(.*)/ ) {
876 if ( $field eq 'Class' ) {
877 my $class = RT::Class->new($RT::SystemUser);
878 $class->Load($value);
882 # now that we've pruned the value, get out if it's different.
886 SUBCLAUSE => $field . 'NoMatch',
891 ENTRYAGGREGATOR => 'AND'
897 if ($order_by && @$order_by) {
898 if ( $order_by->[0] && $order_by->[0] =~ /\|/ ) {
899 @$order_by = split '|', $order_by->[0];
900 @$order = split '|', $order->[0];
903 map { { FIELD => $order_by->[$_], ORDER => $order->[$_] } } 0 .. $#{$order_by};
904 $self->OrderByCols(@tmp);
913 Returns an empty new RT::Article item
919 return(RT::Article->new($self->CurrentUser));
924 RT::Base->_ImportOverlays();