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->Limit( FIELD => 'Disabled',
102 =head2 LimitToDeleted
104 Only find items that have been deleted.
111 $self->{'find_disabled_rows'} = 1;
112 $self->Limit( FIELD => 'Disabled',
118 =head2 LimitAttribute PARAMHASH
120 Takes NAME, OPERATOR and VALUE to find records that has the
123 If EMPTY is set, also select rows with an empty string as
126 If NULL is set, also select rows without the named Attribute.
137 'LIKE' => 'NOT LIKE',
138 'NOT LIKE' => 'LIKE',
144 my ($self, %args) = @_;
145 my $clause = 'ALIAS';
146 my $operator = ($args{OPERATOR} || '=');
148 if ($args{NULL} and exists $args{VALUE}) {
149 $clause = 'LEFTJOIN';
150 $operator = $Negate{$operator};
152 elsif ($args{NEGATE}) {
153 $operator = $Negate{$operator};
156 my $alias = $self->Join(
158 ALIAS1 => $args{ALIAS} || 'main',
160 TABLE2 => 'Attributes',
164 my $type = ref($self);
165 $type =~ s/(?:s|Collection)$//; # XXX - Hack!
169 FIELD => 'ObjectType',
177 VALUE => $args{NAME},
178 ) if exists $args{NAME};
180 return unless exists $args{VALUE};
185 OPERATOR => $operator,
186 VALUE => $args{VALUE},
189 # Capture rows with the attribute defined as an empty string.
195 ENTRYAGGREGATOR => $args{NULL} ? 'AND' : 'OR',
198 # Capture rows without the attribute defined
203 OPERATOR => ($args{NEGATE} ? 'IS NOT' : 'IS'),
208 =head2 LimitCustomField
210 Takes a paramhash of key/value pairs with the following keys:
214 =item CUSTOMFIELD - CustomField id. Optional
216 =item OPERATOR - The usual Limit operators
218 =item VALUE - The value to compare against
226 my $class = ref($self);
227 $class =~ s/s$// or die "Cannot deduce SingularClass for $class";
231 sub LimitCustomField {
233 my %args = ( VALUE => undef,
234 CUSTOMFIELD => undef,
238 my $alias = $self->Join(
242 TABLE2 => 'ObjectCustomFieldValues',
247 FIELD => 'CustomField',
249 VALUE => $args{'CUSTOMFIELD'},
250 ) if ($args{'CUSTOMFIELD'});
253 FIELD => 'ObjectType',
255 VALUE => $self->_SingularClass,
260 OPERATOR => $args{'OPERATOR'},
261 VALUE => $args{'VALUE'},
267 Find all matching rows, regardless of whether they are disabled or not
272 shift->{'find_disabled_rows'} = 1;
275 =head2 Limit PARAMHASH
277 This Limit sub calls SUPER::Limit, but defaults "CASESENSITIVE" to 1, thus
278 making sure that by default lots of things don't do extra work trying to
279 match lower(colname) agaist lc($val);
285 my %args = ( CASESENSITIVE => 1,
288 return $self->SUPER::Limit(%args);
293 If it has a SortOrder attribute, sort the array by SortOrder.
294 Otherwise, if it has a "Name" attribute, sort alphabetically by Name
295 Otherwise, just give up and return it in the order it came from the
304 if ($self->NewItem()->_Accessible('SortOrder','read')) {
305 $items = [ sort { $a->SortOrder <=> $b->SortOrder } @{$items} ];
307 elsif ($self->NewItem()->_Accessible('Name','read')) {
308 $items = [ sort { lc($a->Name) cmp lc($b->Name) } @{$items} ];
316 Return this object's ItemsArray, in the order that ItemsOrderBy sorts
323 return $self->ItemsOrderBy($self->SUPER::ItemsArrayRef());
326 eval "require RT::SearchBuilder_Vendor";
327 die $@ if ($@ && $@ !~ qr{^Can't locate RT/SearchBuilder_Vendor.pm});
328 eval "require RT::SearchBuilder_Local";
329 die $@ if ($@ && $@ !~ qr{^Can't locate RT/SearchBuilder_Local.pm});