7 # #? 'primary_key' => #required when the dbdef doesn't know...???
12 # listref - each item is a literal column name (or method) or hashref
14 # if not specified all columns (except for the primary key) will be editable
17 # { 'field' => 'another_columname',
18 # 'type' => 'text', #text, fixed, hidden, checkbox
19 # #eventually more for <SELECT>, etc.
20 # 'value' => 'Y', #only for checkbox
24 # 'menubar' => '', #menubar arrayref
26 # #run when re-displaying with an error
27 # 'error_callback' => sub { my( $cgi, $object ) = @_; },
30 # 'edit_callback' => sub { my( $cgi, $object ) = @_; },
32 # # returns a hashref for the new object
33 # 'new_hashref_callback'
36 # 'new_callback' => sub { my( $cgi, $object ) = @_; },
39 # 'field_callback' => sub { },
41 # #string or coderef of additional HTML to add before </TABLE>
42 # 'html_table_bottom' => '',
44 # 'viewall_dir' => '', #'search' or 'browse', defaults to 'search'
46 # 'html_bottom' => '', #string
47 # 'html_bottom' => sub {
55 #false laziness w/process.html
56 my $table = $opt{'table'};
57 my $class = "FS::$table";
58 my $pkey = dbdef->table($table)->primary_key; #? $opt{'primary_key'} ||
59 my $fields = $opt{'fields'}
60 #|| [ grep { $_ ne $pkey } dbdef->table($table)->columns ];
61 || [ grep { $_ ne $pkey } fields($table) ];
62 #my @actualfields = map { ref($_) ? $_->{'field'} : $_ } @$fields;
65 if ( $cgi->param('error') ) {
67 $object = $class->new( {
68 map { $_ => scalar($cgi->param($_)) } fields($table)
71 &{$opt{'error_callback'}}($cgi, $object)
72 if $opt{'error_callback'};
74 } elsif ( $cgi->keywords ) { #editing
76 my( $query ) = $cgi->keywords;
78 $object = qsearchs( $table, { $pkey => $1 } );
79 warn "$table $pkey => $1"
82 &{$opt{'edit_callback'}}($cgi, $object)
83 if $opt{'edit_callback'};
87 my $hashref = $opt{'new_hashref_callback'}
88 ? &{$opt{'new_hashref_callback'}}
91 $object = $class->new( $hashref );
93 &{$opt{'new_callback'}}($cgi, $object)
94 if $opt{'new_callback'};
98 my $action = $object->$pkey() ? 'Edit' : 'Add';
100 my $title = "$action $opt{'name'}";
103 if ( $opt{'menubar'} ) {
104 @menubar = @{ $opt{'menubar'} };
107 'Main menu' => $p, #eventually get rid of this when the ACL/UI update is done
108 #eventually use Lingua::bs to pluralize
109 "View all $opt{'name'}s" => $p. ( $opt{'viewall_dir'} || 'search' ).
114 %><%= include("/elements/header.html", $title,
115 include( '/elements/menubar.html', @menubar )
119 <% if ( $cgi->param('error') ) { %>
120 <FONT SIZE="+1" COLOR="#ff0000">Error: <%= $cgi->param('error') %></FONT>
124 <FORM ACTION="<%= popurl(1) %>process/<%= $table %>.html" METHOD=POST>
125 <INPUT TYPE="hidden" NAME="<%= $pkey %>" VALUE="<%= $object->$pkey() %>">
126 <%= ( $opt{labels} && exists $opt{labels}->{$pkey} )
127 ? $opt{labels}->{$pkey}
130 #<%= $object->$pkey() || "(NEW)" %>
132 <%= ntable("#cccccc",2) %>
134 <% foreach my $f ( map { ref($_) ? $_ : {'field'=>$_} }
138 &{ $opt{'field_callback'} }( $f )
139 if $opt{'field_callback'};
141 my $field = $f->{'field'};
142 my $type = $f->{'type'} ||= 'text';
149 <%= ( $opt{labels} && exists $opt{labels}->{$field} )
150 ? $opt{labels}->{$field}
155 <% if ( $type eq 'fixed' ) { %>
157 <TD BGCOLOR="#dddddd"><%= $f->{'value'} %></TD>
158 <INPUT TYPE="hidden" NAME="<%= $field %>" VALUE="<%= $f->{'value'} %>">
160 <% } elsif ( $type eq 'checkbox' ) { %>
163 <INPUT TYPE="checkbox" NAME="<%= $field %>" VALUE="<%= $f->{'value'} %>" <%= $object->$field() eq $f->{'value'} ? ' CHECKED' : '' %>>
169 <INPUT TYPE="<%= $type %>" NAME="<%= $field %>" VALUE="<%= $object->$field() %>">
178 <%= ref( $opt{'html_table_bottom'} )
179 ? &{ $opt{'html_table_bottom'} }( $object )
180 : $opt{'html_table_bottom'}
185 <%= ref( $opt{'html_bottom'} )
186 ? &{ $opt{'html_bottom'} }( $object )
187 : $opt{'html_bottom'}
192 <INPUT TYPE="submit" VALUE="<%= $object->$pkey() ? "Apply changes" : "Add $opt{'name'}" %>">
196 <%= include("/elements/footer.html") %>