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