7964dfa82793d9d93bac493a9b7e8474b30e0aa8
[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    OR
22    'redirect'     => 'view/table.cgi?', # value of primary key is appended
23    OR
24    'popup_reload' => 'Momentary success message', #will reload parent window
25
26    'error_redirect' => popurl(2).'edit/table.cgi?', #query string appended
27
28    'edit_ext' => 'html', #defaults to 'html', you might want 'cgi' while the
29                          #naming is still inconsistent
30
31    'copy_on_empty'  => [ 'old_field_name', 'another_old_field', ... ],
32
33    'clear_on_error' => [ 'form_field1', 'form_field2', ... ],
34
35                     #pass an arrayref of hashrefs for multiple m2ms or m2names
36
37    'process_m2m' => { 'link_table'   => 'link_table_name',
38                       'target_table' => 'target_table_name',
39                       #optional (see m2m_Common::process_m2m), if not specified
40                       # all CGI params will be passed)
41                       'params'       => 
42                     },
43    'process_m2name' => { 'link_table'   => 'link_table_name',
44                          'link_static' => { 'column' => 'value' },
45                          'num_col' => 'column', #if column name is different in
46                                                 #link_table than source_table 
47                          'name_col' => 'name_column',
48                          'names_list' => [ 'list', 'names' ],
49                          
50                          'param_style' => 'link_table.value checkboxes',
51                          #or#
52                          'param_style' => 'name_colN values',
53
54
55                        },
56
57    #checks CGI params and whatever else before much else runs
58    #return an error string or empty for no error
59    'precheck_callback' => sub { my( $cgi ) = @_; },
60
61    #supplies arguments to insert() and replace()
62    # for use with tables that are FS::option_Common
63    'args_callback' => sub { my( $cgi, $object ) = @_; },
64
65    'debug' => 1, #turns on debugging output
66
67    #agent virtualization
68    'agent_virt'       => 1,
69    'agent_null_right' => 'Access Right Name',
70
71  )
72
73 </%doc>
74 %if ( $error ) {
75 %
76 %  my $edit_ext = $opt{'edit_ext'} || 'html';
77 %  my $url = $opt{'error_redirect'} || popurl(2)."$table.$edit_ext";
78 %  if ( length($cgi->query_string) > 1920 ) { #stupid IE 2083 URL limit
79
80 %    my $session = int(rand(4294967296)); #XXX
81 %    my $pref = new FS::access_user_pref({
82 %      'usernum'    => $FS::CurrentUser::CurrentUser->usernum,
83 %      'prefname'   => "redirect$session",
84 %      'prefvalue'  => $cgi->query_string,
85 %      'expiration' => time + 3600, #1h?  1m?
86 %    });
87 %    my $pref_error = $pref->insert;
88 %    if ( $pref_error ) {
89 %      die "FATAL: couldn't even set redirect cookie: $pref_error".
90 %          " attempting to set redirect$session to ". $cgi->query_string."\n";
91 %    }
92 %
93 <% $cgi->redirect("$url?redirect=$session") %>
94 %
95 %  } else {
96 %
97 <% $cgi->redirect("$url?". $cgi->query_string ) %>
98 %
99 %  } 
100 %
101 % #different ways of handling success
102 %
103 %} elsif ( $opt{'popup_reload'} ) {
104
105   <% include('/elements/header-popup.html', $opt{'popup_reload'} ) %>
106
107   <SCRIPT TYPE="text/javascript">
108     window.top.location.reload();
109   </SCRIPT>
110
111   </BODY>
112   </HTML>
113
114 %} elsif ( $opt{'redirect'} ) {
115 %
116 <% $cgi->redirect( $opt{'redirect'}. $pkeyvalue ) %>
117 %
118 %} else { 
119 %  my $ext = $opt{'viewall_ext'} || 'html';
120 <% $cgi->redirect( popurl(3). ($opt{viewall_dir}||'search'). "/$table.$ext" ) %>
121 %}
122 <%once>
123
124   my $me = 'process.html:';
125
126 </%once>
127 <%init>
128
129 my(%opt) = @_;
130
131 my $curuser = $FS::CurrentUser::CurrentUser;
132
133 my $error = '';
134 if ( $opt{'precheck_callback'} ) {
135   $error = &{ $opt{'precheck_callback'} }( $cgi );
136 }
137
138 #false laziness w/edit.html
139 my $table = $opt{'table'};
140 my $class = "FS::$table";
141 my $pkey = dbdef->table($table)->primary_key; #? $opt{'primary_key'} || 
142 my $fields = $opt{'fields'}
143              #|| [ grep { $_ ne $pkey } dbdef->table($table)->columns ];
144              || [ fields($table) ];
145
146 my $pkeyvalue = $cgi->param($pkey);
147
148 my $old = '';
149 if ( $pkeyvalue ) {
150   $old = qsearchs({
151     'table'   => $table,
152     'hashref' => { $pkey => $pkeyvalue },
153     'extra_sql' => ( $opt{'agent_virt'}
154                        ? ' AND '. $curuser->agentnums_sql(
155                                     'null_right' => $opt{'agent_null_right'}
156                                   )
157                        : ''
158                    ),
159   });
160 }
161
162 my %hash =
163   map { my @entry = ( $_ => scalar($cgi->param($_)) );
164         $opt{'value_callback'} ? ( $_ => &{ $opt{'value_callback'} }( @entry ))
165                                : ( @entry )
166       } @$fields;
167
168 my $new = $class->new( \%hash );
169
170 if ( $opt{'agent_virt'} ) {
171   die "illegal agentnum"
172     unless $curuser->agentnums_href->{$new->agentnum}
173         or $opt{'agent_null_right'}
174            && ! $new->agentnum
175            && $curuser->access_right($opt{'agent_null_right'});
176 }
177
178 if ($old && exists($opt{'copy_on_empty'})) {
179   foreach my $field (@{$opt{'copy_on_empty'}}) {
180     $new->set($field, $old->get($field))
181       unless scalar($cgi->param($field));
182   }
183 }
184
185 $error ||= $new->check;
186
187 my @args = ();
188 if ( !$error && $opt{'args_callback'} ) {
189   @args = &{ $opt{'args_callback'} }( $cgi, $new );
190 }
191
192 if ( !$error && $opt{'debug'} ) {
193   warn "$me updating record in $table table using $class class\n";
194   warn Dumper(\%hash);
195   warn "with args: \n". Dumper(\@args) if @args;
196 }
197
198 if ( !$error ) {
199   if ( $pkeyvalue ) {
200     $error = $new->replace($old, @args);
201   } else {
202     $error = $new->insert(@args);
203     $pkeyvalue = $new->getfield($pkey);
204   }
205 }
206
207 if ( !$error && $opt{'process_m2m'} ) {
208
209   my @process_m2m = ref($opt{'process_m2m'}) eq 'ARRAY'
210                       ? @{ $opt{'process_m2m'} }
211                       :  ( $opt{'process_m2m'} );
212
213   foreach my $process_m2m (@process_m2m) {
214
215     $process_m2m->{'params'} ||= scalar($cgi->Vars);
216
217     warn "$me processing m2m:\n". Dumper( %$process_m2m )
218       if $opt{'debug'};
219
220     $error = $new->process_m2m( %$process_m2m );
221   }
222
223 }
224
225 if ( !$error && $opt{'process_m2name'} ) {
226
227   my @process_m2name = ref($opt{'process_m2name'}) eq 'ARRAY'
228                          ? @{ $opt{'process_m2name'} }
229                          :  ( $opt{'process_m2name'} );
230
231
232   foreach my $process_m2name (@process_m2name) {
233
234     if ( $opt{'debug'} ) {
235       warn "$me processing m2name:\n". Dumper( %{ $process_m2name },
236                                                'params' => scalar($cgi->Vars),
237                                              );
238     }
239
240     $error = $new->process_m2name( %{ $process_m2name },
241                                    'params' => scalar($cgi->Vars),
242                                  );
243   }
244
245 }
246
247
248 if ( $error ) {
249   $cgi->param('error', $error);
250   if ( $opt{'clear_on_error'} && scalar(@{$opt{'clear_on_error'}}) ) {
251     foreach my $field (@{$opt{'clear_on_error'}}) {
252       $cgi->param($field, '')
253     }
254   }
255 }
256
257 </%init>