RT 3.8.17
[freeside.git] / rt / lib / RT / ObjectCustomField_Overlay.pm
1 # BEGIN BPS TAGGED BLOCK {{{
2 #
3 # COPYRIGHT:
4 #
5 # This software is Copyright (c) 1996-2013 Best Practical Solutions, LLC
6 #                                          <sales@bestpractical.com>
7 #
8 # (Except where explicitly superseded by other copyright notices)
9 #
10 #
11 # LICENSE:
12 #
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
16 # from www.gnu.org.
17 #
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.
22 #
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.
28 #
29 #
30 # CONTRIBUTION SUBMISSION POLICY:
31 #
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.)
37 #
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.
46 #
47 # END BPS TAGGED BLOCK }}}
48
49 package RT::ObjectCustomField;
50
51 use strict;
52 use warnings;
53 no warnings qw(redefine);
54
55 sub Create {
56     my $self = shift;
57     my %args = (
58         CustomField => 0,
59         ObjectId    => 0,
60         SortOrder   => undef,
61         @_
62     );
63
64     my $cf = $self->CustomFieldObj( $args{'CustomField'} );
65     unless ( $cf->id ) {
66         $RT::Logger->error("Couldn't load '$args{'CustomField'}' custom field");
67         return 0;
68     }
69
70     #XXX: Where is ACL check for 'AssignCustomFields'?
71
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 );
78         return $first->id;
79     }
80
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;
89         } else {
90             $args{'SortOrder'} = 0;
91         }
92     }
93
94     return $self->SUPER::Create(
95         CustomField => $args{'CustomField'},
96         ObjectId    => $args{'ObjectId'},
97         SortOrder   => $args{'SortOrder'},
98     );
99 }
100
101 sub Delete {
102     my $self = shift;
103
104     my $ObjectCFs = RT::ObjectCustomFields->new($self->CurrentUser);
105     $ObjectCFs->LimitToObjectId($self->ObjectId);
106     $ObjectCFs->LimitToLookupType($self->CustomFieldObj->LookupType);
107
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);
114     }
115
116     $self->SUPER::Delete;
117 }
118
119 sub CustomFieldObj {
120     my $self = shift;
121     my $id = shift || $self->CustomField;
122
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
125     # system user first.
126     my $system_CF = RT::CustomField->new( RT->SystemUser );
127     $system_CF->Load( $id );
128     my $class = $system_CF->RecordClassFromLookupType;
129
130     my $obj = $class->new( $self->CurrentUser );
131     $obj->Load( $self->ObjectId );
132
133     my $CF = RT::CustomField->new( $self->CurrentUser );
134     $CF->SetContextObject( $obj );
135     $CF->Load( $id );
136     return $CF;
137 }
138
139 =head2 Sorting custom fields applications
140
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.
151
152 =head3 MoveUp
153
154 Moves custom field up. See </Sorting custom fields applications>.
155
156 =cut
157
158 sub MoveUp {
159     my $self = shift;
160
161     my $ocfs = RT::ObjectCustomFields->new( $self->CurrentUser );
162
163     my $oid = $self->ObjectId;
164     $ocfs->LimitToObjectId( $oid );
165     if ( $oid ) {
166         $ocfs->LimitToObjectId( 0 );
167     }
168
169     my $cf = $self->CustomFieldObj;
170     $ocfs->LimitToLookupType( $cf->LookupType );
171
172     $ocfs->Limit( FIELD => 'SortOrder', OPERATOR => '<', VALUE => $self->SortOrder );
173     $ocfs->OrderByCols( { FIELD => 'SortOrder', ORDER => 'DESC' } );
174
175     my @above = ($ocfs->Next, $ocfs->Next);
176     unless ($above[0]) {
177         return (0, "Can not move up. It's already at the top");
178     }
179
180     my $new_sort_order;
181     if ( $above[0]->ObjectId == $self->ObjectId ) {
182         $new_sort_order = $above[0]->SortOrder;
183         my ($status, $msg) = $above[0]->SetSortOrder( $self->SortOrder );
184         unless ( $status ) {
185             return (0, "Couldn't move custom field");
186         }
187     }
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 );
191         $move_ocfs->Limit(
192             FIELD => 'SortOrder',
193             OPERATOR => '>=',
194             VALUE => $above[0]->SortOrder,
195         );
196         $move_ocfs->OrderByCols( { FIELD => 'SortOrder', ORDER => 'DESC' } );
197         while ( my $record = $move_ocfs->Next ) {
198             my ($status, $msg) = $record->SetSortOrder( $record->SortOrder + 1 );
199             unless ( $status ) {
200                 return (0, "Couldn't move custom field");
201             }
202         }
203         $new_sort_order = $above[0]->SortOrder;
204     } else {
205         $new_sort_order = $above[0]->SortOrder - 1;
206     }
207
208     my ($status, $msg) = $self->SetSortOrder( $new_sort_order );
209     unless ( $status ) {
210         return (0, "Couldn't move custom field");
211     }
212
213     return (1,"Moved custom field up");
214 }
215
216 =head3 MoveDown
217
218 Moves custom field down. See </Sorting custom fields applications>.
219
220 =cut
221
222 sub MoveDown {
223     my $self = shift;
224
225     my $ocfs = RT::ObjectCustomFields->new( $self->CurrentUser );
226
227     my $oid = $self->ObjectId;
228     $ocfs->LimitToObjectId( $oid );
229     if ( $oid ) {
230         $ocfs->LimitToObjectId( 0 );
231     }
232
233     my $cf = $self->CustomFieldObj;
234     $ocfs->LimitToLookupType( $cf->LookupType );
235
236     $ocfs->Limit( FIELD => 'SortOrder', OPERATOR => '>', VALUE => $self->SortOrder );
237     $ocfs->OrderByCols( { FIELD => 'SortOrder', ORDER => 'ASC' } );
238
239     my @below = ($ocfs->Next, $ocfs->Next);
240     unless ($below[0]) {
241         return (0, "Can not move down. It's already at the bottom");
242     }
243
244     my $new_sort_order;
245     if ( $below[0]->ObjectId == $self->ObjectId ) {
246         $new_sort_order = $below[0]->SortOrder;
247         my ($status, $msg) = $below[0]->SetSortOrder( $self->SortOrder );
248         unless ( $status ) {
249             return (0, "Couldn't move custom field");
250         }
251     }
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 );
255         $move_ocfs->Limit(
256             FIELD => 'SortOrder',
257             OPERATOR => '<=',
258             VALUE => $below[0]->SortOrder,
259         );
260         $move_ocfs->OrderByCols( { FIELD => 'SortOrder', ORDER => 'ASC' } );
261         while ( my $record = $move_ocfs->Next ) {
262             my ($status, $msg) = $record->SetSortOrder( $record->SortOrder - 1 );
263             unless ( $status ) {
264                 return (0, "Couldn't move custom field");
265             }
266         }
267         $new_sort_order = $below[0]->SortOrder;
268     } else {
269         $new_sort_order = $below[0]->SortOrder + 1;
270     }
271
272     my ($status, $msg) = $self->SetSortOrder( $new_sort_order );
273     unless ( $status ) {
274         return (0, "Couldn't move custom field");
275     }
276
277     return (1,"Moved custom field down");
278 }
279
280 1;