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