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