1 # BEGIN BPS TAGGED BLOCK {{{
5 # This software is Copyright (c) 1996-2012 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;
140 my $CF = RT::CustomField->new( $self->CurrentUser );
145 =head2 Sorting custom fields applications
147 Custom fields sorted on multiple layers. First of all custom
148 fields with different lookup type are sorted independently. All
149 global custom fields have fixed order for all objects, but you
150 can insert object specific custom fields between them. Object
151 specific custom fields can be applied to several objects and
152 be on different place. For example you have GCF1, GCF2, LCF1,
153 LCF2 and LCF3 that applies to tickets. You can place GCF2
154 above GCF1, but they will be in the same order in all queues.
155 However, LCF1 and other local can be placed at any place
156 for particular queue: above global, between them or below.
160 Moves custom field up. See </Sorting custom fields applications>.
167 my $ocfs = RT::ObjectCustomFields->new( $self->CurrentUser );
169 my $oid = $self->ObjectId;
170 $ocfs->LimitToObjectId( $oid );
172 $ocfs->LimitToObjectId( 0 );
175 my $cf = $self->CustomFieldObj;
176 $ocfs->LimitToLookupType( $cf->LookupType );
178 $ocfs->Limit( FIELD => 'SortOrder', OPERATOR => '<', VALUE => $self->SortOrder );
179 $ocfs->OrderByCols( { FIELD => 'SortOrder', ORDER => 'DESC' } );
181 my @above = ($ocfs->Next, $ocfs->Next);
183 return (0, "Can not move up. It's already at the top");
187 if ( $above[0]->ObjectId == $self->ObjectId ) {
188 $new_sort_order = $above[0]->SortOrder;
189 my ($status, $msg) = $above[0]->SetSortOrder( $self->SortOrder );
191 return (0, "Couldn't move custom field");
194 elsif ( $above[1] && $above[0]->SortOrder == $above[1]->SortOrder + 1 ) {
195 my $move_ocfs = RT::ObjectCustomFields->new( RT->SystemUser );
196 $move_ocfs->LimitToLookupType( $cf->LookupType );
198 FIELD => 'SortOrder',
200 VALUE => $above[0]->SortOrder,
202 $move_ocfs->OrderByCols( { FIELD => 'SortOrder', ORDER => 'DESC' } );
203 while ( my $record = $move_ocfs->Next ) {
204 my ($status, $msg) = $record->SetSortOrder( $record->SortOrder + 1 );
206 return (0, "Couldn't move custom field");
209 $new_sort_order = $above[0]->SortOrder;
211 $new_sort_order = $above[0]->SortOrder - 1;
214 my ($status, $msg) = $self->SetSortOrder( $new_sort_order );
216 return (0, "Couldn't move custom field");
219 return (1,"Moved custom field up");
224 Moves custom field down. See </Sorting custom fields applications>.
231 my $ocfs = RT::ObjectCustomFields->new( $self->CurrentUser );
233 my $oid = $self->ObjectId;
234 $ocfs->LimitToObjectId( $oid );
236 $ocfs->LimitToObjectId( 0 );
239 my $cf = $self->CustomFieldObj;
240 $ocfs->LimitToLookupType( $cf->LookupType );
242 $ocfs->Limit( FIELD => 'SortOrder', OPERATOR => '>', VALUE => $self->SortOrder );
243 $ocfs->OrderByCols( { FIELD => 'SortOrder', ORDER => 'ASC' } );
245 my @below = ($ocfs->Next, $ocfs->Next);
247 return (0, "Can not move down. It's already at the bottom");
251 if ( $below[0]->ObjectId == $self->ObjectId ) {
252 $new_sort_order = $below[0]->SortOrder;
253 my ($status, $msg) = $below[0]->SetSortOrder( $self->SortOrder );
255 return (0, "Couldn't move custom field");
258 elsif ( $below[1] && $below[0]->SortOrder + 1 == $below[1]->SortOrder ) {
259 my $move_ocfs = RT::ObjectCustomFields->new( RT->SystemUser );
260 $move_ocfs->LimitToLookupType( $cf->LookupType );
262 FIELD => 'SortOrder',
264 VALUE => $below[0]->SortOrder,
266 $move_ocfs->OrderByCols( { FIELD => 'SortOrder', ORDER => 'ASC' } );
267 while ( my $record = $move_ocfs->Next ) {
268 my ($status, $msg) = $record->SetSortOrder( $record->SortOrder - 1 );
270 return (0, "Couldn't move custom field");
273 $new_sort_order = $below[0]->SortOrder;
275 $new_sort_order = $below[0]->SortOrder + 1;
278 my ($status, $msg) = $self->SetSortOrder( $new_sort_order );
280 return (0, "Couldn't move custom field");
283 return (1,"Moved custom field down");
289 Returns the current value of id.
290 (In the database, id is stored as int(11).)
298 Returns the current value of CustomField.
299 (In the database, CustomField is stored as int(11).)
303 =head2 SetCustomField VALUE
306 Set CustomField to VALUE.
307 Returns (1, 'Status message') on success and (0, 'Error Message') on failure.
308 (In the database, CustomField will be stored as a int(11).)
316 Returns the current value of ObjectId.
317 (In the database, ObjectId is stored as int(11).)
321 =head2 SetObjectId VALUE
324 Set ObjectId to VALUE.
325 Returns (1, 'Status message') on success and (0, 'Error Message') on failure.
326 (In the database, ObjectId will be stored as a int(11).)
334 Returns the current value of SortOrder.
335 (In the database, SortOrder is stored as int(11).)
339 =head2 SetSortOrder VALUE
342 Set SortOrder to VALUE.
343 Returns (1, 'Status message') on success and (0, 'Error Message') on failure.
344 (In the database, SortOrder will be stored as a int(11).)
352 Returns the current value of Creator.
353 (In the database, Creator is stored as int(11).)
361 Returns the current value of Created.
362 (In the database, Created is stored as datetime.)
370 Returns the current value of LastUpdatedBy.
371 (In the database, LastUpdatedBy is stored as int(11).)
379 Returns the current value of LastUpdated.
380 (In the database, LastUpdated is stored as datetime.)
387 sub _CoreAccessible {
391 {read => 1, sql_type => 4, length => 11, is_blob => 0, is_numeric => 1, type => 'int(11)', default => ''},
393 {read => 1, write => 1, sql_type => 4, length => 11, is_blob => 0, is_numeric => 1, type => 'int(11)', default => ''},
395 {read => 1, write => 1, sql_type => 4, length => 11, is_blob => 0, is_numeric => 1, type => 'int(11)', default => ''},
397 {read => 1, write => 1, sql_type => 4, length => 11, is_blob => 0, is_numeric => 1, type => 'int(11)', default => '0'},
399 {read => 1, auto => 1, sql_type => 4, length => 11, is_blob => 0, is_numeric => 1, type => 'int(11)', default => '0'},
401 {read => 1, auto => 1, sql_type => 11, length => 0, is_blob => 0, is_numeric => 0, type => 'datetime', default => ''},
403 {read => 1, auto => 1, sql_type => 4, length => 11, is_blob => 0, is_numeric => 1, type => 'int(11)', default => '0'},
405 {read => 1, auto => 1, sql_type => 11, length => 0, is_blob => 0, is_numeric => 0, type => 'datetime', default => ''},
410 RT::Base->_ImportOverlays();