import rt 3.8.7
[freeside.git] / rt / lib / RT / SearchBuilder.pm
1 # BEGIN BPS TAGGED BLOCK {{{
2
3 # COPYRIGHT:
4
5 # This software is Copyright (c) 1996-2009 Best Practical Solutions, LLC
6 #                                          <jesse@bestpractical.com>
7
8 # (Except where explicitly superseded by other copyright notices)
9
10
11 # LICENSE:
12
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
16 # from www.gnu.org.
17
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.
22
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.
28
29
30 # CONTRIBUTION SUBMISSION POLICY:
31
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.)
37
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.
46
47 # END BPS TAGGED BLOCK }}}
48
49 =head1 NAME
50
51   RT::SearchBuilder - a baseclass for RT collection objects
52
53 =head1 SYNOPSIS
54
55 =head1 DESCRIPTION
56
57
58 =head1 METHODS
59
60
61
62
63 =cut
64
65 package RT::SearchBuilder;
66
67 use RT::Base;
68 use DBIx::SearchBuilder "1.40";
69
70 use strict;
71 use warnings;
72
73 use base qw(DBIx::SearchBuilder RT::Base);
74
75 sub _Init  {
76     my $self = shift;
77     
78     $self->{'user'} = shift;
79     unless(defined($self->CurrentUser)) {
80         use Carp;
81         Carp::confess("$self was created without a CurrentUser");
82         $RT::Logger->err("$self was created without a CurrentUser");
83         return(0);
84     }
85     $self->SUPER::_Init( 'Handle' => $RT::Handle);
86 }
87
88 =head2 LimitToEnabled
89
90 Only find items that haven't been disabled
91
92 =cut
93
94 sub LimitToEnabled {
95     my $self = shift;
96     
97     $self->Limit( FIELD => 'Disabled',
98                   VALUE => '0',
99                   OPERATOR => '=' );
100 }
101
102 =head2 LimitToDeleted
103
104 Only find items that have been deleted.
105
106 =cut
107
108 sub LimitToDeleted {
109     my $self = shift;
110     
111     $self->{'find_disabled_rows'} = 1;
112     $self->Limit( FIELD => 'Disabled',
113                   OPERATOR => '=',
114                   VALUE => '1'
115                 );
116 }
117
118 =head2 LimitAttribute PARAMHASH
119
120 Takes NAME, OPERATOR and VALUE to find records that has the
121 matching Attribute.
122
123 If EMPTY is set, also select rows with an empty string as
124 Attribute's Content.
125
126 If NULL is set, also select rows without the named Attribute.
127
128 =cut
129
130 my %Negate = (
131     '='        => '!=',
132     '!='       => '=',
133     '>'        => '<=',
134     '<'        => '>=',
135     '>='       => '<',
136     '<='       => '>',
137     'LIKE'     => 'NOT LIKE',
138     'NOT LIKE' => 'LIKE',
139     'IS'       => 'IS NOT',
140     'IS NOT'   => 'IS',
141 );
142
143 sub LimitAttribute {
144     my ($self, %args) = @_;
145     my $clause = 'ALIAS';
146     my $operator = ($args{OPERATOR} || '=');
147     
148     if ($args{NULL} and exists $args{VALUE}) {
149         $clause = 'LEFTJOIN';
150         $operator = $Negate{$operator};
151     }
152     elsif ($args{NEGATE}) {
153         $operator = $Negate{$operator};
154     }
155     
156     my $alias = $self->Join(
157         TYPE   => 'left',
158         ALIAS1 => $args{ALIAS} || 'main',
159         FIELD1 => 'id',
160         TABLE2 => 'Attributes',
161         FIELD2 => 'ObjectId'
162     );
163
164     my $type = ref($self);
165     $type =~ s/(?:s|Collection)$//; # XXX - Hack!
166
167     $self->Limit(
168         $clause    => $alias,
169         FIELD      => 'ObjectType',
170         OPERATOR   => '=',
171         VALUE      => $type,
172     );
173     $self->Limit(
174         $clause    => $alias,
175         FIELD      => 'Name',
176         OPERATOR   => '=',
177         VALUE      => $args{NAME},
178     ) if exists $args{NAME};
179
180     return unless exists $args{VALUE};
181
182     $self->Limit(
183         $clause    => $alias,
184         FIELD      => 'Content',
185         OPERATOR   => $operator,
186         VALUE      => $args{VALUE},
187     );
188
189     # Capture rows with the attribute defined as an empty string.
190     $self->Limit(
191         $clause    => $alias,
192         FIELD      => 'Content',
193         OPERATOR   => '=',
194         VALUE      => '',
195         ENTRYAGGREGATOR => $args{NULL} ? 'AND' : 'OR',
196     ) if $args{EMPTY};
197
198     # Capture rows without the attribute defined
199     $self->Limit(
200         %args,
201         ALIAS      => $alias,
202         FIELD      => 'id',
203         OPERATOR   => ($args{NEGATE} ? 'IS NOT' : 'IS'),
204         VALUE      => 'NULL',
205     ) if $args{NULL};
206 }
207
208 =head2 LimitCustomField
209
210 Takes a paramhash of key/value pairs with the following keys:
211
212 =over 4
213
214 =item CUSTOMFIELD - CustomField id. Optional
215
216 =item OPERATOR - The usual Limit operators
217
218 =item VALUE - The value to compare against
219
220 =back
221
222 =cut
223
224 sub _SingularClass {
225     my $self = shift;
226     my $class = ref($self);
227     $class =~ s/s$// or die "Cannot deduce SingularClass for $class";
228     return $class;
229 }
230
231 sub LimitCustomField {
232     my $self = shift;
233     my %args = ( VALUE        => undef,
234                  CUSTOMFIELD  => undef,
235                  OPERATOR     => '=',
236                  @_ );
237
238     my $alias = $self->Join(
239         TYPE       => 'left',
240         ALIAS1     => 'main',
241         FIELD1     => 'id',
242         TABLE2     => 'ObjectCustomFieldValues',
243         FIELD2     => 'ObjectId'
244     );
245     $self->Limit(
246         ALIAS      => $alias,
247         FIELD      => 'CustomField',
248         OPERATOR   => '=',
249         VALUE      => $args{'CUSTOMFIELD'},
250     ) if ($args{'CUSTOMFIELD'});
251     $self->Limit(
252         ALIAS      => $alias,
253         FIELD      => 'ObjectType',
254         OPERATOR   => '=',
255         VALUE      => $self->_SingularClass,
256     );
257     $self->Limit(
258         ALIAS      => $alias,
259         FIELD      => 'Content',
260         OPERATOR   => $args{'OPERATOR'},
261         VALUE      => $args{'VALUE'},
262     );
263 }
264
265 =head2 FindAllRows
266
267 Find all matching rows, regardless of whether they are disabled or not
268
269 =cut
270
271 sub FindAllRows {
272     shift->{'find_disabled_rows'} = 1;
273 }
274
275 =head2 Limit PARAMHASH
276
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);
280
281 =cut
282
283 sub Limit {
284     my $self = shift;
285     my %args = ( CASESENSITIVE => 1,
286                  @_ );
287
288     return $self->SUPER::Limit(%args);
289 }
290
291 =head2 ItemsOrderBy
292
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
296 db.
297
298 =cut
299
300 sub ItemsOrderBy {
301     my $self = shift;
302     my $items = shift;
303   
304     if ($self->NewItem()->_Accessible('SortOrder','read')) {
305         $items = [ sort { $a->SortOrder <=> $b->SortOrder } @{$items} ];
306     }
307     elsif ($self->NewItem()->_Accessible('Name','read')) {
308         $items = [ sort { lc($a->Name) cmp lc($b->Name) } @{$items} ];
309     }
310
311     return $items;
312 }
313
314 =head2 ItemsArrayRef
315
316 Return this object's ItemsArray, in the order that ItemsOrderBy sorts
317 it.
318
319 =cut
320
321 sub ItemsArrayRef {
322     my $self = shift;
323     return $self->ItemsOrderBy($self->SUPER::ItemsArrayRef());
324 }
325
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});
330
331 1;