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