nomadix, RT#5876
[freeside.git] / httemplate / config / config-process.cgi
1 <%init>
2 die "access denied\n"
3   unless $FS::CurrentUser::CurrentUser->access_right('Configuration');
4
5 my $conf = new FS::Conf;
6 $FS::Conf::DEBUG = 1;
7 my @config_items = grep { $_->key != ~/^invoice_(html|latex|template)/ }
8                         $conf->config_items;
9 my %confitems = map { $_->key => $_ } $conf->config_items;
10
11 my $agentnum = $cgi->param('agentnum');
12 my $key = $cgi->param('key');
13 my $i = $confitems{$key};
14
15 my @touch = ();
16 my @delete = ();
17 my $n = 0;
18 foreach my $type ( ref($i->type) ? @{$i->type} : $i->type ) {
19   if ( $type eq '' ) {
20   } elsif ( $type eq 'textarea' ) {
21     if ( $cgi->param($i->key.$n) ne '' ) {
22       my $value = $cgi->param($i->key.$n);
23       $value =~ s/\r\n/\n/g; #browsers?
24       $conf->set($i->key, $value, $agentnum);
25     } else {
26       $conf->delete($i->key, $agentnum);
27     }
28   } elsif ( $type eq 'binary' || $type eq 'image' ) {
29     if ( defined($cgi->param($i->key.$n)) && $cgi->param($i->key.$n) ) {
30       my $fh = $cgi->upload($i->key.$n);
31       if (defined($fh)) {
32         local $/;
33         $conf->set_binary($i->key, <$fh>, $agentnum);
34       }
35     }else{
36       warn "Condition failed for " . $i->key;
37     }
38   } elsif ( $type eq 'checkbox' ) {
39     if ( defined $cgi->param($i->key.$n) ) {
40       push @touch, $i->key;
41     } else {
42       push @delete, $i->key;
43     }
44   } elsif ( $type =~ /^(editlist|selectmultiple)$/
45             or ( $type =~ /^select(-(sub|part_svc))?$/ || $i->multiple )
46           )
47   {
48     if ( scalar(@{[ $cgi->param($i->key.$n) ]}) ) {
49       $conf->set($i->key, join("\n", @{[ $cgi->param($i->key.$n) ]} ), $agentnum);
50     } else {
51       $conf->delete($i->key, $agentnum);
52     }
53   } elsif ( $type =~ /^(text|select(-(sub|part_svc))?)$/ ) {
54     if ( $cgi->param($i->key.$n) ne '' ) {
55       $conf->set($i->key, $cgi->param($i->key.$n), $agentnum);
56     } else {
57       $conf->delete($i->key, $agentnum);
58     }
59   }
60   $n++;
61 }
62 # warn @touch;
63 $conf->touch($_, $agentnum) foreach @touch;
64 $conf->delete($_, $agentnum) foreach @delete;
65
66 </%init>
67 <% header('Configuration set') %>
68   <SCRIPT TYPE="text/javascript">
69 %   my $n = 0;
70 %   foreach my $type ( ref($i->type) ? @{$i->type} : $i->type ) {
71     var configCell = window.top.document.getElementById('<% $agentnum. $i->key. $n %>');
72     if ( ! configCell ) {
73       window.top.location.reload();
74     }
75     //alert('found cell ' + configCell);
76 %     if (    $type eq 'textarea'
77 %          || $type eq 'editlist'
78 %          || $type eq 'selectmultiple' ) {
79         configCell.innerHTML =
80           '<font size="-2"><pre>' + "\n" +
81           <% encode_entities(join("\n",
82                map { length($_) > 88 ? substr($_,0,88).'...' : $_ }
83                    $conf->config($i->key, $agentnum)
84              ) )
85           |js_string %> +
86           '</pre></font>';
87
88 %     } elsif ( $type eq 'checkbox' ) {
89 %       if ( $conf->exists($i->key, $agentnum) ) {
90           configCell.style.backgroundColor = '#00ff00';
91           configCell.innerHTML = 'YES';
92 %       } else {
93           configCell.style.backgroundColor = '#ff0000';
94           configCell.innerHTML = 'NO';
95 %       }
96 %     } elsif ( $type eq 'select' && $i->select_hash ) {
97 %       my %hash;
98 %       if ( ref($i->select_hash) eq 'ARRAY' ) {
99 %         tie %hash, 'Tie::IxHash', '' => '', @{ $i->select_hash };
100 %       } else {
101 %         tie %hash, 'Tie::IxHash', '' => '', %{ $i->select_hash };
102 %       }
103         configCell.innerHTML = <% $conf->exists($i->key, $agentnum) ? $hash{ $conf->config($i->key, $agentnum) } : '' |js_string %>;
104
105 %     } elsif ( $type eq 'text' || $type eq 'select' ) {
106         configCell.innerHTML = <% $conf->exists($i->key, $agentnum) ? $conf->config($i->key, $agentnum) : '' |js_string %>;
107 %     } elsif ( $type eq 'select-part_svc' && ! $i->multiple ) {
108         configCell.innerHTML =
109           <% $conf->config($i->key, $agentnum) |js_string %>
110 %# + ': ' +
111 %#          <% &{ $i->option_sub }( $conf->config($i->key, $agentnum) ) |js_string %>;
112 %     } elsif ( $type eq 'select-sub' ) {
113         configCell.innerHTML =
114           <% $conf->config($i->key, $agentnum) |js_string %> + ': ' +
115           <% &{ $i->option_sub }( $conf->config($i->key, $agentnum) ) |js_string %>;
116 %     } else {
117         //alert('unknown type <% $type %>');
118         window.top.location.reload();
119 %     }
120
121 %     $n++;
122 %   }
123     parent.cClick();
124   </SCRIPT>
125   </BODY></HTML>