Upgrade 4.0.17 clean.
[usit-rt.git] / lib / RT / CustomFields.pm
CommitLineData
84fb5b46
MKG
1# BEGIN BPS TAGGED BLOCK {{{
2#
3# COPYRIGHT:
4#
403d7b0b 5# This software is Copyright (c) 1996-2013 Best Practical Solutions, LLC
84fb5b46
MKG
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::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
66package RT::CustomFields;
67
68use strict;
69use warnings;
70
71use DBIx::SearchBuilder::Unique;
72
73use RT::CustomField;
74
75use base 'RT::SearchBuilder';
76
77sub Table { 'CustomFields'}
78
79sub _Init {
80 my $self = shift;
81
82 # By default, order by SortOrder
83 $self->OrderByCols(
84 { ALIAS => 'main',
85 FIELD => 'SortOrder',
86 ORDER => 'ASC' },
87 { ALIAS => 'main',
88 FIELD => 'Name',
89 ORDER => 'ASC' },
90 { ALIAS => 'main',
91 FIELD => 'id',
92 ORDER => 'ASC' },
93 );
94 $self->{'with_disabled_column'} = 1;
95
96 return ( $self->SUPER::_Init(@_) );
97}
98
99
100=head2 LimitToLookupType
101
102Takes LookupType and limits collection.
103
104=cut
105
106sub LimitToLookupType {
107 my $self = shift;
108 my $lookup = shift;
109
110 $self->Limit( FIELD => 'LookupType', VALUE => "$lookup" );
111}
112
113=head2 LimitToChildType
114
115Takes partial LookupType and limits collection to records
116where LookupType is equal or ends with the value.
117
118=cut
119
120sub LimitToChildType {
121 my $self = shift;
122 my $lookup = shift;
123
124 $self->Limit( FIELD => 'LookupType', VALUE => "$lookup" );
125 $self->Limit( FIELD => 'LookupType', ENDSWITH => "$lookup" );
126}
127
128
129=head2 LimitToParentType
130
131Takes partial LookupType and limits collection to records
132where LookupType is equal or starts with the value.
133
134=cut
135
136sub LimitToParentType {
137 my $self = shift;
138 my $lookup = shift;
139
140 $self->Limit( FIELD => 'LookupType', VALUE => "$lookup" );
141 $self->Limit( FIELD => 'LookupType', STARTSWITH => "$lookup" );
142}
143
01e3b242
MKG
144=head2 LimitToObjectId
145
146Takes an ObjectId and limits the collection to CFs applied to said object.
147
148When called multiple times the ObjectId limits are joined with OR.
149
150=cut
151
152sub LimitToObjectId {
153 my $self = shift;
154 my $id = shift;
155 $self->Limit(
156 ALIAS => $self->_OCFAlias,
157 FIELD => 'ObjectId',
158 OPERATOR => '=',
159 VALUE => $id || 0,
160 ENTRYAGGREGATOR => 'OR'
161 );
162}
84fb5b46
MKG
163
164=head2 LimitToGlobalOrObjectId
165
166Takes list of object IDs and limits collection to custom
167fields that are applied to these objects or globally.
168
169=cut
170
171sub LimitToGlobalOrObjectId {
172 my $self = shift;
173 my $global_only = 1;
174
175
176 foreach my $id (@_) {
01e3b242
MKG
177 $self->LimitToObjectId($id);
178 $global_only = 0 if $id;
84fb5b46
MKG
179 }
180
01e3b242 181 $self->LimitToObjectId(0) unless $global_only;
84fb5b46
MKG
182}
183
184sub _LimitToOCFs {
185 my $self = shift;
186 my @ids = @_;
187
188 my $ocfs_alias = $self->_OCFAlias( New => 1, Left => 1 );
189 if ( @ids ) {
190 # XXX: we need different EA in join clause, but DBIx::SB
191 # doesn't support them, use IN (X) instead
192 my $dbh = $self->_Handle->dbh;
193 $self->Limit(
194 LEFTJOIN => $ocfs_alias,
195 ALIAS => $ocfs_alias,
196 FIELD => 'ObjectId',
197 OPERATOR => 'IN',
198 QUOTEVALUE => 0,
199 VALUE => "(". join( ',', map $dbh->quote($_), @ids ) .")",
200 );
201 }
202
203 return $ocfs_alias;
204}
205
206=head2 LimitToNotApplied
207
208Takes either list of object ids or nothing. Limits collection
209to custom fields to listed objects or any corespondingly. Use
210zero to mean global.
211
212=cut
213
214sub LimitToNotApplied {
215 my $self = shift;
216 my @ids = @_;
217
218 my $ocfs_alias = $self->_LimitToOCFs(@ids);
219
220 $self->Limit(
221 ENTRYAGGREGATOR => 'AND',
222 ALIAS => $ocfs_alias,
223 FIELD => 'id',
224 OPERATOR => 'IS',
225 VALUE => 'NULL',
226 );
227}
228
229=head2 LimitToApplied
230
231Limits collection to custom fields to listed objects or any corespondingly. Use
232zero to mean global.
233
234=cut
235
236sub LimitToApplied {
237 my $self = shift;
238 my @ids = @_;
239
240 my $ocfs_alias = $self->_LimitToOCFs(@ids);
241
242 $self->Limit(
243 ENTRYAGGREGATOR => 'AND',
244 ALIAS => $ocfs_alias,
245 FIELD => 'id',
246 OPERATOR => 'IS NOT',
247 VALUE => 'NULL',
248 );
249}
250
251=head2 LimitToGlobalOrQueue QUEUEID
252
253DEPRECATED since CFs are applicable not only to tickets these days.
254
255Limits the set of custom fields found to global custom fields or those tied to the queue with ID QUEUEID
256
257=cut
258
259sub LimitToGlobalOrQueue {
260 my $self = shift;
261 my $queue = shift;
262 $self->LimitToGlobalOrObjectId( $queue );
263 $self->LimitToLookupType( 'RT::Queue-RT::Ticket' );
264}
265
266
267=head2 LimitToQueue QUEUEID
268
269DEPRECATED since CFs are applicable not only to tickets these days.
270
271Takes a queue id (numerical) as its only argument. Makes sure that
272Scopes it pulls out apply to this queue (or another that you've selected with
273another call to this method
274
275=cut
276
277sub LimitToQueue {
278 my $self = shift;
279 my $queue = shift;
280
281 $self->Limit (ALIAS => $self->_OCFAlias,
282 ENTRYAGGREGATOR => 'OR',
283 FIELD => 'ObjectId',
284 VALUE => "$queue")
285 if defined $queue;
286 $self->LimitToLookupType( 'RT::Queue-RT::Ticket' );
287}
288
289
290=head2 LimitToGlobal
291
292DEPRECATED since CFs are applicable not only to tickets these days.
293
294Makes sure that Scopes it pulls out apply to all queues
295(or another that you've selected with
296another call to this method or LimitToQueue)
297
298=cut
299
300sub LimitToGlobal {
301 my $self = shift;
302
303 $self->Limit (ALIAS => $self->_OCFAlias,
304 ENTRYAGGREGATOR => 'OR',
305 FIELD => 'ObjectId',
306 VALUE => 0);
307 $self->LimitToLookupType( 'RT::Queue-RT::Ticket' );
308}
309
310
311=head2 ApplySortOrder
312
313Sort custom fields according to thier order application to objects. It's
314expected that collection contains only records of one
315L<RT::CustomField/LookupType> and applied to one object or globally
316(L</LimitToGlobalOrObjectId>), otherwise sorting makes no sense.
317
318=cut
319
320sub ApplySortOrder {
321 my $self = shift;
322 my $order = shift || 'ASC';
323 $self->OrderByCols( {
324 ALIAS => $self->_OCFAlias,
325 FIELD => 'SortOrder',
326 ORDER => $order,
327 } );
328}
329
330
331=head2 ContextObject
332
333Returns context object for this collection of custom fields,
334but only if it's defined.
335
336=cut
337
338sub ContextObject {
339 my $self = shift;
340 return $self->{'context_object'};
341}
342
343
344=head2 SetContextObject
345
346Sets context object for this collection of custom fields.
347
348=cut
349
350sub SetContextObject {
351 my $self = shift;
352 return $self->{'context_object'} = shift;
353}
354
355
356sub _OCFAlias {
357 my $self = shift;
358 my %args = ( New => 0, Left => 0, @_ );
359
360 return $self->{'_sql_ocfalias'} if $self->{'_sql_ocfalias'} && !$args{'New'};
361
362 my $alias = $self->Join(
363 $args{'Left'} ? (TYPE => 'LEFT') : (),
364 ALIAS1 => 'main',
365 FIELD1 => 'id',
366 TABLE2 => 'ObjectCustomFields',
367 FIELD2 => 'CustomField'
368 );
369 return $alias if $args{'New'};
370 return $self->{'_sql_ocfalias'} = $alias;
371}
372
373
374=head2 Next
375
376Returns the next custom field that this user can see.
377
378=cut
379
380sub Next {
381 my $self = shift;
382
383 my $CF = $self->SUPER::Next();
384 return $CF unless $CF;
385
386 $CF->SetContextObject( $self->ContextObject );
387
388 return $self->Next unless $CF->CurrentUserHasRight('SeeCustomField');
389 return $CF;
390}
391
392=head2 NewItem
393
394Returns an empty new RT::CustomField item
395Overrides <RT::SearchBuilder/NewItem> to make sure </ContextObject>
396is inherited.
397
398=cut
399
400sub NewItem {
401 my $self = shift;
402 my $res = RT::CustomField->new($self->CurrentUser);
403 $res->SetContextObject($self->ContextObject);
404 return $res;
405}
406
407RT::Base->_ImportOverlays();
408
4091;