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", OPERATOR => "ENDSWITH" );
128 =head2 LimitToParentType
130 Takes partial LookupType and limits collection to records
131 where LookupType is equal or starts with the value.
135 sub LimitToParentType {
139 $self->Limit( FIELD => 'LookupType', VALUE => "$lookup", OPERATOR => "STARTSWITH" );
142 =head2 LimitToObjectId
144 Takes an ObjectId and limits the collection to CFs applied to said object.
146 When called multiple times the ObjectId limits are joined with OR.
150 sub LimitToObjectId {
154 ALIAS => $self->_OCFAlias,
158 ENTRYAGGREGATOR => 'OR'
162 =head2 LimitToGlobalOrObjectId
164 Takes list of object IDs and limits collection to custom
165 fields that are applied to these objects or globally.
169 sub LimitToGlobalOrObjectId {
174 foreach my $id (@_) {
175 $self->LimitToObjectId($id);
176 $global_only = 0 if $id;
179 $self->LimitToObjectId(0) unless $global_only;
186 my $ocfs_alias = $self->_OCFAlias( New => 1, Left => 1 );
188 # XXX: we need different EA in join clause, but DBIx::SB
189 # doesn't support them, use IN (X) instead
190 my $dbh = $self->_Handle->dbh;
192 LEFTJOIN => $ocfs_alias,
193 ALIAS => $ocfs_alias,
197 VALUE => "(". join( ',', map $dbh->quote($_), @ids ) .")",
204 =head2 LimitToNotApplied
206 Takes either list of object ids or nothing. Limits collection
207 to custom fields to listed objects or any corespondingly. Use
212 sub LimitToNotApplied {
216 my $ocfs_alias = $self->_LimitToOCFs(@ids);
219 ENTRYAGGREGATOR => 'AND',
220 ALIAS => $ocfs_alias,
227 =head2 LimitToApplied
229 Limits collection to custom fields to listed objects or any corespondingly. Use
238 my $ocfs_alias = $self->_LimitToOCFs(@ids);
241 ENTRYAGGREGATOR => 'AND',
242 ALIAS => $ocfs_alias,
244 OPERATOR => 'IS NOT',
249 =head2 LimitToGlobalOrQueue QUEUEID
251 DEPRECATED since CFs are applicable not only to tickets these days.
253 Limits the set of custom fields found to global custom fields or those tied to the queue with ID QUEUEID
257 sub LimitToGlobalOrQueue {
260 $self->LimitToGlobalOrObjectId( $queue );
261 $self->LimitToLookupType( 'RT::Queue-RT::Ticket' );
265 =head2 LimitToQueue QUEUEID
267 DEPRECATED since CFs are applicable not only to tickets these days.
269 Takes a queue id (numerical) as its only argument. Makes sure that
270 Scopes it pulls out apply to this queue (or another that you've selected with
271 another call to this method
279 $self->Limit (ALIAS => $self->_OCFAlias,
280 ENTRYAGGREGATOR => 'OR',
284 $self->LimitToLookupType( 'RT::Queue-RT::Ticket' );
290 DEPRECATED since CFs are applicable not only to tickets these days.
292 Makes sure that Scopes it pulls out apply to all queues
293 (or another that you've selected with
294 another call to this method or LimitToQueue)
301 $self->Limit (ALIAS => $self->_OCFAlias,
302 ENTRYAGGREGATOR => 'OR',
305 $self->LimitToLookupType( 'RT::Queue-RT::Ticket' );
309 =head2 ApplySortOrder
311 Sort custom fields according to thier order application to objects. It's
312 expected that collection contains only records of one
313 L<RT::CustomField/LookupType> and applied to one object or globally
314 (L</LimitToGlobalOrObjectId>), otherwise sorting makes no sense.
320 my $order = shift || 'ASC';
321 $self->OrderByCols( {
322 ALIAS => $self->_OCFAlias,
323 FIELD => 'SortOrder',
331 Returns context object for this collection of custom fields,
332 but only if it's defined.
338 return $self->{'context_object'};
342 =head2 SetContextObject
344 Sets context object for this collection of custom fields.
348 sub SetContextObject {
350 return $self->{'context_object'} = shift;
356 my %args = ( New => 0, Left => 0, @_ );
358 return $self->{'_sql_ocfalias'} if $self->{'_sql_ocfalias'} && !$args{'New'};
360 my $alias = $self->Join(
361 $args{'Left'} ? (TYPE => 'LEFT') : (),
364 TABLE2 => 'ObjectCustomFields',
365 FIELD2 => 'CustomField'
367 return $alias if $args{'New'};
368 return $self->{'_sql_ocfalias'} = $alias;
374 Returns the next custom field that this user can see.
381 my $CF = $self->SUPER::Next();
382 return $CF unless $CF;
384 $CF->SetContextObject( $self->ContextObject );
386 return $self->Next unless $CF->CurrentUserHasRight('SeeCustomField');
392 Returns an empty new RT::CustomField item
393 Overrides <RT::SearchBuilder/NewItem> to make sure </ContextObject>
400 my $res = RT::CustomField->new($self->CurrentUser);
401 $res->SetContextObject($self->ContextObject);
405 RT::Base->_ImportOverlays();