oops, fix edit redirect fallout from multiple DID provisioning changes, RT#14087
[freeside.git] / httemplate / edit / process / elements / process.html
1 <%doc>
2
3 Example:
4
5  <& elements/process.html,
6
7    ###
8    # required
9    ###
10
11   'table' => 'tablename',
12
13    #? 'primary_key' => #required when the dbdef doesn't know...???
14    #? 'fields' => []   #""
15
16    ###
17    # optional
18    ###
19
20    'viewall_dir'  => '', #'search' or 'browse', defaults to 'search'
21    'viewall_ext'  => 'html', #'cgi' or 'html', defaults to 'html'
22    OR
23    'redirect'     => 'view/table.cgi?', # value of primary key is appended
24                                         # (string or coderef returning a string)
25    OR
26    'popup_reload' => 'Momentary success message', #will reload parent window
27
28    'error_redirect' => popurl(2).'edit/table.cgi?', #query string appended
29
30    'edit_ext' => 'html', #defaults to 'html', you might want 'cgi' while the
31                          #naming is still inconsistent
32
33    'copy_on_empty'  => [ 'old_field_name', 'another_old_field', ... ],
34
35    'clear_on_error' => [ 'form_field1', 'form_field2', ... ],
36
37                   #pass an arrayref of hashrefs for multiple m2ms or m2names
38                   #be certain you incorporate m2m_Common if you see error: param
39
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)
44                       'params'       => 
45                     },
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' ],
52                          
53                          'param_style' => 'link_table.value checkboxes',
54                          #or#
55                          'param_style' => 'name_colN values',
56
57
58                        },
59    'process_o2m' => { 'table' => table_name',
60                       'num_col' => 'column', #if column name is different in
61                                              #link_table than source_table 
62                     },
63
64    #checks CGI params and whatever else before much else runs
65    #return an error string or empty for no error
66    'precheck_callback' => sub { my( $cgi ) = @_; },
67
68    #after everything's inserted
69    'noerror_callback' => sub { my( $cgi, $object ) = @_; },
70
71    #supplies arguments to insert() and replace()
72    # for use with tables that are FS::option_Common (among other things)
73    'args_callback' => sub { my( $cgi, $object ) = @_; },
74
75    'debug' => 1, #turns on debugging output
76
77    #agent virtualization
78    'agent_virt'       => 1,
79    'agent_null_right' => 'Access Right Name',
80
81    #special bulk insert mode
82    'bulk' => 'field',
83
84  &>
85
86 </%doc>
87 %if ( $error ) {
88 %
89 %  my $edit_ext = $opt{'edit_ext'} || 'html';
90 %  my $url = $opt{'error_redirect'} || popurl(2)."$table.$edit_ext";
91
92 %  #my $query = $m->scomp('/elements/create_uri_query');
93 %  #$cgi->redirect("$url?$query");
94 %  if ( length($cgi->query_string) > 1920 ) { #stupid IE 2083 URL limit
95
96 %    my $session = int(rand(4294967296)); #XXX
97 %    my $pref = new FS::access_user_pref({
98 %      'usernum'    => $FS::CurrentUser::CurrentUser->usernum,
99 %      'prefname'   => "redirect$session",
100 %      'prefvalue'  => $cgi->query_string,
101 %      'expiration' => time + 3600, #1h?  1m?
102 %    });
103 %    my $pref_error = $pref->insert;
104 %    if ( $pref_error ) {
105 %      die "FATAL: couldn't even set redirect cookie: $pref_error".
106 %          " attempting to set redirect$session to ". $cgi->query_string."\n";
107 %    }
108 %
109 <% $cgi->redirect("$url?redirect=$session") %>
110 %
111 %  } else {
112 %
113 <% $cgi->redirect("$url?". $cgi->query_string ) %>
114 %
115 %  } 
116 %
117 % #different ways of handling success
118 %
119 %} elsif ( $opt{'popup_reload'} ) {
120
121   <% include('/elements/header-popup.html', $opt{'popup_reload'} ) %>
122
123   <SCRIPT TYPE="text/javascript">
124     window.top.location.reload();
125   </SCRIPT>
126
127   </BODY>
128   </HTML>
129
130 %} else {
131 %  
132 %  $opt{'redirect'} = &{$opt{'redirect'}}($cgi, $new)
133 %    if ref($opt{'redirect'}) eq 'CODE';
134 %
135 %  if ( $opt{'redirect'} ) {
136 %
137 <% $cgi->redirect( $opt{'redirect'}. $new_pkey) %>
138 %
139 %  } else { 
140 %
141 %    my $ext = $opt{'viewall_ext'} || 'html';
142 %    my $viewall_dir = $opt{'viewall_dir'} || 'search';
143 %    my $viewall_url = $opt{'viewall_url'} || ($viewall_dir . "/$table.$ext");
144 %
145 %#<% $cgi->redirect( popurl(3). ($opt{viewall_dir}||'search'). "/$table.$ext" ) %>
146 <% $cgi->redirect( popurl(3) . $viewall_url ) %>
147 %  }
148 %
149 %}
150 %
151 <%init>
152
153 my $me = 'process.html:';
154
155 my(%opt) = @_;
156
157 my $curuser = $FS::CurrentUser::CurrentUser;
158
159 my $error = '';
160 if ( $opt{'precheck_callback'} ) {
161   $error = &{ $opt{'precheck_callback'} }( $cgi );
162 }
163
164 #false laziness w/edit.html
165 my $table = $opt{'table'};
166 my $class = "FS::$table";
167 my $pkey = dbdef->table($table)->primary_key; #? $opt{'primary_key'} || 
168 my $fields = $opt{'fields'}
169              #|| [ grep { $_ ne $pkey } dbdef->table($table)->columns ];
170              || [ fields($table) ];
171
172 my $old_pkey = $cgi->param($pkey);
173
174 my $old = '';
175 if ( $old_pkey ) {
176   $old = qsearchs({
177     'table'   => $table,
178     'hashref' => { $pkey => $old_pkey },
179     'extra_sql' => ( $opt{'agent_virt'}
180                        ? ' AND '. $curuser->agentnums_sql(
181                                     'null_right' => $opt{'agent_null_right'}
182                                   )
183                        : ''
184                    ),
185   });
186 }
187
188 my $bfield = $opt{'bulk'};
189
190 my %hash =
191   map { my @entry = ( $_ => scalar($cgi->param($_)) );
192         $opt{'value_callback'} ? ( $_ => &{ $opt{'value_callback'} }( @entry ))
193                                : ( @entry )
194       } grep { $_ ne $bfield } @$fields;
195
196 my @values = ( 1 );
197 if ( $bfield ) {
198   @values = $cgi->param($bfield);
199   warn join(',', @values);
200 }
201
202 my $new;
203 my $new_pkey = '';
204 foreach my $value ( @values ) {
205
206   $new = $class->new( \%hash );
207
208   $new->$bfield($value) if $bfield;
209
210   if ($old && exists($opt{'copy_on_empty'})) {
211     foreach my $field (@{$opt{'copy_on_empty'}}) {
212       $new->set($field, $old->get($field))
213         unless scalar($cgi->param($field));
214     }
215   }
216
217   if ( $opt{'agent_virt'} ) {
218
219     if ( ! $new->agentnum
220          && (    ! $opt{'agent_null_right'}
221               || ! $curuser->access_right($opt{'agent_null_right'})
222             )
223        )
224     {
225
226       $error ||= 'Select an agent';
227
228     } else {
229
230       die "illegal agentnum"
231         unless $curuser->agentnums_href->{$new->agentnum}
232             or $curuser->access_right('View customers of all agents')
233             or $opt{'agent_null_right'}
234                && ! $new->agentnum
235                && $curuser->access_right($opt{'agent_null_right'});
236
237     }
238
239   }
240
241   $error ||= $new->check;
242
243   my @args = ();
244   if ( !$error && $opt{'args_callback'} ) {
245     @args = &{ $opt{'args_callback'} }( $cgi, $new );
246   }
247
248   if ( !$error && $opt{'debug'} ) {
249     warn "$me updating record in $table table using $class class\n";
250     warn Dumper(\%hash);
251     warn "with args: \n". Dumper(\@args) if @args;
252   }
253
254   if ( !$error ) {
255     if ( $old_pkey ) {
256       $error = $new->replace($old, @args);
257     } else {
258       $error = $new->insert(@args);
259     }
260     $new_pkey = $new->getfield($pkey);
261   }
262
263   if ( !$error && $opt{'process_m2m'} ) {
264
265     my @process_m2m = ref($opt{'process_m2m'}) eq 'ARRAY'
266                         ? @{ $opt{'process_m2m'} }
267                         :  ( $opt{'process_m2m'} );
268
269     foreach my $process_m2m (@process_m2m) {
270
271       $process_m2m->{'params'} ||= scalar($cgi->Vars);
272
273       warn "$me processing m2m:\n". Dumper( %$process_m2m )
274         if $opt{'debug'};
275
276       $error = $new->process_m2m( %$process_m2m );
277     }
278
279   }
280
281   if ( !$error && $opt{'process_m2name'} ) {
282
283     my @process_m2name = ref($opt{'process_m2name'}) eq 'ARRAY'
284                            ? @{ $opt{'process_m2name'} }
285                            :  ( $opt{'process_m2name'} );
286
287
288     foreach my $process_m2name (@process_m2name) {
289
290       if ( $opt{'debug'} ) {
291         warn "$me processing m2name:\n". Dumper( %{ $process_m2name },
292                                                  'params' => scalar($cgi->Vars),
293                                                );
294       }
295
296       $error = $new->process_m2name( %{ $process_m2name },
297                                      'params' => scalar($cgi->Vars),
298                                    );
299     }
300
301   }
302
303   if ( !$error && $opt{'process_o2m'} ) {
304
305     my @process_o2m = ref($opt{'process_o2m'}) eq 'ARRAY'
306                            ? @{ $opt{'process_o2m'} }
307                            :  ( $opt{'process_o2m'} );
308
309
310     foreach my $process_o2m (@process_o2m) {
311
312       if ( $opt{'debug'} ) {
313         warn "$me processing o2m:\n". Dumper( %{ $process_o2m },
314                                                  'params' => scalar($cgi->Vars),
315                                                );
316       }
317
318       $error = $new->process_o2m( %{ $process_o2m },
319                                      'params' => scalar($cgi->Vars),
320                                    );
321     }
322
323   }
324
325
326   if ( $error ) {
327
328     $cgi->param('error', $error);
329     if ( $opt{'clear_on_error'} && scalar(@{$opt{'clear_on_error'}}) ) {
330       foreach my $field (@{$opt{'clear_on_error'}}) {
331         $cgi->param($field, '')
332       }
333     }
334
335   } else {
336
337     if ( $opt{'noerror_callback'} ) {
338       &{ $opt{'noerror_callback'} }( $cgi, $new );
339     }
340
341   }
342
343   last if $error;
344
345 }
346
347 </%init>