RT# 82949 - changes section name from fees to pricing, better opiton
[freeside.git] / httemplate / edit / process / generic.cgi
1 %if($error) {
2 %  $cgi->param('error', $error);
3 <% $cgi->redirect($redirect_error . '?' . $cgi->query_string) %>
4 %} else {
5 <% $cgi->redirect($redirect_ok) %>
6 %}
7 <%doc>
8
9 See elements/process.html, newer and somewhat along the same lines,
10 though it still makes you setup a process file for the table.
11 Perhaps safer, perhaps more of a pain in the ass.
12
13 In any case, this is probably pretty deprecated; it is only used by
14 part_virtual_field.cgi, and so its ACL is hardcoded to 'Configuration'.
15
16 Welcome to generic.cgi.
17
18 This script provides a generic edit/process/ backend for simple table 
19 editing.  All it knows how to do is take the values entered into 
20 the script and insert them into the table specified by $cgi->param('table').
21 If there's an existing record with the same primary key, it will be 
22 replaced.  (Deletion will be added in the future.)
23
24 Special cgi params for this script:
25 table: the name of the table to be edited.  The script will die horribly 
26        if it can't find the table.
27 redirect_ok: URL to be displayed after a successful edit.  The value of 
28              the record's primary key will be passed as a keyword.
29              Defaults to (freeside root)/view/$table.cgi.
30 redirect_error: URL to be displayed if there's an error.  The original 
31                 query string, plus the error message, will be passed.
32                 Defaults to $cgi->referer() (i.e. go back where you 
33                 came from).
34
35 </%doc>
36 <%init>
37
38 die "access denied"
39   unless $FS::CurrentUser::CurrentUser->access_right('Configuration');
40
41 my $error;
42 my $p2 = popurl(2);
43 my $p3 = popurl(3);
44 my $table = $cgi->param('table');
45 my $dbdef = dbdef or die "Cannot fetch dbdef!";
46
47 my $dbdef_table = $dbdef->table($table) or die "Cannot fetch schema for $table";
48
49 my $pkey = $dbdef_table->primary_key or die "Cannot fetch pkey for $table";
50 my $pkey_val = $cgi->param($pkey);
51
52
53 #warn "new FS::Record ( $table, (hashref) )";
54 my $new = FS::Record::new ( "FS::$table", {
55     map { $_, scalar($cgi->param($_)) } fields($table) 
56 } );
57
58 #warn 'created $new of class '.ref($new);
59
60 if($pkey_val and (my $old = qsearchs($table, { $pkey, $pkey_val} ))) {
61   # edit
62   $error = $new->replace($old);
63 } else {
64   #add
65   $error = $new->insert;
66   $pkey_val = $new->getfield($pkey);
67   # New records usually don't have their primary keys set until after 
68   # they've been checked/inserted, so grab the new $pkey_val so we can 
69   # redirect to it.
70 }
71
72 my $redirect_ok = (($cgi->param('redirect_ok')) ?
73                     $cgi->param('redirect_ok') : $p3."browse/generic.cgi?$table");
74 my $redirect_error = (($cgi->param('redirect_error')) ?
75                        $cgi->param('redirect_error') : $cgi->referer());
76
77 </%init>