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,
365 ALIAS => $ObjectValuesAlias,
370 ENTRYAGGREGATOR => 'AND',
371 SUBCLAUSE => $clause,
376 ALIAS => $ObjectValuesAlias,
377 FIELD => 'LargeContent',
378 OPERATOR => $fix_op->($args{'OPERATOR'}),
380 QUOTEVALUE => $args{'QUOTEVALUE'},
381 ENTRYAGGREGATOR => $args{'ENTRYAGGREGATOR'},
382 SUBCLAUSE => $clause,
385 ALIAS => $ObjectValuesAlias,
387 OPERATOR => $args{'OPERATOR'},
389 QUOTEVALUE => $args{'QUOTEVALUE'},
390 ENTRYAGGREGATOR => $args{'ENTRYAGGREGATOR'},
391 SUBCLAUSE => $clause,
403 my $topics = $self->NewAlias('ObjectTopics');
408 ENTRYAGGREGATOR => 'OR'
414 FIELD => 'ObjectType',
415 VALUE => 'RT::Article',
421 FIELD2 => 'ObjectId',
427 # {{{ LimitRefersTo URI
429 =head2 LimitRefersTo URI
431 Limit the result set to only articles which are referred to by the URI passed in.
439 my $uri_obj = RT::URI->new($self->CurrentUser);
440 $uri_obj->FromURI($uri);
441 my $links = $self->NewAlias('Links');
445 VALUE => $uri_obj->URI
459 # {{{ LimitReferredToBy URI
461 =head2 LimitReferredToBy URI
463 Limit the result set to only articles which are referred to by the URI passed in.
467 sub LimitReferredToBy {
471 my $uri_obj = RT::URI->new($self->CurrentUser);
472 $uri_obj->FromURI($uri);
473 my $links = $self->NewAlias('Links');
477 VALUE => $uri_obj->URI
491 =head2 LimitHostlistClasses
493 Only fetch Articles from classes where Hotlist is true.
497 sub LimitHotlistClasses {
500 my $classes = $self->Join(
506 $self->Limit( ALIAS => $classes, FIELD => 'HotList', VALUE => 1 );
509 =head2 LimitAppliedClasses Queue => QueueObj
511 Takes a Queue and limits articles returned to classes which are applied to that Queue
513 Accepts either a Queue obj or a Queue id
517 sub LimitAppliedClasses {
521 unless (ref $args{Queue} || $args{Queue} =~/^[0-9]+$/) {
522 $RT::Logger->error("Not a valid Queue: $args{Queue}");
526 my $queue = ( ref $args{Queue} ? $args{Queue}->Id : $args{Queue} );
528 my $oc_alias = $self->Join(
531 TABLE2 => 'ObjectClasses',
535 my $subclause = "possibleobjectclasses";
536 $self->_OpenParen($subclause);
537 $self->Limit( ALIAS => $oc_alias,
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);
549 $self->_OpenParen($subclause);
550 $self->Limit( ALIAS => $oc_alias,
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);
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};
577 SUBCLAUSE => 'NameOrSummary',
579 ENTRYAGGREGATOR => 'OR',
585 SUBCLAUSE => 'NameOrSummary',
587 ENTRYAGGREGATOR => 'OR',
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;
602 if ( $date =~ /^(.*?)<$/i ) {
606 ENTRYAGGREGATOR => "AND",
607 VALUE => $date_obj->ISO
611 if ( $date =~ /^(.*?)>$/i ) {
615 ENTRYAGGREGATOR => "AND",
616 VALUE => $date_obj->ISO
622 if ($args{'RefersTo'}) {
623 foreach my $link ( split( /\s+/, $args{'RefersTo'} ) ) {
625 $self->LimitRefersTo($link);
629 if ($args{'ReferredToBy'}) {
630 foreach my $link ( split( /\s+/, $args{'ReferredToBy'} ) ) {
632 $self->LimitReferredToBy($link);
636 if ( $args{'Topics'} ) {
638 ( ref $args{'Topics'} eq 'ARRAY' )
639 ? @{ $args{'Topics'} }
640 : ( $args{'Topics'} );
641 @Topics = map { split } @Topics;
642 if ( $args{'ExpandTopics'} ) {
645 my $id = shift @Topics;
646 next if $topics{$id};
648 RT::Topics->new( $self->CurrentUser );
649 $Topics->Limit( FIELD => 'Parent', VALUE => $id );
650 push @Topics, $_->Id while $_ = $Topics->Next;
653 @Topics = keys %topics;
654 $args{'Topics'} = \@Topics;
656 $self->LimitTopics(@Topics);
660 $customfields->LimitToLookupType(
661 RT::Article->new( $self->CurrentUser )
662 ->CustomFieldLookupType );
663 if ( $args{'Class'} ) {
665 ( ref $args{'Class'} eq 'ARRAY' )
666 ? @{ $args{'Class'} }
667 : ( $args{'Class'} );
668 foreach my $class (@Classes) {
669 $customfields->LimitToGlobalOrObjectId($class);
673 $customfields->LimitToGlobalOrObjectId();
675 while ( my $cf = $customfields->Next ) {
676 $cfs{ $cf->Name } = $cf->Id;
679 # reset the iterator because we use this to build the UI
680 $customfields->GotoFirstItem;
682 foreach my $field ( keys %cfs ) {
685 ( ref $args{ $field . "~" } eq 'ARRAY' )
686 ? @{ $args{ $field . "~" } }
687 : ( $args{ $field . "~" } );
689 ( ref $args{ $field . "!~" } eq 'ARRAY' )
690 ? @{ $args{ $field . "!~" } }
691 : ( $args{ $field . "!~" } );
694 ( ref $args{$field} eq 'ARRAY' )
698 ( ref $args{ $field . "!" } eq 'ARRAY' )
699 ? @{ $args{ $field . "!" } }
700 : ( $args{ $field . "!" } );
702 foreach my $val (@MatchLike) {
704 push @Match, "~" . $val;
707 foreach my $val (@NoMatchLike) {
709 push @NoMatch, "~" . $val;
712 foreach my $value (@Match) {
715 if ( $value =~ /^~(.*)$/ ) {
722 $self->LimitCustomField(
723 FIELD => $cfs{$field},
726 ENTRYAGGREGATOR => 'OR',
730 foreach my $value (@NoMatch) {
733 if ( $value =~ /^~(.*)$/ ) {
740 $self->LimitCustomField(
741 FIELD => $cfs{$field},
744 ENTRYAGGREGATOR => 'OR',
750 ### Searches for any field
752 if ( $args{'Article~'} ) {
753 $self->LimitCustomField(
754 VALUE => $args{'Article~'},
755 ENTRYAGGREGATOR => 'OR',
758 SUBCLAUSE => 'SearchAll'
761 SUBCLAUSE => 'SearchAll',
763 VALUE => $args{'Article~'},
764 ENTRYAGGREGATOR => 'OR',
769 SUBCLAUSE => 'SearchAll',
771 VALUE => $args{'Article~'},
772 ENTRYAGGREGATOR => 'OR',
778 if ( $args{'Article!~'} ) {
779 $self->LimitCustomField(
780 VALUE => $args{'Article!~'},
781 OPERATOR => 'NOT LIKE',
783 SUBCLAUSE => 'SearchAll'
786 SUBCLAUSE => 'SearchAll',
788 VALUE => $args{'Article!~'},
789 ENTRYAGGREGATOR => 'AND',
791 OPERATOR => 'NOT LIKE'
794 SUBCLAUSE => 'SearchAll',
796 VALUE => $args{'Article!~'},
797 ENTRYAGGREGATOR => 'AND',
799 OPERATOR => 'NOT LIKE'
803 foreach my $field (qw(Name Summary Class)) {
806 ( ref $args{ $field . "~" } eq 'ARRAY' )
807 ? @{ $args{ $field . "~" } }
808 : ( $args{ $field . "~" } );
810 ( ref $args{ $field . "!~" } eq 'ARRAY' )
811 ? @{ $args{ $field . "!~" } }
812 : ( $args{ $field . "!~" } );
815 ( ref $args{$field} eq 'ARRAY' )
819 ( ref $args{ $field . "!" } eq 'ARRAY' )
820 ? @{ $args{ $field . "!" } }
821 : ( $args{ $field . "!" } );
823 foreach my $val (@MatchLike) {
825 push @Match, "~" . $val;
828 foreach my $val (@NoMatchLike) {
830 push @NoMatch, "~" . $val;
834 foreach my $value (@Match) {
835 if ( $value && $value =~ /^~(.*)$/ ) {
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);
850 # now that we've pruned the value, get out if it's different.
854 SUBCLAUSE => $field . 'Match',
859 ENTRYAGGREGATOR => 'OR'
863 foreach my $value (@NoMatch) {
865 # preprocess Classes, so we can search on class
866 if ( $value && $value =~ /^~(.*)/ ) {
873 if ( $field eq 'Class' ) {
874 my $class = RT::Class->new($RT::SystemUser);
875 $class->Load($value);
879 # now that we've pruned the value, get out if it's different.
883 SUBCLAUSE => $field . 'NoMatch',
888 ENTRYAGGREGATOR => 'AND'
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];
900 map { { FIELD => $order_by->[$_], ORDER => $order->[$_] } } 0 .. $#{$order_by};
901 $self->OrderByCols(@tmp);
910 Returns an empty new RT::Article item
916 return(RT::Article->new($self->CurrentUser));
921 RT::Base->_ImportOverlays();