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