This commit was generated by cvs2svn to compensate for changes in r4888,
[freeside.git] / httemplate / edit / process / generic.cgi
1 %# Welcome to generic.cgi.
2 %# 
3 %# This script provides a generic edit/process/ backend for simple table 
4 %# editing.  All it knows how to do is take the values entered into 
5 %# the script and insert them into the table specified by $cgi->param('table').
6 %# If there's an existing record with the same primary key, it will be 
7 %# replaced.  (Deletion will be added in the future.)
8 %# 
9 %# also 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 %# Special cgi params for this script:
14 %# table: the name of the table to be edited.  The script will die horribly 
15 %#        if it can't find the table.
16 %# redirect_ok: URL to be displayed after a successful edit.  The value of 
17 %#              the record's primary key will be passed as a keyword.
18 %#              Defaults to (freeside root)/view/$table.cgi.
19 %# redirect_error: URL to be displayed if there's an error.  The original 
20 %#                 query string, plus the error message, will be passed.
21 %#                 Defaults to $cgi->referer() (i.e. go back where you 
22 %#                 came from).
23 %
24 %
25 %use FS::Record qw(qsearchs dbdef);
26 %use DBIx::DBSchema;
27 %use DBIx::DBSchema::Table;
28 %
29 %
30 %my $error;
31 %my $p2 = popurl(2);
32 %my $p3 = popurl(3);
33 %my $table = $cgi->param('table');
34 %my $dbdef = dbdef or die "Cannot fetch dbdef!";
35 %
36 %my $dbdef_table = $dbdef->table($table) or die "Cannot fetch schema for $table";
37 %
38 %my $pkey = $dbdef_table->primary_key or die "Cannot fetch pkey for $table";
39 %my $pkey_val = $cgi->param($pkey);
40 %
41 %
42 %#warn "new FS::Record ( $table, (hashref) )";
43 %my $new = FS::Record::new ( "FS::$table", {
44 %    map { $_, scalar($cgi->param($_)) } fields($table) 
45 %} );
46 %
47 %#warn 'created $new of class '.ref($new);
48 %
49 %if($pkey_val and (my $old = qsearchs($table, { $pkey, $pkey_val} ))) {
50 %  # edit
51 %  $error = $new->replace($old);
52 %} else {
53 %  #add
54 %  $error = $new->insert;
55 %  $pkey_val = $new->getfield($pkey);
56 %  # New records usually don't have their primary keys set until after 
57 %  # they've been checked/inserted, so grab the new $pkey_val so we can 
58 %  # redirect to it.
59 %}
60 %
61 %my $redirect_ok = (($cgi->param('redirect_ok')) ?
62 %                    $cgi->param('redirect_ok') : $p3."browse/generic.cgi?$table");
63 %my $redirect_error = (($cgi->param('redirect_error')) ?
64 %                       $cgi->param('redirect_error') : $cgi->referer());
65 %
66 %if($error) {
67 %  $cgi->param('error', $error);
68 %  print $cgi->redirect($redirect_error . '?' . $cgi->query_string);
69 %} else {
70 %  print $cgi->redirect($redirect_ok);
71 %}
72 %
73