-<%
-
- # options example...
- #
- ###
- ##req
- ##
- #
- # 'table' =>
- #
- # #? 'primary_key' => #required when the dbdef doesn't know...???
- # #? 'fields' => []
- #
- ###
- ##opt
- ###
- #
- # 'viewall_dir' => '', #'search' or 'browse', defaults to 'search'
- # OR
- # 'redirect' => 'view/table.cgi?', # value of primary key is appended
- #
- # 'edit_ext' => 'html', #defaults to 'html', you might want 'cgi' while the
- # #naming is still inconsistent
- #
- # 'process_m2m' => { 'link_table' => 'link_table_name',
- # 'target_table' => 'target_table_name',
- # },
- # 'process_m2name' => { 'link_table' => 'link_table_name',
- # 'link_static' => { 'column' => 'value' },
- # 'num_col' => 'column', #if column name is different in
- # #link_table than source_table
- # 'name_col' => 'name_column',
- # 'names_list' => [ 'list', 'names' ],
- # },
-
- my(%opt) = @_;
-
- #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 $pkeyvalue = $cgi->param($pkey);
-
- my $old = qsearchs( $table, { $pkey => $pkeyvalue } ) if $pkeyvalue;
-
- my $new = $class->new( {
- map {
- $_, scalar($cgi->param($_));
- } @$fields
- } );
-
- my $error;
- if ( $pkeyvalue ) {
- $error = $new->replace($old);
+<%doc>
+
+Example:
+
+ <& elements/process.html,
+
+ ###
+ # required
+ ###
+
+ 'table' => 'tablename',
+
+ #? 'primary_key' => #required when the dbdef doesn't know...???
+ #? 'fields' => [] #""
+
+ ###
+ # optional
+ ###
+
+ '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
+
+ 'error_redirect' => popurl(2).'edit/table.cgi?', #query string appended
+
+ 'edit_ext' => 'html', #defaults to 'html', you might want 'cgi' while the
+ #naming is still inconsistent
+
+ 'copy_on_empty' => [ 'old_field_name', 'another_old_field', ... ],
+
+ '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' },
+ 'num_col' => 'column', #if column name is different in
+ #link_table than source_table
+ 'name_col' => 'name_column',
+ 'names_list' => [ 'list', 'names' ],
+
+ 'param_style' => 'link_table.value checkboxes',
+ #or#
+ 'param_style' => 'name_colN values',
+
+
+ },
+ 'process_o2m' => { 'table' => table_name',
+ 'num_col' => 'column', #if column name is different in
+ #link_table than source_table
+ 'fields' => [qw( fieldname fieldname2 )],
+ },
+
+ 'process_locale' => 'fieldname', # update entries in the _msgcat table
+
+ 'process_upload' => {
+ 'process' => 'misc/mytable-import.html',
+ # fields to pass to the back end job, besides the
+ # primary key of the object
+ 'fields' => [qw( fieldname fieldname2 )],
+ },
+
+ 'skip_process' => 0, #boolean, if set true, will skip the main table
+ #add/edit processing and only run any linked table
+ #process_ items
+
+ #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 the new object is created
+ 'post_new_object_callback' => sub { my( $cgi, $object ) = @_; },
+
+ #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 (among other things)
+ 'args_callback' => sub { my( $cgi, $object ) = @_; },
+
+ 'debug' => 1, #turns on debugging output
+
+ #agent virtualization
+ 'agent_virt' => 1,
+ 'agent_null_right' => 'Access Right Name',
+
+ #special bulk insert mode
+ 'bulk' => 'field',
+
+ &>
+
+</%doc>
+%if ( $error ) {
+%
+% #my $query = $m->scomp('/elements/create_uri_query');
+% #$cgi->redirect("$url?$query");
+% 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("$error_redirect?redirect=$session") %>
+%
+% } else {
+%
+<% $cgi->redirect("$error_redirect?". $cgi->query_string ) %>
+%
+% }
+%
+% #different ways of handling success
+%
+%} elsif ( $opt{'process_upload'} and @uploaded_files ) {
+%
+% # construct a form to pass all the requested fields, the
+%
+<& /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;
+