1 # BEGIN BPS TAGGED BLOCK {{{
5 # This software is Copyright (c) 1996-2013 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" );
145 =head2 LimitToGlobalOrObjectId
147 Takes list of object IDs and limits collection to custom
148 fields that are applied to these objects or globally.
152 sub LimitToGlobalOrObjectId {
157 foreach my $id (@_) {
158 $self->Limit( ALIAS => $self->_OCFAlias,
162 ENTRYAGGREGATOR => 'OR' );
163 $global_only = 0 if $id;
166 $self->Limit( ALIAS => $self->_OCFAlias,
170 ENTRYAGGREGATOR => 'OR' ) unless $global_only;
177 my $ocfs_alias = $self->_OCFAlias( New => 1, Left => 1 );
179 # XXX: we need different EA in join clause, but DBIx::SB
180 # doesn't support them, use IN (X) instead
181 my $dbh = $self->_Handle->dbh;
183 LEFTJOIN => $ocfs_alias,
184 ALIAS => $ocfs_alias,
188 VALUE => "(". join( ',', map $dbh->quote($_), @ids ) .")",
195 =head2 LimitToNotApplied
197 Takes either list of object ids or nothing. Limits collection
198 to custom fields to listed objects or any corespondingly. Use
203 sub LimitToNotApplied {
207 my $ocfs_alias = $self->_LimitToOCFs(@ids);
210 ENTRYAGGREGATOR => 'AND',
211 ALIAS => $ocfs_alias,
218 =head2 LimitToApplied
220 Limits collection to custom fields to listed objects or any corespondingly. Use
229 my $ocfs_alias = $self->_LimitToOCFs(@ids);
232 ENTRYAGGREGATOR => 'AND',
233 ALIAS => $ocfs_alias,
235 OPERATOR => 'IS NOT',
240 =head2 LimitToGlobalOrQueue QUEUEID
242 DEPRECATED since CFs are applicable not only to tickets these days.
244 Limits the set of custom fields found to global custom fields or those tied to the queue with ID QUEUEID
248 sub LimitToGlobalOrQueue {
251 $self->LimitToGlobalOrObjectId( $queue );
252 $self->LimitToLookupType( 'RT::Queue-RT::Ticket' );
256 =head2 LimitToQueue QUEUEID
258 DEPRECATED since CFs are applicable not only to tickets these days.
260 Takes a queue id (numerical) as its only argument. Makes sure that
261 Scopes it pulls out apply to this queue (or another that you've selected with
262 another call to this method
270 $self->Limit (ALIAS => $self->_OCFAlias,
271 ENTRYAGGREGATOR => 'OR',
275 $self->LimitToLookupType( 'RT::Queue-RT::Ticket' );
281 DEPRECATED since CFs are applicable not only to tickets these days.
283 Makes sure that Scopes it pulls out apply to all queues
284 (or another that you've selected with
285 another call to this method or LimitToQueue)
292 $self->Limit (ALIAS => $self->_OCFAlias,
293 ENTRYAGGREGATOR => 'OR',
296 $self->LimitToLookupType( 'RT::Queue-RT::Ticket' );
300 =head2 ApplySortOrder
302 Sort custom fields according to thier order application to objects. It's
303 expected that collection contains only records of one
304 L<RT::CustomField/LookupType> and applied to one object or globally
305 (L</LimitToGlobalOrObjectId>), otherwise sorting makes no sense.
311 my $order = shift || 'ASC';
312 $self->OrderByCols( {
313 ALIAS => $self->_OCFAlias,
314 FIELD => 'SortOrder',
322 Returns context object for this collection of custom fields,
323 but only if it's defined.
329 return $self->{'context_object'};
333 =head2 SetContextObject
335 Sets context object for this collection of custom fields.
339 sub SetContextObject {
341 return $self->{'context_object'} = shift;
347 my %args = ( New => 0, Left => 0, @_ );
349 return $self->{'_sql_ocfalias'} if $self->{'_sql_ocfalias'} && !$args{'New'};
351 my $alias = $self->Join(
352 $args{'Left'} ? (TYPE => 'LEFT') : (),
355 TABLE2 => 'ObjectCustomFields',
356 FIELD2 => 'CustomField'
358 return $alias if $args{'New'};
359 return $self->{'_sql_ocfalias'} = $alias;
365 Returns the next custom field that this user can see.
372 my $CF = $self->SUPER::Next();
373 return $CF unless $CF;
375 $CF->SetContextObject( $self->ContextObject );
377 return $self->Next unless $CF->CurrentUserHasRight('SeeCustomField');
383 Returns an empty new RT::CustomField item
384 Overrides <RT::SearchBuilder/NewItem> to make sure </ContextObject>
391 my $res = RT::CustomField->new($self->CurrentUser);
392 $res->SetContextObject($self->ContextObject);
396 RT::Base->_ImportOverlays();