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 }}}
49 package RT::ObjectCustomField;
56 use base 'RT::Record';
58 sub Table {'ObjectCustomFields'}
74 my $cf = $self->CustomFieldObj( $args{'CustomField'} );
76 $RT::Logger->error("Couldn't load '$args{'CustomField'}' custom field");
80 #XXX: Where is ACL check for 'AssignCustomFields'?
82 my $ObjectCFs = RT::ObjectCustomFields->new($self->CurrentUser);
83 $ObjectCFs->LimitToObjectId( $args{'ObjectId'} );
84 $ObjectCFs->LimitToCustomField( $cf->id );
85 $ObjectCFs->LimitToLookupType( $cf->LookupType );
86 if ( my $first = $ObjectCFs->First ) {
87 $self->Load( $first->id );
91 unless ( defined $args{'SortOrder'} ) {
92 my $ObjectCFs = RT::ObjectCustomFields->new( RT->SystemUser );
93 $ObjectCFs->LimitToObjectId( $args{'ObjectId'} );
94 $ObjectCFs->LimitToObjectId( 0 ) if $args{'ObjectId'};
95 $ObjectCFs->LimitToLookupType( $cf->LookupType );
96 $ObjectCFs->OrderBy( FIELD => 'SortOrder', ORDER => 'DESC' );
97 if ( my $first = $ObjectCFs->First ) {
98 $args{'SortOrder'} = $first->SortOrder + 1;
100 $args{'SortOrder'} = 0;
104 return $self->SUPER::Create(
105 CustomField => $args{'CustomField'},
106 ObjectId => $args{'ObjectId'},
107 SortOrder => $args{'SortOrder'},
114 my $ObjectCFs = RT::ObjectCustomFields->new($self->CurrentUser);
115 $ObjectCFs->LimitToObjectId($self->ObjectId);
116 $ObjectCFs->LimitToLookupType($self->CustomFieldObj->LookupType);
118 # Move everything below us up
119 my $sort_order = $self->SortOrder;
120 while (my $OCF = $ObjectCFs->Next) {
121 my $this_order = $OCF->SortOrder;
122 next if $this_order <= $sort_order;
123 $OCF->SetSortOrder($this_order - 1);
126 $self->SUPER::Delete;
130 =head2 CustomFieldObj
132 Returns the CustomField Object which has the id returned by CustomField
139 my $id = shift || $self->CustomField;
141 # To find out the proper context object to load the CF with, we need
142 # data from the CF -- namely, the record class. Go find that as the
144 my $system_CF = RT::CustomField->new( RT->SystemUser );
145 $system_CF->Load( $id );
146 my $class = $system_CF->RecordClassFromLookupType;
148 my $obj = $class->new( $self->CurrentUser );
149 $obj->Load( $self->ObjectId );
151 my $CF = RT::CustomField->new( $self->CurrentUser );
152 $CF->SetContextObject( $obj );
157 =head2 Sorting custom fields applications
159 Custom fields sorted on multiple layers. First of all custom
160 fields with different lookup type are sorted independently. All
161 global custom fields have fixed order for all objects, but you
162 can insert object specific custom fields between them. Object
163 specific custom fields can be applied to several objects and
164 be on different place. For example you have GCF1, GCF2, LCF1,
165 LCF2 and LCF3 that applies to tickets. You can place GCF2
166 above GCF1, but they will be in the same order in all queues.
167 However, LCF1 and other local can be placed at any place
168 for particular queue: above global, between them or below.
172 Moves custom field up. See </Sorting custom fields applications>.
179 my $ocfs = RT::ObjectCustomFields->new( $self->CurrentUser );
181 my $oid = $self->ObjectId;
182 $ocfs->LimitToObjectId( $oid );
184 $ocfs->LimitToObjectId( 0 );
187 my $cf = $self->CustomFieldObj;
188 $ocfs->LimitToLookupType( $cf->LookupType );
190 $ocfs->Limit( FIELD => 'SortOrder', OPERATOR => '<', VALUE => $self->SortOrder );
191 $ocfs->OrderByCols( { FIELD => 'SortOrder', ORDER => 'DESC' } );
193 my @above = ($ocfs->Next, $ocfs->Next);
195 return (0, "Can not move up. It's already at the top");
199 if ( $above[0]->ObjectId == $self->ObjectId ) {
200 $new_sort_order = $above[0]->SortOrder;
201 my ($status, $msg) = $above[0]->SetSortOrder( $self->SortOrder );
203 return (0, "Couldn't move custom field");
206 elsif ( $above[1] && $above[0]->SortOrder == $above[1]->SortOrder + 1 ) {
207 my $move_ocfs = RT::ObjectCustomFields->new( RT->SystemUser );
208 $move_ocfs->LimitToLookupType( $cf->LookupType );
210 FIELD => 'SortOrder',
212 VALUE => $above[0]->SortOrder,
214 $move_ocfs->OrderByCols( { FIELD => 'SortOrder', ORDER => 'DESC' } );
215 while ( my $record = $move_ocfs->Next ) {
216 my ($status, $msg) = $record->SetSortOrder( $record->SortOrder + 1 );
218 return (0, "Couldn't move custom field");
221 $new_sort_order = $above[0]->SortOrder;
223 $new_sort_order = $above[0]->SortOrder - 1;
226 my ($status, $msg) = $self->SetSortOrder( $new_sort_order );
228 return (0, "Couldn't move custom field");
231 return (1,"Moved custom field up");
236 Moves custom field down. See </Sorting custom fields applications>.
243 my $ocfs = RT::ObjectCustomFields->new( $self->CurrentUser );
245 my $oid = $self->ObjectId;
246 $ocfs->LimitToObjectId( $oid );
248 $ocfs->LimitToObjectId( 0 );
251 my $cf = $self->CustomFieldObj;
252 $ocfs->LimitToLookupType( $cf->LookupType );
254 $ocfs->Limit( FIELD => 'SortOrder', OPERATOR => '>', VALUE => $self->SortOrder );
255 $ocfs->OrderByCols( { FIELD => 'SortOrder', ORDER => 'ASC' } );
257 my @below = ($ocfs->Next, $ocfs->Next);
259 return (0, "Can not move down. It's already at the bottom");
263 if ( $below[0]->ObjectId == $self->ObjectId ) {
264 $new_sort_order = $below[0]->SortOrder;
265 my ($status, $msg) = $below[0]->SetSortOrder( $self->SortOrder );
267 return (0, "Couldn't move custom field");
270 elsif ( $below[1] && $below[0]->SortOrder + 1 == $below[1]->SortOrder ) {
271 my $move_ocfs = RT::ObjectCustomFields->new( RT->SystemUser );
272 $move_ocfs->LimitToLookupType( $cf->LookupType );
274 FIELD => 'SortOrder',
276 VALUE => $below[0]->SortOrder,
278 $move_ocfs->OrderByCols( { FIELD => 'SortOrder', ORDER => 'ASC' } );
279 while ( my $record = $move_ocfs->Next ) {
280 my ($status, $msg) = $record->SetSortOrder( $record->SortOrder - 1 );
282 return (0, "Couldn't move custom field");
285 $new_sort_order = $below[0]->SortOrder;
287 $new_sort_order = $below[0]->SortOrder + 1;
290 my ($status, $msg) = $self->SetSortOrder( $new_sort_order );
292 return (0, "Couldn't move custom field");
295 return (1,"Moved custom field down");
301 Returns the current value of id.
302 (In the database, id is stored as int(11).)
310 Returns the current value of CustomField.
311 (In the database, CustomField is stored as int(11).)
315 =head2 SetCustomField VALUE
318 Set CustomField to VALUE.
319 Returns (1, 'Status message') on success and (0, 'Error Message') on failure.
320 (In the database, CustomField will be stored as a int(11).)
328 Returns the current value of ObjectId.
329 (In the database, ObjectId is stored as int(11).)
333 =head2 SetObjectId VALUE
336 Set ObjectId to VALUE.
337 Returns (1, 'Status message') on success and (0, 'Error Message') on failure.
338 (In the database, ObjectId will be stored as a int(11).)
346 Returns the current value of SortOrder.
347 (In the database, SortOrder is stored as int(11).)
351 =head2 SetSortOrder VALUE
354 Set SortOrder to VALUE.
355 Returns (1, 'Status message') on success and (0, 'Error Message') on failure.
356 (In the database, SortOrder will be stored as a int(11).)
364 Returns the current value of Creator.
365 (In the database, Creator is stored as int(11).)
373 Returns the current value of Created.
374 (In the database, Created is stored as datetime.)
382 Returns the current value of LastUpdatedBy.
383 (In the database, LastUpdatedBy is stored as int(11).)
391 Returns the current value of LastUpdated.
392 (In the database, LastUpdated is stored as datetime.)
399 sub _CoreAccessible {
403 {read => 1, sql_type => 4, length => 11, is_blob => 0, is_numeric => 1, type => 'int(11)', default => ''},
405 {read => 1, write => 1, sql_type => 4, length => 11, is_blob => 0, is_numeric => 1, type => 'int(11)', default => ''},
407 {read => 1, write => 1, sql_type => 4, length => 11, is_blob => 0, is_numeric => 1, type => 'int(11)', default => ''},
409 {read => 1, write => 1, sql_type => 4, length => 11, is_blob => 0, is_numeric => 1, type => 'int(11)', default => '0'},
411 {read => 1, auto => 1, sql_type => 4, length => 11, is_blob => 0, is_numeric => 1, type => 'int(11)', default => '0'},
413 {read => 1, auto => 1, sql_type => 11, length => 0, is_blob => 0, is_numeric => 0, type => 'datetime', default => ''},
415 {read => 1, auto => 1, sql_type => 4, length => 11, is_blob => 0, is_numeric => 1, type => 'int(11)', default => '0'},
417 {read => 1, auto => 1, sql_type => 11, length => 0, is_blob => 0, is_numeric => 0, type => 'datetime', default => ''},
422 RT::Base->_ImportOverlays();