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