correctly allow re-editing of config options with " in them
[freeside.git] / httemplate / config / config.cgi
1 <% include("/elements/header-popup.html", $title) %>
2
3 <SCRIPT>
4 var gSafeOnload = new Array();
5 var gSafeOnsubmit = new Array();
6 window.onload = SafeOnload;
7 function SafeAddOnLoad(f) {
8   gSafeOnload[gSafeOnload.length] = f;
9 }
10 function SafeOnload() {
11   for (var i=0;i<gSafeOnload.length;i++)
12     gSafeOnload[i]();
13 }
14 function SafeAddOnSubmit(f) {
15   gSafeOnsubmit[gSafeOnsubmit.length] = f;
16 }
17 function SafeOnsubmit() {
18   for (var i=0;i<gSafeOnsubmit.length;i++)
19     gSafeOnsubmit[i]();
20 }
21 </SCRIPT>
22
23 <% include('/elements/error.html') %>
24
25 <FORM NAME="OneTrueForm" ACTION="config-process.cgi" METHOD="POST" enctype="multipart/form-data" onSubmit="SafeOnsubmit()">
26 <INPUT TYPE="hidden" NAME="agentnum" VALUE="<% $agentnum %>">
27 <INPUT TYPE="hidden" NAME="key" VALUE="<% $key %>">
28
29 Setting <b><% $key %></b>
30
31 % my $description_printed = 0;
32 % if ( grep $_ eq 'textarea', @types ) {
33 %   $description_printed = 1;
34
35     - <% $description %>
36
37 % }
38
39 <table><tr><td>
40
41 % my $n = 0;
42 % foreach my $type (@types) {
43 %   if ( $type eq '' ) {
44
45   <font color="#ff0000">no type</font>
46
47 %   } elsif ( $type eq 'binary' ) { 
48
49   Filename <input type="file" name="<% "$key$n" %>">
50
51 %   } elsif ( $type eq 'textarea' ) { 
52
53   <textarea name="<% "$key$n" %>" rows=12 cols=88 wrap="off"><% join("\n", $conf->config($key, $agentnum)) %></textarea>
54
55 %   } elsif ( $type eq 'checkbox' ) { 
56
57   <input name="<% "$key$n" %>" type="checkbox" value="1"
58     <% $conf->exists($key, $agentnum) ? 'CHECKED' : '' %> >
59
60 %   } elsif ( $type eq 'text' )  { 
61
62   <input name="<% "$key$n" %>" type="text" value="<% $conf->exists($key, $agentnum) ? $conf->config($key, $agentnum) : '' |h %>">
63
64 %   } elsif ( $type eq 'select' || $type eq 'selectmultiple' )  { 
65
66   <select name="<% "$key$n" %>" <% $type eq 'selectmultiple' ? 'MULTIPLE' : '' %>>
67
68 %
69 %     my %hash = ();
70 %     if ( $config_item->select_enum ) {
71 %       tie %hash, 'Tie::IxHash',
72 %         '' => '', map { $_ => $_ } @{ $config_item->select_enum };
73 %     } elsif ( $config_item->select_hash ) {
74 %       if ( ref($config_item->select_hash) eq 'ARRAY' ) {
75 %         tie %hash, 'Tie::IxHash',
76 %           '' => '', @{ $config_item->select_hash };
77 %       } else {
78 %         tie %hash, 'Tie::IxHash',
79 %           '' => '', %{ $config_item->select_hash };
80 %       }
81 %     } else {
82 %       %hash = ( '' => 'WARNING: neither select_enum nor select_hash specified in Conf.pm for configuration option "'. $key. '"' );
83 %     }
84 %
85 %     my %saw = ();
86 %     foreach my $value ( keys %hash ) {
87 %       local($^W)=0; next if $saw{$value}++;
88 %       my $label = $hash{$value};
89 %        
90
91     <option value="<% $value %>"
92
93 %       if ( $value eq $conf->config($key, $agentnum)
94 %            || ( $type eq 'selectmultiple'
95 %                 && grep { $_ eq $value } $conf->config($key, $agentnum) ) ) {
96
97       SELECTED
98
99 %       }
100
101     ><% $label %>
102
103 %     } 
104 %     my $curvalue = $conf->config($key, $agentnum);
105 %     if ( $conf->exists($key, $agentnum) && $curvalue && ! $hash{$curvalue} ) {
106
107     <option value="<% $curvalue %>" SELECTED>
108
109 %       if ( exists( $hash{ $conf->config($key, $agentnum) } ) ) {
110
111       <% $hash{ $conf->config($key, $agentnum) } %>
112
113 %       }else{
114
115       <% $curvalue %>
116
117 %       }
118 %     } 
119
120   </select>
121
122 %   } elsif ( $type eq 'select-sub' ) { 
123
124   <select name="<% "$key$n" %>"><option value="">
125
126 %     my %options = &{$config_item->options_sub};
127 %     my @options = sort { $a <=> $b } keys %options;
128 %     my %saw;
129 %     foreach my $value ( @options ) {
130 %       local($^W)=0; next if $saw{$value}++;
131
132     <option value="<% $value %>" <% $value eq $conf->config($key, $agentnum) ? 'SELECTED' : '' %>><% $value %>: <% $options{$value} %>
133
134 %     } 
135 %     my $curvalue = $conf->config($key, $agentnum);
136 %     if ( $conf->exists($key, $agentnum) && $curvalue && ! $options{$curvalue} ) {
137
138     <option value="<% $curvalue %>" SELECTED> <% $curvalue %>: <% &{ $config_item->option_sub }( $curvalue ) %> 
139
140 %     } 
141
142   </select>
143
144 %   } elsif ( $type eq 'editlist' ) { 
145 %
146   <script>
147     function doremove<% "$key$n" %>() {
148       fromObject = document.OneTrueForm.<% "$key$n" %>;
149       for (var i=fromObject.options.length-1;i>-1;i--) {
150         if (fromObject.options[i].selected)
151           deleteOption<% "$key$n" %>(fromObject,i);
152       }
153     }
154     function deleteOption<% "$key$n" %>(object,index) {
155       object.options[index] = null;
156     }
157     function selectall<% "$key$n" %>() {
158       fromObject = document.OneTrueForm.<% "$key$n" %>;
159       for (var i=fromObject.options.length-1;i>-1;i--) {
160         fromObject.options[i].selected = true;
161       }
162     }
163     function doadd<% "$key$n" %>(object) {
164       var myvalue = "";
165
166 %     if ( defined($config_item->editlist_parts) ) { 
167 %       foreach my $pnum ( 0 .. scalar(@{$config_item->editlist_parts})-1 ) { 
168
169       if ( myvalue != "" ) { myvalue = myvalue + " "; }
170
171 %         if ( $config_item->editlist_parts->[$pnum]{type} eq 'select' ) { 
172
173       myvalue = myvalue + object.add<% "$key${n}_$pnum" %>.options[object.add<% "$key${n}_$pnum" %>.selectedIndex].value
174       <!-- #RESET SELECT??  maybe not... -->
175
176 %         } elsif ( $config_item->editlist_parts->[$pnum]{type} eq 'immutable' ) { 
177
178       myvalue = myvalue + object.add<% "$key${n}_$pnum" %>.value
179
180 %         } else { 
181
182       myvalue = myvalue + object.add<% "$key${n}_$pnum" %>.value
183       object.add<% "$key${n}_$pnum" %>.value = ""
184
185 %         } 
186 %       } 
187 %     } else { 
188
189       myvalue = object.add<% "$key${n}_1" %>.value
190
191 %     } 
192
193       var optionName = new Option(myvalue, myvalue);
194       var length = object.<% "$key$n" %>.length;
195       object.<% "$key$n" %>.options[length] = optionName;
196     }
197   </script>
198   <select multiple size=5 name="<% "$key$n" %>">
199     <option selected>----------------------------------------------------------------</option>
200
201 %     foreach my $line ( $conf->config($key, $agentnum) ) { 
202
203     <option value="<% $line %>"><% $line %></option>
204
205 %     } 
206
207   </select><br>
208   <input type="button" value="remove selected" onClick="doremove<% "$key$n" %>()">
209   <script>SafeAddOnLoad(doremove<% "$key$n" %>);
210     SafeAddOnSubmit(selectall<% "$key$n" %>);
211   </script>
212   <br><% itable() %><tr>
213
214 %     if ( defined $config_item->editlist_parts ) { 
215 %       my $pnum=0;
216 %       foreach my $part ( @{$config_item->editlist_parts} ) { 
217
218     <td>
219
220 %         if ( $part->{type} eq 'text' ) { 
221
222       <input type="text" name="add<% "$key${n}_$pnum" %>">
223
224 %         } elsif ( $part->{type} eq 'immutable' ) { 
225
226       <% $part->{value} %>
227       <input type="hidden" name="add<% "$key${n}_$pnum" %>" value="<% $part->{value} %>">
228
229 %         } elsif ( $part->{type} eq 'select' ) { 
230
231       <select name="add<% qq!$key${n}_$pnum! %>">
232
233 %           foreach my $key ( keys %{$part->{select_enum}} ) { 
234
235         <option value="<% $key %>"><% $part->{select_enum}{$key} %></option>
236
237 %           } 
238
239       </select>
240
241 %         } else { 
242
243       <font color="#ff0000">unknown type <% $part->type %> </font>
244
245 %         } 
246
247     </td>
248
249 %         $pnum++;
250 %       } 
251 %     } else { 
252
253     <td><input type="text" name="add<% "$key${n}_0" %>></td>
254
255 %     } 
256
257     <td><input type="button" value="add" onClick="doadd<% "$key$n" %>(this.form)"></td>
258   </tr></table>
259
260 %   } else {
261
262   <font color="#ff0000">unknown type $type</font>
263
264 %   }
265 % $n++;
266 % }
267
268   </td>
269 % unless ( $description_printed ) {
270     <td><% $description %></td>
271 % }
272 </tr>
273 </table>
274 <INPUT TYPE="submit" VALUE="<% $title %>">
275 </FORM>
276
277 </BODY>
278 </HTML>
279 <%once>
280
281 my $conf = new FS::Conf;
282 my @config_items = grep { $_->key != ~/^invoice_(html|latex|template)/ }
283                         $conf->config_items; 
284 my %confitems = map { $_->key => $_ } @config_items;
285
286 </%once>
287 <%init>
288
289 die "access denied"
290   unless $FS::CurrentUser::CurrentUser->access_right('Configuration');
291
292 my $action = 'Set';
293
294 my $agentnum = '';
295 if ($cgi->param('agentnum') =~ /(\d+)$/) {
296   $agentnum=$1;
297 }
298
299 my $agent = '';
300 my $title;
301 if ($agentnum) {
302   $agent = qsearchs('agent', { 'agentnum' => $1 } );
303   die "Agent $agentnum not found!" unless $agent;
304
305   $title = "$action configuration override for ". $agent->agent;
306 } else {
307   $title = "$action global configuration";
308 }
309
310 $cgi->param('key') =~ /^([-.\w]+)$/ or die "illegal configuration item";
311 my $key = $1;
312 my $value = $conf->config($key);
313 my $config_item = $confitems{$key};
314
315 my $description = $config_item->description;
316 my $config_type = $config_item->type;
317 my @types = ref($config_type) ? @$config_type : ($config_type);
318
319 </%init>