Option to ignore old CDRs, RT#81480
[freeside.git] / httemplate / config / config-process.cgi
1 %if ( scalar(@error) ) {
2 %
3 %  my $url = popurl(1)."config.cgi";
4 %  if ( length($cgi->query_string) > 1920 ) { #stupid IE 2083 URL limit
5 %
6 %    my $session = int(rand(4294967296)); #XXX
7 %    my $pref = new FS::access_user_pref({
8 %      'usernum'    => $FS::CurrentUser::CurrentUser->usernum,
9 %      'prefname'   => "redirect$session",
10 %      'prefvalue'  => $cgi->query_string,
11 %      'expiration' => time + 3600, #1h?  1m?
12 %    });
13 %    my $pref_error = $pref->insert;
14 %    if ( $pref_error ) {
15 %      die "FATAL: couldn't even set redirect cookie: $pref_error".
16 %          " attempting to set redirect$session to ". $cgi->query_string."\n";
17 %    }
18 %
19 <% $cgi->redirect("$url?redirect=$session") %>
20 %
21 %  } else {
22 %
23 <% $cgi->redirect("$url?". $cgi->query_string ) %>
24 %
25 %  }
26 %
27 %} else {
28 <& /elements/header-popup.html, 'Configuration set' &>
29   <SCRIPT TYPE="text/javascript">
30 %   my $n = 0;
31 %   foreach my $type ( ref($i->type) ? @{$i->type} : $i->type ) {
32     var configCell = window.top.document.getElementById('<% $agentnum. $i->key. $n %>');
33     if ( ! configCell ) {
34       window.top.location.reload();
35     }
36     //alert('found cell ' + configCell);
37 %     if (    $type eq 'textarea'
38 %          || $type eq 'editlist'
39 %          || $type eq 'selectmultiple' ) {
40         configCell.innerHTML =
41           '<font size="-2"><pre>' + "\n" +
42           <% encode_entities(join("\n",
43                map { length($_) > 88 ? substr($_,0,88).'...' : $_ }
44                    $conf->config($i->key, $agentnum)
45              ) )
46           |js_string %> +
47           '</pre></font>';
48
49 %     } elsif ( $type eq 'checkbox' ) {
50 %       if ( $conf->config_bool($i->key, $agentnum) ) {
51           configCell.style.backgroundColor = '#00ff00';
52           configCell.innerHTML = 'YES';
53 %       } else {
54           configCell.style.backgroundColor = '#ff0000';
55           configCell.innerHTML = 'NO';
56 %       }
57 %     } elsif ( $type eq 'select' && $i->select_hash ) {
58 %       my %hash;
59 %       if ( ref($i->select_hash) eq 'ARRAY' ) {
60 %         tie %hash, 'Tie::IxHash', '' => '', @{ $i->select_hash };
61 %       } else {
62 %         tie %hash, 'Tie::IxHash', '' => '', %{ $i->select_hash };
63 %       }
64         configCell.innerHTML = <% $conf->exists($i->key, $agentnum) ? $hash{ $conf->config($i->key, $agentnum) } : '' |js_string %>;
65
66 %     } elsif ( $type eq 'text' || $type eq 'select' ) {
67         configCell.innerHTML = <% $conf->exists($i->key, $agentnum) ? $conf->config($i->key, $agentnum) : '' |js_string %>;
68 %     } elsif ( $type =~ /^select-(part_svc|part_pkg|pkg_class|agent)$/ && ! $i->multiple ) {
69 %       my $table = $1;
70 %       my $namecol = $namecol{$table};
71 %       my $pkey = dbdef->table($table)->primary_key;
72 %       my $key = $conf->config($i->key, $agentnum);
73 %       my $record = qsearchs($table, { $pkey => $key });
74 %       my $value = $record ? "$key: ".$record->$namecol() : $key;
75         configCell.innerHTML = <% $value |js_string %>;
76 %     } elsif ( $type eq 'select-sub' && ! $i->multiple ) {
77         configCell.innerHTML =
78           <% $conf->exists($i->key, $agentnum) ? $conf->config($i->key, $agentnum) : '' |js_string %> + ': ' +
79           <% &{ $i->option_sub }( $conf->config($i->key, $agentnum) ) |js_string %>;
80 %     } else {
81         //alert('unknown type <% $type %>');
82         window.top.location.reload();
83 %     }
84
85 %     $n++;
86 %   }
87     parent.cClick();
88   </SCRIPT>
89 </BODY>
90 </HTML>
91 %}
92 <%once>
93 #false laziness w/config-view.cgi
94 my %namecol = (
95   'part_svc'  => 'svc',
96   'part_pkg'  => 'pkg',
97   'pkg_class' => 'classname',
98   'agent'     => 'agent',
99 );
100 </%once>
101 <%init>
102
103 my $curuser = $FS::CurrentUser::CurrentUser;
104 die "access denied\n" unless $curuser->access_right('Configuration');
105
106 my $locale = $cgi->param('locale') || '';
107
108 my $conf = new FS::Conf { 'locale' => $locale };
109
110 if ( $conf->exists('disable_settings_changes') ) {
111   my @changers = split(/\s*,\s*/, $conf->config('disable_settings_changes'));
112   my %changers = map { $_=>1 } @changers;
113   unless ( $changers{$curuser->username} ) {
114     errorpage_popup("Disabled in web demo");
115     die "shouldn't be reached";
116   }
117 }
118
119 #$FS::Conf::DEBUG = 1;
120 my @config_items = grep { $_->key != ~/^invoice_(html|latex|template)/ }
121                         $conf->config_items;
122 my %confitems = map { $_->key => $_ } $conf->config_items;
123
124 my $agentnum = $cgi->param('agentnum');
125 my $key = $cgi->param('key');
126 my $i = $confitems{$key};
127
128 my @error = ();
129 my @touch = ();
130 my @delete = ();
131 my $n = 0;
132 foreach my $type ( ref($i->type) ? @{$i->type} : $i->type ) {
133   if ( $type eq '' ) {
134   } elsif ( $type eq 'textarea' ) {
135     if ( $cgi->param($i->key.$n) ne '' ) {
136       my $value = $cgi->param($i->key.$n);
137       $value =~ s/\r\n/\n/g; #browsers?
138       my $error = &{$i->validate}($value, $n) if $i->validate;
139       push @error, $error if $error;
140       $conf->set($i->key, $value, $agentnum);
141     } else {
142       $conf->delete($i->key, $agentnum);
143     }
144   } elsif ( $type eq 'binary' || $type eq 'image' ) {
145     if ( defined($cgi->param($i->key.$n)) && $cgi->param($i->key.$n) ) {
146       my $fh = $cgi->upload($i->key.$n);
147       my $error = &{$i->validate}($fh, $n) if $i->validate;
148       push @error, $error if $error;
149       if (defined($fh)) {
150         local $/;
151         $conf->set_binary($i->key, <$fh>, $agentnum);
152       }
153     }else{
154       warn "Condition failed for " . $i->key;
155     }
156   } elsif ( $type eq 'checkbox' ) {
157     if ( defined $cgi->param($i->key.$n) ) {
158       push @touch, $i->key;
159     } else {
160       push @delete, $i->key;
161     }
162   } elsif (
163     $type =~ /^(editlist|selectmultiple)$/
164     or ( $type =~ /^select(-(sub|part_svc|part_pkg|pkg_class|agent))?$/
165          || $i->multiple )
166   ) {
167     if ( scalar(@{[ $cgi->param($i->key.$n) ]}) && $cgi->param($i->key.$n) ne '' ) {
168       my $error = &{$i->validate}([ $cgi->param($i->key.$n) ], $n) if $i->validate;
169       push @error, $error if $error;
170       $conf->set($i->key, join("\n", @{[ $cgi->param($i->key.$n) ]} ), $agentnum);
171     } else {
172       $conf->delete($i->key, $agentnum);
173     }
174   } elsif ( $type =~ /^(text|select(-(sub|part_svc|part_pkg|pkg_class|agent))?)$/ ) {
175     if ( $cgi->param($i->key.$n) ne '' ) {
176       my $error = &{$i->validate}($cgi->param($i->key.$n), $n) if $i->validate;
177       push @error, $error if $error;
178       $conf->set($i->key, $cgi->param($i->key.$n), $agentnum);
179     } else {
180       $conf->delete($i->key, $agentnum);
181     }
182   }
183   $n++;
184 }
185 # warn @touch;
186 $conf->touch($_, $agentnum) foreach @touch;
187 $conf->delete_bool($_, $agentnum) foreach @delete;
188
189 if (scalar(@error)) {
190   $cgi->param('error', join(' ', @error));
191 }
192
193 </%init>