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