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