%doc> Example: include( 'elements/edit.html', 'name_singular' => #singular name for the record # (preferred, will be pluralized automatically) 'name' => #name for the record # (deprecated, will be pluralized simplistically) 'table' => #? 'primary_key' => #required when the dbdef doesn't know...??? 'labels' => { 'column' => 'Label', } #listref - each item is a literal column name (or method) or hashref # or (notyet) coderef #if not specified all columns (except for the primary key) will be editable 'fields' => [ 'columname', { 'field' => 'another_columname', 'type' => 'text', #text #password #money #percentage #checkbox #select #selectlayers (can now use after a tablebreak-tr-title... but not inside columnstart/columnnext/columnend) #title #tablebreak-tr-title #columnstart #columnnext #columnend #hidden - hidden value from object #fixed - display fixed value from object or here #fixed-country #fixed-state 'value' => 'Y', #for checkbox, title, fixed, hidden 'disabled' => 0, 'onchange' => 'javascript_function', #m2 stuff only tested w/selectlayers so far #might work w/select too, dunno others 'm2name_table' => 'table_name', 'm2name_namecol' => 'name_column', #OR# 'm2m_method' => #'m2m_srccol' => #opt, if not the same as this table 'm2m_dstcol' => #required for now, eventuaully opt, if not the same as target table 'm2_label' => 'Label', # 'm2_new_default' => \@table_name_objects, #default #m2 objects for #new records 'm2_error_callback' => sub { my($cgi, $object) = @_; }, 'm2_remove_warnings' => \%warnings, #hashref of warning #messages for m2 #removal 'm2_new_js' => 'function_name', #javascript function called #on spawned rows (one arg: #new_element) 'm2_remove_js' => 'function_name', #js function called when #a row is deleted (three #args: value, text, #'no_match') #layer_fields & layer_values_callback only for selectlayer 'layer_fields' => [ 'fieldname' => 'Label', 'another_field' => { label=>'Label', type =>'text', #text, money }, ], 'layer_values_callback' => sub { my( $cgi, $object ) = @_; { 'layer' => { 'fieldname' => 'current_value', 'fieldname2' => 'field2value', ... }, 'layer2' => { 'l2fieldname' => 'l2value', ... }, ... }; }, }, ] 'menubar' => '', #menubar arrayref #agent virtualization 'agent_virt' => 1, 'agent_null_right' => 'Access Right Name', 'agent_clone_extra_sql' => '', #if provided, this overrides the extra_sql #implementing agent virt, for clone #operations. i.e. pass "1=1" to allow #cloning anything 'viewall_dir' => '', #'search' or 'browse', defaults to 'search' # overrides default popurl(1)."process/$table.html" 'post_url' => popurl(1).'process/something', #we're in a popup (no title/menu/searchboxes) 'popup' => 1, ### # HTML callbacks ### 'body_etc' => '', # Additional BODY attributes, i.e. onLoad="" 'html_init' => '', #after the header/menubar #string or coderef of additional HTML to add before 'html_table_bottom' => '', #after but before the submit 'html_bottom' => '', #string 'html_bottom' => sub { my $object = shift; # ... "html_string"; }, #at the very bottom (well, as low as you can go from here) 'html_foot' => '', ### # initialization callbacks ### ###global callbacks #always run if provided, after decoding long CGI "redirect=" responses but # before object creation/search # (useful if you have a long form that might trigger redirect= and you need # to do things with $cgi params - they're not decoded in the calling # <%init> block yet) 'begin_callback' = sub { my( $cgi, $fields_listref, $opt_hashref ) = @_; }, #always run, after the mode-specific object creation/search 'end_callback' = sub { my( $cgi, $object, $fields_listref, $opt_hashref ) = @_; }, ###mode-specific callbacks #run when re-displaying with an error 'error_callback' => sub { my( $cgi, $object, $fields_listref, $opt_hashref ) = @_; }, #run when editing 'edit_callback' => sub { my( $cgi, $object, $fields_listref ) = @_; }, # returns a hashref for the new object 'new_hashref_callback' # returns the new object iself (otherwise, ->new is called) 'new_object_callback' #run when adding 'new_callback' => sub { my( $cgi, $object, $fields_listref ) = @_; }, #run when cloning 'clone_callback' => sub { my( $cgi, $object, $fields_listref, $opt_hashref ) = @_; }, ###display callbacks #run before display to return a different value 'value_callback' => sub { my( $columname, $value ) = @_; }, #run before display to manipulate element of the 'fields' arrayref 'field_callback' => sub { my( $cgi, $object, $field_hashref ) = @_; }, ); %doc> <% include('/elements/header'. ( $opt{popup} ? '-popup' : '' ). '.html', $title, include( '/elements/menubar.html', @menubar ), $opt{'body_etc'}, ) %> <% defined($opt{'html_init'}) ? ( ref($opt{'html_init'}) ? &{$opt{'html_init'}}() : $opt{'html_init'} ) : '' %> <% include('/elements/error.html') %> % my $url = $opt{'post_url'} || popurl(1)."process/$table.html";
<% ref( $opt{'html_foot'} ) ? &{ $opt{'html_foot'} }( $object ) : $opt{'html_foot'} %> <% include("/elements/footer.html") %> <%init> my(%opt) = @_; my $curuser = $FS::CurrentUser::CurrentUser; #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; if ( $cgi->param('redirect') ) { my $session = $cgi->param('redirect'); my $pref = $curuser->option("redirect$session"); die "unknown redirect session $session\n" unless length($pref); $cgi = new CGI($pref); } &{$opt{'begin_callback'}}( $cgi, $fields, \%opt ) if $opt{'begin_callback'}; my %qsearch = ( 'table' => $table, 'extra_sql' => ( $opt{'agent_virt'} ? ' AND '. $curuser->agentnums_sql( 'null_right' => $opt{'agent_null_right'} ) : '' ), ); my $mode; my $object; my $clone = ''; if ( $cgi->param('error') ) { $mode = 'error'; $object = $class->new( { map { $_ => scalar($cgi->param($_)) } fields($table) }); &{$opt{'error_callback'}}($cgi, $object, $fields, \%opt ) if $opt{'error_callback'}; } elsif ( $cgi->param('clone') =~ /^(\d+)$/ ) { $mode = 'clone'; $clone = $1; $qsearch{'extra_sql'} = ' AND '. $opt{'agent_clone_extra_sql'} if $opt{'agent_clone_extra_sql'}; $object = qsearchs({ %qsearch, 'hashref' => { $pkey => $clone } }); &{$opt{'clone_callback'}}($cgi, $object, $fields, \%opt ) if $opt{'clone_callback'}; #$object->$pkey(''); $opt{action} ||= 'Add'; } elsif ( $cgi->keywords || $cgi->param($pkey) ) { #editing $mode = 'edit'; my $value; if ( $cgi->param($pkey) ) { $value = $cgi->param($pkey) } else { my( $query ) = $cgi->keywords; $value = $query; } $value =~ /^(\d+)$/ or die "unparsable $pkey"; $object = qsearchs({ %qsearch, 'hashref' => { $pkey => $1 } }) or die "$pkey $1 not found in $table"; warn "$table $pkey => $1" if $opt{'debug'}; &{$opt{'edit_callback'}}($cgi, $object, $fields) if $opt{'edit_callback'}; } else { #adding $mode = 'new'; my $hashref = $opt{'new_hashref_callback'} ? &{$opt{'new_hashref_callback'}} : {}; $object = $opt{'new_object_callback'} ? &{$opt{'new_object_callback'}}( $cgi, $hashref, $fields, \%opt ) : $class->new( $hashref ); &{$opt{'new_callback'}}($cgi, $object, $fields) if $opt{'new_callback'}; } &{$opt{'end_callback'}}( $cgi, $object, $fields, \%opt ) if $opt{'end_callback'}; $opt{action} ||= $object->$pkey() ? 'Edit' : 'Add'; my $title = $opt{action}. ' '. $opt{name}; my $viewall_url = $p . ( $opt{'viewall_dir'} || 'search' ) . "/$table.html"; $viewall_url = $opt{'viewall_url'} if $opt{'viewall_url'}; my @menubar = (); if ( $opt{'menubar'} ) { @menubar = @{ $opt{'menubar'} }; } else { my $items = $opt{'name'} ? $opt{'name'}.'s' : PL($opt{'name_singular'}); @menubar = ( "View all $items" => $viewall_url, ); } %init>