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 }}}
49 package RT::ObjectCustomField;
53 no warnings qw(redefine);
64 my $cf = $self->CustomFieldObj( $args{'CustomField'} );
66 $RT::Logger->error("Couldn't load '$args{'CustomField'}' custom field");
70 #XXX: Where is ACL check for 'AssignCustomFields'?
72 my $ObjectCFs = RT::ObjectCustomFields->new($self->CurrentUser);
73 $ObjectCFs->LimitToObjectId( $args{'ObjectId'} );
74 $ObjectCFs->LimitToCustomField( $cf->id );
75 $ObjectCFs->LimitToLookupType( $cf->LookupType );
76 if ( my $first = $ObjectCFs->First ) {
77 $self->Load( $first->id );
81 unless ( defined $args{'SortOrder'} ) {
82 my $ObjectCFs = RT::ObjectCustomFields->new( $RT::SystemUser );
83 $ObjectCFs->LimitToObjectId( $args{'ObjectId'} );
84 $ObjectCFs->LimitToObjectId( 0 ) if $args{'ObjectId'};
85 $ObjectCFs->LimitToLookupType( $cf->LookupType );
86 $ObjectCFs->OrderBy( FIELD => 'SortOrder', ORDER => 'DESC' );
87 if ( my $first = $ObjectCFs->First ) {
88 $args{'SortOrder'} = $first->SortOrder + 1;
90 $args{'SortOrder'} = 0;
94 return $self->SUPER::Create(
95 CustomField => $args{'CustomField'},
96 ObjectId => $args{'ObjectId'},
97 SortOrder => $args{'SortOrder'},
104 my $ObjectCFs = RT::ObjectCustomFields->new($self->CurrentUser);
105 $ObjectCFs->LimitToObjectId($self->ObjectId);
106 $ObjectCFs->LimitToLookupType($self->CustomFieldObj->LookupType);
108 # Move everything below us up
109 my $sort_order = $self->SortOrder;
110 while (my $OCF = $ObjectCFs->Next) {
111 my $this_order = $OCF->SortOrder;
112 next if $this_order <= $sort_order;
113 $OCF->SetSortOrder($this_order - 1);
116 $self->SUPER::Delete;
121 my $id = shift || $self->CustomField;
123 # To find out the proper context object to load the CF with, we need
124 # data from the CF -- namely, the record class. Go find that as the
126 my $system_CF = RT::CustomField->new( RT->SystemUser );
127 $system_CF->Load( $id );
128 my $class = $system_CF->RecordClassFromLookupType;
130 my $obj = $class->new( $self->CurrentUser );
131 $obj->Load( $self->ObjectId );
133 my $CF = RT::CustomField->new( $self->CurrentUser );
134 $CF->SetContextObject( $obj );
139 =head2 Sorting custom fields applications
141 Custom fields sorted on multiple layers. First of all custom
142 fields with different lookup type are sorted independently. All
143 global custom fields have fixed order for all objects, but you
144 can insert object specific custom fields between them. Object
145 specific custom fields can be applied to several objects and
146 be on different place. For example you have GCF1, GCF2, LCF1,
147 LCF2 and LCF3 that applies to tickets. You can place GCF2
148 above GCF1, but they will be in the same order in all queues.
149 However, LCF1 and other local can be placed at any place
150 for particular queue: above global, between them or below.
154 Moves custom field up. See </Sorting custom fields applications>.
161 my $ocfs = RT::ObjectCustomFields->new( $self->CurrentUser );
163 my $oid = $self->ObjectId;
164 $ocfs->LimitToObjectId( $oid );
166 $ocfs->LimitToObjectId( 0 );
169 my $cf = $self->CustomFieldObj;
170 $ocfs->LimitToLookupType( $cf->LookupType );
172 $ocfs->Limit( FIELD => 'SortOrder', OPERATOR => '<', VALUE => $self->SortOrder );
173 $ocfs->OrderByCols( { FIELD => 'SortOrder', ORDER => 'DESC' } );
175 my @above = ($ocfs->Next, $ocfs->Next);
177 return (0, "Can not move up. It's already at the top");
181 if ( $above[0]->ObjectId == $self->ObjectId ) {
182 $new_sort_order = $above[0]->SortOrder;
183 my ($status, $msg) = $above[0]->SetSortOrder( $self->SortOrder );
185 return (0, "Couldn't move custom field");
188 elsif ( $above[1] && $above[0]->SortOrder == $above[1]->SortOrder + 1 ) {
189 my $move_ocfs = RT::ObjectCustomFields->new( $RT::SystemUser );
190 $move_ocfs->LimitToLookupType( $cf->LookupType );
192 FIELD => 'SortOrder',
194 VALUE => $above[0]->SortOrder,
196 $move_ocfs->OrderByCols( { FIELD => 'SortOrder', ORDER => 'DESC' } );
197 while ( my $record = $move_ocfs->Next ) {
198 my ($status, $msg) = $record->SetSortOrder( $record->SortOrder + 1 );
200 return (0, "Couldn't move custom field");
203 $new_sort_order = $above[0]->SortOrder;
205 $new_sort_order = $above[0]->SortOrder - 1;
208 my ($status, $msg) = $self->SetSortOrder( $new_sort_order );
210 return (0, "Couldn't move custom field");
213 return (1,"Moved custom field up");
218 Moves custom field down. See </Sorting custom fields applications>.
225 my $ocfs = RT::ObjectCustomFields->new( $self->CurrentUser );
227 my $oid = $self->ObjectId;
228 $ocfs->LimitToObjectId( $oid );
230 $ocfs->LimitToObjectId( 0 );
233 my $cf = $self->CustomFieldObj;
234 $ocfs->LimitToLookupType( $cf->LookupType );
236 $ocfs->Limit( FIELD => 'SortOrder', OPERATOR => '>', VALUE => $self->SortOrder );
237 $ocfs->OrderByCols( { FIELD => 'SortOrder', ORDER => 'ASC' } );
239 my @below = ($ocfs->Next, $ocfs->Next);
241 return (0, "Can not move down. It's already at the bottom");
245 if ( $below[0]->ObjectId == $self->ObjectId ) {
246 $new_sort_order = $below[0]->SortOrder;
247 my ($status, $msg) = $below[0]->SetSortOrder( $self->SortOrder );
249 return (0, "Couldn't move custom field");
252 elsif ( $below[1] && $below[0]->SortOrder + 1 == $below[1]->SortOrder ) {
253 my $move_ocfs = RT::ObjectCustomFields->new( $RT::SystemUser );
254 $move_ocfs->LimitToLookupType( $cf->LookupType );
256 FIELD => 'SortOrder',
258 VALUE => $below[0]->SortOrder,
260 $move_ocfs->OrderByCols( { FIELD => 'SortOrder', ORDER => 'ASC' } );
261 while ( my $record = $move_ocfs->Next ) {
262 my ($status, $msg) = $record->SetSortOrder( $record->SortOrder - 1 );
264 return (0, "Couldn't move custom field");
267 $new_sort_order = $below[0]->SortOrder;
269 $new_sort_order = $below[0]->SortOrder + 1;
272 my ($status, $msg) = $self->SetSortOrder( $new_sort_order );
274 return (0, "Couldn't move custom field");
277 return (1,"Moved custom field down");