1 # BEGIN BPS TAGGED BLOCK {{{
5 # This software is Copyright (c) 1996-2011 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;
122 my $CF = RT::CustomField->new( $self->CurrentUser );
127 =head2 Sorting custom fields applications
129 Custom fields sorted on multiple layers. First of all custom
130 fields with different lookup type are sorted independently. All
131 global custom fields have fixed order for all objects, but you
132 can insert object specific custom fields between them. Object
133 specific custom fields can be applied to several objects and
134 be on different place. For example you have GCF1, GCF2, LCF1,
135 LCF2 and LCF3 that applies to tickets. You can place GCF2
136 above GCF1, but they will be in the same order in all queues.
137 However, LCF1 and other local can be placed at any place
138 for particular queue: above global, between them or below.
142 Moves custom field up. See </Sorting custom fields applications>.
149 my $ocfs = RT::ObjectCustomFields->new( $self->CurrentUser );
151 my $oid = $self->ObjectId;
152 $ocfs->LimitToObjectId( $oid );
154 $ocfs->LimitToObjectId( 0 );
157 my $cf = $self->CustomFieldObj;
158 $ocfs->LimitToLookupType( $cf->LookupType );
160 $ocfs->Limit( FIELD => 'SortOrder', OPERATOR => '<', VALUE => $self->SortOrder );
161 $ocfs->OrderByCols( { FIELD => 'SortOrder', ORDER => 'DESC' } );
163 my @above = ($ocfs->Next, $ocfs->Next);
165 return (0, "Can not move up. It's already at the top");
169 if ( $above[0]->ObjectId == $self->ObjectId ) {
170 $new_sort_order = $above[0]->SortOrder;
171 my ($status, $msg) = $above[0]->SetSortOrder( $self->SortOrder );
173 return (0, "Couldn't move custom field");
176 elsif ( $above[1] && $above[0]->SortOrder == $above[1]->SortOrder + 1 ) {
177 my $move_ocfs = RT::ObjectCustomFields->new( $RT::SystemUser );
178 $move_ocfs->LimitToLookupType( $cf->LookupType );
180 FIELD => 'SortOrder',
182 VALUE => $above[0]->SortOrder,
184 $move_ocfs->OrderByCols( { FIELD => 'SortOrder', ORDER => 'DESC' } );
185 while ( my $record = $move_ocfs->Next ) {
186 my ($status, $msg) = $record->SetSortOrder( $record->SortOrder + 1 );
188 return (0, "Couldn't move custom field");
191 $new_sort_order = $above[0]->SortOrder;
193 $new_sort_order = $above[0]->SortOrder - 1;
196 my ($status, $msg) = $self->SetSortOrder( $new_sort_order );
198 return (0, "Couldn't move custom field");
201 return (1,"Moved custom field up");
206 Moves custom field down. See </Sorting custom fields applications>.
213 my $ocfs = RT::ObjectCustomFields->new( $self->CurrentUser );
215 my $oid = $self->ObjectId;
216 $ocfs->LimitToObjectId( $oid );
218 $ocfs->LimitToObjectId( 0 );
221 my $cf = $self->CustomFieldObj;
222 $ocfs->LimitToLookupType( $cf->LookupType );
224 $ocfs->Limit( FIELD => 'SortOrder', OPERATOR => '>', VALUE => $self->SortOrder );
225 $ocfs->OrderByCols( { FIELD => 'SortOrder', ORDER => 'ASC' } );
227 my @below = ($ocfs->Next, $ocfs->Next);
229 return (0, "Can not move down. It's already at the bottom");
233 if ( $below[0]->ObjectId == $self->ObjectId ) {
234 $new_sort_order = $below[0]->SortOrder;
235 my ($status, $msg) = $below[0]->SetSortOrder( $self->SortOrder );
237 return (0, "Couldn't move custom field");
240 elsif ( $below[1] && $below[0]->SortOrder + 1 == $below[1]->SortOrder ) {
241 my $move_ocfs = RT::ObjectCustomFields->new( $RT::SystemUser );
242 $move_ocfs->LimitToLookupType( $cf->LookupType );
244 FIELD => 'SortOrder',
246 VALUE => $below[0]->SortOrder,
248 $move_ocfs->OrderByCols( { FIELD => 'SortOrder', ORDER => 'ASC' } );
249 while ( my $record = $move_ocfs->Next ) {
250 my ($status, $msg) = $record->SetSortOrder( $record->SortOrder - 1 );
252 return (0, "Couldn't move custom field");
255 $new_sort_order = $below[0]->SortOrder;
257 $new_sort_order = $below[0]->SortOrder + 1;
260 my ($status, $msg) = $self->SetSortOrder( $new_sort_order );
262 return (0, "Couldn't move custom field");
265 return (1,"Moved custom field down");