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