import rt 3.8.9
[freeside.git] / rt / lib / RT / SearchBuilder.pm
1 # BEGIN BPS TAGGED BLOCK {{{
2 #
3 # COPYRIGHT:
4 #
5 # This software is Copyright (c) 1996-2011 Best Practical Solutions, LLC
6 #                                          <sales@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->{'handled_disabled_column'} = 1;
98     $self->Limit( FIELD => 'Disabled', VALUE => '0' );
99 }
100
101 =head2 LimitToDeleted
102
103 Only find items that have been deleted.
104
105 =cut
106
107 sub LimitToDeleted {
108     my $self = shift;
109
110     $self->{'handled_disabled_column'} = $self->{'find_disabled_rows'} = 1;
111     $self->Limit( FIELD => 'Disabled', VALUE => '1' );
112 }
113
114 =head2 FindAllRows
115
116 Find all matching rows, regardless of whether they are disabled or not
117
118 =cut
119
120 sub FindAllRows {
121     shift->{'find_disabled_rows'} = 1;
122 }
123
124 =head2 LimitAttribute PARAMHASH
125
126 Takes NAME, OPERATOR and VALUE to find records that has the
127 matching Attribute.
128
129 If EMPTY is set, also select rows with an empty string as
130 Attribute's Content.
131
132 If NULL is set, also select rows without the named Attribute.
133
134 =cut
135
136 my %Negate = (
137     '='        => '!=',
138     '!='       => '=',
139     '>'        => '<=',
140     '<'        => '>=',
141     '>='       => '<',
142     '<='       => '>',
143     'LIKE'     => 'NOT LIKE',
144     'NOT LIKE' => 'LIKE',
145     'IS'       => 'IS NOT',
146     'IS NOT'   => 'IS',
147 );
148
149 sub LimitAttribute {
150     my ($self, %args) = @_;
151     my $clause = 'ALIAS';
152     my $operator = ($args{OPERATOR} || '=');
153     
154     if ($args{NULL} and exists $args{VALUE}) {
155         $clause = 'LEFTJOIN';
156         $operator = $Negate{$operator};
157     }
158     elsif ($args{NEGATE}) {
159         $operator = $Negate{$operator};
160     }
161     
162     my $alias = $self->Join(
163         TYPE   => 'left',
164         ALIAS1 => $args{ALIAS} || 'main',
165         FIELD1 => 'id',
166         TABLE2 => 'Attributes',
167         FIELD2 => 'ObjectId'
168     );
169
170     my $type = ref($self);
171     $type =~ s/(?:s|Collection)$//; # XXX - Hack!
172
173     $self->Limit(
174         $clause    => $alias,
175         FIELD      => 'ObjectType',
176         OPERATOR   => '=',
177         VALUE      => $type,
178     );
179     $self->Limit(
180         $clause    => $alias,
181         FIELD      => 'Name',
182         OPERATOR   => '=',
183         VALUE      => $args{NAME},
184     ) if exists $args{NAME};
185
186     return unless exists $args{VALUE};
187
188     $self->Limit(
189         $clause    => $alias,
190         FIELD      => 'Content',
191         OPERATOR   => $operator,
192         VALUE      => $args{VALUE},
193     );
194
195     # Capture rows with the attribute defined as an empty string.
196     $self->Limit(
197         $clause    => $alias,
198         FIELD      => 'Content',
199         OPERATOR   => '=',
200         VALUE      => '',
201         ENTRYAGGREGATOR => $args{NULL} ? 'AND' : 'OR',
202     ) if $args{EMPTY};
203
204     # Capture rows without the attribute defined
205     $self->Limit(
206         %args,
207         ALIAS      => $alias,
208         FIELD      => 'id',
209         OPERATOR   => ($args{NEGATE} ? 'IS NOT' : 'IS'),
210         VALUE      => 'NULL',
211     ) if $args{NULL};
212 }
213
214 =head2 LimitCustomField
215
216 Takes a paramhash of key/value pairs with the following keys:
217
218 =over 4
219
220 =item CUSTOMFIELD - CustomField id. Optional
221
222 =item OPERATOR - The usual Limit operators
223
224 =item VALUE - The value to compare against
225
226 =back
227
228 =cut
229
230 sub _SingularClass {
231     my $self = shift;
232     my $class = ref($self);
233     $class =~ s/s$// or die "Cannot deduce SingularClass for $class";
234     return $class;
235 }
236
237 sub LimitCustomField {
238     my $self = shift;
239     my %args = ( VALUE        => undef,
240                  CUSTOMFIELD  => undef,
241                  OPERATOR     => '=',
242                  @_ );
243
244     my $alias = $self->Join(
245         TYPE       => 'left',
246         ALIAS1     => 'main',
247         FIELD1     => 'id',
248         TABLE2     => 'ObjectCustomFieldValues',
249         FIELD2     => 'ObjectId'
250     );
251     $self->Limit(
252         ALIAS      => $alias,
253         FIELD      => 'CustomField',
254         OPERATOR   => '=',
255         VALUE      => $args{'CUSTOMFIELD'},
256     ) if ($args{'CUSTOMFIELD'});
257     $self->Limit(
258         ALIAS      => $alias,
259         FIELD      => 'ObjectType',
260         OPERATOR   => '=',
261         VALUE      => $self->_SingularClass,
262     );
263     $self->Limit(
264         ALIAS      => $alias,
265         FIELD      => 'Content',
266         OPERATOR   => $args{'OPERATOR'},
267         VALUE      => $args{'VALUE'},
268     );
269 }
270
271 =head2 Limit PARAMHASH
272
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);
276
277 =cut
278
279 sub Limit {
280     my $self = shift;
281     my %args = ( CASESENSITIVE => 1,
282                  @_ );
283
284     return $self->SUPER::Limit(%args);
285 }
286
287 =head2 ItemsOrderBy
288
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
292 db.
293
294 =cut
295
296 sub ItemsOrderBy {
297     my $self = shift;
298     my $items = shift;
299   
300     if ($self->NewItem()->_Accessible('SortOrder','read')) {
301         $items = [ sort { $a->SortOrder <=> $b->SortOrder } @{$items} ];
302     }
303     elsif ($self->NewItem()->_Accessible('Name','read')) {
304         $items = [ sort { lc($a->Name) cmp lc($b->Name) } @{$items} ];
305     }
306
307     return $items;
308 }
309
310 =head2 ItemsArrayRef
311
312 Return this object's ItemsArray, in the order that ItemsOrderBy sorts
313 it.
314
315 =cut
316
317 sub ItemsArrayRef {
318     my $self = shift;
319     return $self->ItemsOrderBy($self->SUPER::ItemsArrayRef());
320 }
321
322 # make sure that Disabled rows never get seen unless
323 # we're explicitly trying to see them.
324
325 sub _DoSearch {
326     my $self = shift;
327
328     if ( $self->{'with_disabled_column'}
329         && !$self->{'handled_disabled_column'}
330         && !$self->{'find_disabled_rows'}
331     ) {
332         $self->LimitToEnabled;
333     }
334     return $self->SUPER::_DoSearch(@_);
335 }
336 sub _DoCount {
337     my $self = shift;
338
339     if ( $self->{'with_disabled_column'}
340         && !$self->{'handled_disabled_column'}
341         && !$self->{'find_disabled_rows'}
342     ) {
343         $self->LimitToEnabled;
344     }
345     return $self->SUPER::_DoCount(@_);
346 }
347
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});
352
353 1;