fix one-time charges and package customization for employees who don't have 'Edit...
[freeside.git] / httemplate / edit / elements / edit.html
index 86eb2b3..f4e4195 100644 (file)
@@ -3,8 +3,12 @@
 Example:
 
   include( 'elements/edit.html',  
-    'name'  =>
-    'table' =>
+    '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',
@@ -32,7 +36,7 @@ Example:
                                        #fixed - display fixed value from object or here
                                        #fixed-country
                                        #fixed-state
-                    'value' => 'Y', #for checkbox, title, fixed, fixedhidden
+                    'value' => 'Y', #for checkbox, title, fixed, hidden
                     'disabled' => 0,
                     'onchange' => 'javascript_function',
 
@@ -89,29 +93,18 @@ Example:
     #agent virtualization
     'agent_virt'       => 1,
     'agent_null_right' => 'Access Right Name',
-   
-    #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 before display to return a different value
-    'value_callback' => sub { my( $columname, $value ) = @_; },
+    'viewall_dir' => '', #'search' or 'browse', defaults to 'search'
 
-    #XXX describe
-    'field_callback' => sub { },
+    # overrides default popurl(1)."process/$table.html"
+    'post_url' => popurl(1).'process/something', 
 
-    'viewall_dir' => '', #'search' or 'browse', defaults to 'search'
+    #we're in a popup (no title/menu/searchboxes)
+    'popup' => 1,
+
+    ###
+    # HTML callbacks
+    ###
 
     'html_init'   => '', #after the header/menubar
 
@@ -129,12 +122,50 @@ Example:
     #at the very bottom (well, as low as you can go from here)
     'html_foot'  => '',
 
-    # overrides default popurl(1)."process/$table.html"
-    'post_url' => popurl(1).'process/something', 
+    ###
+    # initialization callbacks
+    ###
 
-    #we're in a popup (no title/menu/searchboxes)
-    'popup' => 1,
+    ###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>
@@ -160,7 +191,7 @@ Example:
 <FORM ACTION="<% $url %>" METHOD=POST NAME="edit_topform">
 
 <INPUT TYPE="hidden" NAME="svcdb" VALUE="<% $table %>">
-<INPUT TYPE="hidden" NAME="<% $pkey %>" VALUE="<% $object->$pkey() %>">
+<INPUT TYPE="hidden" NAME="<% $pkey %>" VALUE="<% $clone ? '' : $object->$pkey() %>">
 
 <FONT SIZE="+1"><B>
 <% ( $opt{labels} && exists $opt{labels}->{$pkey} )
@@ -168,7 +199,7 @@ Example:
       : $pkey
 %>
 </B></FONT>
-#<% $object->$pkey() || "(NEW)" %>
+#<% ( !$clone && $object->$pkey() ) || "(NEW)" %>
 
 % my $tablenum = 0;
 <TABLE ID="TableNumber<% $tablenum++ %>" BGCOLOR="#cccccc" BORDER=0 CELLSPACING=0>
@@ -179,7 +210,7 @@ Example:
 %                       @$fields
 %                 ) {
 %
-%   &{ $opt{'field_callback'} }( $f )
+%   my $trash = &{ $opt{'field_callback'} }( $cgi, $object, $f )
 %     if $opt{'field_callback'};
 %
 %   my $field = $f->{'field'};
@@ -204,15 +235,15 @@ Example:
 %
 %     #text and derivitives
 %     'size'          => $f->{'size'},
+%     'maxlength'     => $f->{'maxlength'},
 %
-%     #checkbox, title, fixed, fixedhidden
+%     #checkbox, title, fixed, hidden
 %     #& deprecated weird value hashref used only by reason.html
 %     'value'         => $f->{'value'},
 %
 %     #select(-*)
 %     'options'       => $f->{'options'},
 %     'labels'        => $f->{'labels'},
-%     'empty_label'   => $f->{'empty_label'},
 %     'multiple'      => $f->{'multiple'},
 %     'disable_empty' => $f->{'disable_empty'},
 %     #select-reason
@@ -227,10 +258,27 @@ Example:
 %     'disabled'      => $f->{'disabled'},
 %   );
 %
-%   #select-table
+%   #selectlayers, others?
+%   $include_common{$_} = $f->{$_}
+%     foreach grep exists($f->{$_}),
+%                  qw( js_only html_only select_only layers_only cell_style);
+%
+%   #select-*
+%   $include_common{$_} = $f->{$_}
+%     foreach grep exists($f->{$_}), qw( empty_label );
+%
+%   #select-table, checkboxes-table
 %   $include_common{$_} = $f->{$_}
 %     foreach grep exists($f->{$_}), qw( table name_col );
 %
+%   #checkboxes-table
+%   $include_common{$_} = $f->{$_}
+%     foreach grep exists($f->{$_}), qw( target_table link_table );
+%
+%   #*-table
+%   $include_common{$_} = $f->{$_}
+%     foreach grep exists($f->{$_}), qw( hashref agent_virt agent_null_right );
+%
 %   if ( $type eq 'tablebreak-tr-title' ) {
 %     $include_common{'table_id'} = 'TableNumber'. $tablenum++
 %   }
@@ -266,7 +314,7 @@ Example:
 %     $g_row++ if $type eq 'title';
 %   } else {
 %     if ( $type eq 'columnstart' ) {
-%       push @g_row_stack, $g_row++;
+%       push @g_row_stack, $g_row;
 %       $g_row = 0;
 %     #} elsif ( $type eq 'columnnext' ) {
 %     } elsif ( $type eq 'columnend' ) {
@@ -293,7 +341,7 @@ Example:
 %     my @existing = ();
 %     if ( $mode eq 'error' ) {
 %       @existing = &{ $f->{'m2_error_callback'} }( $cgi, $object );
-%     } elsif ( $object->$pkey() ) { # $mode eq 'edit'
+%     } elsif ( $object->$pkey() ) { # $mode eq 'edit'||'clone'
 %       @existing = $object->$table();
 %      warn scalar(@existing). " from $object->$table: ". join('/', @existing)
 %        if $opt{'debug'};
@@ -350,7 +398,7 @@ Example:
 %     'object'     => $object,
 %     'cgi'        => $cgi,
 %     'onchange'   => $onchange,
-%     'cell_style'   => ( $fieldnum ? 'border-top:1px solid black' : '' ),
+%     ( $fieldnum ? ('cell_style' => 'border-top:1px solid black') : () ),
 %   );
 
     <% include( @include ) %>
@@ -507,7 +555,7 @@ Example:
 
 <BR>
 
-<INPUT TYPE="submit" ID="submit" VALUE="<% $object->$pkey() ? "Apply changes" : "Add $opt{'name'}" %>">
+<INPUT TYPE="submit" ID="submit" VALUE="<% ( !$clone && $object->$pkey() ) ? "Apply changes" : "Add $opt{'name'}" %>">
 
 </FORM>
 
@@ -539,8 +587,22 @@ if ( $cgi->param('redirect') ) {
   $cgi = new CGI($pref);
 }
 
-my $object;
+&{$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';
@@ -552,6 +614,21 @@ if ( $cgi->param('error') ) {
   &{$opt{'error_callback'}}($cgi, $object, $fields, \%opt )
     if $opt{'error_callback'};
 
+} elsif ( $cgi->param('clone') =~ /^(\d+)$/ ) {
+
+  $mode = 'clone';
+
+  $clone = $1;
+
+  $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';
@@ -564,16 +641,8 @@ if ( $cgi->param('error') ) {
     $value = $query;
   }
   $value =~ /^(\d+)$/ or die "unparsable $pkey";
-  $object = qsearchs({
-    'table'     => $table,
-    'hashref'   => { $pkey => $1 },
-    'extra_sql' => ( $opt{'agent_virt'}
-                       ? ' AND '. $curuser->agentnums_sql(
-                                    'null_right' => $opt{'agent_null_right'}
-                                  )
-                       : ''
-                   ),
-  });
+  $object = qsearchs({ %qsearch, 'hashref' => { $pkey => $1 } });
+  
   warn "$table $pkey => $1"
     if $opt{'debug'};
 
@@ -597,6 +666,9 @@ if ( $cgi->param('error') ) {
 
 }
 
+&{$opt{'end_callback'}}( $cgi, $object, $fields, \%opt )
+  if $opt{'end_callback'};
+
 $opt{action} ||= $object->$pkey() ? 'Edit' : 'Add';
 
 my $title = $opt{action}. ' '. $opt{name};
@@ -608,9 +680,9 @@ my @menubar = ();
 if ( $opt{'menubar'} ) {
   @menubar = @{ $opt{'menubar'} };
 } else {
+  my $items = $opt{'name'} ? $opt{'name'}.'s' : PL($opt{'name_singular'});
   @menubar = (
-    #eventually use Lingua::bs to pluralize
-    "View all $opt{'name'}s" => $viewall_url,
+    "View all $items" => $viewall_url,
   );
 }