summaryrefslogtreecommitdiff
path: root/httemplate/edit/process/elements/process.html
blob: 53419cde0eb58769c477ff51d566de5a7adb0412 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
<%doc>

Example:

 include( 'elements/process.html',

   ###
   # required
   ###

  'table' => 'tablename',

   #? 'primary_key' => #required when the dbdef doesn't know...???
   #? 'fields' => []   #""

   ###
   # optional
   ###

   'viewall_dir'  => '', #'search' or 'browse', defaults to 'search'
   'viewall_ext'  => 'html', #'cgi' or 'html', defaults to 'html'
   OR
   'redirect'     => 'view/table.cgi?', # value of primary key is appended
                                        # (string or coderef returning a string)
   OR
   'popup_reload' => 'Momentary success message', #will reload parent window

   'error_redirect' => popurl(2).'edit/table.cgi?', #query string appended

   'edit_ext' => 'html', #defaults to 'html', you might want 'cgi' while the
                         #naming is still inconsistent

   'copy_on_empty'  => [ 'old_field_name', 'another_old_field', ... ],

   'clear_on_error' => [ 'form_field1', 'form_field2', ... ],

                  #pass an arrayref of hashrefs for multiple m2ms or m2names
                  #be certain you incorporate m2m_Common if you see error: param

   'process_m2m' => { 'link_table'   => 'link_table_name',
                      'target_table' => 'target_table_name',
                      #optional (see m2m_Common::process_m2m), if not specified
                      # all CGI params will be passed)
                      'params'       => 
                    },
   'process_m2name' => { 'link_table'   => 'link_table_name',
                         'link_static' => { 'column' => 'value' },
                         'num_col' => 'column', #if column name is different in
                                                #link_table than source_table 
                         'name_col' => 'name_column',
                         'names_list' => [ 'list', 'names' ],
                         
                         'param_style' => 'link_table.value checkboxes',
                         #or#
                         'param_style' => 'name_colN values',


                       },
   'process_o2m' => { 'table' => table_name',
                      'num_col' => 'column', #if column name is different in
                                             #link_table than source_table 
                    },

   #checks CGI params and whatever else before much else runs
   #return an error string or empty for no error
   'precheck_callback' => sub { my( $cgi ) = @_; },

   #after everything's inserted
   'noerror_callback' => sub { my( $cgi, $object ) = @_; },

   #supplies arguments to insert() and replace()
   # for use with tables that are FS::option_Common (among other things)
   'args_callback' => sub { my( $cgi, $object ) = @_; },

   'debug' => 1, #turns on debugging output

   #agent virtualization
   'agent_virt'       => 1,
   'agent_null_right' => 'Access Right Name',

 )

</%doc>
%if ( $error ) {
%
%  my $edit_ext = $opt{'edit_ext'} || 'html';
%  my $url = $opt{'error_redirect'} || popurl(2)."$table.$edit_ext";
%  if ( length($cgi->query_string) > 1920 ) { #stupid IE 2083 URL limit
% 
%    my $session = int(rand(4294967296)); #XXX
%    my $pref = new FS::access_user_pref({
%      'usernum'    => $FS::CurrentUser::CurrentUser->usernum,
%      'prefname'   => "redirect$session",
%      'prefvalue'  => $cgi->query_string,
%      'expiration' => time + 3600, #1h?  1m?
%    });
%    my $pref_error = $pref->insert;
%    if ( $pref_error ) {
%      die "FATAL: couldn't even set redirect cookie: $pref_error".
%          " attempting to set redirect$session to ". $cgi->query_string."\n";
%    }
%
<% $cgi->redirect("$url?redirect=$session") %>
%
%  } else {
%
<% $cgi->redirect("$url?". $cgi->query_string ) %>
%
%  } 
%
% #different ways of handling success
%
%} elsif ( $opt{'popup_reload'} ) {

  <% include('/elements/header-popup.html', $opt{'popup_reload'} ) %>

  <SCRIPT TYPE="text/javascript">
    window.top.location.reload();
  </SCRIPT>

  </BODY>
  </HTML>

%} else {
%  
%  $opt{'redirect'} = &{$opt{'redirect'}}($cgi, $new)
%    if ref($opt{'redirect'}) eq 'CODE';
%
%  if ( $opt{'redirect'} ) {
%
<% $cgi->redirect( $opt{'redirect'}. $pkeyvalue ) %>
%
%  } else { 
%
%    my $ext = $opt{'viewall_ext'} || 'html';
%
<% $cgi->redirect( popurl(3). ($opt{viewall_dir}||'search'). "/$table.$ext" ) %>
%
%  }
%
%}
%
<%init>

my $me = 'process.html:';

my(%opt) = @_;

my $curuser = $FS::CurrentUser::CurrentUser;

my $error = '';
if ( $opt{'precheck_callback'} ) {
  $error = &{ $opt{'precheck_callback'} }( $cgi );
}

#false laziness w/edit.html
my $table = $opt{'table'};
my $class = "FS::$table";
my $pkey = dbdef->table($table)->primary_key; #? $opt{'primary_key'} || 
my $fields = $opt{'fields'}
             #|| [ grep { $_ ne $pkey } dbdef->table($table)->columns ];
             || [ fields($table) ];

my $pkeyvalue = $cgi->param($pkey);

my $old = '';
if ( $pkeyvalue ) {
  $old = qsearchs({
    'table'   => $table,
    'hashref' => { $pkey => $pkeyvalue },
    'extra_sql' => ( $opt{'agent_virt'}
                       ? ' AND '. $curuser->agentnums_sql(
                                    'null_right' => $opt{'agent_null_right'}
                                  )
                       : ''
                   ),
  });
}

my %hash =
  map { my @entry = ( $_ => scalar($cgi->param($_)) );
        $opt{'value_callback'} ? ( $_ => &{ $opt{'value_callback'} }( @entry ))
                               : ( @entry )
      } @$fields;

my $new = $class->new( \%hash );

if ($old && exists($opt{'copy_on_empty'})) {
  foreach my $field (@{$opt{'copy_on_empty'}}) {
    $new->set($field, $old->get($field))
      unless scalar($cgi->param($field));
  }
}

if ( $opt{'agent_virt'} ) {

  if ( ! $new->agentnum
       && (    ! $opt{'agent_null_right'}
            || ! $curuser->access_right($opt{'agent_null_right'})
          )
     )
  {

    $error ||= 'Select an agent';

  } else {

    die "illegal agentnum"
      unless $curuser->agentnums_href->{$new->agentnum}
          or $curuser->access_right('View customers of all agents')
          or $opt{'agent_null_right'}
             && ! $new->agentnum
             && $curuser->access_right($opt{'agent_null_right'});

  }

}

$error ||= $new->check;

my @args = ();
if ( !$error && $opt{'args_callback'} ) {
  @args = &{ $opt{'args_callback'} }( $cgi, $new );
}

if ( !$error && $opt{'debug'} ) {
  warn "$me updating record in $table table using $class class\n";
  warn Dumper(\%hash);
  warn "with args: \n". Dumper(\@args) if @args;
}

if ( !$error ) {
  if ( $pkeyvalue ) {
    $error = $new->replace($old, @args);
  } else {
    $error = $new->insert(@args);
    $pkeyvalue = $new->getfield($pkey);
  }
}

if ( !$error && $opt{'process_m2m'} ) {

  my @process_m2m = ref($opt{'process_m2m'}) eq 'ARRAY'
                      ? @{ $opt{'process_m2m'} }
                      :  ( $opt{'process_m2m'} );

  foreach my $process_m2m (@process_m2m) {

    $process_m2m->{'params'} ||= scalar($cgi->Vars);

    warn "$me processing m2m:\n". Dumper( %$process_m2m )
      if $opt{'debug'};

    $error = $new->process_m2m( %$process_m2m );
  }

}

if ( !$error && $opt{'process_m2name'} ) {

  my @process_m2name = ref($opt{'process_m2name'}) eq 'ARRAY'
                         ? @{ $opt{'process_m2name'} }
                         :  ( $opt{'process_m2name'} );


  foreach my $process_m2name (@process_m2name) {

    if ( $opt{'debug'} ) {
      warn "$me processing m2name:\n". Dumper( %{ $process_m2name },
                                               'params' => scalar($cgi->Vars),
                                             );
    }

    $error = $new->process_m2name( %{ $process_m2name },
                                   'params' => scalar($cgi->Vars),
                                 );
  }

}

if ( !$error && $opt{'process_o2m'} ) {

  my @process_o2m = ref($opt{'process_o2m'}) eq 'ARRAY'
                         ? @{ $opt{'process_o2m'} }
                         :  ( $opt{'process_o2m'} );


  foreach my $process_o2m (@process_o2m) {

    if ( $opt{'debug'} ) {
      warn "$me processing o2m:\n". Dumper( %{ $process_o2m },
                                               'params' => scalar($cgi->Vars),
                                             );
    }

    $error = $new->process_o2m( %{ $process_o2m },
                                   'params' => scalar($cgi->Vars),
                                 );
  }

}


if ( $error ) {

  $cgi->param('error', $error);
  if ( $opt{'clear_on_error'} && scalar(@{$opt{'clear_on_error'}}) ) {
    foreach my $field (@{$opt{'clear_on_error'}}) {
      $cgi->param($field, '')
    }
  }

} else {

  if ( $opt{'noerror_callback'} ) {
    &{ $opt{'noerror_callback'} }( $cgi, $new );
  }

}

</%init>