import torrus 1.0.9
[freeside.git] / rt / lib / RT / ObjectCustomField_Overlay.pm
1 # BEGIN BPS TAGGED BLOCK {{{
2
3 # COPYRIGHT:
4
5 # This software is Copyright (c) 1996-2009 Best Practical Solutions, LLC
6 #                                          <jesse@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     my $CF = RT::CustomField->new( $self->CurrentUser );
123     $CF->Load( $id );
124     return $CF;
125 }
126
127 =head2 Sorting custom fields applications
128
129 Custom fields sorted on multiple layers. First of all custom
130 fields with different lookup type are sorted indepedantly. 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.
139
140 =head3 MoveDown
141
142 Moves custom field up. See </Sorting custom fields applications>.
143
144 =cut
145
146 sub MoveUp {
147     my $self = shift;
148
149     my $ocfs = RT::ObjectCustomFields->new( $self->CurrentUser );
150
151     my $oid = $self->ObjectId;
152     $ocfs->LimitToObjectId( $oid );
153     if ( $oid ) {
154         $ocfs->LimitToObjectId( 0 );
155     }
156
157     my $cf = $self->CustomFieldObj;
158     $ocfs->LimitToLookupType( $cf->LookupType );
159
160     $ocfs->Limit( FIELD => 'SortOrder', OPERATOR => '<', VALUE => $self->SortOrder );
161     $ocfs->OrderByCols( { FIELD => 'SortOrder', ORDER => 'DESC' } );
162
163     my @above = ($ocfs->Next, $ocfs->Next);
164     unless ($above[0]) {
165         return (0, "Can not move up. It's already at the top");
166     }
167
168     my $new_sort_order;
169     if ( $above[0]->ObjectId == $self->ObjectId ) {
170         $new_sort_order = $above[0]->SortOrder;
171         my ($status, $msg) = $above[0]->SetSortOrder( $self->SortOrder );
172         unless ( $status ) {
173             return (0, "Couldn't move custom field");
174         }
175     }
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 );
179         $move_ocfs->Limit(
180             FIELD => 'SortOrder',
181             OPERATOR => '>=',
182             VALUE => $above[0]->SortOrder,
183         );
184         $move_ocfs->OrderByCols( { FIELD => 'SortOrder', ORDER => 'DESC' } );
185         while ( my $record = $move_ocfs->Next ) {
186             my ($status, $msg) = $record->SetSortOrder( $record->SortOrder + 1 );
187             unless ( $status ) {
188                 return (0, "Couldn't move custom field");
189             }
190         }
191         $new_sort_order = $above[0]->SortOrder;
192     } else {
193         $new_sort_order = $above[0]->SortOrder - 1;
194     }
195
196     my ($status, $msg) = $self->SetSortOrder( $new_sort_order );
197     unless ( $status ) {
198         return (0, "Couldn't move custom field");
199     }
200
201     return (1,"Moved custom field up");
202 }
203
204 =head3 MoveDown
205
206 Moves custom field down. See </Sorting custom fields applications>.
207
208 =cut
209
210 sub MoveDown {
211     my $self = shift;
212
213     my $ocfs = RT::ObjectCustomFields->new( $self->CurrentUser );
214
215     my $oid = $self->ObjectId;
216     $ocfs->LimitToObjectId( $oid );
217     if ( $oid ) {
218         $ocfs->LimitToObjectId( 0 );
219     }
220
221     my $cf = $self->CustomFieldObj;
222     $ocfs->LimitToLookupType( $cf->LookupType );
223
224     $ocfs->Limit( FIELD => 'SortOrder', OPERATOR => '>', VALUE => $self->SortOrder );
225     $ocfs->OrderByCols( { FIELD => 'SortOrder', ORDER => 'ASC' } );
226
227     my @below = ($ocfs->Next, $ocfs->Next);
228     unless ($below[0]) {
229         return (0, "Can not move down. It's already at the bottom");
230     }
231
232     my $new_sort_order;
233     if ( $below[0]->ObjectId == $self->ObjectId ) {
234         $new_sort_order = $below[0]->SortOrder;
235         my ($status, $msg) = $below[0]->SetSortOrder( $self->SortOrder );
236         unless ( $status ) {
237             return (0, "Couldn't move custom field");
238         }
239     }
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 );
243         $move_ocfs->Limit(
244             FIELD => 'SortOrder',
245             OPERATOR => '<=',
246             VALUE => $below[0]->SortOrder,
247         );
248         $move_ocfs->OrderByCols( { FIELD => 'SortOrder', ORDER => 'ASC' } );
249         while ( my $record = $move_ocfs->Next ) {
250             my ($status, $msg) = $record->SetSortOrder( $record->SortOrder - 1 );
251             unless ( $status ) {
252                 return (0, "Couldn't move custom field");
253             }
254         }
255         $new_sort_order = $below[0]->SortOrder;
256     } else {
257         $new_sort_order = $below[0]->SortOrder + 1;
258     }
259
260     my ($status, $msg) = $self->SetSortOrder( $new_sort_order );
261     unless ( $status ) {
262         return (0, "Couldn't move custom field");
263     }
264
265     return (1,"Moved custom field down");
266 }
267
268 1;