import torrus 1.0.9
[freeside.git] / rt / lib / RT / CustomFields_Overlay.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::CustomFields - a collection of RT CustomField objects
52
53 =head1 SYNOPSIS
54
55   use RT::CustomFields;
56
57 =head1 DESCRIPTION
58
59 =head1 METHODS
60
61
62
63 =cut
64
65
66 package RT::CustomFields;
67
68 use strict;
69 use warnings;
70 no warnings qw(redefine);
71 use DBIx::SearchBuilder::Unique;
72
73 sub _Init {
74     my $self = shift;
75     $self->{'table'} = 'CustomFields';
76     $self->{'primary_key'} = 'id';
77     $self->{'with_disabled_column'} = 1;
78
79     return $self->SUPER::_Init(@_);
80 }
81
82
83 =head2 LimitToLookupType
84
85 Takes LookupType and limits collection.
86
87 =cut
88
89 sub LimitToLookupType  {
90     my $self = shift;
91     my $lookup = shift;
92
93     $self->Limit( FIELD => 'LookupType', VALUE => "$lookup" );
94 }
95
96 =head2 LimitToChildType
97
98 Takes partial LookupType and limits collection to records
99 where LookupType is equal or ends with the value.
100
101 =cut
102
103 sub LimitToChildType  {
104     my $self = shift;
105     my $lookup = shift;
106
107     $self->Limit( FIELD => 'LookupType', VALUE => "$lookup" );
108     $self->Limit( FIELD => 'LookupType', ENDSWITH => "$lookup" );
109 }
110
111
112 =head2 LimitToParentType
113
114 Takes partial LookupType and limits collection to records
115 where LookupType is equal or starts with the value.
116
117 =cut
118
119 sub LimitToParentType  {
120     my $self = shift;
121     my $lookup = shift;
122
123     $self->Limit( FIELD => 'LookupType', VALUE => "$lookup" );
124     $self->Limit( FIELD => 'LookupType', STARTSWITH => "$lookup" );
125 }
126
127
128 =head2 LimitToGlobalOrObjectId
129
130 Takes list of object IDs and limits collection to custom
131 fields that are applied to these objects or globally.
132
133 =cut
134
135 sub LimitToGlobalOrObjectId {
136     my $self = shift;
137     my $global_only = 1;
138
139
140     foreach my $id (@_) {
141         $self->Limit( ALIAS           => $self->_OCFAlias,
142                     FIELD           => 'ObjectId',
143                     OPERATOR        => '=',
144                     VALUE           => $id || 0,
145                     ENTRYAGGREGATOR => 'OR' );
146         $global_only = 0 if $id;
147     }
148
149     $self->Limit( ALIAS           => $self->_OCFAlias,
150                  FIELD           => 'ObjectId',
151                  OPERATOR        => '=',
152                  VALUE           => 0,
153                  ENTRYAGGREGATOR => 'OR' ) unless $global_only;
154 }
155
156 =head2 LimitToNotApplied
157
158 Takes either list of object ids or nothing. Limits collection
159 to custom fields to listed objects or any corespondingly. Use
160 zero to mean global.
161
162 =cut
163
164 sub LimitToNotApplied {
165     my $self = shift;
166     my @ids = @_;
167
168     my $ocfs_alias = $self->_OCFAlias( New => 1, Left => 1 );
169     if ( @ids ) {
170         # XXX: we need different EA in join clause, but DBIx::SB
171         # doesn't support them, use IN (X) instead
172         my $dbh = $self->_Handle->dbh;
173         $self->Limit(
174             LEFTJOIN   => $ocfs_alias,
175             ALIAS      => $ocfs_alias,
176             FIELD      => 'ObjectId',
177             OPERATOR   => 'IN',
178             QUOTEVALUE => 0,
179             VALUE      => "(". join( ',', map $dbh->quote($_), @ids ) .")",
180         );
181     }
182
183     $self->Limit(
184         ENTRYAGGREGATOR => 'AND',
185         ALIAS    => $ocfs_alias,
186         FIELD    => 'id',
187         OPERATOR => 'IS',
188         VALUE    => 'NULL',
189     );
190 }
191
192 =head2 LimitToGlobalOrQueue QUEUEID
193
194 DEPRECATED since CFs are applicable not only to tickets these days.
195
196 Limits the set of custom fields found to global custom fields or those tied to the queue with ID QUEUEID
197
198 =cut
199
200 sub LimitToGlobalOrQueue {
201     my $self = shift;
202     my $queue = shift;
203     $self->LimitToGlobalOrObjectId( $queue );
204     $self->LimitToLookupType( 'RT::Queue-RT::Ticket' );
205 }
206
207
208 =head2 LimitToQueue QUEUEID
209
210 DEPRECATED since CFs are applicable not only to tickets these days.
211
212 Takes a queue id (numerical) as its only argument. Makes sure that
213 Scopes it pulls out apply to this queue (or another that you've selected with
214 another call to this method
215
216 =cut
217
218 sub LimitToQueue  {
219    my $self = shift;
220   my $queue = shift;
221
222   $self->Limit (ALIAS => $self->_OCFAlias,
223                 ENTRYAGGREGATOR => 'OR',
224                 FIELD => 'ObjectId',
225                 VALUE => "$queue")
226       if defined $queue;
227   $self->LimitToLookupType( 'RT::Queue-RT::Ticket' );
228 }
229
230
231 =head2 LimitToGlobal
232
233 DEPRECATED since CFs are applicable not only to tickets these days.
234
235 Makes sure that Scopes it pulls out apply to all queues
236 (or another that you've selected with
237 another call to this method or LimitToQueue)
238
239 =cut
240
241 sub LimitToGlobal  {
242    my $self = shift;
243
244   $self->Limit (ALIAS => $self->_OCFAlias,
245                 ENTRYAGGREGATOR => 'OR',
246                 FIELD => 'ObjectId',
247                 VALUE => 0);
248   $self->LimitToLookupType( 'RT::Queue-RT::Ticket' );
249 }
250
251
252 =head2 ApplySortOrder
253
254 Sort custom fields according to thier order application to objects. It's
255 expected that collection contains only records of one
256 L<RT::CustomField/LookupType> and applied to one object or globally
257 (L</LimitToGlobalOrObjectId>), otherwise sorting makes no sense.
258
259 =cut
260
261 sub ApplySortOrder {
262     my $self = shift;
263     my $order = shift || 'ASC';
264     $self->OrderByCols( {
265         ALIAS => $self->_OCFAlias,
266         FIELD => 'SortOrder',
267         ORDER => $order,
268     } );
269 }
270
271
272 =head2 ContextObject
273
274 Returns context object for this collection of custom fields,
275 but only if it's defined.
276
277 =cut
278
279 sub ContextObject {
280     my $self = shift;
281     return $self->{'context_object'};
282 }
283
284
285 =head2 SetContextObject
286
287 Sets context object for this collection of custom fields.
288
289 =cut
290
291 sub SetContextObject {
292     my $self = shift;
293     return $self->{'context_object'} = shift;
294 }
295
296
297 sub _OCFAlias {
298     my $self = shift;
299     my %args = ( New => 0, Left => 0, @_ );
300
301     return $self->{'_sql_ocfalias'} if $self->{'_sql_ocfalias'} && !$args{'New'};
302
303     my $alias = $self->Join(
304         $args{'Left'} ? (TYPE => 'LEFT') : (),
305         ALIAS1 => 'main',
306         FIELD1 => 'id',
307         TABLE2 => 'ObjectCustomFields',
308         FIELD2 => 'CustomField'
309     );
310     return $alias if $args{'New'};
311     return $self->{'_sql_ocfalias'} = $alias;
312 }
313
314
315 =head2 Next
316
317 Returns the next custom field that this user can see.
318
319 =cut
320
321 sub Next {
322     my $self = shift;
323
324     my $CF = $self->SUPER::Next();
325     return $CF unless $CF;
326
327     $CF->SetContextOject( $self->ContextObject );
328
329     return $self->Next unless $CF->CurrentUserHasRight('SeeCustomField');
330     return $CF;
331 }
332
333 =head2 Next
334
335 Overrides <RT::SearchBuilder/Next> to make sure </ContextObject>
336 is inherited.
337
338 =cut
339
340 sub NewItem {
341     my $self = shift;
342     my $res = RT::CustomField->new($self->CurrentUser);
343     $res->SetContextObject($self->ContextObject);
344     return $res;
345 }
346
347 1;