simple protection against selecting and using package add-ons that point to the same...
[freeside.git] / httemplate / edit / elements / edit.html
1 <%doc>
2
3 Example:
4
5   include( 'elements/edit.html',  
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)
10     'table'         =>
11
12     #? 'primary_key' => #required when the dbdef doesn't know...???
13     'labels' => {
14                   'column' => 'Label',
15                 }
16    
17     #listref - each item is a literal column name (or method) or hashref
18     #                                                        or (notyet) coderef
19     #if not specified all columns (except for the primary key) will be editable
20     'fields' => [
21                   'columname',
22                   { 'field' => 'another_columname',
23                     'type'  => 'text', #text
24                                        #password
25                                        #money
26                                        #percentage
27                                        #checkbox
28                                        #select
29                                        #selectlayers (can now use after a tablebreak-tr-title... but not inside columnstart/columnnext/columnend)
30                                        #title
31                                        #tablebreak-tr-title
32                                        #columnstart
33                                        #columnnext
34                                        #columnend
35                                        #hidden - hidden value from object
36                                        #fixed - display fixed value from object or here
37                                        #fixed-country
38                                        #fixed-state
39                     'value' => 'Y', #for checkbox, title, fixed, hidden
40                     'disabled' => 0,
41                     'onchange' => 'javascript_function',
42
43                     'include_opt_callback' => sub { my $object = @_;
44                                                     ( 'option' => 'value', );
45                                                   },
46
47                     'm2name_table'   => 'table_name',
48                     'm2name_namecol' => 'name_column',
49                     #OR#
50                     'm2m_method'       =>
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
53                     #OR#
54                     'o2m_table' =>
55
56                     'm2_label'   => 'Label',        #
57                     'm2_new_default' => \@table_name_objects, #default
58                                                               #m2 objects for
59                                                               #new records
60                     'm2_error_callback' => sub { my($cgi, $object) = @_; },
61                     'm2_remove_warnings' => \%warnings, #hashref of warning
62                                                         #messages for m2
63                                                         #removal
64                     'm2_new_js' => 'function_name', #javascript function called
65                                                     #on spawned rows (one arg:
66                                                     #new_element)
67                     'm2_remove_js' => 'function_name', #js function called when
68                                                        #a row is deleted (three
69                                                        #args: value, text,
70                                                        #'no_match')
71                     #layer_fields & layer_values_callback only for selectlayer
72                     'layer_fields' => [
73                                         'fieldname'     => 'Label',
74                                         'another_field' => {
75                                           label=>'Label',
76                                           type =>'text', #text, money
77                                         },
78                                       ],
79                     'layer_values_callback' =>
80                       sub {
81                             my( $cgi, $object ) = @_;
82                             { 'layer'  => { 'fieldname'  => 'current_value',
83                                             'fieldname2' => 'field2value',
84                                             ...
85                                           },
86                               'layer2' => { 'l2fieldname' => 'l2value',
87                                             ...
88                                           },
89                               ...
90                             };
91                           },
92                   },
93                 ]
94    
95     'menubar'     => '', #menubar arrayref
96
97     #agent virtualization
98     'agent_virt'            => 1,
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
104                                    #cloning anything
105
106     'viewall_dir' => '', #'search' or 'browse', defaults to 'search'
107
108     # overrides default popurl(1)."process/$table.html"
109     'post_url' => popurl(1).'process/something', 
110
111     #we're in a popup (no title/menu/searchboxes)
112     'popup' => 1,
113
114     ###
115     # HTML callbacks
116     ###
117
118     'body_etc' => '', # Additional BODY attributes, i.e. onLoad=""
119
120     'html_init'   => '', #after the header/menubar
121
122     #string or coderef of additional HTML to add before </TABLE>
123     'html_table_bottom' => '',
124
125     #after </TABLE> but before the submit
126     'html_bottom' => '', #string
127     'html_bottom' => sub {
128                            my $object = shift;
129                            # ...
130                            "html_string";
131                          },
132     
133     #javascript function name, will be called with form name as arg
134     'onsubmit'   => 'check_form_data',
135
136     #at the very bottom (well, as low as you can go from here)
137     'html_foot'  => '',
138
139     ###
140     # initialization callbacks
141     ###
142
143     ###global callbacks, always run if provided
144
145     #after decoding long CGI "redirect=" responses but
146     # before object creation/search
147     # (useful if you have a long form that might trigger redirect= and you need
148     #  to do things with $cgi params - they're not decoded in the calling
149     #  <%init> block yet)
150     'begin_callback' = sub { my( $cgi, $fields_listref, $opt_hashref ) = @_; },
151
152     #after the mode-specific object creation/search
153     'end_callback' = sub { my( $cgi, $object, $fields_listref, $opt_hashref ) = @_; },
154
155     ###mode-specific callbacks.  one (and only one) of these four is called
156    
157     #run when adding
158     'new_callback' => sub { my( $cgi, $object, $fields_listref, $opt_hashref ) = @_; },
159
160     #run when editing
161     'edit_callback' => sub { my( $cgi, $object, $fields_listref, $opt_hashref ) = @_; },
162
163     #run when re-displaying with an error
164     'error_callback' => sub { my( $cgi, $object, $fields_listref, $opt_hashref ) = @_; },
165    
166     #run when cloning
167     'clone_callback' => sub { my( $cgi, $object, $fields_listref, $opt_hashref ) = @_; },
168
169     ###callbacks called in new mode only
170
171     # returns a hashref for the new object
172     'new_hashref_callback'
173
174     # returns the new object iself (otherwise, ->new is called)
175     'new_object_callback'
176
177     ###display callbacks
178
179     #run before display to return a different value
180     'value_callback' => sub { my( $columname, $value ) = @_; },
181
182     #run before display to manipulate element of the 'fields' arrayref
183     'field_callback' => sub { my( $cgi, $object, $field_hashref ) = @_; },
184
185   );
186
187 </%doc>
188
189 <% include('/elements/header'. ( $opt{popup} ? '-popup' : '' ). '.html',
190               $title,
191               include( '/elements/menubar.html', @menubar ),
192               $opt{'body_etc'},
193            )
194 %>
195
196 <% defined($opt{'html_init'}) 
197       ? ( ref($opt{'html_init'})
198             ? &{$opt{'html_init'}}()
199             : $opt{'html_init'}
200         )
201       : ''
202 %>
203
204 <% include('/elements/error.html') %>
205
206 % my $url = $opt{'post_url'} || popurl(1)."process/$table.html";
207
208 <FORM NAME   = "edit_topform"
209       METHOD = POST
210       ACTION = "<% $url %>"
211       <% $opt{onsubmit} ? 'onSubmit="return '.$opt{onsubmit}.'(this)"' : '' %>
212 >
213
214 <INPUT TYPE="hidden" NAME="svcdb" VALUE="<% $table %>">
215 <INPUT TYPE="hidden" NAME="<% $pkey %>" VALUE="<% $clone ? '' : $object->$pkey() %>">
216
217 <FONT SIZE="+1"><B>
218 <% ( $opt{labels} && exists $opt{labels}->{$pkey} )
219       ? $opt{labels}->{$pkey}
220       : $pkey
221 %>
222 </B></FONT>
223 #<% ( !$clone && $object->$pkey() ) || "(NEW)" %>
224
225 % my $tablenum = 0;
226 <TABLE ID="TableNumber<% $tablenum++ %>" BGCOLOR="#cccccc" BORDER=0 CELLSPACING=0>
227
228 % my $g_row = 0;
229 % my @g_row_stack = ();
230 % foreach my $f ( map { ref($_) ? $_ : {'field'=>$_} }
231 %                       @$fields
232 %                 ) {
233 %
234 %   my $trash = &{ $opt{'field_callback'} }( $cgi, $object, $f )
235 %     if $opt{'field_callback'};
236 %
237 %   my $field = $f->{'field'};
238 %   my $type = $f->{'type'} ||= 'text';
239 %
240 %   my $label = ( $opt{labels} && exists $opt{labels}->{$field} )
241 %                   ? $opt{labels}->{$field}
242 %                   : $field;
243 %
244 %   my $onchange = $f->{'onchange'};
245 %
246 %   my $layer_values = {};
247 %   $layer_values = &{ $f->{'layer_values_callback'} }( $cgi, $object )
248 %     if $f->{'layer_values_callback'}
249 %     && ! $f->{'m2name_table'}
250 %     && ! $f->{'o2m_table'}
251 %     && ! $f->{'m2m_method'};
252 %
253 %   warn "layer values: ". Dumper($layer_values)
254 %     if $opt{'debug'};
255 %
256 %   my %include_common = (
257 %
258 %     #text and derivitives
259 %     'size'          => $f->{'size'},
260 %     'maxlength'     => $f->{'maxlength'},
261 %     'postfix'       => $f->{'postfix'},
262 %
263 %     #checkbox, title, fixed, hidden
264 %     #& deprecated weird value hashref used only by reason.html
265 %     'value'         => $f->{'value'},
266 %
267 %     #select(-*)
268 %     'options'       => $f->{'options'},
269 %     'labels'        => $f->{'labels'},
270 %     'multiple'      => $f->{'multiple'},
271 %     'disable_empty' => $f->{'disable_empty'},
272 %     #select-reason
273 %     'reason_class'  => $f->{'reason_class'},
274 %
275 %     #selectlayers
276 %     'layer_fields'  => $f->{'layer_fields'},
277 %     'layer_values'  => $layer_values,
278 %     'html_between'  => $f->{'html_between'},
279 %
280 %     #umm.  for select-agent_types at least
281 %     'disabled'      => $f->{'disabled'},
282 %   );
283 %
284 %   #selectlayers, others?
285 %   $include_common{$_} = $f->{$_}
286 %     foreach grep exists($f->{$_}),
287 %                  qw( js_only html_only select_only layers_only cell_style);
288 %
289 %   #select-*
290 %   $include_common{$_} = $f->{$_}
291 %     foreach grep exists($f->{$_}), qw( empty_label );
292 %
293 %   #select-table
294 %   $include_common{$_} = $f->{$_}
295 %     foreach grep exists($f->{$_}), qw( value_col );
296 %   $include_common{$_} = ref( $f->{$_} ) eq 'CODE'
297 %                           ? &{ $f->{$_} }( $cgi, $object ) #, $f )
298 %                           : $f->{$_}
299 %     foreach grep exists($f->{$_}), qw( extra_sql );
300 %
301 %   #select-table, checkboxes-table
302 %   $include_common{$_} = $f->{$_}
303 %     foreach grep exists($f->{$_}), qw( table name_col  );
304 %
305 %   #checkboxes-table
306 %   $include_common{$_} = $f->{$_}
307 %     foreach grep exists($f->{$_}), qw( target_table link_table );
308 %
309 %   #*-table
310 %   $include_common{$_} = $f->{$_}
311 %     foreach grep exists($f->{$_}),
312 %       qw( hashref agent_virt agent_null agent_null_right );
313 %
314 %   #htmlarea
315 %   $include_common{$_} = $f->{$_}
316 %     foreach grep exists($f->{$_}), qw( width height );
317 %
318 %
319 %   if ( $type eq 'tablebreak-tr-title' ) {
320 %     $include_common{'table_id'} = 'TableNumber'. $tablenum++;
321 %   }
322 %   if ( $type eq 'tablebreak-tr-title' || $type eq 'title' ) {
323 %     $include_common{'colspan'} = $f->{colspan} if $f->{colspan};
324 %   }
325 %
326 %   if ( $f->{include_opt_callback} ) {
327 %     %include_common = ( %include_common,
328 %                         &{ $f->{include_opt_callback} }( $object )
329 %                       );
330 %   }
331 %
332 %   my $layer_prefix_on = '';
333 %
334 %   my $include_sub = sub {
335 %     my %opt = @_;
336 %
337 %     my $fieldnum   = delete $opt{'fieldnum'};
338 %
339 %     my $include = $type;
340 %     $include = "input-$include" if $include =~ /^(text|money|percentage)$/;
341 %     $include = "tr-$include" unless $include =~ /^(hidden|tablebreak|column)/;
342 %
343 %     $include_common{'layer_prefix'} = "$field$fieldnum."
344 %       if $layer_prefix_on;
345 %
346 %     my @include = 
347 %     ( "/elements/$include.html",
348 %         'field'      => "$field$fieldnum",
349 %         'id'         => "$field$fieldnum", #separate?
350 %         'label_id'   => $field."_label$fieldnum", #don't want field0_label0...
351 %         %include_common,
352 %         %opt,
353 %     );
354 %     @include;
355 %   };
356 %
357 %   my $column_sub = sub {
358 %     my %opt = @_;
359 %
360 %     my $column   = delete($opt{field});
361 %     my $fieldnum = delete($opt{fieldnum});
362 %     my $include  = delete($opt{type}) || 'text';
363 %     $include = "input-$include" if $include =~ /^(text|money|percentage)$/;
364 %
365 %     ( "/elements/$include.html",
366 %         'field'        => $field.'__'.$column.$fieldnum,
367 %         'id'           => $field.'__'.$column.$fieldnum,
368 %         'layer_prefix' => $field.'__'.$column.$fieldnum.".",
369 %         ( $fieldnum
370 %             ? ('cell_style' => 'border-top:1px solid black')
371 %             : ()
372 %         ),
373 %         'cgi' => $cgi,
374 %         %opt,
375 %     );
376 %   };
377 %
378 %   unless ( $type =~ /^column/ ) {
379 %     $g_row = 1 if $type eq 'tablebreak-tr-title';
380 %     $g_row++;
381 %     $g_row++ if $type eq 'title';
382 %   } else {
383 %     if ( $type eq 'columnstart' ) {
384 %       push @g_row_stack, $g_row;
385 %       $g_row = 0;
386 %     #} elsif ( $type eq 'columnnext' ) {
387 %     } elsif ( $type eq 'columnend' ) {
388 %       $g_row = pop @g_row_stack; 
389 %     }
390 %  
391 %   }
392 %
393 %   my $fieldnum = '';
394 %   my $curr_value = '';
395 %   if ( $f->{'m2name_table'} || $f->{'o2m_table'} || $f->{'m2m_method'} ) {
396 %
397 %     my($table, $col);
398 %     if ( $f->{'m2name_table'} ) {
399 %       $table = $f->{'m2name_table'};
400 %       $col   = $f->{'m2name_namecol'};
401 %     } elsif ( $f->{'o2m_table'} ) {
402 %       $table = $f->{'o2m_table'};
403 %       $col   = dbdef->table($f->{'o2m_table'})->primary_key;
404 %     } elsif ( $f->{'m2m_method'} ) {
405 %       $table = $f->{'m2m_method'};
406 %       $col   = $f->{'m2m_dstcol'};
407 %     }
408 %     $fieldnum = 0;
409 %     $layer_prefix_on = 1;
410 %     #print out the fields for the existing m2s
411 %     my @existing = ();
412 %     if ( $mode eq 'error' ) {
413 %       @existing = &{ $f->{'m2_error_callback'} }( $cgi, $object );
414 %     } elsif ( $object->$pkey() ) { # $mode eq 'edit'||'clone'
415 %       @existing = $object->$table();
416 %       warn scalar(@existing). " from $object->$table: ". join('/', @existing)
417 %         if $opt{'debug'};
418 %     } elsif ( $f->{'m2_new_default'} ) { # && $mode eq 'new'
419 %       @existing = @{ $f->{'m2_new_default'} };
420 %     }
421 %     foreach my $name_obj ( @existing ) {
422 %
423 %       my $ex_label = '<INPUT TYPE="button" VALUE="X" TITLE="Remove this '.
424 %                      lc($f->{'m2_label'}).
425 %                      qq(" onClick="remove_$field($fieldnum);").
426 %                      ' STYLE="color:#ff0000;font-weight:bold;'.
427 %                              'padding-left:2px;padding-right:2px"'.
428 %                      '>&nbsp;'. ($f->{'m2_label'} || $field ). ' ';
429 %       
430 %       if ( $f->{'layer_values_callback'} ) {
431 %         my %switches = ( 'mode' => $mode );
432 %         $layer_values =
433 %           &{ $f->{'layer_values_callback'} }( $cgi, $name_obj, \%switches );
434 %       }
435 %       warn "layer values: ". Dumper($layer_values)
436 %         if $opt{'debug'};
437 %
438 %       my @existing = &{ $include_sub }(
439 %         'label'        => $ex_label,
440 %         'fieldnum'     => $fieldnum,
441 %         'curr_value'   => $name_obj->$col(),
442 %         'onchange'     => $onchange,
443 %         'layer_values' => $layer_values,
444 %         'cell_style'   => ( $fieldnum ? 'border-top:1px solid black' : '' ),
445 %       );
446 %       $existing[0] =~ s(^/elements/tr-)(/elements/);
447 %       my @label = @existing;
448 %       $label[0] = '/elements/tr-td-label.html';
449
450         <% include( @label ) %>
451         <TD COLSPAN="<% $f->{'colspan'} || 1 %>">
452         <% include( @existing ) %>
453         </TD>
454
455 %       if ( $f->{'m2_fields'} ) {
456 %         foreach my $c ( @{ $f->{'m2_fields'} } ) {
457 %           my $column = $c->{field};
458 %           my @column = &{ $column_sub }( %$c,
459 %                                          'fieldnum' => $fieldnum,
460 %                                          'curr_value' => $name_obj->$column()
461 %                                        );
462
463             <TD id='<% $field %>__<% $column %>_label<% $fieldnum %>'
464                 style='text-align:right;vertical-align:top;
465                        border-top:1px solid black;padding-top:5px;'>
466               <% $c->{'label'} || '' %>
467             </TD>
468             <TD style='border-top:1px solid black;padding-top:3px;'>
469               <% include( @column ) %>
470             </TD>
471 %         }
472 %       }
473
474         </TR>
475
476 %       $fieldnum++;
477 %       $g_row++;
478 %     }
479 %     #$field .= $fieldnum;
480 %     $onchange .= "\nspawn_$field(what);";
481 %   } else {
482 %     if ( $f->{curr_value_callback} ) {
483 %       $curr_value = &{ $f->{curr_value_callback} }( $cgi, $object, $field ),
484 %     } else {
485 %       $curr_value = $object->$field();
486 %     }
487 %     $curr_value = &{ $opt{'value_callback'} }( $f->{'field'}, $curr_value )
488 %       if $opt{'value_callback'} && $mode ne 'error';
489 %   }
490 %
491 %   my @include = &{ $include_sub }(
492 %     'label'      => $label,
493 %     'fieldnum'   => $fieldnum,
494 %     'curr_value' => $curr_value,
495 %     'object'     => $object,
496 %     'cgi'        => $cgi,
497 %     'onchange'   => $onchange,
498 %     ( $fieldnum ? ('cell_style' => 'border-top:1px solid black') : () ),
499 %   );
500 %
501 %   if ( $f->{'m2name_table'} || $f->{'o2m_table'} || $f->{'m2m_method'} ) {
502 %     $include[0] =~ s(^/elements/tr-)(/elements/);
503 %     my @label = @include;
504 %     $label[0] = '/elements/tr-td-label.html';
505
506       <% include( @label ) %>
507       <TD COLSPAN="<% $f->{'colspan'} || 1 %>">
508       <% include( @include ) %>
509       </TD>
510
511 %     if ( $f->{'m2_fields'} ) {
512 %       foreach my $c ( @{ $f->{'m2_fields'} } ) {
513 %         my $column = $c->{field};
514 %         my @column = &{ $column_sub }( %$c, 'fieldnum' => $fieldnum );
515
516           <TD id='<% $field %>__<% $column %>_label<% $fieldnum %>'
517               style='text-align:right;vertical-align:top;
518                      border-top:1px solid black;padding-top:5px;'>
519             <% $c->{'label'} || '' %>
520           </TD>
521           <TD style='border-top:1px solid black;padding-top:3px;'>
522             <% include( @column ) %>
523           </TD>
524 %       }
525 %     }
526
527       </TR>
528
529 %   } else {
530
531       <% include( @include ) %>
532
533 %   }
534 %   if ( $f->{'m2name_table'} || $f->{'o2m_table'} || $f->{'m2m_method'} ) {
535
536       <SCRIPT TYPE="text/javascript">
537
538         var <%$field%>_rownum = <% $g_row %>;
539         var <%$field%>_fieldnum = <% $fieldnum %>;
540
541         function spawn_<%$field%>(what) {
542
543           // only spawn if we're the last element... return if not
544
545           var field_regex = /(\d+)(_[a-z]+)?$/;
546           var match = field_regex.exec(what.name);
547           if ( !match ) {
548             alert(what.name + " didn't match for " + what);
549             return;
550           }
551           if ( match[1] != <%$field%>_fieldnum ) {
552             return;
553           }
554
555           // change the label on the last entry & add a remove button
556           var prev_label = document.getElementById('<% $field %>_label' + <%$field%>_fieldnum );
557           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" >&nbsp;<% $f->{'m2_label'} || $field %>';
558
559           <%$field%>_fieldnum++;
560
561           //get the new widget
562
563 %         $include[0] =~ s(^/elements/tr-)(/elements/);
564 %         my @layer_opt = ( @include,
565 %                           'field'        => $field."MAGIC_NUMBER",
566 %                           'id'           => $field."MAGIC_NUMBER",
567 %                           'layer_prefix' => $field."MAGIC_NUMBER.",
568 %                         );
569 %         warn @layer_opt if $opt{'debug'};
570
571           var newrow =  <% include(@layer_opt, html_only=>1) |js_string %>;
572
573 %         #until the rest have html/js_only
574 %         if ( $type eq 'selectlayers' || $type =~ /^select-cgp_rule_/ ) {
575             var newfunc = <% include(@layer_opt, js_only=>1) |js_string %>;
576 %         } else {
577             var newfunc = '';
578 %         }
579
580           // substitute in the new field name
581           var magic_regex = /MAGIC_NUMBER/g;
582           newrow  = newrow.replace(  magic_regex, <%$field%>_fieldnum );
583           newfunc = newfunc.replace( magic_regex, <%$field%>_fieldnum );
584
585           // evaluate new_func
586           if (window.ActiveXObject) {
587             window.execScript(newfunc);
588           } else { /* (window.XMLHttpRequest) */
589             //window.eval(newfunc);
590             setTimeout(newfunc, 0);
591           }
592
593           // add new row
594
595           //hmm, can't use selectlayers after a tablebreak-title for now
596           var table = document.getElementById('TableNumber<% $tablenum-1 %>');
597
598           var row = table.insertRow(<%$field%>_rownum++);
599
600           var label_cell = document.createElement('TD');
601
602           label_cell.id = '<% $field %>_label' + <%$field%>_fieldnum;
603
604           label_cell.style.textAlign = "right";
605           label_cell.style.verticalAlign = "top";
606           label_cell.style.borderTop = "1px solid black";
607           label_cell.style.paddingTop = "5px";
608
609           label_cell.innerHTML = '<% $label %>';
610
611           row.appendChild(label_cell);
612           
613           var widget_cell = document.createElement('TD');
614
615           widget_cell.style.borderTop = "1px solid black";
616           widget_cell.style.paddingTop = "3px";
617           widget_cell.colSpan = "<% $f->{'colspan'} || 1 %>"
618
619           widget_cell.innerHTML = newrow;
620
621           row.appendChild(widget_cell);
622
623 %         if ( $f->{'m2_fields'} ) {
624 %           foreach my $c ( @{ $f->{'m2_fields'} } ) {
625 %             my $column = $c->{field};
626 %             my @column = &{ $column_sub }(%$c, 'fieldnum' => 'MAGIC_NUMBER');
627
628               var column =  <% include(@column, html_only=>1) |js_string %>;
629               column  = column.replace(  magic_regex, <%$field%>_fieldnum );
630
631               var column_label = document.createElement('TD');
632               column_label.id =
633                 '<% $field %>__<% $column %>_label' + <%$field%>_fieldnum;
634
635               column_label.style.textAlign = "right";
636               column_label.style.verticalAlign = "top";
637               column_label.style.borderTop = "1px solid black";
638               column_label.style.paddingTop = "5px";
639
640               column_label.innerHTML = '<% $c->{'label'} || '' %>';
641
642               row.appendChild(column_label);
643           
644               var column_widget = document.createElement('TD');
645
646               column_widget.style.borderTop = "1px solid black";
647               column_widget.style.paddingTop = "3px";
648
649               column_widget.innerHTML = column;
650
651               row.appendChild(column_widget);
652
653 %           }
654 %         }
655
656 %         if ( $f->{'m2_new_js'} ) {
657             // take out items selected in previous dropdowns
658             var new_element = document.getElementById("<%$field%>" + <%$field%>_fieldnum );
659             <% $f->{'m2_new_js'} %>(new_element);
660
661             if ( new_element.length < 2 ) {
662               //just the ** Select new **, so don't display the row
663               row.style.display = 'none';
664             }
665 %         }
666
667         }
668
669         function remove_<%$field%>(remove_fieldnum) {
670           //alert("remove <%$field%> " + remove_fieldnum);
671           var select = document.getElementById('<%$field%>' + remove_fieldnum);
672
673           if ( ! select ) {
674             alert("can't find element <%$field%>" + remove_fieldnum);
675             return;
676           }
677
678 %         my $warnings = $f->{'m2_remove_warnings'};
679 %         if ( $warnings ) {
680             var sel_value = select.options[select.selectedIndex].value;
681 %           foreach my $value ( keys %$warnings ) {
682               if ( sel_value == '<% $value %>' ) {
683                 if ( ! confirm( <% $warnings->{$value} |js_string %> ) ) {
684                   return;
685                 }
686               }
687 %           }
688 %         }
689
690           select.disabled = 'disabled'; // this seems to prevent it from being submitted on tested browsers so far (IE, moz, konq at least)
691           var label_td = document.getElementById('<%$field%>_label' + remove_fieldnum );
692           label_td.parentNode.style.display = 'none';
693
694 %         if ( $f->{m2_remove_js} ) {
695             var opt = select.options[select.selectedIndex];
696             <% $f->{m2_remove_js} %>( opt.value, opt.text, 'no_match');
697 %         }
698
699         }
700
701       </SCRIPT>
702
703 %   }
704
705 % } 
706
707 <% ref( $opt{'html_table_bottom'} )
708       ? &{ $opt{'html_table_bottom'} }( $object )
709       : $opt{'html_table_bottom'}
710 %>
711
712 </TABLE>
713
714 <% ref( $opt{'html_bottom'} )
715       ? &{ $opt{'html_bottom'} }( $object )
716       : $opt{'html_bottom'}
717 %>
718
719 <BR>
720
721 <INPUT TYPE     = "submit"
722        ID       = "submit"
723        VALUE    = "<% ( !$clone && $object->$pkey() )
724                         ? "Apply changes"
725                         : "Add ". ( $opt{'name'} || $opt{'name_singular'} )
726                    %>"
727 >
728
729 </FORM>
730
731 <% ref( $opt{'html_foot'} )
732       ? &{ $opt{'html_foot'} }( $object )
733       : $opt{'html_foot'}
734 %>
735
736 <% include("/elements/footer.html") %>
737 <%init>
738
739 my(%opt) = @_;
740
741 my $curuser = $FS::CurrentUser::CurrentUser;
742
743 #false laziness w/process.html
744 my $table = $opt{'table'};
745 my $class = "FS::$table";
746 my $pkey = dbdef->table($table)->primary_key; #? $opt{'primary_key'} || 
747 my $fields = $opt{'fields'}
748              #|| [ grep { $_ ne $pkey } dbdef->table($table)->columns ];
749              || [ grep { $_ ne $pkey } fields($table) ];
750 #my @actualfields = map { ref($_) ? $_->{'field'} : $_ } @$fields;
751
752 if ( $cgi->param('redirect') ) {
753   my $session = $cgi->param('redirect');
754   my $pref = $curuser->option("redirect$session");
755   die "unknown redirect session $session\n" unless length($pref);
756   $cgi = new CGI($pref);
757 }
758
759 &{$opt{'begin_callback'}}( $cgi, $fields, \%opt )
760   if $opt{'begin_callback'};
761
762 my %qsearch = (
763     'table'     => $table,
764     'extra_sql' => ( $opt{'agent_virt'}
765                        ? ' AND '. $curuser->agentnums_sql(
766                                     'null_right' => $opt{'agent_null_right'}
767                                   )
768                        : ''
769                    ),
770 );
771
772 my $mode;
773 my $object;
774 my $clone = '';
775 if ( $cgi->param('error') ) {
776
777   $mode = 'error';
778
779   $object = $class->new( {
780     map { $_ => scalar($cgi->param($_)) } fields($table)
781   });
782
783   &{$opt{'error_callback'}}( $cgi, $object, $fields, \%opt )
784     if $opt{'error_callback'};
785
786 } elsif ( $cgi->param('clone') =~ /^(\d+)$/ ) {
787
788   $mode = 'clone';
789
790   $clone = $1;
791
792   $qsearch{'extra_sql'} = ' AND '. $opt{'agent_clone_extra_sql'}
793     if $opt{'agent_clone_extra_sql'};
794
795   $object = qsearchs({ %qsearch, 'hashref' => { $pkey => $clone } })
796     or die "$pkey $clone not found in $table";
797
798   &{$opt{'clone_callback'}}( $cgi, $object, $fields, \%opt )
799     if $opt{'clone_callback'};
800
801   #$object->$pkey('');
802
803   $opt{action} ||= 'Add';
804
805 } elsif ( $cgi->keywords || $cgi->param($pkey) ) { #editing
806
807   $mode = 'edit';
808
809   my $value;
810   if ( $cgi->param($pkey) ) {
811     $value = $cgi->param($pkey)
812   } else { 
813     my( $query ) = $cgi->keywords;
814     $value = $query;
815   }
816   $value =~ /^(\d+)$/ or die "unparsable $pkey";
817   $object = qsearchs({ %qsearch, 'hashref' => { $pkey => $1 } })
818     or die "$pkey $1 not found in $table";
819   
820   warn "$table $pkey => $1"
821     if $opt{'debug'};
822
823   &{$opt{'edit_callback'}}( $cgi, $object, $fields, \%opt )
824     if $opt{'edit_callback'};
825
826 } else { #adding
827
828   $mode = 'new';
829
830   my $hashref = $opt{'new_hashref_callback'}
831                   ? &{$opt{'new_hashref_callback'}}
832                   : {};
833
834   $object = $opt{'new_object_callback'}
835               ? &{$opt{'new_object_callback'}}( $cgi, $hashref, $fields, \%opt )
836               : $class->new( $hashref );
837
838   &{$opt{'new_callback'}}( $cgi, $object, $fields, \%opt )
839     if $opt{'new_callback'};
840
841 }
842
843 &{$opt{'end_callback'}}( $cgi, $object, $fields, \%opt )
844   if $opt{'end_callback'};
845
846 $opt{action} ||= $object->$pkey() ? 'Edit' : 'Add';
847
848 my $title = $opt{action}. ' '. ( $opt{name} || $opt{'name_singular'} );
849
850 my $viewall_url = $p . ( $opt{'viewall_dir'} || 'search' ) . "/$table.html";
851 $viewall_url = $opt{'viewall_url'} if $opt{'viewall_url'};  
852
853 my @menubar = ();
854 if ( $opt{'menubar'} ) {
855   @menubar = @{ $opt{'menubar'} };
856 } else {
857   my $items = $opt{'name'} ? $opt{'name'}.'s' : PL($opt{'name_singular'});
858   @menubar = (
859     "View all $items" => $viewall_url,
860   );
861 }
862
863 </%init>