1 # BEGIN BPS TAGGED BLOCK {{{
5 # This software is Copyright (c) 1996-2015 Best Practical Solutions, LLC
6 # <sales@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::CustomFields - a collection of RT CustomField objects
66 package RT::CustomFields;
73 use base 'RT::SearchBuilder';
75 sub Table { 'CustomFields'}
80 # By default, order by SortOrder
92 $self->{'with_disabled_column'} = 1;
94 return ( $self->SUPER::_Init(@_) );
98 =head2 LimitToLookupType
100 Takes LookupType and limits collection.
104 sub LimitToLookupType {
108 $self->Limit( FIELD => 'LookupType', VALUE => "$lookup" );
111 =head2 LimitToChildType
113 Takes partial LookupType and limits collection to records
114 where LookupType is equal or ends with the value.
118 sub LimitToChildType {
122 $self->Limit( FIELD => 'LookupType', VALUE => "$lookup", OPERATOR => "ENDSWITH" );
126 =head2 LimitToParentType
128 Takes partial LookupType and limits collection to records
129 where LookupType is equal or starts with the value.
133 sub LimitToParentType {
137 $self->Limit( FIELD => 'LookupType', VALUE => "$lookup", OPERATOR => "STARTSWITH" );
140 =head2 LimitToObjectId
142 Takes an ObjectId and limits the collection to CFs applied to said object.
144 When called multiple times the ObjectId limits are joined with OR.
148 sub LimitToObjectId {
152 ALIAS => $self->_OCFAlias,
156 ENTRYAGGREGATOR => 'OR'
160 =head2 LimitToGlobalOrObjectId
162 Takes list of object IDs and limits collection to custom
163 fields that are applied to these objects or globally.
167 sub LimitToGlobalOrObjectId {
172 foreach my $id (@_) {
173 $self->LimitToObjectId($id);
174 $global_only = 0 if $id;
177 $self->LimitToObjectId(0) unless $global_only;
184 my $ocfs_alias = $self->_OCFAlias( New => 1, Left => 1 );
186 # XXX: we need different EA in join clause, but DBIx::SB
187 # doesn't support them, use IN (X) instead
188 my $dbh = $self->_Handle->dbh;
190 LEFTJOIN => $ocfs_alias,
191 ALIAS => $ocfs_alias,
195 VALUE => "(". join( ',', map $dbh->quote($_), @ids ) .")",
202 =head2 LimitToNotApplied
204 Takes either list of object ids or nothing. Limits collection
205 to custom fields to listed objects or any corespondingly. Use
210 sub LimitToNotApplied {
214 my $ocfs_alias = $self->_LimitToOCFs(@ids);
217 ENTRYAGGREGATOR => 'AND',
218 ALIAS => $ocfs_alias,
225 =head2 LimitToApplied
227 Limits collection to custom fields to listed objects or any corespondingly. Use
236 my $ocfs_alias = $self->_LimitToOCFs(@ids);
239 ENTRYAGGREGATOR => 'AND',
240 ALIAS => $ocfs_alias,
242 OPERATOR => 'IS NOT',
247 =head2 LimitToGlobalOrQueue QUEUEID
249 DEPRECATED since CFs are applicable not only to tickets these days.
251 Limits the set of custom fields found to global custom fields or those tied to the queue with ID QUEUEID
255 sub LimitToGlobalOrQueue {
258 $self->LimitToGlobalOrObjectId( $queue );
259 $self->LimitToLookupType( 'RT::Queue-RT::Ticket' );
263 =head2 LimitToQueue QUEUEID
265 DEPRECATED since CFs are applicable not only to tickets these days.
267 Takes a queue id (numerical) as its only argument. Makes sure that
268 Scopes it pulls out apply to this queue (or another that you've selected with
269 another call to this method
277 $self->Limit (ALIAS => $self->_OCFAlias,
278 ENTRYAGGREGATOR => 'OR',
282 $self->LimitToLookupType( 'RT::Queue-RT::Ticket' );
288 DEPRECATED since CFs are applicable not only to tickets these days.
290 Makes sure that Scopes it pulls out apply to all queues
291 (or another that you've selected with
292 another call to this method or LimitToQueue)
299 $self->Limit (ALIAS => $self->_OCFAlias,
300 ENTRYAGGREGATOR => 'OR',
303 $self->LimitToLookupType( 'RT::Queue-RT::Ticket' );
307 =head2 ApplySortOrder
309 Sort custom fields according to thier order application to objects. It's
310 expected that collection contains only records of one
311 L<RT::CustomField/LookupType> and applied to one object or globally
312 (L</LimitToGlobalOrObjectId>), otherwise sorting makes no sense.
318 my $order = shift || 'ASC';
319 $self->OrderByCols( {
320 ALIAS => $self->_OCFAlias,
321 FIELD => 'SortOrder',
329 Returns context object for this collection of custom fields,
330 but only if it's defined.
336 return $self->{'context_object'};
340 =head2 SetContextObject
342 Sets context object for this collection of custom fields.
346 sub SetContextObject {
348 return $self->{'context_object'} = shift;
354 my %args = ( New => 0, Left => 0, @_ );
356 return $self->{'_sql_ocfalias'} if $self->{'_sql_ocfalias'} && !$args{'New'};
358 my $alias = $self->Join(
359 $args{'Left'} ? (TYPE => 'LEFT') : (),
362 TABLE2 => 'ObjectCustomFields',
363 FIELD2 => 'CustomField'
365 return $alias if $args{'New'};
366 return $self->{'_sql_ocfalias'} = $alias;
372 Overrides the collection to ensure that only custom fields the user can
373 see are returned; also propagates down the L</ContextObject>.
381 $record->SetContextObject( $self->ContextObject );
382 return unless $record->CurrentUserHasRight('SeeCustomField');
383 return $self->SUPER::AddRecord( $record );
388 Returns an empty new RT::CustomField item
389 Overrides <RT::SearchBuilder/NewItem> to make sure </ContextObject>
396 my $res = RT::CustomField->new($self->CurrentUser);
397 $res->SetContextObject($self->ContextObject);
401 RT::Base->_ImportOverlays();