1 # BEGIN BPS TAGGED BLOCK {{{
5 # This software is Copyright (c) 1996-2014 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;
71 use DBIx::SearchBuilder::Unique;
75 use base 'RT::SearchBuilder';
77 sub Table { 'CustomFields'}
82 # By default, order by SortOrder
94 $self->{'with_disabled_column'} = 1;
96 return ( $self->SUPER::_Init(@_) );
100 =head2 LimitToLookupType
102 Takes LookupType and limits collection.
106 sub LimitToLookupType {
110 $self->Limit( FIELD => 'LookupType', VALUE => "$lookup" );
113 =head2 LimitToChildType
115 Takes partial LookupType and limits collection to records
116 where LookupType is equal or ends with the value.
120 sub LimitToChildType {
124 $self->Limit( FIELD => 'LookupType', VALUE => "$lookup" );
125 $self->Limit( FIELD => 'LookupType', ENDSWITH => "$lookup" );
129 =head2 LimitToParentType
131 Takes partial LookupType and limits collection to records
132 where LookupType is equal or starts with the value.
136 sub LimitToParentType {
140 $self->Limit( FIELD => 'LookupType', VALUE => "$lookup" );
141 $self->Limit( FIELD => 'LookupType', STARTSWITH => "$lookup" );
144 =head2 LimitToObjectId
146 Takes an ObjectId and limits the collection to CFs applied to said object.
148 When called multiple times the ObjectId limits are joined with OR.
152 sub LimitToObjectId {
156 ALIAS => $self->_OCFAlias,
160 ENTRYAGGREGATOR => 'OR'
164 =head2 LimitToGlobalOrObjectId
166 Takes list of object IDs and limits collection to custom
167 fields that are applied to these objects or globally.
171 sub LimitToGlobalOrObjectId {
176 foreach my $id (@_) {
177 $self->LimitToObjectId($id);
178 $global_only = 0 if $id;
181 $self->LimitToObjectId(0) unless $global_only;
188 my $ocfs_alias = $self->_OCFAlias( New => 1, Left => 1 );
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;
194 LEFTJOIN => $ocfs_alias,
195 ALIAS => $ocfs_alias,
199 VALUE => "(". join( ',', map $dbh->quote($_), @ids ) .")",
206 =head2 LimitToNotApplied
208 Takes either list of object ids or nothing. Limits collection
209 to custom fields to listed objects or any corespondingly. Use
214 sub LimitToNotApplied {
218 my $ocfs_alias = $self->_LimitToOCFs(@ids);
221 ENTRYAGGREGATOR => 'AND',
222 ALIAS => $ocfs_alias,
229 =head2 LimitToApplied
231 Limits collection to custom fields to listed objects or any corespondingly. Use
240 my $ocfs_alias = $self->_LimitToOCFs(@ids);
243 ENTRYAGGREGATOR => 'AND',
244 ALIAS => $ocfs_alias,
246 OPERATOR => 'IS NOT',
251 =head2 LimitToGlobalOrQueue QUEUEID
253 DEPRECATED since CFs are applicable not only to tickets these days.
255 Limits the set of custom fields found to global custom fields or those tied to the queue with ID QUEUEID
259 sub LimitToGlobalOrQueue {
262 $self->LimitToGlobalOrObjectId( $queue );
263 $self->LimitToLookupType( 'RT::Queue-RT::Ticket' );
267 =head2 LimitToQueue QUEUEID
269 DEPRECATED since CFs are applicable not only to tickets these days.
271 Takes a queue id (numerical) as its only argument. Makes sure that
272 Scopes it pulls out apply to this queue (or another that you've selected with
273 another call to this method
281 $self->Limit (ALIAS => $self->_OCFAlias,
282 ENTRYAGGREGATOR => 'OR',
286 $self->LimitToLookupType( 'RT::Queue-RT::Ticket' );
292 DEPRECATED since CFs are applicable not only to tickets these days.
294 Makes sure that Scopes it pulls out apply to all queues
295 (or another that you've selected with
296 another call to this method or LimitToQueue)
303 $self->Limit (ALIAS => $self->_OCFAlias,
304 ENTRYAGGREGATOR => 'OR',
307 $self->LimitToLookupType( 'RT::Queue-RT::Ticket' );
311 =head2 ApplySortOrder
313 Sort custom fields according to thier order application to objects. It's
314 expected that collection contains only records of one
315 L<RT::CustomField/LookupType> and applied to one object or globally
316 (L</LimitToGlobalOrObjectId>), otherwise sorting makes no sense.
322 my $order = shift || 'ASC';
323 $self->OrderByCols( {
324 ALIAS => $self->_OCFAlias,
325 FIELD => 'SortOrder',
333 Returns context object for this collection of custom fields,
334 but only if it's defined.
340 return $self->{'context_object'};
344 =head2 SetContextObject
346 Sets context object for this collection of custom fields.
350 sub SetContextObject {
352 return $self->{'context_object'} = shift;
358 my %args = ( New => 0, Left => 0, @_ );
360 return $self->{'_sql_ocfalias'} if $self->{'_sql_ocfalias'} && !$args{'New'};
362 my $alias = $self->Join(
363 $args{'Left'} ? (TYPE => 'LEFT') : (),
366 TABLE2 => 'ObjectCustomFields',
367 FIELD2 => 'CustomField'
369 return $alias if $args{'New'};
370 return $self->{'_sql_ocfalias'} = $alias;
376 Returns the next custom field that this user can see.
383 my $CF = $self->SUPER::Next();
384 return $CF unless $CF;
386 $CF->SetContextObject( $self->ContextObject );
388 return $self->Next unless $CF->CurrentUserHasRight('SeeCustomField');
394 Returns an empty new RT::CustomField item
395 Overrides <RT::SearchBuilder/NewItem> to make sure </ContextObject>
402 my $res = RT::CustomField->new($self->CurrentUser);
403 $res->SetContextObject($self->ContextObject);
407 RT::Base->_ImportOverlays();