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