Add a new table for inventory with for DIDs/serials/etc., and an additional
[freeside.git] / httemplate / edit / elements / edit.html
1 <%
2
3   # options example...
4   #
5   # 'name'  =>
6   # 'table' =>
7   # #? 'primary_key' => #required when the dbdef doesn't know...???
8   # 'labels' => {
9   #               'column' => 'Label',
10   #             }
11   #
12   # listref - each item is a literal column name (or method) or (notyet) coderef
13   # if not specified all columns (except for the primary key) will be editable
14   # 'fields' => [
15   #             ]
16   #
17   # 'menubar'     => '', #menubar arrayref
18
19   my(%opt) = @_;
20
21   #false laziness w/process.html
22   my $table = $opt{'table'};
23   my $class = "FS::$table";
24   my $pkey = dbdef->table($table)->primary_key; #? $opt{'primary_key'} || 
25   my $fields = $opt{'fields'}
26                #|| [ grep { $_ ne $pkey } dbdef->table($table)->columns ];
27                || [ grep { $_ ne $pkey } fields($table) ];
28
29   my $object;
30   if ( $cgi->param('error') ) {
31
32     $object = $class->new( {
33       map { $_ => scalar($cgi->param($_)) } fields($table)
34     });
35
36   } elsif ( $cgi->keywords ) { #editing
37
38     my( $query ) = $cgi->keywords;
39     $query =~ /^(\d+)$/;
40     $object = qsearchs( $table, { $pkey => $1 } );
41
42   } else { #adding
43
44     $object = $class->new( {} );
45
46   }
47
48   my $action = $object->$pkey() ? 'Edit' : 'Add';
49
50   my $title = "$action $opt{'name'}";
51
52   my @menubar = ();
53   if ( $opt{'menubar'} ) {
54     @menubar = @{ $opt{'menubar'} };
55   } else {
56     @menubar = (
57       'Main menu' => $p, #eventually get rid of this when the ACL/UI update is done
58       "View all $opt{'name'}s" => "${p}search/$table.html", #eventually use Lingua::bs to pluralize
59     );
60   }
61
62 %>
63
64
65 <%= include("/elements/header.html", $title,
66               include( '/elements/menubar.html', @menubar )
67            )
68 %>
69
70 <% if ( $cgi->param('error') ) { %>
71   <FONT SIZE="+1" COLOR="#ff0000">Error: <%= $cgi->param('error') %></FONT>
72   <BR><BR>
73 <% } %>
74
75 <FORM ACTION="<%= popurl(1) %>process/<%= $table %>.html" METHOD=POST>
76 <INPUT TYPE="hidden" NAME="<%= $pkey %>" VALUE="<%= $object->$pkey() %>">
77 <%= ( $opt{labels} && exists $opt{labels}->{$pkey} )
78       ? $opt{labels}->{$pkey}
79       : $pkey
80 %>
81 #<%= $object->$pkey() || "(NEW)" %>
82
83 <%= ntable("#cccccc",2) %>
84
85 <% foreach my $field ( @$fields ) { %>
86
87   <TR>
88
89     <TD ALIGN="right">
90       <%= ( $opt{labels} && exists $opt{labels}->{$field} )
91               ? $opt{labels}->{$field}
92               : $field
93       %>
94     </TD>
95
96     <%
97       #just text in one size for now... eventually more options for
98       # uneditable, hidden, <SELECT>, etc. fields
99     %>
100
101     <TD>
102       <INPUT TYPE="text" NAME="<%= $field %>" VALUE="<%= $object->$field() %>">
103     <TD>
104
105   </TR>
106
107 <% } %>
108
109 </TABLE>
110
111 <BR>
112
113 <INPUT TYPE="submit" VALUE="<%= $object->$pkey() ? "Apply changes" : "Add $opt{'name'}" %>">
114
115 </FORM>
116
117 <%= include("/elements/footer.html") %>
118