add disable_settings_changes conf for the demo
[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
74 my $curuser = $FS::CurrentUser::CurrentUser;
75 die "access denied\n" unless $curuser->access_right('Configuration');
76
77 my $conf = new FS::Conf;
78
79 if ( $conf->exists('disable_settings_changes') ) {
80   my @changers = split(/\s*,\s*/, $conf->config('disable_settings_changes'));
81   my %changers = map { $_=>1 } @changers;
82   unless ( $changers{$curuser->username} ) {
83     errorpage("Disabled in web demo");
84     die "shouldn't be reached";
85   }
86 }
87
88 $FS::Conf::DEBUG = 1;
89 my @config_items = grep { $_->key != ~/^invoice_(html|latex|template)/ }
90                         $conf->config_items;
91 my %confitems = map { $_->key => $_ } $conf->config_items;
92
93 my $agentnum = $cgi->param('agentnum');
94 my $key = $cgi->param('key');
95 my $i = $confitems{$key};
96
97 my @touch = ();
98 my @delete = ();
99 my $n = 0;
100 foreach my $type ( ref($i->type) ? @{$i->type} : $i->type ) {
101   if ( $type eq '' ) {
102   } elsif ( $type eq 'textarea' ) {
103     if ( $cgi->param($i->key.$n) ne '' ) {
104       my $value = $cgi->param($i->key.$n);
105       $value =~ s/\r\n/\n/g; #browsers?
106       $conf->set($i->key, $value, $agentnum);
107     } else {
108       $conf->delete($i->key, $agentnum);
109     }
110   } elsif ( $type eq 'binary' || $type eq 'image' ) {
111     if ( defined($cgi->param($i->key.$n)) && $cgi->param($i->key.$n) ) {
112       my $fh = $cgi->upload($i->key.$n);
113       if (defined($fh)) {
114         local $/;
115         $conf->set_binary($i->key, <$fh>, $agentnum);
116       }
117     }else{
118       warn "Condition failed for " . $i->key;
119     }
120   } elsif ( $type eq 'checkbox' ) {
121     if ( defined $cgi->param($i->key.$n) ) {
122       push @touch, $i->key;
123     } else {
124       push @delete, $i->key;
125     }
126   } elsif (
127     $type =~ /^(editlist|selectmultiple)$/
128     or ( $type =~ /^select(-(sub|part_svc|part_pkg|pkg_class))?$/
129          || $i->multiple )
130   ) {
131     if ( scalar(@{[ $cgi->param($i->key.$n) ]}) ) {
132       $conf->set($i->key, join("\n", @{[ $cgi->param($i->key.$n) ]} ), $agentnum);
133     } else {
134       $conf->delete($i->key, $agentnum);
135     }
136   } elsif ( $type =~ /^(text|select(-(sub|part_svc|part_pkg|pkg_class))?)$/ ) {
137     if ( $cgi->param($i->key.$n) ne '' ) {
138       $conf->set($i->key, $cgi->param($i->key.$n), $agentnum);
139     } else {
140       $conf->delete($i->key, $agentnum);
141     }
142   }
143   $n++;
144 }
145 # warn @touch;
146 $conf->touch($_, $agentnum) foreach @touch;
147 $conf->delete($_, $agentnum) foreach @delete;
148
149 </%init>