stray closing /TABLE in the no-ticket case
[freeside.git] / httemplate / elements / selectlayers.html
index 4496892..785ee36 100644 (file)
@@ -14,7 +14,7 @@ Example:
 
     #XXX put this handling it its own selectlayers-fields.html element?
     'layer_prefix' => 'prefix_', #optional prefix for fieldnames
-    'layer_fields' => [ 'layer'  => [ 'fieldname',
+    'layer_fields' => { 'layer'  => [ 'fieldname',
                                       { label => 'fieldname2',
                                         type  => 'text', #implemented:
                                                          # text, money, fixed,
@@ -23,6 +23,7 @@ Example:
                                                          # select, select-agent,
                                                          # select-pkg_class,
                                                          # select-part_referral,
+                                                         # select-taxclass,
                                                          # select-table,
                                                          #XXX tbd:
                                                          # more?
@@ -32,6 +33,7 @@ Example:
                         'layer2' => [ 'l2fieldname',
                                       ...
                                     ],
+                      },
 
     #current values for layer fields above
     'layer_values' => { 'layer'  => { 'fieldname'  => 'current_value',
@@ -44,31 +46,48 @@ Example:
                         ...
                       },
 
+    #or manual control, instead of layer_fields and layer_values above
+    #called with args: my( $layer, $layer_fields, $layer_values, $layer_prefix )
+    'layer_callback' => 
+
     'html_between  => '', #optional HTML displayed between the SELECT and the
                           #layers, scalar or coderef ('field' passed as a param)
     'onchange'     => '', #javascript code run when the SELECT changes
                           # ("what" is the element)
     'js_only'      => 0, #set true to return only the JS portions
     'html_only'    => 0, #set true to return only the HTML portions
+    'select_only'  => 0, #set true to return only the <SELECT> HTML
+    'layers_only'  => 0, #set true to return only the layers <DIV> HTML
   )
 
 </%doc>
-% unless ( $opt{html_only} || $opt{js_only} ) {
+% unless ( grep $opt{$_}, qw(html_only js_only select_only layers_only) ) {
     <SCRIPT TYPE="text/javascript">
 % }
-% unless ( $opt{html_only} ) {
-      //alert('start function define');
+% unless ( grep $opt{$_}, qw(html_only select_only layers_only) ) {
+
+%     if ( $opt{layermap} ) {
+%       my %map = %{ $opt{layermap} };
+        var layermap = { "":"",
+                         <% join(',', map { qq("$_":"$map{$_}") } keys %map ) %>
+                       };
+%     }
+
       function <% $key %>changed(what) {
 
         <% $opt{'onchange'} %>
 
         var <% $key %>layer = what.options[what.selectedIndex].value;
 
-%       foreach my $layer ( keys %$options ) {
-
+%       foreach my $layer ( @layers ) {
+%
+%         if ( $opt{layermap} ) {
+          if ( layermap[ <% $key %>layer ] == "<% $layer %>" ) {
+%         } else {
           if (<% $key %>layer == "<% $layer %>" ) {
+%         }
 
-%           foreach my $not ( grep { $_ ne $layer } keys %$options ) {
+%           foreach my $not ( grep { $_ ne $layer } @layers ) {
 %             my $element = "document.getElementById('${key}d$not').style";
               <% $element %>.display = "none";
               <% $element %>.zIndex = 0;
@@ -84,13 +103,12 @@ Example:
         //<% $opt{'onchange'} %>
 
       }
-      //alert('end function define');
 % }
-% unless ( $opt{html_only} || $opt{js_only} ) {
+% unless ( grep $opt{$_}, qw(html_only js_only select_only layers_only) ) {
     </SCRIPT>
 % }
 %
-% unless ( $opt{js_only} ) {
+% unless ( grep $opt{$_}, qw(js_only layers_only) ) {
 
     <SELECT NAME          = "<% $key %>"
             ID            = "<% $key %>"
@@ -103,24 +121,37 @@ Example:
 
         <OPTION VALUE="<% $option %>"
                 <% $option eq $selected ? ' SELECTED' : '' %>
-        ><% $options->{$option} %></OPTION>
+        ><% $options->{$option} |h %></OPTION>
 
 %     }
 
     </SELECT>
 
+% }
+% unless ( grep $opt{$_}, qw(js_only select_only layers_only) ) {
+
 <% ref($between) ? &{$between}($key) : $between %>
 
-%   foreach my $layer ( keys %$options ) {
+% }
+%
+% unless ( grep $opt{$_}, qw(js_only select_only) ) {
+
+%   foreach my $layer ( @layers ) {
+%     my $selected_layer;
+%     if ( $opt{layermap} ) {
+%       $selected_layer = $opt{layermap}->{$selected};
+%     } else {
+%       $selected_layer = $selected;
+%     }
 
       <DIV ID="<% $key %>d<% $layer %>"
-           STYLE="<% $layer eq $selected
-                       ? 'display: ""  ; z-index: 1'
+           STYLE="<% $selected_layer eq $layer
+                       ? 'display: block; z-index: 1'
                        : 'display: none; z-index: 0'
                   %>"
       >
 
-        <% layer_callback($layer, $layer_fields, $layer_values, $layer_prefix) %>
+        <% &{$layer_callback}($layer, $layer_fields, $layer_values, $layer_prefix) %>
 
       </DIV>
 
@@ -131,6 +162,7 @@ Example:
 
 my $conf = new FS::Conf;
 my $money_char = $conf->config('money_char') || '$';
+my $date_noinit = 0;
 
 </%once>
 <%init>
@@ -149,6 +181,14 @@ tie my %options, 'Tie::IxHash',
 my $between = exists($opt{html_between}) ? $opt{html_between} : '';
 my $options = \%options;
 
+my @layers = ();
+if ( $opt{layermap} ) {
+  my %layers = map { $opt{layermap}->{$_} => 1 } keys %options;
+  @layers = keys %layers;
+} else {
+  @layers = keys %options;
+}
+
 my $selected = exists($opt{curr_value}) ? $opt{curr_value} : '';
 
 #XXX eek.  also eek $layer_fields in the layer_callback() call...
@@ -156,6 +196,8 @@ my $layer_fields = $opt{layer_fields};
 my $layer_values = $opt{layer_values};
 my $layer_prefix = $opt{layer_prefix};
 
+my $layer_callback = $opt{layer_callback} || \&layer_callback;
+
 sub layer_callback {
   my( $layer, $layer_fields, $layer_values, $layer_prefix ) = @_;
 
@@ -179,10 +221,24 @@ sub layer_callback {
     my $type = $lf->{type} || 'text';
 
     my $include = $type;
-    $include = "input-$include" if $include =~ /^(text|money)$/;
-    $include = "tr-$include" unless $include eq 'hidden';
 
-    $html .= include( "/elements/$include.html",
+    if ( $include eq 'date' ) {
+      # several important differences from other tr-*
+      $html .= include( '/elements/tr-input-date-field.html',
+        {
+          'name'  => "$layer_prefix$field",
+          'value' => $value,
+          'label' => $lf->{label},
+          'format'=> $lf->{format},
+          'noinit'=> $date_noinit,
+        }
+      );
+      $date_noinit = 1;
+    }
+    else {
+      $include = "input-$include" if $include =~ /^(text|money|percentage)$/;
+      $include = "tr-$include" unless $include eq 'hidden';
+      $html .= include( "/elements/$include.html",
                         %$lf,
                         'field'      => "$layer_prefix$field",
                         'id'         => "$layer_prefix$field", #separate?
@@ -192,8 +248,8 @@ sub layer_callback {
                         'value'      => ( $lf->{'value'} || $value ), #hmm.
                         'curr_value' => $value,
                     );
-
-  }
+    }
+  } #foreach $field
   $html .= '</TABLE>';
   return $html;
 }