1 # BEGIN BPS TAGGED BLOCK {{{
5 # This software is Copyright (c) 1996-2005 Best Practical Solutions, LLC
6 # <jesse@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., 675 Mass Ave, Cambridge, MA 02139, USA.
28 # CONTRIBUTION SUBMISSION POLICY:
30 # (The following paragraph is not intended to limit the rights granted
31 # to you to modify and distribute this software under the terms of
32 # the GNU General Public License and is only of importance to you if
33 # you choose to contribute your changes and enhancements to the
34 # community by submitting them to Best Practical Solutions, LLC.)
36 # By intentionally submitting any modifications, corrections or
37 # derivatives to this work, or any other work intended for use with
38 # Request Tracker, to Best Practical Solutions, LLC, you confirm that
39 # you are the copyright holder for those contributions and you grant
40 # Best Practical Solutions, LLC a nonexclusive, worldwide, irrevocable,
41 # royalty-free, perpetual, license to use, copy, create derivative
42 # works based on those contributions, and sublicense and distribute
43 # those contributions and any derivatives thereof.
45 # END BPS TAGGED BLOCK }}}
49 RT::SearchBuilder - a baseclass for RT collection objects
61 ok (require RT::SearchBuilder);
68 package RT::SearchBuilder;
71 use DBIx::SearchBuilder;
75 @ISA = qw(DBIx::SearchBuilder RT::Base);
81 $self->{'user'} = shift;
82 unless(defined($self->CurrentUser)) {
84 Carp::confess("$self was created without a CurrentUser");
85 $RT::Logger->err("$self was created without a CurrentUser");
88 $self->SUPER::_Init( 'Handle' => $RT::Handle);
92 # {{{ sub LimitToEnabled
96 Only find items that haven\'t been disabled
103 $self->Limit( FIELD => 'Disabled',
109 # {{{ sub LimitToDisabled
111 =head2 LimitToDeleted
113 Only find items that have been deleted.
120 $self->{'find_disabled_rows'} = 1;
121 $self->Limit( FIELD => 'Disabled',
128 # {{{ sub LimitAttribute
130 =head2 LimitAttribute PARAMHASH
132 Takes NAME, OPERATOR and VALUE to find records that has the
135 If EMPTY is set, also select rows with an empty string as
138 If NULL is set, also select rows without the named Attribute.
156 my ($self, %args) = @_;
157 my $clause = 'ALIAS';
158 my $operator = ($args{OPERATOR} || '=');
160 if ($args{NULL} and exists $args{VALUE}) {
161 $clause = 'LEFTJOIN';
162 $operator = $Negate{$operator};
164 elsif ($args{NEGATE}) {
165 $operator = $Negate{$operator};
168 my $alias = $self->Join(
170 ALIAS1 => $args{ALIAS} || 'main',
172 TABLE2 => 'Attributes',
176 my $type = ref($self);
177 $type =~ s/(?:s|Collection)$//; # XXX - Hack!
181 FIELD => 'ObjectType',
189 VALUE => $args{NAME},
190 ) if exists $args{NAME};
192 return unless exists $args{VALUE};
197 OPERATOR => $operator,
198 VALUE => $args{VALUE},
201 # Capture rows with the attribute defined as an empty string.
207 ENTRYAGGREGATOR => $args{NULL} ? 'AND' : 'OR',
210 # Capture rows without the attribute defined
215 OPERATOR => ($args{NEGATE} ? 'IS NOT' : 'IS'),
221 # {{{ sub LimitCustomField
223 =head2 LimitCustomField
225 Takes a paramhash of key/value pairs with the following keys:
229 =item CUSTOMFIELD - CustomField id. Optional
231 =item OPERATOR - The usual Limit operators
233 =item VALUE - The value to compare against
241 my $class = ref($self);
242 $class =~ s/s$// or die "Cannot deduce SingularClass for $class";
246 sub LimitCustomField {
248 my %args = ( VALUE => undef,
249 CUSTOMFIELD => undef,
253 my $alias = $self->Join(
257 TABLE2 => 'ObjectCustomFieldValues',
262 FIELD => 'CustomField',
264 VALUE => $args{'CUSTOMFIELD'},
265 ) if ($args{'CUSTOMFIELD'});
268 FIELD => 'ObjectType',
270 VALUE => $self->_SingularClass,
275 OPERATOR => $args{'OPERATOR'},
276 VALUE => $args{'VALUE'},
280 # {{{ sub FindAllRows
284 Find all matching rows, regardless of whether they are disabled or not
289 shift->{'find_disabled_rows'} = 1;
294 =head2 Limit PARAMHASH
296 This Limit sub calls SUPER::Limit, but defaults "CASESENSITIVE" to 1, thus
297 making sure that by default lots of things don't do extra work trying to
298 match lower(colname) agaist lc($val);
304 my %args = ( CASESENSITIVE => 1,
307 return $self->SUPER::Limit(%args);
312 # {{{ sub ItemsOrderBy
316 If it has a SortOrder attribute, sort the array by SortOrder.
317 Otherwise, if it has a "Name" attribute, sort alphabetically by Name
318 Otherwise, just give up and return it in the order it came from the
327 if ($self->NewItem()->_Accessible('SortOrder','read')) {
328 $items = [ sort { $a->SortOrder <=> $b->SortOrder } @{$items} ];
330 elsif ($self->NewItem()->_Accessible('Name','read')) {
331 $items = [ sort { lc($a->Name) cmp lc($b->Name) } @{$items} ];
339 # {{{ sub ItemsArrayRef
343 Return this object's ItemsArray, in the order that ItemsOrderBy sorts
349 ok(my $queues = RT::Queues->new($RT::SystemUser), 'Created a queues object');
350 ok( $queues->UnLimit(),'Unlimited the result set of the queues object');
351 my $items = $queues->ItemsArrayRef();
352 my @items = @{$items};
354 ok($queues->NewItem->_Accessible('Name','read'));
355 my @sorted = sort {lc($a->Name) cmp lc($b->Name)} @items;
356 ok (@sorted, "We have an array of queues, sorted". join(',',map {$_->Name} @sorted));
358 ok (@items, "We have an array of queues, raw". join(',',map {$_->Name} @items));
359 my @sorted_ids = map {$_->id } @sorted;
360 my @items_ids = map {$_->id } @items;
362 is ($#sorted, $#items);
363 is ($sorted[0]->Name, $items[0]->Name);
364 is ($sorted[-1]->Name, $items[-1]->Name);
365 is_deeply(\@items_ids, \@sorted_ids, "ItemsArrayRef sorts alphabetically by name");;
376 return $self->ItemsOrderBy($self->SUPER::ItemsArrayRef());
381 eval "require RT::SearchBuilder_Vendor";
382 die $@ if ($@ && $@ !~ qr{^Can't locate RT/SearchBuilder_Vendor.pm});
383 eval "require RT::SearchBuilder_Local";
384 die $@ if ($@ && $@ !~ qr{^Can't locate RT/SearchBuilder_Local.pm});