NG auth: pw changes, RT#21563
[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                       'fields' => [qw( fieldname fieldname2 )],
63                     },
64
65    'skip_process' => 0, #boolean, if set true, will skip the main table
66                         #add/edit processing and only run any linked table
67                         #process_ items
68
69    #checks CGI params and whatever else before much else runs
70    #return an error string or empty for no error
71    'precheck_callback' => sub { my( $cgi ) = @_; },
72
73    #after the new object is created
74    'post_new_object_callback' => sub { my( $cgi, $object ) = @_; },
75
76    #after everything's inserted
77    'noerror_callback' => sub { my( $cgi, $object ) = @_; },
78
79    #supplies arguments to insert() and replace()
80    # for use with tables that are FS::option_Common (among other things)
81    'args_callback' => sub { my( $cgi, $object ) = @_; },
82
83    'debug' => 1, #turns on debugging output
84
85    #agent virtualization
86    'agent_virt'       => 1,
87    'agent_null_right' => 'Access Right Name',
88
89    #special bulk insert mode
90    'bulk' => 'field',
91
92  &>
93
94 </%doc>
95 %if ( $error ) {
96 %
97 %  my $edit_ext = $opt{'edit_ext'} || 'html';
98 %  my $url = $opt{'error_redirect'} || popurl(2)."$table.$edit_ext";
99
100 %  #my $query = $m->scomp('/elements/create_uri_query');
101 %  #$cgi->redirect("$url?$query");
102 %  if ( length($cgi->query_string) > 1920 ) { #stupid IE 2083 URL limit
103
104 %    my $session = int(rand(4294967296)); #XXX
105 %    my $pref = new FS::access_user_pref({
106 %      'usernum'    => $FS::CurrentUser::CurrentUser->usernum,
107 %      'prefname'   => "redirect$session",
108 %      'prefvalue'  => $cgi->query_string,
109 %      'expiration' => time + 3600, #1h?  1m?
110 %    });
111 %    my $pref_error = $pref->insert;
112 %    if ( $pref_error ) {
113 %      die "FATAL: couldn't even set redirect cookie: $pref_error".
114 %          " attempting to set redirect$session to ". $cgi->query_string."\n";
115 %    }
116 %
117 <% $cgi->redirect("$url?redirect=$session") %>
118 %
119 %  } else {
120 %
121 <% $cgi->redirect("$url?". $cgi->query_string ) %>
122 %
123 %  } 
124 %
125 % #different ways of handling success
126 %
127 %} elsif ( $opt{'popup_reload'} ) {
128
129   <% include('/elements/header-popup.html', $opt{'popup_reload'} ) %>
130
131   <SCRIPT TYPE="text/javascript">
132     window.top.location.reload();
133   </SCRIPT>
134
135   </BODY>
136   </HTML>
137
138 %} else {
139 %  
140 %  $opt{'redirect'} = &{$opt{'redirect'}}($cgi, $new)
141 %    if ref($opt{'redirect'}) eq 'CODE';
142 %
143 %  if ( $opt{'redirect'} ) {
144 %
145 <% $cgi->redirect( $opt{'redirect'}. $new_pkey) %>
146 %
147 %  } else { 
148 %
149 %    my $ext = $opt{'viewall_ext'} || 'html';
150 %    my $viewall_dir = $opt{'viewall_dir'} || 'search';
151 %    my $viewall_url = $opt{'viewall_url'} || ($viewall_dir . "/$table.$ext");
152 %
153 %#<% $cgi->redirect( popurl(3). ($opt{viewall_dir}||'search'). "/$table.$ext" ) %>
154 <% $cgi->redirect( popurl(3) . $viewall_url ) %>
155 %  }
156 %
157 %}
158 %
159 <%init>
160
161 my $me = 'process.html:';
162
163 my(%opt) = @_;
164
165 my $curuser = $FS::CurrentUser::CurrentUser;
166
167 my $error = '';
168 if ( $opt{'precheck_callback'} ) {
169   $error = &{ $opt{'precheck_callback'} }( $cgi );
170 }
171
172 #false laziness w/edit.html
173 my $table = $opt{'table'};
174 my $class = "FS::$table";
175 my $pkey = dbdef->table($table)->primary_key; #? $opt{'primary_key'} || 
176 my $fields = $opt{'fields'}
177              #|| [ grep { $_ ne $pkey } dbdef->table($table)->columns ];
178              || [ fields($table) ];
179
180 my $old_pkey = $cgi->param($pkey);
181
182 my $old = '';
183 if ( $old_pkey ) {
184   $old = qsearchs({
185     'table'   => $table,
186     'hashref' => { $pkey => $old_pkey },
187     'extra_sql' => ( $opt{'agent_virt'}
188                        ? ' AND '. $curuser->agentnums_sql(
189                                     'null_right' => $opt{'agent_null_right'}
190                                   )
191                        : ''
192                    ),
193   });
194 }
195
196 my $bfield = $opt{'bulk'};
197
198 my %hash =
199   map { my @entry = ( $_ => scalar($cgi->param($_)) );
200         $opt{'value_callback'} ? ( $_ => &{ $opt{'value_callback'} }( @entry ))
201                                : ( @entry )
202       } grep { $_ ne $bfield } @$fields;
203
204 my @values = ( 1 );
205 if ( $bfield ) {
206   @values = $cgi->param($bfield);
207   #warn join(',', @values);
208 }
209
210 my $new;
211 my $new_pkey = '';
212 foreach my $value ( @values ) {
213
214   if ($opt{'skip_process'}) {
215
216     $new = $old;
217     $new_pkey = $old_pkey;
218
219   } else {
220
221     $new = $class->new( \%hash );
222
223     $new->$bfield($value) if $bfield;
224
225     if ($old && exists($opt{'copy_on_empty'})) {
226       foreach my $field (@{$opt{'copy_on_empty'}}) {
227         $new->set($field, $old->get($field))
228           unless scalar($cgi->param($field));
229       }
230     }
231
232     if ( $opt{'post_new_object_callback'} ) {
233       &{ $opt{'post_new_object_callback'} }( $cgi, $new );
234     }
235
236     if ( $opt{'agent_virt'} ) {
237
238       if ( ! $new->agentnum
239            && (    ! $opt{'agent_null_right'}
240                 || ! $curuser->access_right($opt{'agent_null_right'})
241               )
242          )
243       {
244
245         $error ||= 'Select an agent';
246
247       } else {
248
249         die "illegal agentnum"
250           unless $curuser->agentnums_href->{$new->agentnum}
251               or $curuser->access_right('View customers of all agents')
252               or $opt{'agent_null_right'}
253                  && ! $new->agentnum
254                  && $curuser->access_right($opt{'agent_null_right'});
255
256       }
257
258     }
259
260     my @args = ();
261     if ( !$error && $opt{'args_callback'} ) {
262       @args = &{ $opt{'args_callback'} }( $cgi, $new );
263     }
264
265     if ( !$error && $opt{'debug'} ) {
266       warn "$me updating record in $table table using $class class\n";
267       warn Dumper(\%hash);
268       warn "with args: \n". Dumper(\@args) if @args;
269     }
270
271     if ( !$error ) {
272       if ( $old_pkey ) {
273
274         &{ $opt{'edit_callback'} }( $new, $old ) if $opt{'edit_callback'};
275
276         $error = $new->replace($old, @args);
277       } else {
278         $error = $new->insert(@args);
279       }
280       $new_pkey = $new->getfield($pkey);
281     }
282
283   } #unless $opt{'skip_process'}
284
285   if ( !$error && $opt{'process_m2m'} ) {
286
287     my @process_m2m = ref($opt{'process_m2m'}) eq 'ARRAY'
288                         ? @{ $opt{'process_m2m'} }
289                         :  ( $opt{'process_m2m'} );
290
291     foreach my $process_m2m (@process_m2m) {
292
293       $process_m2m->{'params'} ||= scalar($cgi->Vars);
294
295       warn "$me processing m2m:\n". Dumper( %$process_m2m )
296         if $opt{'debug'};
297
298       $error = $new->process_m2m( %$process_m2m );
299     }
300
301   }
302
303   if ( !$error && $opt{'process_m2name'} ) {
304
305     my @process_m2name = ref($opt{'process_m2name'}) eq 'ARRAY'
306                            ? @{ $opt{'process_m2name'} }
307                            :  ( $opt{'process_m2name'} );
308
309
310     foreach my $process_m2name (@process_m2name) {
311
312       if ( $opt{'debug'} ) {
313         warn "$me processing m2name:\n". Dumper( %{ $process_m2name },
314                                                  'params' => scalar($cgi->Vars),
315                                                );
316       }
317
318       $error = $new->process_m2name( %{ $process_m2name },
319                                      'params' => scalar($cgi->Vars),
320                                    );
321     }
322
323   }
324
325   if ( !$error && $opt{'process_o2m'} ) {
326
327     my @process_o2m = ref($opt{'process_o2m'}) eq 'ARRAY'
328                            ? @{ $opt{'process_o2m'} }
329                            :  ( $opt{'process_o2m'} );
330
331
332     foreach my $process_o2m (@process_o2m) {
333
334       if ( $opt{'debug'} ) {
335         warn "$me processing o2m:\n". Dumper( %{ $process_o2m },
336                                                  'params' => scalar($cgi->Vars),
337                                                );
338       }
339
340       $error = $new->process_o2m( %{ $process_o2m },
341                                      'params' => scalar($cgi->Vars),
342                                    );
343     }
344
345   }
346
347
348   if ( $error ) {
349
350     $cgi->param('error', $error);
351     if ( $opt{'clear_on_error'} && scalar(@{$opt{'clear_on_error'}}) ) {
352       foreach my $field (@{$opt{'clear_on_error'}}) {
353         $cgi->param($field, '')
354       }
355     }
356
357   } else {
358
359     if ( $opt{'noerror_callback'} ) {
360       &{ $opt{'noerror_callback'} }( $cgi, $new );
361     }
362
363   }
364
365   last if $error;
366
367 }
368 </%init>