import rt 3.2.2
[freeside.git] / rt / html / Admin / Elements / EditCustomFields
1 %# {{{ BEGIN BPS TAGGED BLOCK
2 %# 
3 %# COPYRIGHT:
4 %#  
5 %# This software is Copyright (c) 1996-2004 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., 675 Mass Ave, Cambridge, MA 02139, USA.
26 %# 
27 %# 
28 %# CONTRIBUTION SUBMISSION POLICY:
29 %# 
30 %# (The following paragraph is not intended to limit the rights granted
31 %# to you to modify and distribute this software under the terms of
32 %# the GNU General Public License and is only of importance to you if
33 %# you choose to contribute your changes and enhancements to the
34 %# community by submitting them to Best Practical Solutions, LLC.)
35 %# 
36 %# By intentionally submitting any modifications, corrections or
37 %# derivatives to this work, or any other work intended for use with
38 %# Request Tracker, to Best Practical Solutions, LLC, you confirm that
39 %# you are the copyright holder for those contributions and you grant
40 %# Best Practical Solutions,  LLC a nonexclusive, worldwide, irrevocable,
41 %# royalty-free, perpetual, license to use, copy, create derivative
42 %# works based on those contributions, and sublicense and distribute
43 %# those contributions and any derivatives thereof.
44 %# 
45 %# }}} END BPS TAGGED BLOCK
46 <& /Elements/ListActions, actions => \@actions &>
47
48 <TABLE>
49 <TR>
50 <TD VALIGN=TOP>
51 <h2><%$caption%></h2>
52 </TD></TR></TABLE>
53 % if ($CustomFields->Count == 0 ) {
54 <P><i><&|/l&>(No custom fields)</&></i></P>
55 % } else {
56 <TABLE cellspacing=0 cellpadding=2>
57 % my $count;
58 % while (my $CustomFieldObj = $CustomFields->Next) { 
59 <TR>
60   <TD valign="TOP">
61 % if ($CustomFieldObj->Name) {
62     <A HREF="CustomField.html?Queue=<%$id%>&CustomField=<%$CustomFieldObj->id()%>"><b><%$CustomFieldObj->Name%></b></a><br>
63 % } else {
64     <A HREF="CustomField.html?Queue=<%$id%>&CustomField=<%$CustomFieldObj->id()%>"><i>(<%loc("no name")%>)</i></a><br>
65 % }
66     <%$CustomFieldObj->Description%>
67   </TD>
68   <TD valign="TOP">
69     <i><% $CustomFieldObj->FriendlyType %></i>
70   </TD>
71 %  # show 'move up' unless it's the first item
72 %  if ($count++) {
73   <TD valign="TOP">
74     <a href="CustomFields.html?id=<%$id%>&CustomField=<%$CustomFieldObj->id%>&Move=-1"><&|/l&>Move up</&></a>
75 %  } else {
76   <TD valign="TOP" ALIGN=RIGHT>
77 %  }
78
79 %  # show 'move down' unless it's the last item
80 %  if (!$CustomFields->IsLast) {
81 %  $m->print(' | ') if $count > 1;
82     <a href="CustomFields.html?id=<%$id%>&CustomField=<%$CustomFieldObj->id%>&Move=1"><&|/l&>Move down</&></a>
83 %  }
84 </TD>
85 </TR>
86 % }
87
88 </TABLE>
89 % }
90 <FORM METHOD=GET ACTION="CustomFields.html">
91 % if ($id) {
92 <INPUT TYPE="Hidden" NAME="id" VALUE="<%$id%>">
93 % }
94 <input type="checkbox" name="FindDisabledCustomFields"> <&|/l&>Include disabled custom fields in listing.</&>
95 <input type=submit value="<&|/l&>Go!</&>">
96 </FORM>
97
98
99 <%INIT>
100 my $CustomFields = RT::CustomFields->new($session{'CurrentUser'});
101 my $QueueObj = RT::Queue->new($session{'CurrentUser'});
102 my $caption;
103
104 if ($id)  {
105         $QueueObj->Load($id);                        
106 }
107
108 if ($QueueObj->id) {
109         $CustomFields->LimitToQueue($id);
110 }                                            
111 else {                                       
112         $CustomFields->LimitToGlobal();
113 }                                           
114
115 if ($FindDisabledCustomFields) {
116     $caption = loc("All Custom Fields");
117     $CustomFields->{'find_disabled_rows'} = 1;
118 } else {
119     $caption = loc("Enabled Custom Fields");
120 }
121
122 # {{{ deal with moving sortorder of custom fields
123 if ($CustomField and $Move) {
124     my $SourceObj = RT::CustomField->new($session{'CurrentUser'});
125     $SourceObj->Load($CustomField) || Abort(loc('No CustomField'));
126
127     my $TargetObj;
128     my $target_order = $SourceObj->SortOrder + $Move;
129     while (my $CustomFieldObj = $CustomFields->Next) { 
130         my $this_order = $CustomFieldObj->SortOrder;
131
132         # if we have an exact match, finish the loop now
133         ($TargetObj = $CustomFieldObj, last) if $this_order == $target_order;
134
135         # otherwise, we need to apropos toward the general direction
136         # ... first, check the sign is correct
137         next unless ($this_order - $SourceObj->SortOrder) * $Move > 0;
138
139         # ... next, see if we already have a candidate
140         if ($TargetObj) {
141             # ... if yes, compare the delta and choose the smaller one
142             my $orig_delta = abs($TargetObj->SortOrder - $target_order);
143             my $this_delta = abs($this_order - $target_order);
144             next if $orig_delta < $this_delta;
145         }
146
147         $TargetObj = $CustomFieldObj;
148     }
149
150     if ($TargetObj) {
151         # swap their sort order
152         my ($s, $t) = ($SourceObj->SortOrder, $TargetObj->SortOrder);
153         $TargetObj->SetSortOrder($s);
154         $SourceObj->SetSortOrder($t);
155         # because order changed, we must redo search for subsequent uses
156         $CustomFields->RedoSearch;
157     }
158
159     $CustomFields->GotoFirstItem;
160 }
161 # }}}
162
163 # {{{ now process the 'copy queue' action
164 my @actions;
165 if ($Source and $Source ne $id) {
166     my $SourceQueue = RT::Queue->new($session{'CurrentUser'});
167     $SourceQueue->Load($Source) || Abort(loc("Couldn't load queue"));
168     my $SourceCustomFields = RT::CustomFields->new($session{'CurrentUser'});
169     $SourceCustomFields->LimitToQueue($SourceQueue->id);
170
171     # delete old fields
172     foreach my $CustomFieldObj ( @{$CustomFields->ItemsArrayRef} ) { 
173         $CustomFieldObj->Delete;
174     }
175
176     # add new fields
177     while (my $SourceCustomFieldObj = $SourceCustomFields->Next) {
178         my $CustomFieldObj = RT::CustomField->new($session{'CurrentUser'});
179         my ($val, $msg) =  $CustomFieldObj->Create(
180             id => $SourceCustomFieldObj->id,
181             Queue => $id,
182             Name => $SourceCustomFieldObj->Name,
183             Type => $SourceCustomFieldObj->Type,
184             Description => $SourceCustomFieldObj->Description,
185         );
186         Abort(loc("Could not create CustomField") . ": $msg") unless ($val);
187         push @actions, $msg;
188
189         $CustomFieldObj->SetSortOrder($SourceCustomFieldObj->SortOrder);
190
191         # add new values
192         my $values = $SourceCustomFieldObj->Values();
193         while (my $v = $values->Next) {
194             my ( $addval, $addmsg ) = $CustomFieldObj->AddValue(
195                 Name => $v->Name,
196                 Description => $v->Description,
197                 SortOrder => $v->SortOrder
198             );
199         }
200     }
201
202     # because content changed, we must redo search for subsequent uses
203     $CustomFields->RedoSearch;
204     $CustomFields->GotoFirstItem;
205 }
206 # }}}
207
208 # {{{ deal with deleting existing custom fields
209 foreach my $key (keys %ARGS) {
210   # {{{ if we're trying to delete the custom field
211   if ($key =~ /^DeleteCustomField-(\d+)/) {
212     my $id = $1;
213     my $CustomFieldObj = RT::CustomField->new($session{'CurrentUser'});
214     $CustomFieldObj->Load($id);
215     my ($retval, $msg) = $CustomFieldObj->Delete;
216     if ($retval) {
217       push @actions, loc("Custom field deleted");
218     }
219     else {
220       push @actions, $msg;
221     }
222   }
223   # }}}
224 }
225 # }}}
226
227 </%INIT>
228 <%ARGS>
229 $id => 0
230 $title => undef
231 $Move => undef
232 $Source => undef
233 $CustomField => undef
234 $FindDisabledCustomFields => undef
235 </%ARGS>