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