1 # BEGIN BPS TAGGED BLOCK {{{
5 # This software is Copyright (c) 1996-2009 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., 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 }}}
51 RT::SearchBuilder - a baseclass for RT collection objects
65 package RT::SearchBuilder;
68 use DBIx::SearchBuilder "1.50";
73 use base qw(DBIx::SearchBuilder RT::Base);
78 $self->{'user'} = shift;
79 unless(defined($self->CurrentUser)) {
81 Carp::confess("$self was created without a CurrentUser");
82 $RT::Logger->err("$self was created without a CurrentUser");
85 $self->SUPER::_Init( 'Handle' => $RT::Handle);
90 Only find items that haven't been disabled
97 $self->{'handled_disabled_column'} = 1;
98 $self->Limit( FIELD => 'Disabled', VALUE => '0' );
101 =head2 LimitToDeleted
103 Only find items that have been deleted.
110 $self->{'handled_disabled_column'} = $self->{'find_disabled_rows'} = 1;
111 $self->Limit( FIELD => 'Disabled', VALUE => '1' );
116 Find all matching rows, regardless of whether they are disabled or not
121 shift->{'find_disabled_rows'} = 1;
124 =head2 LimitAttribute PARAMHASH
126 Takes NAME, OPERATOR and VALUE to find records that has the
129 If EMPTY is set, also select rows with an empty string as
132 If NULL is set, also select rows without the named Attribute.
143 'LIKE' => 'NOT LIKE',
144 'NOT LIKE' => 'LIKE',
150 my ($self, %args) = @_;
151 my $clause = 'ALIAS';
152 my $operator = ($args{OPERATOR} || '=');
154 if ($args{NULL} and exists $args{VALUE}) {
155 $clause = 'LEFTJOIN';
156 $operator = $Negate{$operator};
158 elsif ($args{NEGATE}) {
159 $operator = $Negate{$operator};
162 my $alias = $self->Join(
164 ALIAS1 => $args{ALIAS} || 'main',
166 TABLE2 => 'Attributes',
170 my $type = ref($self);
171 $type =~ s/(?:s|Collection)$//; # XXX - Hack!
175 FIELD => 'ObjectType',
183 VALUE => $args{NAME},
184 ) if exists $args{NAME};
186 return unless exists $args{VALUE};
191 OPERATOR => $operator,
192 VALUE => $args{VALUE},
195 # Capture rows with the attribute defined as an empty string.
201 ENTRYAGGREGATOR => $args{NULL} ? 'AND' : 'OR',
204 # Capture rows without the attribute defined
209 OPERATOR => ($args{NEGATE} ? 'IS NOT' : 'IS'),
214 =head2 LimitCustomField
216 Takes a paramhash of key/value pairs with the following keys:
220 =item CUSTOMFIELD - CustomField id. Optional
222 =item OPERATOR - The usual Limit operators
224 =item VALUE - The value to compare against
232 my $class = ref($self);
233 $class =~ s/s$// or die "Cannot deduce SingularClass for $class";
237 sub LimitCustomField {
239 my %args = ( VALUE => undef,
240 CUSTOMFIELD => undef,
244 my $alias = $self->Join(
248 TABLE2 => 'ObjectCustomFieldValues',
253 FIELD => 'CustomField',
255 VALUE => $args{'CUSTOMFIELD'},
256 ) if ($args{'CUSTOMFIELD'});
259 FIELD => 'ObjectType',
261 VALUE => $self->_SingularClass,
266 OPERATOR => $args{'OPERATOR'},
267 VALUE => $args{'VALUE'},
271 =head2 Limit PARAMHASH
273 This Limit sub calls SUPER::Limit, but defaults "CASESENSITIVE" to 1, thus
274 making sure that by default lots of things don't do extra work trying to
275 match lower(colname) agaist lc($val);
281 my %args = ( CASESENSITIVE => 1,
284 return $self->SUPER::Limit(%args);
289 If it has a SortOrder attribute, sort the array by SortOrder.
290 Otherwise, if it has a "Name" attribute, sort alphabetically by Name
291 Otherwise, just give up and return it in the order it came from the
300 if ($self->NewItem()->_Accessible('SortOrder','read')) {
301 $items = [ sort { $a->SortOrder <=> $b->SortOrder } @{$items} ];
303 elsif ($self->NewItem()->_Accessible('Name','read')) {
304 $items = [ sort { lc($a->Name) cmp lc($b->Name) } @{$items} ];
312 Return this object's ItemsArray, in the order that ItemsOrderBy sorts
319 return $self->ItemsOrderBy($self->SUPER::ItemsArrayRef());
322 # make sure that Disabled rows never get seen unless
323 # we're explicitly trying to see them.
328 if ( $self->{'with_disabled_column'}
329 && !$self->{'handled_disabled_column'}
330 && !$self->{'find_disabled_rows'}
332 $self->LimitToEnabled;
334 return $self->SUPER::_DoSearch(@_);
339 if ( $self->{'with_disabled_column'}
340 && !$self->{'handled_disabled_column'}
341 && !$self->{'find_disabled_rows'}
343 $self->LimitToEnabled;
345 return $self->SUPER::_DoCount(@_);
348 eval "require RT::SearchBuilder_Vendor";
349 die $@ if ($@ && $@ !~ qr{^Can't locate RT/SearchBuilder_Vendor.pm});
350 eval "require RT::SearchBuilder_Local";
351 die $@ if ($@ && $@ !~ qr{^Can't locate RT/SearchBuilder_Local.pm});