start of package class web UI (add/edit package classes, package class selection...
[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   # 'viewall_dir' => '', #'search' or 'browse', defaults to 'search'
20
21   my(%opt) = @_;
22
23   #false laziness w/process.html
24   my $table = $opt{'table'};
25   my $class = "FS::$table";
26   my $pkey = dbdef->table($table)->primary_key; #? $opt{'primary_key'} || 
27   my $fields = $opt{'fields'}
28                #|| [ grep { $_ ne $pkey } dbdef->table($table)->columns ];
29                || [ grep { $_ ne $pkey } fields($table) ];
30
31   my $object;
32   if ( $cgi->param('error') ) {
33
34     $object = $class->new( {
35       map { $_ => scalar($cgi->param($_)) } fields($table)
36     });
37
38   } elsif ( $cgi->keywords ) { #editing
39
40     my( $query ) = $cgi->keywords;
41     $query =~ /^(\d+)$/;
42     $object = qsearchs( $table, { $pkey => $1 } );
43
44   } else { #adding
45
46     $object = $class->new( {} );
47
48   }
49
50   my $action = $object->$pkey() ? 'Edit' : 'Add';
51
52   my $title = "$action $opt{'name'}";
53
54   my @menubar = ();
55   if ( $opt{'menubar'} ) {
56     @menubar = @{ $opt{'menubar'} };
57   } else {
58     @menubar = (
59       'Main menu' => $p, #eventually get rid of this when the ACL/UI update is done
60       #eventually use Lingua::bs to pluralize
61       "View all $opt{'name'}s" => $p. ( $opt{'viewall_dir'} || 'search' ).
62                                   "/$table.html",
63     );
64   }
65
66 %>
67
68
69 <%= include("/elements/header.html", $title,
70               include( '/elements/menubar.html', @menubar )
71            )
72 %>
73
74 <% if ( $cgi->param('error') ) { %>
75   <FONT SIZE="+1" COLOR="#ff0000">Error: <%= $cgi->param('error') %></FONT>
76   <BR><BR>
77 <% } %>
78
79 <FORM ACTION="<%= popurl(1) %>process/<%= $table %>.html" METHOD=POST>
80 <INPUT TYPE="hidden" NAME="<%= $pkey %>" VALUE="<%= $object->$pkey() %>">
81 <%= ( $opt{labels} && exists $opt{labels}->{$pkey} )
82       ? $opt{labels}->{$pkey}
83       : $pkey
84 %>
85 #<%= $object->$pkey() || "(NEW)" %>
86
87 <%= ntable("#cccccc",2) %>
88
89 <% foreach my $field ( @$fields ) { %>
90
91   <TR>
92
93     <TD ALIGN="right">
94       <%= ( $opt{labels} && exists $opt{labels}->{$field} )
95               ? $opt{labels}->{$field}
96               : $field
97       %>
98     </TD>
99
100     <%
101       #just text in one size for now... eventually more options for
102       # uneditable, hidden, <SELECT>, etc. fields
103     %>
104
105     <TD>
106       <INPUT TYPE="text" NAME="<%= $field %>" VALUE="<%= $object->$field() %>">
107     <TD>
108
109   </TR>
110
111 <% } %>
112
113 </TABLE>
114
115 <BR>
116
117 <INPUT TYPE="submit" VALUE="<%= $object->$pkey() ? "Apply changes" : "Add $opt{'name'}" %>">
118
119 </FORM>
120
121 <%= include("/elements/footer.html") %>
122