%
# options example...
#
# 'name' =>
# 'table' =>
# #? 'primary_key' => #required when the dbdef doesn't know...???
# 'labels' => {
# 'column' => 'Label',
# }
#
# listref - each item is a literal column name (or method) or (notyet) coderef
# if not specified all columns (except for the primary key) will be editable
# 'fields' => [
# ]
#
# 'menubar' => '', #menubar arrayref
#
# #run when re-displaying with an error
# 'error_callback' => sub { my $cgi, $object = @_; },
#
# #run when editing
# 'edit_callback' => sub { my $cgi, $object = @_; },
#
# #run when adding
# 'new_callback' => sub { my $cgi, $object = @_; },
#
# #broken'html_table_bottom' => '', #string or listref of additinal HTML to
# #add before
#
# 'viewall_dir' => '', #'search' or 'browse', defaults to 'search'
#
# 'html_bottom' => '', #string
# 'html_bottom' => sub {
# my $object = shift;
# # ...
# "html_string";
# },
my(%opt) = @_;
#false laziness w/process.html
my $table = $opt{'table'};
my $class = "FS::$table";
my $pkey = dbdef->table($table)->primary_key; #? $opt{'primary_key'} ||
my $fields = $opt{'fields'}
#|| [ grep { $_ ne $pkey } dbdef->table($table)->columns ];
|| [ grep { $_ ne $pkey } fields($table) ];
#my @actualfields = map { ref($_) ? $_->{'field'} : $_ } @$fields;
my $object;
if ( $cgi->param('error') ) {
$object = $class->new( {
map { $_ => scalar($cgi->param($_)) } fields($table)
});
&{$opt{'error_callback'}}($cgi, $object)
if $opt{'error_callback'};
} elsif ( $cgi->keywords ) { #editing
my( $query ) = $cgi->keywords;
$query =~ /^(\d+)$/;
$object = qsearchs( $table, { $pkey => $1 } );
&{$opt{'edit_callback'}}($cgi, $object)
if $opt{'edit_callback'};
} else { #adding
$object = $class->new( {} );
&{$opt{'new_callback'}}($cgi, $object)
if $opt{'new_callback'};
}
my $action = $object->$pkey() ? 'Edit' : 'Add';
my $title = "$action $opt{'name'}";
my @menubar = ();
if ( $opt{'menubar'} ) {
@menubar = @{ $opt{'menubar'} };
} else {
@menubar = (
'Main menu' => $p, #eventually get rid of this when the ACL/UI update is done
#eventually use Lingua::bs to pluralize
"View all $opt{'name'}s" => $p. ( $opt{'viewall_dir'} || 'search' ).
"/$table.html",
);
}
%><%= include("/elements/header.html", $title,
include( '/elements/menubar.html', @menubar )
)
%>
<% if ( $cgi->param('error') ) { %>
Error: <%= $cgi->param('error') %>
<% } %>