This commit was generated by cvs2svn to compensate for changes in r6255,
[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 = map { $_ => scalar($cgi->param($_)) } @$fields;
149
150 my $new = $class->new( \%hash );
151
152 if ( $opt{'agent_virt'} ) {
153   die "illegal agentnum"
154     unless $curuser->agentnums_href->{$new->agentnum}
155         or $opt{'agent_null_right'}
156            && ! $new->agentnum
157            && $curuser->access_right($opt{'agent_null_right'});
158 }
159
160 if ($old && exists($opt{'copy_on_empty'})) {
161   foreach my $field (@{$opt{'copy_on_empty'}}) {
162     $new->set($field, $old->get($field))
163       unless scalar($cgi->param($field));
164   }
165 }
166
167 my $error = $new->check;
168
169 my @args = ();
170 if ( !$error && $opt{'args_callback'} ) {
171   @args = &{ $opt{'args_callback'} }( $cgi, $new );
172 }
173
174 if ( !$error && $opt{'debug'} ) {
175   warn "$me updating record in $table table using $class class\n";
176   warn Dumper(\%hash);
177   warn "with args: \n". Dumper(\@args) if @args;
178 }
179
180 if ( !$error ) {
181   if ( $pkeyvalue ) {
182     $error = $new->replace($old, @args);
183   } else {
184     $error = $new->insert(@args);
185     $pkeyvalue = $new->getfield($pkey);
186   }
187 }
188
189 if ( !$error && $opt{'process_m2m'} ) {
190
191   if ( $opt{'debug'} ) {
192     warn "$me processing m2m:\n". Dumper( %{ $opt{'process_m2m'} },
193                                           'params' => scalar($cgi->Vars),
194                                         );
195   }
196
197   $error = $new->process_m2m( %{ $opt{'process_m2m'} },
198                               'params' => scalar($cgi->Vars),
199                             );
200 }
201
202 if ( !$error && $opt{'process_m2name'} ) {
203
204   if ( $opt{'debug'} ) {
205     warn "$me processing m2name:\n". Dumper( %{ $opt{'process_m2name'} },
206                                              'params' => scalar($cgi->Vars),
207                                            );
208   }
209
210   $error = $new->process_m2name( %{ $opt{'process_m2name'} },
211                                  'params' => scalar($cgi->Vars),
212                                );
213 }
214
215
216 if ( $error ) {
217   $cgi->param('error', $error);
218   if ( $opt{'clear_on_error'} && scalar(@{$opt{'clear_on_error'}}) ) {
219     foreach my $field (@{$opt{'clear_on_error'}}) {
220       $cgi->param($field, '')
221     }
222   }
223 }
224
225 </%init>