import of rt 3.0.4
[freeside.git] / rt / html / Admin / Elements / EditCustomFields
1 %# BEGIN LICENSE BLOCK
2 %# 
3 %# Copyright (c) 1996-2003 Jesse Vincent <jesse@bestpractical.com>
4 %# 
5 %# (Except where explictly superceded by other copyright notices)
6 %# 
7 %# This work is made available to you under the terms of Version 2 of
8 %# the GNU General Public License. A copy of that license should have
9 %# been provided with this software, but in any event can be snarfed
10 %# from www.gnu.org.
11 %# 
12 %# This work is distributed in the hope that it will be useful, but
13 %# WITHOUT ANY WARRANTY; without even the implied warranty of
14 %# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 %# General Public License for more details.
16 %# 
17 %# Unless otherwise specified, all modifications, corrections or
18 %# extensions to this work which alter its source code become the
19 %# property of Best Practical Solutions, LLC when submitted for
20 %# inclusion in the work.
21 %# 
22 %# 
23 %# END LICENSE BLOCK
24 <& /Elements/ListActions, actions => \@actions &>
25
26 <TABLE>
27 <TR>
28 <TD VALIGN=TOP>
29 <%$caption%>:<BR>
30 </TD></TR></TABLE>
31 % if ($CustomFields->Count == 0 ) {
32 <P><i><&|/l&>(No custom fields)</&></i></P>
33 % } else {
34 <TABLE>
35
36 <TR>
37 <TD ROWSPAN="<% $CustomFields->Count %>">
38 <UL>
39 % while (my $CustomFieldObj = $CustomFields->Next) { 
40 <LI><A HREF="CustomField.html?Queue=<%$id%>&CustomField=<%$CustomFieldObj->id()%>"><b><%$CustomFieldObj->Name%></b></a> (<% $CustomFieldObj->FriendlyType %>)<br>
41 <%$CustomFieldObj->Description%>
42 </LI>
43 % }
44 </UL>
45 </TD>
46
47 % my $count;
48 % while (my $CustomFieldObj = $CustomFields->Next) { 
49 %  # show 'move up' unless it's the first item
50 %  if ($count++) {
51 <TR><TD>
52 <a href="CustomFields.html?id=<%$id%>&CustomField=<%$CustomFieldObj->id%>&Move=-1"><&|/l&>Move up</&></a>
53 %  } else {
54 <TD ALIGN=RIGHT>
55 %  }
56
57 %  # show 'move down' unless it's the last item
58 %  if (!$CustomFields->IsLast) {
59 %  $m->print(' | ') if $count > 1;
60 <a href="CustomFields.html?id=<%$id%>&CustomField=<%$CustomFieldObj->id%>&Move=1"><&|/l&>Move down</&></a>
61 %  }
62 </TD></TR>
63 % }
64
65 </TD>
66 </TR>
67 </TABLE>
68 % }
69 <FORM METHOD=GET ACTION="CustomFields.html">
70 % if ($id) {
71 <INPUT TYPE="Hidden" NAME="id" VALUE="<%$id%>">
72 % }
73 <input type="checkbox" name="FindDisabledCustomFields"> <&|/l&>Include disabled custom fields in listing.</&>
74 <input type=submit value="<&|/l&>Go!</&>">
75 </FORM>
76
77
78 <%INIT>
79 my $CustomFields = RT::CustomFields->new($session{'CurrentUser'});
80 my $QueueObj = RT::Queue->new($session{'CurrentUser'});
81 my $caption;
82
83 if ($id)  {
84         $QueueObj->Load($id);                        
85 }
86
87 if ($QueueObj->id) {
88         $CustomFields->LimitToQueue($id);
89 }                                            
90 else {                                       
91         $CustomFields->LimitToGlobal();
92 }                                           
93
94 if ($FindDisabledCustomFields) {
95     $caption = loc("All Custom Fields");
96     $CustomFields->{'find_disabled_rows'} = 1;
97 } else {
98     $caption = loc("Enabled Custom Fields");
99 }
100
101 # {{{ deal with moving sortorder of custom fields
102 if ($CustomField and $Move) {
103     my $SourceObj = RT::CustomField->new($session{'CurrentUser'});
104     $SourceObj->Load($CustomField) || Abort(loc('No CustomField'));
105
106     my $TargetObj;
107     my $target_order = $SourceObj->SortOrder + $Move;
108     while (my $CustomFieldObj = $CustomFields->Next) { 
109         my $this_order = $CustomFieldObj->SortOrder;
110
111         # if we have an exact match, finish the loop now
112         ($TargetObj = $CustomFieldObj, last) if $this_order == $target_order;
113
114         # otherwise, we need to apropos toward the general direction
115         # ... first, check the sign is correct
116         next unless ($this_order - $SourceObj->SortOrder) * $Move > 0;
117
118         # ... next, see if we already have a candidate
119         if ($TargetObj) {
120             # ... if yes, compare the delta and choose the smaller one
121             my $orig_delta = abs($TargetObj->SortOrder - $target_order);
122             my $this_delta = abs($this_order - $target_order);
123             next if $orig_delta < $this_delta;
124         }
125
126         $TargetObj = $CustomFieldObj;
127     }
128
129     if ($TargetObj) {
130         # swap their sort order
131         my ($s, $t) = ($SourceObj->SortOrder, $TargetObj->SortOrder);
132         $TargetObj->SetSortOrder($s);
133         $SourceObj->SetSortOrder($t);
134         # because order changed, we must redo search for subsequent uses
135         $CustomFields->RedoSearch;
136     }
137
138     $CustomFields->GotoFirstItem;
139 }
140 # }}}
141
142 # {{{ now process the 'copy queue' action
143 my @actions;
144 if ($Source and $Source ne $id) {
145     my $SourceQueue = RT::Queue->new($session{'CurrentUser'});
146     $SourceQueue->Load($Source) || Abort(loc("Couldn't load queue"));
147     my $SourceCustomFields = RT::CustomFields->new($session{'CurrentUser'});
148     $SourceCustomFields->LimitToQueue($SourceQueue->id);
149
150     # delete old fields
151     foreach my $CustomFieldObj ( @{$CustomFields->ItemsArrayRef} ) { 
152         $CustomFieldObj->Delete;
153     }
154
155     # add new fields
156     while (my $SourceCustomFieldObj = $SourceCustomFields->Next) {
157         my $CustomFieldObj = RT::CustomField->new($session{'CurrentUser'});
158         my ($val, $msg) =  $CustomFieldObj->Create(
159             id => $SourceCustomFieldObj->id,
160             Queue => $id,
161             Name => $SourceCustomFieldObj->Name,
162             Type => $SourceCustomFieldObj->Type,
163             Description => $SourceCustomFieldObj->Description,
164         );
165         Abort(loc("Could not create CustomField") . ": $msg") unless ($val);
166         push @actions, $msg;
167
168         $CustomFieldObj->SetSortOrder($SourceCustomFieldObj->SortOrder);
169
170         # add new values
171         my $values = $SourceCustomFieldObj->Values();
172         while (my $v = $values->Next) {
173             my ( $addval, $addmsg ) = $CustomFieldObj->AddValue(
174                 Name => $v->Name,
175                 Description => $v->Description,
176                 SortOrder => $v->SortOrder
177             );
178         }
179     }
180
181     # because content changed, we must redo search for subsequent uses
182     $CustomFields->RedoSearch;
183     $CustomFields->GotoFirstItem;
184 }
185 # }}}
186
187 # {{{ deal with deleting existing custom fields
188 foreach my $key (keys %ARGS) {
189   # {{{ if we're trying to delete the custom field
190   if ($key =~ /^DeleteCustomField-(\d+)/) {
191     my $id = $1;
192     my $CustomFieldObj = RT::CustomField->new($session{'CurrentUser'});
193     $CustomFieldObj->Load($id);
194     my ($retval, $msg) = $CustomFieldObj->Delete;
195     if ($retval) {
196       push @actions, loc("Custom field deleted");
197     }
198     else {
199       push @actions, $msg;
200     }
201   }
202   # }}}
203 }
204 # }}}
205
206 </%INIT>
207 <%ARGS>
208 $id => 0
209 $title => undef
210 $Move => undef
211 $Source => undef
212 $CustomField => undef
213 $FindDisabledCustomFields => undef
214 </%ARGS>