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');
306 FIELD => 'LookupType',
308 RT::Article->new($RT::SystemUser)->CustomFieldLookupType()
313 # If we're trying to find articles where a custom field value
314 # doesn't match something, be sure to find things where it's null
316 # basically, we do a left join on the value being applicable to
317 # the article and then we turn around and make sure that it's
318 # actually null in practise
320 # TODO this should deal with starts with and ends with
324 return $op unless RT->Config->Get('DatabaseType') eq 'Oracle';
325 return 'MATCHES' if $op eq '=';
326 return 'NOT MATCHES' if $op eq '!=';
330 my $clause = $args{'SUBCLAUSE'} || $ObjectValuesAlias;
332 if ( $args{'OPERATOR'} eq '!=' || $args{'OPERATOR'} =~ /^not like$/i ) {
334 if ( $args{'OPERATOR'} eq '!=' ) {
337 elsif ( $args{'OPERATOR'} =~ /^not like$/i ) {
342 LEFTJOIN => $ObjectValuesAlias,
346 QUOTEVALUE => $args{'QUOTEVALUE'},
347 ENTRYAGGREGATOR => 'AND', #$args{'ENTRYAGGREGATOR'},
348 SUBCLAUSE => $clause,
352 ALIAS => $ObjectValuesAlias,
357 ENTRYAGGREGATOR => 'AND',
358 SUBCLAUSE => $clause,
363 ALIAS => $ObjectValuesAlias,
364 FIELD => 'LargeContent',
365 OPERATOR => $fix_op->($args{'OPERATOR'}),
367 QUOTEVALUE => $args{'QUOTEVALUE'},
368 ENTRYAGGREGATOR => $args{'ENTRYAGGREGATOR'},
369 SUBCLAUSE => $clause,
373 ALIAS => $ObjectValuesAlias,
375 OPERATOR => $args{'OPERATOR'},
377 QUOTEVALUE => $args{'QUOTEVALUE'},
378 ENTRYAGGREGATOR => $args{'ENTRYAGGREGATOR'},
379 SUBCLAUSE => $clause,
392 my $topics = $self->NewAlias('ObjectTopics');
397 ENTRYAGGREGATOR => 'OR'
403 FIELD => 'ObjectType',
404 VALUE => 'RT::Article',
410 FIELD2 => 'ObjectId',
416 # {{{ LimitRefersTo URI
418 =head2 LimitRefersTo URI
420 Limit the result set to only articles which are referred to by the URI passed in.
428 my $uri_obj = RT::URI->new($self->CurrentUser);
429 $uri_obj->FromURI($uri);
430 my $links = $self->NewAlias('Links');
434 VALUE => $uri_obj->URI
448 # {{{ LimitReferredToBy URI
450 =head2 LimitReferredToBy URI
452 Limit the result set to only articles which are referred to by the URI passed in.
456 sub LimitReferredToBy {
460 my $uri_obj = RT::URI->new($self->CurrentUser);
461 $uri_obj->FromURI($uri);
462 my $links = $self->NewAlias('Links');
466 VALUE => $uri_obj->URI
480 =head2 LimitHostlistClasses
482 Only fetch Articles from classes where Hotlist is true.
486 sub LimitHotlistClasses {
489 my $classes = $self->Join(
495 $self->Limit( ALIAS => $classes, FIELD => 'HotList', VALUE => 1 );
498 =head2 LimitAppliedClasses Queue => QueueObj
500 Takes a Queue and limits articles returned to classes which are applied to that Queue
502 Accepts either a Queue obj or a Queue id
506 sub LimitAppliedClasses {
510 unless (ref $args{Queue} || $args{Queue} =~/^[0-9]+$/) {
511 $RT::Logger->error("Not a valid Queue: $args{Queue}");
515 my $queue = ( ref $args{Queue} ? $args{Queue}->Id : $args{Queue} );
517 my $oc_alias = $self->Join(
520 TABLE2 => 'ObjectClasses',
524 my $subclause = "possibleobjectclasses";
525 $self->_OpenParen($subclause);
526 $self->Limit( ALIAS => $oc_alias,
529 SUBCLAUSE => $subclause,
530 ENTRYAGGREGATOR => 'OR' );
531 $self->Limit( ALIAS => $oc_alias,
532 FIELD => 'ObjectType',
533 VALUE => 'RT::Queue',
534 SUBCLAUSE => $subclause,
535 ENTRYAGGREGATOR => 'AND' );
536 $self->_CloseParen($subclause);
538 $self->_OpenParen($subclause);
539 $self->Limit( ALIAS => $oc_alias,
542 SUBCLAUSE => $subclause,
543 ENTRYAGGREGATOR => 'OR' );
544 $self->Limit( ALIAS => $oc_alias,
545 FIELD => 'ObjectType',
546 VALUE => 'RT::System',
547 SUBCLAUSE => $subclause,
548 ENTRYAGGREGATOR => 'AND' );
549 $self->_CloseParen($subclause);
558 my $customfields = $args{CustomFields}
559 || RT::CustomFields->new( $self->CurrentUser );
560 my $dates = $args{Dates} || {};
561 my $order_by = $args{OrderBy};
562 my $order = $args{Order};
566 SUBCLAUSE => 'NameOrSummary',
568 ENTRYAGGREGATOR => 'OR',
574 SUBCLAUSE => 'NameOrSummary',
576 ENTRYAGGREGATOR => 'OR',
583 require Time::ParseDate;
584 foreach my $date (qw(Created< Created> LastUpdated< LastUpdated>)) {
585 next unless ( $args{$date} );
586 my ($seconds, $error) = Time::ParseDate::parsedate( $args{$date}, FUZZY => 1, PREFER_PAST => 1 );
587 unless ( defined $seconds ) {
588 $RT::Logger->warning(
589 "Couldn't parse date '$args{$date}' by Time::ParseDate" );
591 my $date_obj = RT::Date->new( $self->CurrentUser );
592 $date_obj->Set( Format => 'unix', Value => $seconds );
593 $dates->{$date} = $date_obj;
595 if ( $date =~ /^(.*?)<$/i ) {
599 ENTRYAGGREGATOR => "AND",
600 VALUE => $date_obj->ISO
604 if ( $date =~ /^(.*?)>$/i ) {
608 ENTRYAGGREGATOR => "AND",
609 VALUE => $date_obj->ISO
615 if ($args{'RefersTo'}) {
616 foreach my $link ( split( /\s+/, $args{'RefersTo'} ) ) {
618 $self->LimitRefersTo($link);
622 if ($args{'ReferredToBy'}) {
623 foreach my $link ( split( /\s+/, $args{'ReferredToBy'} ) ) {
625 $self->LimitReferredToBy($link);
629 if ( $args{'Topics'} ) {
631 ( ref $args{'Topics'} eq 'ARRAY' )
632 ? @{ $args{'Topics'} }
633 : ( $args{'Topics'} );
634 @Topics = map { split } @Topics;
635 if ( $args{'ExpandTopics'} ) {
638 my $id = shift @Topics;
639 next if $topics{$id};
641 RT::Topics->new( $self->CurrentUser );
642 $Topics->Limit( FIELD => 'Parent', VALUE => $id );
643 push @Topics, $_->Id while $_ = $Topics->Next;
646 @Topics = keys %topics;
647 $args{'Topics'} = \@Topics;
649 $self->LimitTopics(@Topics);
653 $customfields->LimitToLookupType(
654 RT::Article->new( $self->CurrentUser )
655 ->CustomFieldLookupType );
656 if ( $args{'Class'} ) {
658 ( ref $args{'Class'} eq 'ARRAY' )
659 ? @{ $args{'Class'} }
660 : ( $args{'Class'} );
661 foreach my $class (@Classes) {
662 $customfields->LimitToGlobalOrObjectId($class);
666 $customfields->LimitToGlobalOrObjectId();
668 while ( my $cf = $customfields->Next ) {
669 $cfs{ $cf->Name } = $cf->Id;
672 # reset the iterator because we use this to build the UI
673 $customfields->GotoFirstItem;
675 foreach my $field ( keys %cfs ) {
678 ( ref $args{ $field . "~" } eq 'ARRAY' )
679 ? @{ $args{ $field . "~" } }
680 : ( $args{ $field . "~" } );
682 ( ref $args{ $field . "!~" } eq 'ARRAY' )
683 ? @{ $args{ $field . "!~" } }
684 : ( $args{ $field . "!~" } );
687 ( ref $args{$field} eq 'ARRAY' )
691 ( ref $args{ $field . "!" } eq 'ARRAY' )
692 ? @{ $args{ $field . "!" } }
693 : ( $args{ $field . "!" } );
695 foreach my $val (@MatchLike) {
697 push @Match, "~" . $val;
700 foreach my $val (@NoMatchLike) {
702 push @NoMatch, "~" . $val;
705 foreach my $value (@Match) {
708 if ( $value =~ /^~(.*)$/ ) {
715 $self->LimitCustomField(
716 FIELD => $cfs{$field},
719 ENTRYAGGREGATOR => 'OR',
723 foreach my $value (@NoMatch) {
726 if ( $value =~ /^~(.*)$/ ) {
733 $self->LimitCustomField(
734 FIELD => $cfs{$field},
737 ENTRYAGGREGATOR => 'OR',
743 ### Searches for any field
745 if ( $args{'Article~'} ) {
746 $self->LimitCustomField(
747 VALUE => $args{'Article~'},
748 ENTRYAGGREGATOR => 'OR',
751 SUBCLAUSE => 'SearchAll'
754 SUBCLAUSE => 'SearchAll',
756 VALUE => $args{'Article~'},
757 ENTRYAGGREGATOR => 'OR',
762 SUBCLAUSE => 'SearchAll',
764 VALUE => $args{'Article~'},
765 ENTRYAGGREGATOR => 'OR',
771 if ( $args{'Article!~'} ) {
772 $self->LimitCustomField(
773 VALUE => $args{'Article!~'},
774 OPERATOR => 'NOT LIKE',
776 SUBCLAUSE => 'SearchAll'
779 SUBCLAUSE => 'SearchAll',
781 VALUE => $args{'Article!~'},
782 ENTRYAGGREGATOR => 'AND',
784 OPERATOR => 'NOT LIKE'
787 SUBCLAUSE => 'SearchAll',
789 VALUE => $args{'Article!~'},
790 ENTRYAGGREGATOR => 'AND',
792 OPERATOR => 'NOT LIKE'
796 foreach my $field (qw(Name Summary Class)) {
799 ( ref $args{ $field . "~" } eq 'ARRAY' )
800 ? @{ $args{ $field . "~" } }
801 : ( $args{ $field . "~" } );
803 ( ref $args{ $field . "!~" } eq 'ARRAY' )
804 ? @{ $args{ $field . "!~" } }
805 : ( $args{ $field . "!~" } );
808 ( ref $args{$field} eq 'ARRAY' )
812 ( ref $args{ $field . "!" } eq 'ARRAY' )
813 ? @{ $args{ $field . "!" } }
814 : ( $args{ $field . "!" } );
816 foreach my $val (@MatchLike) {
818 push @Match, "~" . $val;
821 foreach my $val (@NoMatchLike) {
823 push @NoMatch, "~" . $val;
827 foreach my $value (@Match) {
828 if ( $value && $value =~ /^~(.*)$/ ) {
836 # preprocess Classes, so we can search on class
837 if ( $field eq 'Class' && $value ) {
838 my $class = RT::Class->new($RT::SystemUser);
839 $class->Load($value);
843 # now that we've pruned the value, get out if it's different.
847 SUBCLAUSE => $field . 'Match',
852 ENTRYAGGREGATOR => 'OR'
856 foreach my $value (@NoMatch) {
858 # preprocess Classes, so we can search on class
859 if ( $value && $value =~ /^~(.*)/ ) {
866 if ( $field eq 'Class' ) {
867 my $class = RT::Class->new($RT::SystemUser);
868 $class->Load($value);
872 # now that we've pruned the value, get out if it's different.
876 SUBCLAUSE => $field . 'NoMatch',
881 ENTRYAGGREGATOR => 'AND'
887 if ($order_by && @$order_by) {
888 if ( $order_by->[0] && $order_by->[0] =~ /\|/ ) {
889 @$order_by = split '|', $order_by->[0];
890 @$order = split '|', $order->[0];
893 map { { FIELD => $order_by->[$_], ORDER => $order->[$_] } } 0 .. $#{$order_by};
894 $self->OrderByCols(@tmp);
903 Returns an empty new RT::Article item
909 return(RT::Article->new($self->CurrentUser));
914 RT::Base->_ImportOverlays();