import torrus 1.0.9
[freeside.git] / httemplate / edit / process / elements / process.html
index a671ca1..53419cd 100644 (file)
@@ -18,8 +18,10 @@ Example:
    ###
 
    'viewall_dir'  => '', #'search' or 'browse', defaults to 'search'
+   'viewall_ext'  => 'html', #'cgi' or 'html', defaults to 'html'
    OR
    'redirect'     => 'view/table.cgi?', # value of primary key is appended
+                                        # (string or coderef returning a string)
    OR
    'popup_reload' => 'Momentary success message', #will reload parent window
 
@@ -32,8 +34,14 @@ Example:
 
    'clear_on_error' => [ 'form_field1', 'form_field2', ... ],
 
+                  #pass an arrayref of hashrefs for multiple m2ms or m2names
+                  #be certain you incorporate m2m_Common if you see error: param
+
    'process_m2m' => { 'link_table'   => 'link_table_name',
                       'target_table' => 'target_table_name',
+                      #optional (see m2m_Common::process_m2m), if not specified
+                      # all CGI params will be passed)
+                      'params'       => 
                     },
    'process_m2name' => { 'link_table'   => 'link_table_name',
                          'link_static' => { 'column' => 'value' },
@@ -48,9 +56,20 @@ Example:
 
 
                        },
+   'process_o2m' => { 'table' => table_name',
+                      'num_col' => 'column', #if column name is different in
+                                             #link_table than source_table 
+                    },
+
+   #checks CGI params and whatever else before much else runs
+   #return an error string or empty for no error
+   'precheck_callback' => sub { my( $cgi ) = @_; },
+
+   #after everything's inserted
+   'noerror_callback' => sub { my( $cgi, $object ) = @_; },
 
    #supplies arguments to insert() and replace()
-   # for use with tables that are FS::option_Common
+   # for use with tables that are FS::option_Common (among other things)
    'args_callback' => sub { my( $cgi, $object ) = @_; },
 
    'debug' => 1, #turns on debugging output
@@ -102,25 +121,38 @@ Example:
   </BODY>
   </HTML>
 
-%} elsif ( $opt{'redirect'} ) {
+%} else {
+%  
+%  $opt{'redirect'} = &{$opt{'redirect'}}($cgi, $new)
+%    if ref($opt{'redirect'}) eq 'CODE';
+%
+%  if ( $opt{'redirect'} ) {
 %
 <% $cgi->redirect( $opt{'redirect'}. $pkeyvalue ) %>
 %
-%} else { 
+%  } else { 
+%
+%    my $ext = $opt{'viewall_ext'} || 'html';
+%
+<% $cgi->redirect( popurl(3). ($opt{viewall_dir}||'search'). "/$table.$ext" ) %>
+%
+%  }
 %
-<% $cgi->redirect( popurl(3). ($opt{viewall_dir}||'search'). "/$table.html" ) %>
 %}
-<%once>
-
-  my $me = 'process.html:';
-
-</%once>
+%
 <%init>
 
+my $me = 'process.html:';
+
 my(%opt) = @_;
 
 my $curuser = $FS::CurrentUser::CurrentUser;
 
+my $error = '';
+if ( $opt{'precheck_callback'} ) {
+  $error = &{ $opt{'precheck_callback'} }( $cgi );
+}
+
 #false laziness w/edit.html
 my $table = $opt{'table'};
 my $class = "FS::$table";
@@ -145,18 +177,14 @@ if ( $pkeyvalue ) {
   });
 }
 
-my %hash = map { $_ => scalar($cgi->param($_)) } @$fields;
+my %hash =
+  map { my @entry = ( $_ => scalar($cgi->param($_)) );
+        $opt{'value_callback'} ? ( $_ => &{ $opt{'value_callback'} }( @entry ))
+                               : ( @entry )
+      } @$fields;
 
 my $new = $class->new( \%hash );
 
-if ( $opt{'agent_virt'} ) {
-  die "illegal agentnum"
-    unless $curuser->agentnums_href->{$new->agentnum}
-        or $opt{'agent_null_right'}
-           && ! $new->agentnum
-           && $curuser->access_right($opt{'agent_null_right'});
-}
-
 if ($old && exists($opt{'copy_on_empty'})) {
   foreach my $field (@{$opt{'copy_on_empty'}}) {
     $new->set($field, $old->get($field))
@@ -164,7 +192,31 @@ if ($old && exists($opt{'copy_on_empty'})) {
   }
 }
 
-my $error = $new->check;
+if ( $opt{'agent_virt'} ) {
+
+  if ( ! $new->agentnum
+       && (    ! $opt{'agent_null_right'}
+            || ! $curuser->access_right($opt{'agent_null_right'})
+          )
+     )
+  {
+
+    $error ||= 'Select an agent';
+
+  } else {
+
+    die "illegal agentnum"
+      unless $curuser->agentnums_href->{$new->agentnum}
+          or $curuser->access_right('View customers of all agents')
+          or $opt{'agent_null_right'}
+             && ! $new->agentnum
+             && $curuser->access_right($opt{'agent_null_right'});
+
+  }
+
+}
+
+$error ||= $new->check;
 
 my @args = ();
 if ( !$error && $opt{'args_callback'} ) {
@@ -188,38 +240,82 @@ if ( !$error ) {
 
 if ( !$error && $opt{'process_m2m'} ) {
 
-  if ( $opt{'debug'} ) {
-    warn "$me processing m2m:\n". Dumper( %{ $opt{'process_m2m'} },
-                                          'params' => scalar($cgi->Vars),
-                                        );
+  my @process_m2m = ref($opt{'process_m2m'}) eq 'ARRAY'
+                      ? @{ $opt{'process_m2m'} }
+                      :  ( $opt{'process_m2m'} );
+
+  foreach my $process_m2m (@process_m2m) {
+
+    $process_m2m->{'params'} ||= scalar($cgi->Vars);
+
+    warn "$me processing m2m:\n". Dumper( %$process_m2m )
+      if $opt{'debug'};
+
+    $error = $new->process_m2m( %$process_m2m );
   }
 
-  $error = $new->process_m2m( %{ $opt{'process_m2m'} },
-                              'params' => scalar($cgi->Vars),
-                            );
 }
 
 if ( !$error && $opt{'process_m2name'} ) {
 
-  if ( $opt{'debug'} ) {
-    warn "$me processing m2name:\n". Dumper( %{ $opt{'process_m2name'} },
-                                             'params' => scalar($cgi->Vars),
-                                           );
+  my @process_m2name = ref($opt{'process_m2name'}) eq 'ARRAY'
+                         ? @{ $opt{'process_m2name'} }
+                         :  ( $opt{'process_m2name'} );
+
+
+  foreach my $process_m2name (@process_m2name) {
+
+    if ( $opt{'debug'} ) {
+      warn "$me processing m2name:\n". Dumper( %{ $process_m2name },
+                                               'params' => scalar($cgi->Vars),
+                                             );
+    }
+
+    $error = $new->process_m2name( %{ $process_m2name },
+                                   'params' => scalar($cgi->Vars),
+                                 );
+  }
+
+}
+
+if ( !$error && $opt{'process_o2m'} ) {
+
+  my @process_o2m = ref($opt{'process_o2m'}) eq 'ARRAY'
+                         ? @{ $opt{'process_o2m'} }
+                         :  ( $opt{'process_o2m'} );
+
+
+  foreach my $process_o2m (@process_o2m) {
+
+    if ( $opt{'debug'} ) {
+      warn "$me processing o2m:\n". Dumper( %{ $process_o2m },
+                                               'params' => scalar($cgi->Vars),
+                                             );
+    }
+
+    $error = $new->process_o2m( %{ $process_o2m },
+                                   'params' => scalar($cgi->Vars),
+                                 );
   }
 
-  $error = $new->process_m2name( %{ $opt{'process_m2name'} },
-                                 'params' => scalar($cgi->Vars),
-                               );
 }
 
 
 if ( $error ) {
+
   $cgi->param('error', $error);
   if ( $opt{'clear_on_error'} && scalar(@{$opt{'clear_on_error'}}) ) {
     foreach my $field (@{$opt{'clear_on_error'}}) {
       $cgi->param($field, '')
     }
   }
+
+} else {
+
+  if ( $opt{'noerror_callback'} ) {
+    &{ $opt{'noerror_callback'} }( $cgi, $new );
+  }
+
 }
 
 </%init>