config in database cleanup, editing, and agent-specific config (452, 1419)
[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 = $conf->config_items;
8 my %confitems = map { $_->key => $_ } $conf->config_items;
9
10 my $agentnum = $cgi->param('agentnum');
11 my $key = $cgi->param('key');
12 my $i = $confitems{$key};
13
14 my @touch = ();
15 my @delete = ();
16 my $n = 0;
17 foreach my $type ( ref($i->type) ? @{$i->type} : $i->type ) {
18   if ( $type eq '' ) {
19   } elsif ( $type eq 'textarea' ) {
20     if ( $cgi->param($i->key.$n) ne '' ) {
21       my $value = $cgi->param($i->key.$n);
22       $value =~ s/\r\n/\n/g; #browsers?
23       $conf->set($i->key, $value, $agentnum);
24     } else {
25       $conf->delete($i->key, $agentnum);
26     }
27   } elsif ( $type eq 'binary' ) {
28     if ( defined($cgi->param($i->key.$n)) && $cgi->param($i->key.$n) ) {
29       my $fh = $cgi->upload($i->key.$n);
30       if (defined($fh)) {
31         local $/;
32         $conf->set_binary($i->key, <$fh>, $agentnum);
33       }
34     }else{
35       warn "Condition failed for " . $i->key;
36     }
37   } elsif ( $type eq 'checkbox' ) {
38     if ( defined $cgi->param($i->key.$n) ) {
39       push @touch, $i->key;
40     } else {
41       push @delete, $i->key;
42     }
43   } elsif ( $type eq 'text' || $type eq 'select' || $type eq 'select-sub' )  {
44     if ( $cgi->param($i->key.$n) ne '' ) {
45       $conf->set($i->key, $cgi->param($i->key.$n), $agentnum);
46     } else {
47       $conf->delete($i->key, $agentnum);
48     }
49   } elsif ( $type eq 'editlist' || $type eq 'selectmultiple' )  {
50     if ( scalar(@{[ $cgi->param($i->key.$n) ]}) ) {
51       $conf->set($i->key, join("\n", @{[ $cgi->param($i->key.$n) ]} ), $agentnum);
52     } else {
53       $conf->delete($i->key, $agentnum);
54     }
55   }
56   $n++;
57 }
58 # warn @touch;
59 $conf->touch($_, $agentnum) foreach @touch;
60 $conf->delete($_, $agentnum) foreach @delete;
61
62 </%init>
63 <% header('Configuration set') %>
64   <SCRIPT TYPE="text/javascript">
65     window.top.location.reload();
66   </SCRIPT>
67   </BODY></HTML>