import of rt 3.0.9
[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 <h2><%$caption%></h2>
30 </TD></TR></TABLE>
31 % if ($CustomFields->Count == 0 ) {
32 <P><i><&|/l&>(No custom fields)</&></i></P>
33 % } else {
34 <TABLE cellspacing=0 cellpadding=2>
35 % my $count;
36 % while (my $CustomFieldObj = $CustomFields->Next) { 
37 <TR>
38   <TD valign="TOP">
39 % if ($CustomFieldObj->Name) {
40     <A HREF="CustomField.html?Queue=<%$id%>&CustomField=<%$CustomFieldObj->id()%>"><b><%$CustomFieldObj->Name%></b></a><br>
41 % } else {
42     <A HREF="CustomField.html?Queue=<%$id%>&CustomField=<%$CustomFieldObj->id()%>"><i>(<%loc("no name")%>)</i></a><br>
43 % }
44     <%$CustomFieldObj->Description%>
45   </TD>
46   <TD valign="TOP">
47     <i><% $CustomFieldObj->FriendlyType %></i>
48   </TD>
49 %  # show 'move up' unless it's the first item
50 %  if ($count++) {
51   <TD valign="TOP">
52     <a href="CustomFields.html?id=<%$id%>&CustomField=<%$CustomFieldObj->id%>&Move=-1"><&|/l&>Move up</&></a>
53 %  } else {
54   <TD valign="TOP" 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>
63 </TR>
64 % }
65
66 </TABLE>
67 % }
68 <FORM METHOD=GET ACTION="CustomFields.html">
69 % if ($id) {
70 <INPUT TYPE="Hidden" NAME="id" VALUE="<%$id%>">
71 % }
72 <input type="checkbox" name="FindDisabledCustomFields"> <&|/l&>Include disabled custom fields in listing.</&>
73 <input type=submit value="<&|/l&>Go!</&>">
74 </FORM>
75
76
77 <%INIT>
78 my $CustomFields = RT::CustomFields->new($session{'CurrentUser'});
79 my $QueueObj = RT::Queue->new($session{'CurrentUser'});
80 my $caption;
81
82 if ($id)  {
83         $QueueObj->Load($id);                        
84 }
85
86 if ($QueueObj->id) {
87         $CustomFields->LimitToQueue($id);
88 }                                            
89 else {                                       
90         $CustomFields->LimitToGlobal();
91 }                                           
92
93 if ($FindDisabledCustomFields) {
94     $caption = loc("All Custom Fields");
95     $CustomFields->{'find_disabled_rows'} = 1;
96 } else {
97     $caption = loc("Enabled Custom Fields");
98 }
99
100 # {{{ deal with moving sortorder of custom fields
101 if ($CustomField and $Move) {
102     my $SourceObj = RT::CustomField->new($session{'CurrentUser'});
103     $SourceObj->Load($CustomField) || Abort(loc('No CustomField'));
104
105     my $TargetObj;
106     my $target_order = $SourceObj->SortOrder + $Move;
107     while (my $CustomFieldObj = $CustomFields->Next) { 
108         my $this_order = $CustomFieldObj->SortOrder;
109
110         # if we have an exact match, finish the loop now
111         ($TargetObj = $CustomFieldObj, last) if $this_order == $target_order;
112
113         # otherwise, we need to apropos toward the general direction
114         # ... first, check the sign is correct
115         next unless ($this_order - $SourceObj->SortOrder) * $Move > 0;
116
117         # ... next, see if we already have a candidate
118         if ($TargetObj) {
119             # ... if yes, compare the delta and choose the smaller one
120             my $orig_delta = abs($TargetObj->SortOrder - $target_order);
121             my $this_delta = abs($this_order - $target_order);
122             next if $orig_delta < $this_delta;
123         }
124
125         $TargetObj = $CustomFieldObj;
126     }
127
128     if ($TargetObj) {
129         # swap their sort order
130         my ($s, $t) = ($SourceObj->SortOrder, $TargetObj->SortOrder);
131         $TargetObj->SetSortOrder($s);
132         $SourceObj->SetSortOrder($t);
133         # because order changed, we must redo search for subsequent uses
134         $CustomFields->RedoSearch;
135     }
136
137     $CustomFields->GotoFirstItem;
138 }
139 # }}}
140
141 # {{{ now process the 'copy queue' action
142 my @actions;
143 if ($Source and $Source ne $id) {
144     my $SourceQueue = RT::Queue->new($session{'CurrentUser'});
145     $SourceQueue->Load($Source) || Abort(loc("Couldn't load queue"));
146     my $SourceCustomFields = RT::CustomFields->new($session{'CurrentUser'});
147     $SourceCustomFields->LimitToQueue($SourceQueue->id);
148
149     # delete old fields
150     foreach my $CustomFieldObj ( @{$CustomFields->ItemsArrayRef} ) { 
151         $CustomFieldObj->Delete;
152     }
153
154     # add new fields
155     while (my $SourceCustomFieldObj = $SourceCustomFields->Next) {
156         my $CustomFieldObj = RT::CustomField->new($session{'CurrentUser'});
157         my ($val, $msg) =  $CustomFieldObj->Create(
158             id => $SourceCustomFieldObj->id,
159             Queue => $id,
160             Name => $SourceCustomFieldObj->Name,
161             Type => $SourceCustomFieldObj->Type,
162             Description => $SourceCustomFieldObj->Description,
163         );
164         Abort(loc("Could not create CustomField") . ": $msg") unless ($val);
165         push @actions, $msg;
166
167         $CustomFieldObj->SetSortOrder($SourceCustomFieldObj->SortOrder);
168
169         # add new values
170         my $values = $SourceCustomFieldObj->Values();
171         while (my $v = $values->Next) {
172             my ( $addval, $addmsg ) = $CustomFieldObj->AddValue(
173                 Name => $v->Name,
174                 Description => $v->Description,
175                 SortOrder => $v->SortOrder
176             );
177         }
178     }
179
180     # because content changed, we must redo search for subsequent uses
181     $CustomFields->RedoSearch;
182     $CustomFields->GotoFirstItem;
183 }
184 # }}}
185
186 # {{{ deal with deleting existing custom fields
187 foreach my $key (keys %ARGS) {
188   # {{{ if we're trying to delete the custom field
189   if ($key =~ /^DeleteCustomField-(\d+)/) {
190     my $id = $1;
191     my $CustomFieldObj = RT::CustomField->new($session{'CurrentUser'});
192     $CustomFieldObj->Load($id);
193     my ($retval, $msg) = $CustomFieldObj->Delete;
194     if ($retval) {
195       push @actions, loc("Custom field deleted");
196     }
197     else {
198       push @actions, $msg;
199     }
200   }
201   # }}}
202 }
203 # }}}
204
205 </%INIT>
206 <%ARGS>
207 $id => 0
208 $title => undef
209 $Move => undef
210 $Source => undef
211 $CustomField => undef
212 $FindDisabledCustomFields => undef
213 </%ARGS>