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