finish up useful agent stuff on the config editor: adding, deleting overrides too...
[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 eq 'text' || $type eq 'select' || $type eq 'select-sub' )  {
45     if ( $cgi->param($i->key.$n) ne '' ) {
46       $conf->set($i->key, $cgi->param($i->key.$n), $agentnum);
47     } else {
48       $conf->delete($i->key, $agentnum);
49     }
50   } elsif ( $type eq 'editlist' || $type eq 'selectmultiple' )  {
51     if ( scalar(@{[ $cgi->param($i->key.$n) ]}) ) {
52       $conf->set($i->key, join("\n", @{[ $cgi->param($i->key.$n) ]} ), $agentnum);
53     } else {
54       $conf->delete($i->key, $agentnum);
55     }
56   }
57   $n++;
58 }
59 # warn @touch;
60 $conf->touch($_, $agentnum) foreach @touch;
61 $conf->delete($_, $agentnum) foreach @delete;
62
63 </%init>
64 <% header('Configuration set') %>
65   <SCRIPT TYPE="text/javascript">
66 %   my $n = 0;
67 %   foreach my $type ( ref($i->type) ? @{$i->type} : $i->type ) {
68     var configCell = window.top.document.getElementById('<% $agentnum. $i->key. $n %>');
69     if ( ! configCell ) {
70       window.top.location.reload();
71     }
72     //alert('found cell ' + configCell);
73 %     if (    $type eq 'textarea'
74 %          || $type eq 'editlist'
75 %          || $type eq 'selectmultiple' ) {
76         configCell.innerHTML =
77           '<font size="-2"><pre>' + "\n" +
78           <% encode_entities(join("\n",
79                map { length($_) > 88 ? substr($_,0,88).'...' : $_ }
80                    $conf->config($i->key, $agentnum)
81              ) )
82           |js_string %> +
83           '</pre></font>';
84
85 %     } elsif ( $type eq 'checkbox' ) {
86 %       if ( $conf->exists($i->key, $agentnum) ) {
87           configCell.style.backgroundColor = '#00ff00';
88           configCell.innerHTML = 'YES';
89 %       } else {
90           configCell.style.backgroundColor = '#ff0000';
91           configCell.innerHTML = 'NO';
92 %       }
93 %     } elsif ( $type eq 'select' && $i->select_hash ) {
94 %       my %hash;
95 %       if ( ref($i->select_hash) eq 'ARRAY' ) {
96 %         tie %hash, 'Tie::IxHash', '' => '', @{ $i->select_hash };
97 %       } else {
98 %         tie %hash, 'Tie::IxHash', '' => '', %{ $i->select_hash };
99 %       }
100         configCell.innerHTML = <% $conf->exists($i->key, $agentnum) ? $hash{ $conf->config($i->key, $agentnum) } : '' |js_string %>;
101
102 %     } elsif ( $type eq 'text' || $type eq 'select' ) {
103         configCell.innerHTML = <% $conf->exists($i->key, $agentnum) ? $conf->config($i->key, $agentnum) : '' |js_string %>;
104 %     } elsif ( $type eq 'select-sub' ) {
105         configCell.innerHTML =
106           <% $conf->config($i->key, $agentnum) |js_string %> + ': ' +
107           <% &{ $i->option_sub }( $conf->config($i->key, $agentnum) ) |js_string %>;
108 %     } else {
109         //alert('unknown type <% $type %>');
110         window.top.location.reload();
111 %     }
112
113 %     $n++;
114 %   }
115     parent.cClick();
116   </SCRIPT>
117   </BODY></HTML>