5 <& elements/process.html,
11 'table' => 'tablename',
13 #? 'primary_key' => #required when the dbdef doesn't know...???
20 'viewall_dir' => '', #'search' or 'browse', defaults to 'search'
21 'viewall_ext' => 'html', #'cgi' or 'html', defaults to 'html'
23 'redirect' => 'view/table.cgi?', # value of primary key is appended
24 # (string or coderef returning a string)
26 'popup_reload' => 'Momentary success message', #will reload parent window
28 'error_redirect' => popurl(2).'edit/table.cgi?', #query string appended
30 'edit_ext' => 'html', #defaults to 'html', you might want 'cgi' while the
31 #naming is still inconsistent
33 'copy_on_empty' => [ 'old_field_name', 'another_old_field', ... ],
35 'clear_on_error' => [ 'form_field1', 'form_field2', ... ],
37 #pass an arrayref of hashrefs for multiple m2ms or m2names
38 #be certain you incorporate m2m_Common if you see error: param
40 'process_m2m' => { 'link_table' => 'link_table_name',
41 'target_table' => 'target_table_name',
42 #optional (see m2m_Common::process_m2m), if not specified
43 # all CGI params will be passed)
46 'process_m2name' => { 'link_table' => 'link_table_name',
47 'link_static' => { 'column' => 'value' },
48 'num_col' => 'column', #if column name is different in
49 #link_table than source_table
50 'name_col' => 'name_column',
51 'names_list' => [ 'list', 'names' ],
53 'param_style' => 'link_table.value checkboxes',
55 'param_style' => 'name_colN values',
59 'process_o2m' => { 'table' => table_name',
60 'num_col' => 'column', #if column name is different in
61 #link_table than source_table
62 'fields' => [qw( fieldname fieldname2 )],
65 'process_locale' => 'fieldname', # update entries in the _msgcat table
68 'process' => 'misc/mytable-import.html',
69 # fields to pass to the back end job, besides the
70 # primary key of the object
71 'fields' => [qw( fieldname fieldname2 )],
74 'skip_process' => 0, #boolean, if set true, will skip the main table
75 #add/edit processing and only run any linked table
78 #checks CGI params and whatever else before much else runs
79 #return an error string or empty for no error
80 'precheck_callback' => sub { my( $cgi ) = @_; },
82 #after the new object is created
83 #return an error string or empty for no error
84 'post_new_object_callback' => sub { my( $cgi, $object ) = @_; },
86 #run right before replacing (not run for inserts)
87 'edit_callback' => sub { my( $new, $old ) = @_; },
89 #after everything's inserted
90 'noerror_callback' => sub { my( $cgi, $object ) = @_; },
92 #supplies arguments to insert() and replace()
93 # for use with tables that are FS::option_Common (among other things)
94 'args_callback' => sub { my( $cgi, $object ) = @_; },
96 # if no errors after package insert or replace will update services attached to package.
97 'update_svc' => sub { my( $cgi, $object ) = @_; },
99 'debug' => 1, #turns on debugging output
101 #agent virtualization
103 'agent_null_right' => 'Access Right Name',
105 #special bulk insert mode
113 % #my $query = $m->scomp('/elements/create_uri_query');
114 % #$cgi->redirect("$url?$query");
115 % if ( length($cgi->query_string) > 1920 ) { #stupid IE 2083 URL limit
117 % my $session = int(rand(4294967296)); #XXX
118 % my $pref = new FS::access_user_pref({
119 % 'usernum' => $FS::CurrentUser::CurrentUser->usernum,
120 % 'prefname' => "redirect$session",
121 % 'prefvalue' => $cgi->query_string,
122 % 'expiration' => time + 3600, #1h? 1m?
124 % my $pref_error = $pref->insert;
125 % if ( $pref_error ) {
126 % die "FATAL: couldn't even set redirect cookie: $pref_error".
127 % " attempting to set redirect$session to ". $cgi->query_string."\n";
130 <% $cgi->redirect("$error_redirect?redirect=$session") %>
134 <% $cgi->redirect("$error_redirect?". $cgi->query_string ) %>
138 % #different ways of handling success
140 %} elsif ( $opt{'process_upload'} and @uploaded_files ) {
142 % # construct a form to pass all the requested fields, the
144 <& /elements/header.html &>
145 <form name="UploadForm">
146 % my $job_fields = $opt{'process_upload'}{'fields'};
147 % foreach my $field ( @$job_fields ) {
148 <input type="hidden" name="<% $field %>" value="<% $cgi->param($field) |h %>">
150 % push @$job_fields, 'uploaded_files', $pkey;
152 <input type="hidden" name="uploaded_files" value="<% join(',', @uploaded_files) %>">
153 <input type="hidden" name="<% $pkey %>" value="<% $new->get($pkey) %>">
154 <& /elements/progress-init.html,
157 $fsurl . $opt{'process_upload'}{'process'},
160 error_url => "$error_redirect?$new_pkey"
163 <input type="submit" style="display:none">
166 <&| /elements/onload.js &>
170 <& /elements/footer.html &>
172 % } elsif ( $opt{'progress_init'} ) {
173 % # some false laziness with the above
174 % my ($form_name, $job_fields) = @{ $opt{'progress_init'} };
175 <form name="<% $form_name %>">
176 <input type="hidden" name="<% $pkey %>" value="<% $new->get($pkey) %>">
177 % foreach my $field (@$job_fields) {
178 % next if $field eq $pkey;
179 <input type="hidden" name="<% $field %>" value="<% $cgi->param($field) |h %>">
181 <& /elements/progress-init.html,
182 @{ $opt{'progress_init'} }
184 <input type="submit" style="display:none">
187 <&| /elements/onload.js &>
191 <& /elements/footer.html &>
193 % } elsif ( $opt{'popup_reload'} ) {
195 <% include('/elements/header-popup.html', $opt{'popup_reload'} ) %>
197 <SCRIPT TYPE="text/javascript">
205 <% $cgi->redirect($redirect) %>
209 my $me = 'process.html:';
213 my $curuser = $FS::CurrentUser::CurrentUser;
216 if ( $opt{'precheck_callback'} ) {
217 $error = &{ $opt{'precheck_callback'} }( $cgi );
220 #false laziness w/edit.html
221 my $table = $opt{'table'};
222 my $class = "FS::$table";
223 my $pkey = dbdef->table($table)->primary_key; #? $opt{'primary_key'} ||
224 my $fields = $opt{'fields'}
225 #|| [ grep { $_ ne $pkey } dbdef->table($table)->columns ];
226 || [ fields($table) ];
228 my $old_pkey = $cgi->param($pkey);
234 'hashref' => { $pkey => $old_pkey },
235 'extra_sql' => ( $opt{'agent_virt'}
236 ? ' AND '. $curuser->agentnums_sql(
237 'null_right' => $opt{'agent_null_right'}
244 my $bfield = $opt{'bulk'};
247 map { my @entry = ( $_ => scalar($cgi->param($_)) );
248 $opt{'value_callback'} ? ( $_ => &{ $opt{'value_callback'} }( @entry ))
250 } grep { $_ ne $bfield } @$fields;
254 @values = $cgi->param($bfield);
255 #warn join(',', @values);
262 foreach my $value ( @values ) {
264 if ($opt{'skip_process'}) {
267 $new_pkey = $old_pkey;
271 $new = $class->new( \%hash );
273 $new->$bfield($value) if $bfield;
275 if ($old && exists($opt{'copy_on_empty'})) {
276 foreach my $field (@{$opt{'copy_on_empty'}}) {
277 $new->set($field, $old->get($field))
278 unless scalar($cgi->param($field));
282 if ( $opt{'post_new_object_callback'} ) {
283 $error ||= &{ $opt{'post_new_object_callback'} }( $cgi, $new );
286 if ( $opt{'agent_virt'} ) {
288 if ( ! $new->agentnum
289 && ( ! $opt{'agent_null_right'}
290 || ! $curuser->access_right($opt{'agent_null_right'})
295 $error ||= 'Select an agent';
299 die "illegal agentnum"
300 unless $curuser->agentnums_href->{$new->agentnum}
301 or $curuser->access_right('View customers of all agents')
302 or $opt{'agent_null_right'}
304 && $curuser->access_right($opt{'agent_null_right'});
311 if ( !$error && $opt{'args_callback'} ) {
312 @args = &{ $opt{'args_callback'} }( $cgi, $new );
315 if ( !$error && $opt{'debug'} ) {
316 warn "$me updating record in $table table using $class class\n";
318 warn "with args: \n". Dumper(\@args) if @args;
324 &{ $opt{'edit_callback'} }( $new, $old ) if $opt{'edit_callback'};
326 $error = $new->replace($old, @args);
328 $error = $new->insert(@args);
330 $new_pkey = $new->getfield($pkey);
333 } #unless $opt{'skip_process'}
335 if ( !$error && $opt{'process_m2m'} ) {
337 my @process_m2m = ref($opt{'process_m2m'}) eq 'ARRAY'
338 ? @{ $opt{'process_m2m'} }
339 : ( $opt{'process_m2m'} );
341 foreach my $process_m2m (@process_m2m) {
343 $process_m2m->{'params'} ||= scalar($cgi->Vars);
345 warn "$me processing m2m:\n". Dumper( %$process_m2m )
348 $error ||= $new->process_m2m( %$process_m2m );
353 if ( !$error && $opt{'process_m2name'} ) {
355 my @process_m2name = ref($opt{'process_m2name'}) eq 'ARRAY'
356 ? @{ $opt{'process_m2name'} }
357 : ( $opt{'process_m2name'} );
360 foreach my $process_m2name (@process_m2name) {
362 if ( $opt{'debug'} ) {
363 warn "$me processing m2name:\n". Dumper( %{ $process_m2name },
364 'params' => scalar($cgi->Vars),
368 $error = $new->process_m2name( %{ $process_m2name },
369 'params' => scalar($cgi->Vars),
376 if ( $opt{'process_o2m'} ) {
377 @process_o2m = ref($opt{'process_o2m'}) eq 'ARRAY'
378 ? @{ $opt{'process_o2m'} }
379 : ( $opt{'process_o2m'} );
381 if ( $opt{'process_locale'} ) {
384 'table' => $table . '_msgcat',
385 'fields' => [ 'locale', $opt{'process_locale'} ],
391 foreach my $process_o2m (@process_o2m) {
393 if ( $opt{'debug'} ) {
394 warn "$me processing o2m:\n". Dumper( %{ $process_o2m },
395 'params' => scalar($cgi->Vars),
399 $error ||= $new->process_o2m( %{ $process_o2m },
400 'params' => scalar($cgi->Vars),
407 if ( !$error and $opt{'process_upload'} ) {
408 # mostly duplicates misc/file-upload.html
409 $cgi->param('upload_fields') =~ /^([,\w]+)$/
410 or $error = "invalid upload_fields";
413 my $dir = $FS::UID::cache_dir. "/cache.". $FS::UID::datasrc;
415 foreach my $field (split /,/, $fields) {
417 my $source_name = $cgi->param($field);
419 if ( $opt{'debug'} ) {
420 warn "$me processing upload: $source_name\n";
422 my $fh = $cgi->upload( $field ); # if no file provided, that's fine
426 if ( $source_name =~ /(\.\w+)$/ ) {
430 my $sh = File::Temp->new( TEMPLATE => 'upload.XXXXXXXX',
435 or $error ||= "can't open temporary file to store upload: $!\n";
440 $sh->filename =~ m!.*/([.\w]+)$!;
441 push @uploaded_files, "$field:$1";
442 if ( $opt{'debug'} ) {
443 warn "uploaded as $1\n";
448 if ( !$error and $opt{'update_svc'} ) {
450 @args = &{ $opt{'args_callback'} }( $cgi, $new ) if $opt{'args_callback'};
451 $error = &{ $opt{'update_svc'} }( $cgi, $new, @args );
456 $cgi->param('error', $error);
457 if ( $opt{'clear_on_error'} && scalar(@{$opt{'clear_on_error'}}) ) {
458 foreach my $field (@{$opt{'clear_on_error'}}) {
459 $cgi->param($field, '')
465 if ( $opt{'noerror_callback'} ) {
466 &{ $opt{'noerror_callback'} }( $cgi, $new );
475 if ($class eq "FS::tower") {
476 foreach my $part_svc_broadband_export ( FS::tower_sector->part_export_svc_broadband ) {
477 if ($part_svc_broadband_export and $part_svc_broadband_export->can('export_tower_sector')) {
478 $error = $part_svc_broadband_export->export_tower_sector($new);
483 # set up redirect URLs
487 $opt{'redirect'} = &{$opt{'redirect'}}($cgi, $new)
488 if ref($opt{'redirect'}) eq 'CODE';
490 if ( $opt{'redirect'} ) {
492 $redirect = $opt{'redirect'} . $new_pkey;
496 my $ext = $opt{'viewall_ext'} || 'html';
497 my $viewall_dir = $opt{'viewall_dir'} || 'search';
498 my $viewall_url = $opt{'viewall_url'} || ($viewall_dir . "/$table.$ext");
500 $redirect = popurl(3) . $viewall_url;
505 my $edit_ext = $opt{'edit_ext'} || 'html';
506 my $error_redirect = $opt{'error_redirect'}
507 || popurl(2)."$table.$edit_ext";