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