no password in html source on employee edit
[freeside.git] / httemplate / edit / process / elements / process.html
1 %
2 %
3 %  # options example...
4 %  # 
5 %  ###
6 %  ##req
7 %  ##
8 %  #
9 %  # 'table' => 
10 %  #
11 %  # #? 'primary_key' => #required when the dbdef doesn't know...???
12 %  # #? 'fields' => []
13 %  #
14 %  ###
15 %  ##opt
16 %  ###
17 %  #
18 %  # 'viewall_dir' => '', #'search' or 'browse', defaults to 'search'
19 %  # OR
20 %  # 'redirect'    => 'view/table.cgi?', # value of primary key is appended
21 %  #
22 %  # 'error_redirect' => popurl(2).'edit/table.cgi?', #query string appended
23 %  #
24 %  # 'edit_ext' => 'html', #defaults to 'html', you might want 'cgi' while the
25 %  #                       #naming is still inconsistent
26 %  # 
27 %  # 'copy_on_empty' => [ old_field_name, another_old_field, ... ],
28 %  # 
29 %  # 'process_m2m' => { 'link_table'   => 'link_table_name',
30 %  #                    'target_table' => 'target_table_name',
31 %  #                  },
32 %  # 'process_m2name' => { 'link_table'   => 'link_table_name',
33 %  #                       'link_static' => { 'column' => 'value' },
34 %  #                       'num_col' => 'column', #if column name is different in
35 %  #                                              #link_table than source_table 
36 %  #                       'name_col' => 'name_column',
37 %  #                       'names_list' => [ 'list', 'names' ],
38 %  #                     },
39 %
40 %  my(%opt) = @_;
41 %
42 %  #false laziness w/edit.html
43 %  my $table = $opt{'table'};
44 %  my $class = "FS::$table";
45 %  my $pkey = dbdef->table($table)->primary_key; #? $opt{'primary_key'} || 
46 %  my $fields = $opt{'fields'}
47 %               #|| [ grep { $_ ne $pkey } dbdef->table($table)->columns ];
48 %               || [ fields($table) ];
49 %
50 %  my $pkeyvalue = $cgi->param($pkey);
51 %
52 %  my $old = qsearchs( $table, { $pkey => $pkeyvalue } ) if $pkeyvalue;
53 %
54 %  my $new = $class->new( {
55 %    map {
56 %      $_, scalar($cgi->param($_));
57 %    } @$fields
58 %  } );
59 %
60 %  if ($old && scalar($opt{'copy_on_empty'})) {
61 %    foreach my $field (@{$opt{'copy_on_empty'}}) {
62 %      $new->set($field, $old->get($field))
63 %        unless scalar($cgi->param($field));
64 %    }
65 %  }
66 %
67 %  my $error;
68 %  if ( $pkeyvalue ) {
69 %    $error = $new->replace($old);
70 %  } else {
71 %    $error = $new->insert;
72 %    $pkeyvalue = $new->getfield($pkey);
73 %  }
74 %
75 %  if ( !$error && $opt{'process_m2m'} ) {
76 %    $error = $new->process_m2m( %{ $opt{'process_m2m'} },
77 %                                'params' => scalar($cgi->Vars),
78 %                              );
79 %  }
80 %
81 %  if ( !$error && $opt{'process_m2name'} ) {
82 %    $error = $new->process_m2name( %{ $opt{'process_m2name'} },
83 %                                   'params' => scalar($cgi->Vars),
84 %                                 );
85 %  }
86 %
87 %  # XXX print?!?!
88 %
89 %  if ( $error ) {
90 %    $cgi->param('error', $error);
91 %    my $edit_ext = $opt{'edit_ext'} || 'html';
92 %    my $url = $opt{'error_redirect'} || popurl(2)."$table.$edit_ext?";
93 %    print $cgi->redirect($url. $cgi->query_string );
94 %  } elsif ( $opt{'redirect'} ) {
95 %    print $cgi->redirect( $opt{'redirect'}. $pkeyvalue );
96 %  } else { 
97 %    print $cgi->redirect( popurl(3).
98 %                          ( $opt{'viewall_dir'} || 'search' ).
99 %                          "/$table.html"
100 %                        );
101 %  }
102 %
103 %
104