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