1 # {{{ BEGIN BPS TAGGED BLOCK
5 # This software is Copyright (c) 1996-2004 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
48 RT::SearchBuilder - a baseclass for RT collection objects
60 ok (require RT::SearchBuilder);
67 package RT::SearchBuilder;
70 use DBIx::SearchBuilder;
74 @ISA = qw(DBIx::SearchBuilder RT::Base);
80 $self->{'user'} = shift;
81 unless(defined($self->CurrentUser)) {
83 Carp::confess("$self was created without a CurrentUser");
84 $RT::Logger->err("$self was created without a CurrentUser");
87 $self->SUPER::_Init( 'Handle' => $RT::Handle);
91 # {{{ sub LimitToEnabled
95 Only find items that haven\'t been disabled
102 $self->Limit( FIELD => 'Disabled',
108 # {{{ sub LimitToDisabled
110 =head2 LimitToDeleted
112 Only find items that have been deleted.
119 $self->{'find_disabled_rows'} = 1;
120 $self->Limit( FIELD => 'Disabled',
127 # {{{ sub LimitAttribute
129 =head2 LimitAttribute PARAMHASH
131 Takes NAME, OPERATOR and VALUE to find records that has the
137 my ($self, %args) = @_;
139 my $alias = $self->Join(
143 TABLE2 => 'Attributes',
147 my $type = ref($self);
148 $type =~ s/(?:s|Collection)$//; # XXX - Hack!
152 FIELD => 'ObjectType',
160 VALUE => $args{NAME},
161 ) if exists $args{NAME};
163 return unless exists $args{VALUE};
168 OPERATOR => ($args{OPERATOR} || '='),
169 VALUE => $args{VALUE},
170 ENTRYAGGREGATOR => 'OR',
174 # Capture rows without the attribute defined by testing IS NULL.
180 ENTRYAGGREGATOR => 'OR',
181 ) for qw( ObjectType Name Content );
188 # {{{ sub FindAllRows
192 Find all matching rows, regardless of whether they are disabled or not
197 shift->{'find_disabled_rows'} = 1;
202 =head2 Limit PARAMHASH
204 This Limit sub calls SUPER::Limit, but defaults "CASESENSITIVE" to 1, thus
205 making sure that by default lots of things don't do extra work trying to
206 match lower(colname) agaist lc($val);
212 my %args = ( CASESENSITIVE => 1,
215 return $self->SUPER::Limit(%args);
220 # {{{ sub ItemsOrderBy
224 If it has a SortOrder attribute, sort the array by SortOrder.
225 Otherwise, if it has a "Name" attribute, sort alphabetically by Name
226 Otherwise, just give up and return it in the order it came from the
235 if ($self->NewItem()->_Accessible('SortOrder','read')) {
236 $items = [ sort { $a->SortOrder <=> $b->SortOrder } @{$items} ];
238 elsif ($self->NewItem()->_Accessible('Name','read')) {
239 $items = [ sort { lc($a->Name) cmp lc($b->Name) } @{$items} ];
247 # {{{ sub ItemsArrayRef
251 Return this object's ItemsArray, in the order that ItemsOrderBy sorts
257 ok(my $queues = RT::Queues->new($RT::SystemUser), 'Created a queues object');
258 ok( $queues->UnLimit(),'Unlimited the result set of the queues object');
259 my $items = $queues->ItemsArrayRef();
260 my @items = @{$items};
262 ok($queues->NewItem->_Accessible('Name','read'));
263 my @sorted = sort {lc($a->Name) cmp lc($b->Name)} @items;
264 ok (@sorted, "We have an array of queues, sorted". join(',',map {$_->Name} @sorted));
266 ok (@items, "We have an array of queues, raw". join(',',map {$_->Name} @items));
267 my @sorted_ids = map {$_->id } @sorted;
268 my @items_ids = map {$_->id } @items;
270 is ($#sorted, $#items);
271 is ($sorted[0]->Name, $items[0]->Name);
272 is ($sorted[-1]->Name, $items[-1]->Name);
273 is_deeply(\@items_ids, \@sorted_ids, "ItemsArrayRef sorts alphabetically by name");;
284 return $self->ItemsOrderBy($self->SUPER::ItemsArrayRef());
289 eval "require RT::SearchBuilder_Vendor";
290 die $@ if ($@ && $@ !~ qr{^Can't locate RT/SearchBuilder_Vendor.pm});
291 eval "require RT::SearchBuilder_Local";
292 die $@ if ($@ && $@ !~ qr{^Can't locate RT/SearchBuilder_Local.pm});