1 # BEGIN BPS TAGGED BLOCK {{{
5 # This software is Copyright (c) 1996-2009 Best Practical Solutions, LLC
6 # <jesse@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;
70 no warnings qw(redefine);
71 use DBIx::SearchBuilder::Unique;
75 $self->{'table'} = 'CustomFields';
76 $self->{'primary_key'} = 'id';
77 $self->{'with_disabled_column'} = 1;
79 return $self->SUPER::_Init(@_);
83 =head2 LimitToLookupType
85 Takes LookupType and limits collection.
89 sub LimitToLookupType {
93 $self->Limit( FIELD => 'LookupType', VALUE => "$lookup" );
96 =head2 LimitToChildType
98 Takes partial LookupType and limits collection to records
99 where LookupType is equal or ends with the value.
103 sub LimitToChildType {
107 $self->Limit( FIELD => 'LookupType', VALUE => "$lookup" );
108 $self->Limit( FIELD => 'LookupType', ENDSWITH => "$lookup" );
112 =head2 LimitToParentType
114 Takes partial LookupType and limits collection to records
115 where LookupType is equal or starts with the value.
119 sub LimitToParentType {
123 $self->Limit( FIELD => 'LookupType', VALUE => "$lookup" );
124 $self->Limit( FIELD => 'LookupType', STARTSWITH => "$lookup" );
128 =head2 LimitToGlobalOrObjectId
130 Takes list of object IDs and limits collection to custom
131 fields that are applied to these objects or globally.
135 sub LimitToGlobalOrObjectId {
140 foreach my $id (@_) {
141 $self->Limit( ALIAS => $self->_OCFAlias,
145 ENTRYAGGREGATOR => 'OR' );
146 $global_only = 0 if $id;
149 $self->Limit( ALIAS => $self->_OCFAlias,
153 ENTRYAGGREGATOR => 'OR' ) unless $global_only;
156 =head2 LimitToNotApplied
158 Takes either list of object ids or nothing. Limits collection
159 to custom fields to listed objects or any corespondingly. Use
164 sub LimitToNotApplied {
168 my $ocfs_alias = $self->_OCFAlias( New => 1, Left => 1 );
170 # XXX: we need different EA in join clause, but DBIx::SB
171 # doesn't support them, use IN (X) instead
172 my $dbh = $self->_Handle->dbh;
174 LEFTJOIN => $ocfs_alias,
175 ALIAS => $ocfs_alias,
179 VALUE => "(". join( ',', map $dbh->quote($_), @ids ) .")",
184 ENTRYAGGREGATOR => 'AND',
185 ALIAS => $ocfs_alias,
192 =head2 LimitToGlobalOrQueue QUEUEID
194 DEPRECATED since CFs are applicable not only to tickets these days.
196 Limits the set of custom fields found to global custom fields or those tied to the queue with ID QUEUEID
200 sub LimitToGlobalOrQueue {
203 $self->LimitToGlobalOrObjectId( $queue );
204 $self->LimitToLookupType( 'RT::Queue-RT::Ticket' );
208 =head2 LimitToQueue QUEUEID
210 DEPRECATED since CFs are applicable not only to tickets these days.
212 Takes a queue id (numerical) as its only argument. Makes sure that
213 Scopes it pulls out apply to this queue (or another that you've selected with
214 another call to this method
222 $self->Limit (ALIAS => $self->_OCFAlias,
223 ENTRYAGGREGATOR => 'OR',
227 $self->LimitToLookupType( 'RT::Queue-RT::Ticket' );
233 DEPRECATED since CFs are applicable not only to tickets these days.
235 Makes sure that Scopes it pulls out apply to all queues
236 (or another that you've selected with
237 another call to this method or LimitToQueue)
244 $self->Limit (ALIAS => $self->_OCFAlias,
245 ENTRYAGGREGATOR => 'OR',
248 $self->LimitToLookupType( 'RT::Queue-RT::Ticket' );
252 =head2 ApplySortOrder
254 Sort custom fields according to thier order application to objects. It's
255 expected that collection contains only records of one
256 L<RT::CustomField/LookupType> and applied to one object or globally
257 (L</LimitToGlobalOrObjectId>), otherwise sorting makes no sense.
263 my $order = shift || 'ASC';
264 $self->OrderByCols( {
265 ALIAS => $self->_OCFAlias,
266 FIELD => 'SortOrder',
274 Returns context object for this collection of custom fields,
275 but only if it's defined.
281 return $self->{'context_object'};
285 =head2 SetContextObject
287 Sets context object for this collection of custom fields.
291 sub SetContextObject {
293 return $self->{'context_object'} = shift;
299 my %args = ( New => 0, Left => 0, @_ );
301 return $self->{'_sql_ocfalias'} if $self->{'_sql_ocfalias'} && !$args{'New'};
303 my $alias = $self->Join(
304 $args{'Left'} ? (TYPE => 'LEFT') : (),
307 TABLE2 => 'ObjectCustomFields',
308 FIELD2 => 'CustomField'
310 return $alias if $args{'New'};
311 return $self->{'_sql_ocfalias'} = $alias;
317 Returns the next custom field that this user can see.
324 my $CF = $self->SUPER::Next();
325 return $CF unless $CF;
327 $CF->SetContextOject( $self->ContextObject );
329 return $self->Next unless $CF->CurrentUserHasRight('SeeCustomField');
335 Overrides <RT::SearchBuilder/Next> to make sure </ContextObject>
342 my $res = RT::CustomField->new($self->CurrentUser);
343 $res->SetContextObject($self->ContextObject);