6 'name_singular' => #singular name for the record
7 # (preferred, will be pluralized automatically)
8 'name' => #name for the record
9 # (deprecated, will be pluralized simplistically)
12 #? 'primary_key' => #required when the dbdef doesn't know...???
17 #listref - each item is a literal column name (or method) or hashref
19 #if not specified all columns (except for the primary key) will be editable
22 { 'field' => 'another_columname',
23 'type' => 'text', #text
29 #selectlayers (can now use after a tablebreak-tr-title... but not inside columnstart/columnnext/columnend)
35 #hidden - hidden value from object
36 #fixed - display fixed value from object or here
39 'value' => 'Y', #for checkbox, title, fixed, hidden
41 'onchange' => 'javascript_function',
43 'include_opt_callback' => sub { my $object = @_;
44 ( 'option' => 'value', );
47 'm2name_table' => 'table_name',
48 'm2name_namecol' => 'name_column',
51 #'m2m_srccol' => #opt, if not the same as this table
52 'm2m_dstcol' => #required for now, eventuaully opt, if not the same as target table
56 'm2_label' => 'Label', #
57 'm2_new_default' => \@table_name_objects, #default
60 'm2_error_callback' => sub { my($cgi, $object) = @_; },
61 'm2_remove_warnings' => \%warnings, #hashref of warning
64 'm2_new_js' => 'function_name', #javascript function called
65 #on spawned rows (one arg:
67 'm2_remove_js' => 'function_name', #js function called when
68 #a row is deleted (three
71 #layer_fields & layer_values_callback only for selectlayer
73 'fieldname' => 'Label',
76 type =>'text', #text, money
79 'layer_values_callback' =>
81 my( $cgi, $object ) = @_;
82 { 'layer' => { 'fieldname' => 'current_value',
83 'fieldname2' => 'field2value',
86 'layer2' => { 'l2fieldname' => 'l2value',
95 'menubar' => '', #menubar arrayref
99 'agent_null' => 1, #if true, always allow no-agentnum globals
100 'agent_null_right' => 'Access Right Name',
101 'agent_clone_extra_sql' => '', #if provided, this overrides the extra_sql
102 #implementing agent virt, for clone
103 #operations. i.e. pass "1=1" to allow
106 'viewall_dir' => '', #'search' or 'browse', defaults to 'search'
108 # overrides default popurl(1)."process/$table.html"
109 'post_url' => popurl(1).'process/something',
111 # optional link to delete this object; primary key will be appended
112 'delete_url' => $p.'misc/delete-something.html?',
114 #we're in a popup (no title/menu/searchboxes)
117 #we're embedded (rows only: no header at all, no html_init, no error
118 # display, no <FORM>, no hidden fields for table name or primary key, no
119 # display of primary key, no submit button, no html_foot, no footer)
120 'embed' => $object, #need to pass the object
121 'tablenum' => 4, #need to specify a table number when using multiple
122 #embedded edits on a page (and m2 stuff)
124 #don't show the primary key label and value
125 'no_pkey_display' => 1,
131 'body_etc' => '', # Additional BODY attributes, i.e. onLoad=""
133 'html_init' => '', #after the header/menubar
135 #string or coderef of additional HTML to add before </TABLE>
136 'html_table_bottom' => '',
138 #after </TABLE> but before the submit
139 'html_bottom' => '', #string
140 'html_bottom' => sub {
146 #javascript function name, will be called with form name as arg
147 'onsubmit' => 'check_form_data',
149 #at the very bottom (well, as low as you can go from here)
153 # initialization callbacks
156 ###global callbacks, always run if provided
158 #after decoding long CGI "redirect=" responses but
159 # before object creation/search
160 # (useful if you have a long form that might trigger redirect= and you need
161 # to do things with $cgi params - they're not decoded in the calling
163 'begin_callback' = sub { my( $cgi, $fields_listref, $opt_hashref ) = @_; },
165 #after the mode-specific object creation/search
166 'end_callback' = sub { my( $cgi, $object, $fields_listref, $opt_hashref ) = @_; },
168 ###mode-specific callbacks. one (and only one) of these four is called
171 'new_callback' => sub { my( $cgi, $object, $fields_listref, $opt_hashref ) = @_; },
174 'edit_callback' => sub { my( $cgi, $object, $fields_listref, $opt_hashref ) = @_; },
176 #run when re-displaying with an error
177 'error_callback' => sub { my( $cgi, $object, $fields_listref, $opt_hashref ) = @_; },
180 'clone_callback' => sub { my( $cgi, $object, $fields_listref, $opt_hashref ) = @_; },
182 ###callbacks called in new mode only
184 # returns a hashref for the new object
185 'new_hashref_callback'
187 # returns the new object iself (otherwise, ->new is called)
188 'new_object_callback'
192 #run before display to return a different value
193 'value_callback' => sub { my( $columname, $value ) = @_; },
195 #run before display to manipulate element of the 'fields' arrayref
196 'field_callback' => sub { my( $cgi, $object, $field_hashref ) = @_; },
202 % unless ( $opt{embed} ) {
204 % my $title = $opt{action}. ' '. ( $opt{name} || $opt{'name_singular'} );
206 % my $viewall_url = $p . ( $opt{'viewall_dir'} || 'search' ) . "/$table.html";
207 % $viewall_url = $opt{'viewall_url'} if $opt{'viewall_url'};
210 % if ( $opt{'menubar'} ) {
211 % @menubar = @{ $opt{'menubar'} };
213 % my $items = $opt{'name'} ? $opt{'name'}.'s' : PL($opt{'name_singular'});
215 % "View all $items" => $viewall_url,
219 <% include('/elements/header'. ( $opt{popup} ? '-popup' : '' ). '.html',
221 include( '/elements/menubar.html', @menubar ),
226 <% defined($opt{'html_init'})
227 ? ( ref($opt{'html_init'})
228 ? &{$opt{'html_init'}}()
234 <% include('/elements/error.html') %>
236 % my $url = $opt{'post_url'} || popurl(1)."process/$table.html";
238 <FORM NAME = "edit_topform"
240 ACTION = "<% $url %>"
241 <% $opt{onsubmit} ? 'onSubmit="return '.$opt{onsubmit}.'(this)"' : '' %>
244 <INPUT TYPE="hidden" NAME="svcdb" VALUE="<% $table %>">
245 <INPUT TYPE="hidden" NAME="<% $pkey %>" VALUE="<% $clone ? '' : $object->$pkey() %>">
247 % unless ( $opt{'no_pkey_display'} ) {
250 <% ( $opt{labels} && exists $opt{labels}->{$pkey} )
251 ? $opt{labels}->{$pkey}
255 #<% ( !$clone && $object->$pkey() ) || "(NEW)" %>
261 % my $tablenum = $opt{'tablenum'} || 0;
262 <TABLE ID="TableNumber<% $tablenum++ %>" BGCOLOR="#cccccc" BORDER=0 CELLSPACING=0>
265 % my @g_row_stack = ();
266 % foreach my $f ( map { ref($_) ? $_ : {'field'=>$_} }
270 % my $trash = &{ $opt{'field_callback'} }( $cgi, $object, $f )
271 % if $opt{'field_callback'};
273 % my $field = $f->{'field'};
274 % my $type = $f->{'type'} ||= 'text';
276 % my $label = ( $opt{labels} && exists $opt{labels}->{$field} )
277 % ? $opt{labels}->{$field}
280 % my $onchange = $f->{'onchange'};
282 % my $layer_values = {};
283 % $layer_values = &{ $f->{'layer_values_callback'} }( $cgi, $object )
284 % if $f->{'layer_values_callback'}
285 % && ! $f->{'m2name_table'}
286 % && ! $f->{'o2m_table'}
287 % && ! $f->{'m2m_method'};
289 % warn "layer values: ". Dumper($layer_values)
292 % my %include_common = (
294 % #text and derivitives
295 % 'size' => $f->{'size'},
296 % 'maxlength' => $f->{'maxlength'},
297 % 'prefix' => $f->{'prefix'},
298 % 'postfix' => $f->{'postfix'},
301 % 'rows' => $f->{'rows'},
302 % 'cols' => $f->{'cols'},
304 % #checkbox, title, fixed, hidden
305 % #& deprecated weird value hashref used only by reason.html
306 % 'value' => $f->{'value'},
309 % 'noescape' => $f->{'noescape'},
312 % 'options' => $f->{'options'},
313 % 'labels' => $f->{'labels'},
314 % 'multiple' => $f->{'multiple'},
315 % 'label_showkey' => $f->{'label_showkey'},
316 % 'disable_empty' => $f->{'disable_empty'},
318 % 'reason_class' => $f->{'reason_class'},
320 % 'viewall_right' => $f->{'viewall_right'},
323 % 'layer_fields' => $f->{'layer_fields'},
324 % 'layer_values' => $layer_values,
325 % 'html_between' => $f->{'html_between'},
327 % #umm. for select-agent_type at least
328 % 'disabled' => $f->{'disabled'},
329 % 'fixed' => $f->{'fixed'},
330 % 'label_callback'=> $f->{'label_callback'},
331 % 'element_etc' => $f->{'element_etc'},
333 % #for select-ticketing_queueid at least
334 % 'post_options' => $f->{'post_options'},
337 % 'colspan' => $f->{'colspan'},
338 % 'required' => $f->{'required'},
341 % 'custnum' => $f->{'custnum'},
342 % 'prospectnum' => $f->{'prospectnum'},
345 % $include_common{$_} = $f->{$_} foreach grep exists($f->{$_}),
346 % qw( js_only html_only select_only layers_only cell_style ),#selectlayers,?
347 % qw( empty_label ), # select-*
348 % qw( value_col compare_sub order_by ), # select-table
349 % qw( table name_col ), #(select,checkboxes)-table
350 % qw( target_table link_table ), #checkboxes-table
351 % qw( hashref agent_virt agent_null agent_null_right ),#*-table
352 % qw( formatted_value ), #fixed
353 % qw( country ), #select-country
354 % qw( width height config ), #htmlarea
355 % qw( alt_format ), #select-cust_location
356 % qw( classnum ), # select-inventory_item
357 % qw( aligned ), # columnstart
361 % $include_common{$_} = ref( $f->{$_} ) eq 'CODE'
362 % ? &{ $f->{$_} }( $cgi, $object ) #, $f )
364 % foreach grep exists($f->{$_}), qw( extra_sql );
366 % if ( $type eq 'tablebreak-tr-title' ) {
367 % $include_common{'table_id'} = 'TableNumber'. $tablenum++;
369 % if ( $type eq 'tablebreak-tr-title' || $type eq 'title' ) {
370 % $include_common{'colspan'} = $f->{colspan} if $f->{colspan};
373 % if ( $f->{include_opt_callback} ) {
374 % %include_common = ( %include_common,
375 % &{ $f->{include_opt_callback} }( $object )
379 % my $layer_prefix_on = '';
381 % my $include_sub = sub {
384 % my $fieldnum = delete $opt{'fieldnum'};
386 % my $include = $type;
387 % $include = "input-$include" if $include =~ /^(text|money|percentage)$/;
388 % $include = "tr-$include" unless $include =~ /^(hidden|tablebreak|column)/;
390 % $include_common{'layer_prefix'} = "$field$fieldnum."
391 % if $layer_prefix_on;
394 % ( "/elements/$include.html",
395 % 'field' => "$field$fieldnum",
396 % 'id' => "$field$fieldnum", #separate?
397 % 'label_id' => $field."_label$fieldnum", #don't want field0_label0...
402 % if ( $include eq 'tr-input-date-field' ) {
403 % # it's either hacking it here, or changing a lot more stuff
405 % "/elements/$include.html", {
407 % 'value' => $opt{curr_value},
409 % 'noinit' => $f->{noinit},
417 % my $column_sub = sub {
420 % my $column = delete($opt{field});
421 % my $fieldnum = delete($opt{fieldnum});
422 % my $include = delete($opt{type}) || 'text';
423 % $include = "input-$include" if $include =~ /^(text|money|percentage)$/;
425 % ( "/elements/$include.html",
426 % 'field' => $field.'__'.$column.$fieldnum,
427 % 'id' => $field.'__'.$column.$fieldnum,
428 % 'layer_prefix' => $field.'__'.$column.$fieldnum.".",
430 % ? ('cell_style' => 'border-top:1px solid black')
438 % unless ( $type =~ /^column/ ) {
439 % $g_row = 1 if $type eq 'tablebreak-tr-title';
441 % $g_row++ if $type eq 'title';
442 % $g_row += scalar( @{ $f->{options} } )-1 if $type eq 'radio';
444 % if ( $type eq 'columnstart' ) {
445 % push @g_row_stack, $g_row;
447 % #} elsif ( $type eq 'columnnext' ) {
448 % } elsif ( $type eq 'columnend' ) {
449 % $g_row = pop @g_row_stack;
455 % my $curr_value = '';
456 % if ( $f->{'m2name_table'} || $f->{'o2m_table'} || $f->{'m2m_method'} ) {
459 % if ( $f->{'m2name_table'} ) {
460 % $table = $f->{'m2name_table'};
461 % $col = $f->{'m2name_namecol'};
462 % } elsif ( $f->{'o2m_table'} ) {
463 % $table = $f->{'o2m_table'};
464 % $col = dbdef->table($f->{'o2m_table'})->primary_key;
465 % } elsif ( $f->{'m2m_method'} ) {
466 % $table = $f->{'m2m_method'};
467 % $col = $f->{'m2m_dstcol'};
470 % $layer_prefix_on = 1;
471 % #print out the fields for the existing m2s
473 % if ( $mode eq 'error' ) {
474 % @existing = &{ $f->{'m2_error_callback'} }( $cgi, $object );
475 % } elsif ( $object->$pkey() ) { # $mode eq 'edit'||'clone'
476 % @existing = $object->$table();
477 % warn scalar(@existing). " from $object->$table: ". join('/', @existing)
479 % } elsif ( $f->{'m2_new_default'} ) { # && $mode eq 'new'
480 % @existing = @{ $f->{'m2_new_default'} };
482 % foreach my $name_obj ( @existing ) {
484 % my $ex_label = '<INPUT TYPE="button" VALUE="X" TITLE="Remove this '.
485 % lc($f->{'m2_label'}).
486 % qq(" onClick="remove_$field($fieldnum);").
487 % ' STYLE="color:#ff0000;font-weight:bold;'.
488 % 'padding-left:2px;padding-right:2px"'.
489 % '> '. ($f->{'m2_label'} || $field ). ' ';
491 % if ( $f->{'layer_values_callback'} ) {
492 % my %switches = ( 'mode' => $mode );
494 % &{ $f->{'layer_values_callback'} }( $cgi, $name_obj, \%switches );
496 % warn "layer values: ". Dumper($layer_values)
499 % my @existing = &{ $include_sub }(
500 % 'label' => $ex_label,
501 % 'fieldnum' => $fieldnum,
502 % 'curr_value' => $name_obj->$col(),
503 % 'onchange' => $onchange,
504 % 'layer_values' => $layer_values,
505 % 'cell_style' => ( $fieldnum ? 'border-top:1px solid black' : '' ),
507 % $existing[0] =~ s(^/elements/tr-)(/elements/);
508 % my @label = @existing;
509 % $label[0] = '/elements/tr-td-label.html';
511 <% include( @label ) %>
512 <TD COLSPAN="<% $f->{'colspan'} || 1 %>">
513 <% include( @existing ) %>
516 % if ( $f->{'m2_fields'} ) {
517 % foreach my $c ( @{ $f->{'m2_fields'} } ) {
518 % my $column = $c->{field};
519 % my @column = &{ $column_sub }( %$c,
520 % 'fieldnum' => $fieldnum,
521 % 'curr_value' => $name_obj->$column()
524 <TD id='<% $field %>__<% $column %>_label<% $fieldnum %>'
525 style='text-align:right;vertical-align:top;
526 border-top:1px solid black;padding-top:5px;'>
527 <% $c->{'label'} || '' %>
529 <TD style='border-top:1px solid black;padding-top:3px;'>
530 <% include( @column ) %>
540 % #$field .= $fieldnum;
541 % $onchange .= "\nspawn_$field(what);";
543 % if ( $f->{curr_value_callback} ) {
544 % $curr_value = &{ $f->{curr_value_callback} }( $cgi, $object, $field ),
546 % $curr_value = $object->$field() if $field;
548 % $curr_value = &{ $opt{'value_callback'} }( $f->{'field'}, $curr_value )
549 % if $opt{'value_callback'} && $mode ne 'error';
552 % my @include = &{ $include_sub }(
554 % 'fieldnum' => $fieldnum,
555 % 'curr_value' => $curr_value,
556 % 'object' => $object,
558 % 'onchange' => $onchange,
559 % ( $fieldnum ? ('cell_style' => 'border-top:1px solid black') : () ),
562 % if ( $f->{'m2name_table'} || $f->{'o2m_table'} || $f->{'m2m_method'} ) {
563 % $include[0] =~ s(^/elements/tr-)(/elements/);
564 % my @label = @include;
565 % $label[0] = '/elements/tr-td-label.html';
567 <% include( @label ) %>
568 <TD COLSPAN="<% $f->{'colspan'} || 1 %>">
569 <% include( @include ) %>
572 % if ( $f->{'m2_fields'} ) {
573 % foreach my $c ( @{ $f->{'m2_fields'} } ) {
574 % my $column = $c->{field};
575 % my @column = &{ $column_sub }( %$c, 'fieldnum' => $fieldnum );
577 <TD id='<% $field %>__<% $column %>_label<% $fieldnum %>'
578 style='text-align:right;vertical-align:top;
579 border-top:1px solid black;padding-top:5px;'>
580 <% $c->{'label'} || '' %>
582 <TD style='border-top:1px solid black;padding-top:3px;'>
583 <% include( @column ) %>
592 <% include( @include ) %>
595 % if ( $f->{'m2name_table'} || $f->{'o2m_table'} || $f->{'m2m_method'} ) {
597 <SCRIPT TYPE="text/javascript">
599 var <%$field%>_rownum = <% $g_row %>;
600 var <%$field%>_fieldnum = <% $fieldnum %>;
602 function spawn_<%$field%>(what) {
604 // only spawn if we're the last element... return if not
606 var field_regex = /(\d+)(_[a-z_]+)?$/;
607 var match = field_regex.exec(what.name);
609 alert(what.name + " didn't match for " + what);
612 if ( match[1] != <%$field%>_fieldnum ) {
616 // change the label on the last entry & add a remove button
617 var prev_label = document.getElementById('<% $field %>_label' + <%$field%>_fieldnum );
618 prev_label.innerHTML = '<INPUT TYPE="button" VALUE="X" TITLE="Remove this <% lc($f->{'m2_label'}) %>" onClick="remove_<% $field %>(' + <%$field%>_fieldnum + ');" STYLE="color:#ff0000;font-weight:bold;padding-left:2px;padding-right:2px" > <% $f->{'m2_label'} || $field %>';
620 <%$field%>_fieldnum++;
624 % $include[0] =~ s(^/elements/tr-)(/elements/);
625 % my @layer_opt = ( @include,
626 % 'field' => $field."MAGIC_NUMBER",
627 % 'id' => $field."MAGIC_NUMBER",
628 % 'layer_prefix' => $field."MAGIC_NUMBER.",
630 % warn @layer_opt if $opt{'debug'};
632 var newrow = <% include(@layer_opt, html_only=>1) |js_string %>;
634 % #until the rest have html/js_only
635 % if ( $type eq 'selectlayers' || $type =~ /^select-cgp_rule_/ ) {
636 var newfunc = <% include(@layer_opt, js_only=>1) |js_string %>;
641 // substitute in the new field name
642 var magic_regex = /MAGIC_NUMBER/g;
643 newrow = newrow.replace( magic_regex, <%$field%>_fieldnum );
644 newfunc = newfunc.replace( magic_regex, <%$field%>_fieldnum );
647 if (window.ActiveXObject) {
648 window.execScript(newfunc);
649 } else { /* (window.XMLHttpRequest) */
650 //window.eval(newfunc);
651 setTimeout(newfunc, 0);
656 //hmm, can't use selectlayers after a tablebreak-title for now
657 var table = document.getElementById('TableNumber<% $tablenum-1 %>');
659 var row = table.insertRow(<%$field%>_rownum++);
661 var label_cell = document.createElement('TD');
663 label_cell.id = '<% $field %>_label' + <%$field%>_fieldnum;
665 label_cell.style.textAlign = "right";
666 label_cell.style.verticalAlign = "top";
667 label_cell.style.borderTop = "1px solid black";
668 label_cell.style.paddingTop = "5px";
670 label_cell.innerHTML = '<% $label %>';
672 row.appendChild(label_cell);
674 var widget_cell = document.createElement('TD');
676 widget_cell.style.borderTop = "1px solid black";
677 widget_cell.style.paddingTop = "3px";
678 widget_cell.colSpan = "<% $f->{'colspan'} || 1 %>";
680 widget_cell.innerHTML = newrow;
682 row.appendChild(widget_cell);
684 % if ( $f->{'m2_fields'} ) {
685 % foreach my $c ( @{ $f->{'m2_fields'} } ) {
686 % my $column = $c->{field};
687 % my @column = &{ $column_sub }(%$c, 'fieldnum' => 'MAGIC_NUMBER');
689 var column = <% include(@column, html_only=>1) |js_string %>;
690 column = column.replace( magic_regex, <%$field%>_fieldnum );
692 var column_label = document.createElement('TD');
694 '<% $field %>__<% $column %>_label' + <%$field%>_fieldnum;
696 column_label.style.textAlign = "right";
697 column_label.style.verticalAlign = "top";
698 column_label.style.borderTop = "1px solid black";
699 column_label.style.paddingTop = "5px";
701 column_label.innerHTML = '<% $c->{'label'} || '' %>';
703 row.appendChild(column_label);
705 var column_widget = document.createElement('TD');
707 column_widget.style.borderTop = "1px solid black";
708 column_widget.style.paddingTop = "3px";
710 column_widget.innerHTML = column;
712 row.appendChild(column_widget);
717 % if ( $f->{'m2_new_js'} ) {
718 // take out items selected in previous dropdowns
719 var new_element = document.getElementById("<%$field%>" + <%$field%>_fieldnum );
720 <% $f->{'m2_new_js'} %>(new_element);
722 if ( new_element.length < 2 ) {
723 //just the ** Select new **, so don't display the row
724 row.style.display = 'none';
730 function remove_<%$field%>(remove_fieldnum) {
731 //alert("remove <%$field%> " + remove_fieldnum);
732 var select = document.getElementById('<%$field%>' + remove_fieldnum);
735 alert("can't find element <%$field%>" + remove_fieldnum);
739 % my $warnings = $f->{'m2_remove_warnings'};
741 var sel_value = select.options[select.selectedIndex].value;
742 % foreach my $value ( keys %$warnings ) {
743 if ( sel_value == '<% $value %>' ) {
744 if ( ! confirm( <% $warnings->{$value} |js_string %> ) ) {
751 select.disabled = 'disabled'; // this seems to prevent it from being submitted on tested browsers so far (IE, moz, konq at least)
752 var label_td = document.getElementById('<%$field%>_label' + remove_fieldnum );
753 label_td.parentNode.style.display = 'none';
755 % if ( $f->{m2_remove_js} ) {
756 var opt = select.options[select.selectedIndex];
757 <% $f->{m2_remove_js} %>( opt.value, opt.text, 'no_match');
768 <% ref( $opt{'html_table_bottom'} )
769 ? &{ $opt{'html_table_bottom'} }( $object )
770 : $opt{'html_table_bottom'}
775 <% ref( $opt{'html_bottom'} )
776 ? &{ $opt{'html_bottom'} }( $object )
777 : $opt{'html_bottom'}
780 % unless ($opt{'embed'}) {
784 % unless ($opt{'no_submit'}) {
785 <INPUT TYPE = "submit"
787 VALUE = "<% ( !$clone && $object->$pkey() )
789 : "Add ". ($opt{'name'} || $opt{'name_singular'})
792 % if ( $opt{'delete_url'} and $object->get($pkey) ) {
793 % my $delete_msg = 'Delete this '.
794 % ($opt{'name_singular'} || $opt{'name'});
795 % my $delete_url = $opt{'delete_url'};
796 % $delete_url .= '?' unless $delete_url =~ /\?/;
797 % $delete_url .= $object->get($pkey);
798 <SCRIPT TYPE="text/javascript">
799 function confirm_delete() {
800 if(confirm(<% $delete_msg . '?' |js_string %>)) {
801 window.location.href = <% $delete_url |js_string %>;
805 <INPUT TYPE = "button"
806 VALUE = "<% $delete_msg |h %>"
807 onclick = "confirm_delete()">
813 <% ref( $opt{'html_foot'} )
814 ? &{ $opt{'html_foot'} }( $object )
818 <% include("/elements/footer.html") %>
825 my $curuser = $FS::CurrentUser::CurrentUser;
827 #false laziness w/process.html
828 my $table = $opt{'table'};
829 my $class = "FS::$table";
830 my $pkey = dbdef->table($table)->primary_key; #? $opt{'primary_key'} ||
831 my $fields = $opt{'fields'}
832 #|| [ grep { $_ ne $pkey } dbdef->table($table)->columns ];
833 || [ grep { $_ ne $pkey } fields($table) ];
834 #my @actualfields = map { ref($_) ? $_->{'field'} : $_ } @$fields;
838 if ( $opt{'embed'} ) {
840 $object = $opt{'embed'};
841 $mode = $cgi->param('error')
849 #$m->comp('/elements/handle_uri_query');
850 if ( $cgi->param('redirect') ) {
851 my $session = $cgi->param('redirect');
852 my $pref = $curuser->option("redirect$session");
853 die "unknown redirect session $session\n" unless length($pref);
854 $cgi = new CGI($pref);
857 &{$opt{'begin_callback'}}( $cgi, $fields, \%opt )
858 if $opt{'begin_callback'};
862 'extra_sql' => ( $opt{'agent_virt'}
863 ? ' AND '. $curuser->agentnums_sql(
864 'null_right' => $opt{'agent_null_right'}
870 if ( $cgi->param('error') ) {
874 $object = $class->new( {
875 map { $_ => scalar($cgi->param($_)) } fields($table)
878 &{$opt{'error_callback'}}( $cgi, $object, $fields, \%opt )
879 if $opt{'error_callback'};
881 } elsif ( $cgi->param('clone') =~ /^(\d+)$/ ) {
887 $qsearch{'extra_sql'} = ' AND '. $opt{'agent_clone_extra_sql'}
888 if $opt{'agent_clone_extra_sql'};
890 $object = qsearchs({ %qsearch, 'hashref' => { $pkey => $clone } })
891 or die "$pkey $clone not found in $table";
893 &{$opt{'clone_callback'}}( $cgi, $object, $fields, \%opt )
894 if $opt{'clone_callback'};
898 $opt{action} ||= 'Add';
900 } elsif ( $cgi->keywords || $cgi->param($pkey) ) { #editing
905 if ( $cgi->param($pkey) ) {
906 $value = $cgi->param($pkey)
908 my( $query ) = $cgi->keywords;
911 $value =~ /^(\d+)$/ or die "unparsable $pkey";
912 $object = qsearchs({ %qsearch, 'hashref' => { $pkey => $1 } })
913 or die "$pkey $1 not found in $table";
915 warn "$table $pkey => $1"
918 &{$opt{'edit_callback'}}( $cgi, $object, $fields, \%opt )
919 if $opt{'edit_callback'};
925 my $hashref = $opt{'new_hashref_callback'}
926 ? &{$opt{'new_hashref_callback'}}
929 $object = $opt{'new_object_callback'}
930 ? &{$opt{'new_object_callback'}}( $cgi, $hashref, $fields, \%opt )
931 : $class->new( $hashref );
933 &{$opt{'new_callback'}}( $cgi, $object, $fields, \%opt )
934 if $opt{'new_callback'};
938 &{$opt{'end_callback'}}( $cgi, $object, $fields, \%opt )
939 if $opt{'end_callback'};
941 $opt{action} ||= $object->$pkey() ? 'Edit' : 'Add';