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