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