fix county selector
authorivan <ivan>
Wed, 27 Dec 2006 01:07:28 +0000 (01:07 +0000)
committerivan <ivan>
Wed, 27 Dec 2006 01:07:28 +0000 (01:07 +0000)
httemplate/edit/cust_main.cgi
httemplate/edit/cust_main/select-county.html
httemplate/misc/counties.cgi

index e2ed0f1..896472d 100755 (executable)
@@ -204,17 +204,19 @@ function bill_changed(what) {
     what.form.ship_<%$_%>.value = what.form.<%$_%>.value;
 % } 
 
-
     what.form.ship_country.selectedIndex = what.form.country.selectedIndex;
+
+    function fix_ship_county() {
+      what.form.ship_county.selectedIndex = what.form.county.selectedIndex;
+    }
+
     function fix_ship_state() {
       what.form.ship_state.selectedIndex = what.form.state.selectedIndex;
+      ship_state_changed(what.form.ship_state, fix_ship_county );
     }
+
     ship_country_changed(what.form.ship_country, fix_ship_state );
 
-    function fix_ship_county() {
-      what.form.ship_county.selectedIndex = what.form.county.selectedIndex;
-    }
-    ship_state_changed(what.form.ship_state, fix_ship_county );
   }
 }
 function samechanged(what) {
@@ -242,7 +244,7 @@ function samechanged(what) {
 %  my $disabledselect = '';
 %  unless ( $cust_main->ship_last && $same ne 'Y' ) {
 %    $checked = 'CHECKED';
-%    $disabled = 'DISABLED style="background-color: #dddddd"';
+%    $disabled = 'DISABLED STYLE="background-color: #dddddd"';
 %    foreach (
 %      qw( last first company address1 address2 city county state zip country
 %          daytime night fax )
index c9b64bf..5b06821 100644 (file)
@@ -16,8 +16,8 @@
   
     function <% $opt{'prefix'} %>state_changed(what, callback) {
 
-      state = what.options[what.selectedIndex].text;
-      country = what.form.<% $opt{'prefix'} %>country.options[what.form.<% $opt{'prefix'} %>country.selectedIndex].text;
+      state = what.options[what.selectedIndex].value;
+      country = what.form.<% $opt{'prefix'} %>country.options[what.form.<% $opt{'prefix'} %>country.selectedIndex].value;
   
       function <% $opt{'prefix'} %>update_counties(counties) {
 
             opt(what.form.<% $opt{'prefix'} %>county, countiesArray[s], countyLabel);
         }
 
+        if ( countiesArray.length > 1 ) { 
+          what.form.<% $opt{'prefix'} %>county.style.display = '';
+        } else {
+          what.form.<% $opt{'prefix'} %>county.style.display = 'none';
+        }
+
         //run the callback
         if ( callback != null ) 
           callback();
   </SCRIPT>
 
   <SELECT NAME="<% $opt{'prefix'} %>county" onChange="<% $opt{'onchange'} %>" <% $opt{'disabled'} %>>
-% foreach my $county (
-%       sort
-%       map { $_->county }
-%       qsearch('cust_main_county', { 'state'   => $opt{'state'},
-%                                     'country' => $opt{'country'},
-%                                   }
-%              )
-% ) {
+
+% foreach my $county ( @counties ) {
 
     <OPTION VALUE="<% $county %>"<% $county eq $opt{'county'} ? ' SELECTED' : '' %>><% $county %>
 
 % } 
 
 <%init>
+
 my %opt = @_;
 foreach my $opt (qw( county state country prefix onchange disabled )) {
   $opt{$_} = '' unless exists($opt{$_}) && defined($opt{$_});
 }
 
+my @counties = ();
+if ( $countyflag ) {
+
+  @counties =
+      sort
+      map { $_->county }
+      qsearch( {
+        'select'  => 'DISTINCT county',
+        'table'   => 'cust_main_county',
+        'hashref' => { 'state'   => $opt{'state'},
+                       'country' => $opt{'country'},
+                     },
+      } );
+
+  # this is very hacky
+  unless ( scalar(@counties) > 1 ) {
+    if ( $opt{'disabled'} =~ /STYLE=/i ) {
+      $opt{'disabled'} =~ s/STYLE="([^"]+)"/STYLE="$1; display:none"/i;
+    } else {
+      $opt{'disabled'} .= ' STYLE="display:none"';
+    }
+  }
+
+}
+
+</%init>
+<%once>
+
 my $sql = "SELECT COUNT(*) FROM cust_main_county".
           " WHERE county IS NOT NULL AND county != ''";
 my $sth = dbh->prepare($sql) or die dbh->errstr;
 $sth->execute or die $sth->errstr;
 my $countyflag = $sth->fetchrow_arrayref->[0];
-</%init>
+
+</%once>
index c9eb985..940cba7 100644 (file)
@@ -1,18 +1,27 @@
-%
-%
-%  my( $state, $country ) = $cgi->param('arg');
-%
-%  my @counties = 
-%     sort
-%     map { s/[\n\r]//g; $_; }
-%     map { $_->county; }
-%     qsearch( 'cust_main_county',
-%              { 'state'   => $state,
-%                'country' => $country,
-%              },
-%            )
-%  ;
-%
-%
-%
 [ <% join(', ', map { qq("$_") } @counties) %> ]
+<%init>
+
+my $DEBUG = 0;
+
+my( $state, $country ) = $cgi->param('arg');
+
+warn "fetching counties for $state / $country \n"
+  if $DEBUG;
+
+my @counties = 
+    sort
+    map { s/[\n\r]//g; $_; }
+    map { $_->county; }
+    qsearch( {
+      'select'  => 'DISTINCT county',
+      'table'   => 'cust_main_county',
+      'hashref' => { 'state'   => $state,
+                     'country' => $country,
+                   },
+    } )
+;
+
+warn "counties: ". join(', ', map { qq("$_") } @counties). "\n"
+  if $DEBUG;
+
+</%init>