RT# 82137 - Added ability for processing fee to be pain on seperate invoice.
[freeside.git] / httemplate / config / config-process.cgi
index 31dce42..6af7d23 100644 (file)
@@ -1,4 +1,31 @@
-<% header('Configuration set') %>
+%if ( scalar(@error) ) {
+%
+%  my $url = popurl(1)."config.cgi";
+%  if ( length($cgi->query_string) > 1920 ) { #stupid IE 2083 URL limit
+%
+%    my $session = int(rand(4294967296)); #XXX
+%    my $pref = new FS::access_user_pref({
+%      'usernum'    => $FS::CurrentUser::CurrentUser->usernum,
+%      'prefname'   => "redirect$session",
+%      'prefvalue'  => $cgi->query_string,
+%      'expiration' => time + 3600, #1h?  1m?
+%    });
+%    my $pref_error = $pref->insert;
+%    if ( $pref_error ) {
+%      die "FATAL: couldn't even set redirect cookie: $pref_error".
+%          " attempting to set redirect$session to ". $cgi->query_string."\n";
+%    }
+%
+<% $cgi->redirect("$url?redirect=$session") %>
+%
+%  } else {
+%
+<% $cgi->redirect("$url?". $cgi->query_string ) %>
+%
+%  }
+%
+%} else {
+<& /elements/header-popup.html, 'Configuration set' &>
   <SCRIPT TYPE="text/javascript">
 %   my $n = 0;
 %   foreach my $type ( ref($i->type) ? @{$i->type} : $i->type ) {
@@ -20,7 +47,7 @@
           '</pre></font>';
 
 %     } elsif ( $type eq 'checkbox' ) {
-%       if ( $conf->exists($i->key, $agentnum) ) {
+%       if ( $conf->config_bool($i->key, $agentnum) ) {
           configCell.style.backgroundColor = '#00ff00';
           configCell.innerHTML = 'YES';
 %       } else {
@@ -38,7 +65,7 @@
 
 %     } elsif ( $type eq 'text' || $type eq 'select' ) {
         configCell.innerHTML = <% $conf->exists($i->key, $agentnum) ? $conf->config($i->key, $agentnum) : '' |js_string %>;
-%     } elsif ( $type =~ /^select-(part_svc|part_pkg|pkg_class)$/ && ! $i->multiple ) {
+%     } elsif ( $type =~ /^select-(part_svc|part_pkg|pkg_class|agent)$/ && ! $i->multiple ) {
 %       my $table = $1;
 %       my $namecol = $namecol{$table};
 %       my $pkey = dbdef->table($table)->primary_key;
@@ -46,9 +73,9 @@
 %       my $record = qsearchs($table, { $pkey => $key });
 %       my $value = $record ? "$key: ".$record->$namecol() : $key;
         configCell.innerHTML = <% $value |js_string %>;
-%     } elsif ( $type eq 'select-sub' ) {
+%     } elsif ( $type eq 'select-sub' && ! $i->multiple ) {
         configCell.innerHTML =
-          <% $conf->config($i->key, $agentnum) |js_string %> + ': ' +
+          <% $conf->exists($i->key, $agentnum) ? $conf->config($i->key, $agentnum) : '' |js_string %> + ': ' +
           <% &{ $i->option_sub }( $conf->config($i->key, $agentnum) ) |js_string %>;
 %     } else {
         //alert('unknown type <% $type %>');
   </SCRIPT>
 </BODY>
 </HTML>
+%}
 <%once>
 #false laziness w/config-view.cgi
 my %namecol = (
   'part_svc'  => 'svc',
   'part_pkg'  => 'pkg',
   'pkg_class' => 'classname',
+  'agent'     => 'agent',
 );
 </%once>
 <%init>
@@ -74,18 +103,20 @@ my %namecol = (
 my $curuser = $FS::CurrentUser::CurrentUser;
 die "access denied\n" unless $curuser->access_right('Configuration');
 
-my $conf = new FS::Conf;
+my $locale = $cgi->param('locale') || '';
+
+my $conf = new FS::Conf { 'locale' => $locale };
 
 if ( $conf->exists('disable_settings_changes') ) {
   my @changers = split(/\s*,\s*/, $conf->config('disable_settings_changes'));
   my %changers = map { $_=>1 } @changers;
   unless ( $changers{$curuser->username} ) {
-    include('/elements/errorpage-popup.html', "Disabled in web demo");
+    errorpage_popup("Disabled in web demo");
     die "shouldn't be reached";
   }
 }
 
-$FS::Conf::DEBUG = 1;
+#$FS::Conf::DEBUG = 1;
 my @config_items = grep { $_->key != ~/^invoice_(html|latex|template)/ }
                         $conf->config_items;
 my %confitems = map { $_->key => $_ } $conf->config_items;
@@ -94,6 +125,7 @@ my $agentnum = $cgi->param('agentnum');
 my $key = $cgi->param('key');
 my $i = $confitems{$key};
 
+my @error = ();
 my @touch = ();
 my @delete = ();
 my $n = 0;
@@ -103,6 +135,8 @@ foreach my $type ( ref($i->type) ? @{$i->type} : $i->type ) {
     if ( $cgi->param($i->key.$n) ne '' ) {
       my $value = $cgi->param($i->key.$n);
       $value =~ s/\r\n/\n/g; #browsers?
+      my $error = &{$i->validate}($value, $n) if $i->validate;
+      push @error, $error if $error;
       $conf->set($i->key, $value, $agentnum);
     } else {
       $conf->delete($i->key, $agentnum);
@@ -110,6 +144,8 @@ foreach my $type ( ref($i->type) ? @{$i->type} : $i->type ) {
   } elsif ( $type eq 'binary' || $type eq 'image' ) {
     if ( defined($cgi->param($i->key.$n)) && $cgi->param($i->key.$n) ) {
       my $fh = $cgi->upload($i->key.$n);
+      my $error = &{$i->validate}($fh, $n) if $i->validate;
+      push @error, $error if $error;
       if (defined($fh)) {
         local $/;
         $conf->set_binary($i->key, <$fh>, $agentnum);
@@ -119,22 +155,28 @@ foreach my $type ( ref($i->type) ? @{$i->type} : $i->type ) {
     }
   } elsif ( $type eq 'checkbox' ) {
     if ( defined $cgi->param($i->key.$n) ) {
-      push @touch, $i->key;
+      my $error = &{$i->validate}('', $n) if $i->validate;
+      push @error, $error if $error;
+      push @touch, $i->key if !$error;
     } else {
       push @delete, $i->key;
     }
   } elsif (
     $type =~ /^(editlist|selectmultiple)$/
-    or ( $type =~ /^select(-(sub|part_svc|part_pkg|pkg_class))?$/
+    or ( $type =~ /^select(-(sub|part_svc|part_pkg|pkg_class|agent))?$/
          || $i->multiple )
   ) {
-    if ( scalar(@{[ $cgi->param($i->key.$n) ]}) ) {
-      $conf->set($i->key, join("\n", @{[ $cgi->param($i->key.$n) ]} ), $agentnum);
+    if ( scalar(@{[ $cgi->param($i->key.$n) ]}) && $cgi->param($i->key.$n) ne '' ) {
+      my $error = &{$i->validate}([ $cgi->param($i->key.$n) ], $n) if $i->validate;
+      push @error, $error if $error;
+      $conf->set($i->key, join("\n", @{[ $cgi->param($i->key.$n) ]} ), $agentnum) if !$error;
     } else {
       $conf->delete($i->key, $agentnum);
     }
-  } elsif ( $type =~ /^(text|select(-(sub|part_svc|part_pkg|pkg_class))?)$/ ) {
+  } elsif ( $type =~ /^(text|select(-(sub|part_svc|part_pkg|pkg_class|agent))?)$/ ) {
     if ( $cgi->param($i->key.$n) ne '' ) {
+      my $error = &{$i->validate}($cgi->param($i->key.$n), $n) if $i->validate;
+      push @error, $error if $error;
       $conf->set($i->key, $cgi->param($i->key.$n), $agentnum);
     } else {
       $conf->delete($i->key, $agentnum);
@@ -144,6 +186,10 @@ foreach my $type ( ref($i->type) ? @{$i->type} : $i->type ) {
 }
 # warn @touch;
 $conf->touch($_, $agentnum) foreach @touch;
-$conf->delete($_, $agentnum) foreach @delete;
+$conf->delete_bool($_, $agentnum) foreach @delete;
+
+if (scalar(@error)) {
+  $cgi->param('error', join(' ', @error));
+}
 
 </%init>