+<& /elements/header.html &>
+<form name="UploadForm">
+% my $job_fields = $opt{'process_upload'}{'fields'};
+% foreach my $field ( @$job_fields ) {
+ <input type="hidden" name="<% $field %>" value="<% $cgi->param($field) |h %>">
+% }
+% push @$job_fields, 'uploaded_files', $pkey;
+%
+ <input type="hidden" name="uploaded_files" value="<% join(',', @uploaded_files) %>">
+ <input type="hidden" name="<% $pkey %>" value="<% $new->get($pkey) %>">
+<& /elements/progress-init.html,
+ 'UploadForm',
+ $job_fields,
+ $fsurl . $opt{'process_upload'}{'process'},
+ {
+ url => $redirect,
+ error_url => "$error_redirect?$new_pkey"
+ },
+&>
+<input type="submit" style="display:none">
+</form>
+<script>
+<&| /elements/onload.js &>
+process();
+</&>
+</script>
+<& /elements/footer.html &>
+
+% } elsif ( $opt{'progress_init'} ) {
+% # some false laziness with the above
+% my ($form_name, $job_fields) = @{ $opt{'progress_init'} };
+<form name="<% $form_name %>">
+ <input type="hidden" name="<% $pkey %>" value="<% $new->get($pkey) %>">
+% foreach my $field (@$job_fields) {
+% next if $field eq $pkey;
+ <input type="hidden" name="<% $field %>" value="<% $cgi->param($field) |h %>">
+% }
+<& /elements/progress-init.html,
+ @{ $opt{'progress_init'} }
+&>
+<input type="submit" style="display:none">
+</form>
+<script>
+<&| /elements/onload.js &>
+process();
+</&>
+</script>
+<& /elements/footer.html &>
+
+% } elsif ( $opt{'popup_reload'} ) {
+
+ <% include('/elements/header-popup.html', $opt{'popup_reload'} ) %>
+
+ <SCRIPT TYPE="text/javascript">
+ window.top.location.reload();
+ </SCRIPT>
+
+ </BODY>
+ </HTML>
+
+%} else {
+<% $cgi->redirect($redirect) %>
+%}
+<%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";
+my $pkey = dbdef->table($table)->primary_key; #? $opt{'primary_key'} ||
+my $fields = $opt{'fields'}
+ #|| [ grep { $_ ne $pkey } dbdef->table($table)->columns ];
+ || [ fields($table) ];
+
+my $old_pkey = $cgi->param($pkey);
+
+my $old = '';
+if ( $old_pkey ) {
+ $old = qsearchs({
+ 'table' => $table,
+ 'hashref' => { $pkey => $old_pkey },
+ 'extra_sql' => ( $opt{'agent_virt'}
+ ? ' AND '. $curuser->agentnums_sql(
+ 'null_right' => $opt{'agent_null_right'}
+ )
+ : ''
+ ),
+ });
+}
+
+my $bfield = $opt{'bulk'};
+
+my %hash =
+ map { my @entry = ( $_ => scalar($cgi->param($_)) );
+ $opt{'value_callback'} ? ( $_ => &{ $opt{'value_callback'} }( @entry ))
+ : ( @entry )
+ } grep { $_ ne $bfield } @$fields;
+
+my @values = ( 1 );
+if ( $bfield ) {
+ @values = $cgi->param($bfield);
+ #warn join(',', @values);
+}
+
+my @uploaded_files;
+
+my $new;
+my $new_pkey = '';
+foreach my $value ( @values ) {
+
+ if ($opt{'skip_process'}) {
+
+ $new = $old;
+ $new_pkey = $old_pkey;
+
+ } else {
+
+ $new = $class->new( \%hash );
+
+ $new->$bfield($value) if $bfield;
+
+ if ($old && exists($opt{'copy_on_empty'})) {
+ foreach my $field (@{$opt{'copy_on_empty'}}) {
+ $new->set($field, $old->get($field))
+ unless scalar($cgi->param($field));
+ }
+ }
+
+ if ( $opt{'post_new_object_callback'} ) {
+ &{ $opt{'post_new_object_callback'} }( $cgi, $new );
+ }
+
+ 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'});
+
+ }
+
+ }
+
+ my @args = ();
+ if ( !$error && $opt{'args_callback'} ) {
+ @args = &{ $opt{'args_callback'} }( $cgi, $new );
+ }
+
+ if ( !$error && $opt{'debug'} ) {
+ warn "$me updating record in $table table using $class class\n";
+ warn Dumper(\%hash);
+ warn "with args: \n". Dumper(\@args) if @args;
+ }
+
+ if ( !$error ) {
+ if ( $old_pkey ) {
+
+ &{ $opt{'edit_callback'} }( $new, $old ) if $opt{'edit_callback'};
+
+ $error = $new->replace($old, @args);
+ } else {
+ $error = $new->insert(@args);
+ }
+ $new_pkey = $new->getfield($pkey);
+ }
+
+ } #unless $opt{'skip_process'}
+
+ if ( !$error && $opt{'process_m2m'} ) {
+
+ 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 );
+ }
+
+ }
+
+ if ( !$error && $opt{'process_m2name'} ) {
+
+ 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),
+ );
+ }
+
+ }
+
+ my @process_o2m;
+ if ( $opt{'process_o2m'} ) {
+ @process_o2m = ref($opt{'process_o2m'}) eq 'ARRAY'
+ ? @{ $opt{'process_o2m'} }
+ : ( $opt{'process_o2m'} );
+ }
+ if ( $opt{'process_locale'} ) {
+ push @process_o2m,
+ {
+ 'table' => $table . '_msgcat',
+ 'fields' => [ 'locale', $opt{'process_locale'} ],
+ };
+ }
+
+ if ( !$error ) {
+
+ 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),
+ );
+ }
+
+ }
+
+
+ if ( !$error and $opt{'process_upload'} ) {
+ # mostly duplicates misc/file-upload.html
+ $cgi->param('upload_fields') =~ /^([,\w]+)$/
+ or $error = "invalid upload_fields";
+ my $fields = $1;
+
+ my $dir = $FS::UID::cache_dir. "/cache.". $FS::UID::datasrc;
+
+ foreach my $field (split /,/, $fields) {
+ next if $error;
+ my $source_name = $cgi->param($field);
+
+ if ( $opt{'debug'} ) {
+ warn "$me processing upload: $source_name\n";
+ }
+ my $fh = $cgi->upload( $field ); # if no file provided, that's fine
+ next if !$fh;
+
+ my $suffix = '';
+ if ( $source_name =~ /(\.\w+)$/ ) {
+ $suffix = lc($1);
+ }
+
+ my $sh = File::Temp->new( TEMPLATE => 'upload.XXXXXXXX',
+ SUFFIX => $suffix,
+ DIR => $dir,
+ UNLINK => 0,
+ )
+ or $error ||= "can't open temporary file to store upload: $!\n";
+ next if $error;
+ while(<$fh>) {
+ print $sh $_;
+ }
+ $sh->filename =~ m!.*/([.\w]+)$!;
+ push @uploaded_files, "$field:$1";
+ if ( $opt{'debug'} ) {
+ warn "uploaded as $1\n";
+ }
+ }
+ }
+
+ 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 );
+ }
+
+ }
+
+ last if $error;
+
+}
+
+# set up redirect URLs
+
+my $redirect;
+if ( !$error ) {
+ $opt{'redirect'} = &{$opt{'redirect'}}($cgi, $new)
+ if ref($opt{'redirect'}) eq 'CODE';
+
+ if ( $opt{'redirect'} ) {
+
+ $redirect = $opt{'redirect'} . $new_pkey;
+
+ } else {
+
+ my $ext = $opt{'viewall_ext'} || 'html';
+ my $viewall_dir = $opt{'viewall_dir'} || 'search';
+ my $viewall_url = $opt{'viewall_url'} || ($viewall_dir . "/$table.$ext");
+
+ $redirect = popurl(3) . $viewall_url;
+
+ }
+}