changes dum big "state/county/country" select to three, linked with javascript
authorivan <ivan>
Tue, 19 Mar 2002 17:48:28 +0000 (17:48 +0000)
committerivan <ivan>
Tue, 19 Mar 2002 17:48:28 +0000 (17:48 +0000)
closes: Bug#353

FS/FS/cust_main_county.pm
httemplate/edit/cust_main.cgi
httemplate/edit/process/cust_main.cgi

index 383360b..17fe164 100644 (file)
@@ -1,10 +1,21 @@
 package FS::cust_main_county;
 
 use strict;
-use vars qw( @ISA );
-use FS::Record;
+use vars qw( @ISA @EXPORT_OK $conf
+             @cust_main_county %cust_main_county $countyflag );
+use Exporter;
+use FS::Record qw( qsearch );
 
 @ISA = qw( FS::Record );
+@EXPORT_OK = qw( regionselector );
+
+@cust_main_county = ();
+$countyflag = '';
+
+#ask FS::UID to run this stuff for us later
+$FS::UID::callback{'FS::cust_main_county'} = sub { 
+  $conf = new FS::Conf;
+};
 
 =head1 NAME
 
@@ -25,6 +36,9 @@ FS::cust_main_county - Object methods for cust_main_county objects
 
   $error = $record->check;
 
+  ($county_html, $state_html, $country_html) =
+    FS::cust_main_county::regionselector( $county, $state, $country );
+
 =head1 DESCRIPTION
 
 An FS::cust_main_county object represents a tax rate, defined by locale.
@@ -94,12 +108,126 @@ sub check {
 
 =back
 
-=head1 VERSION
+=head1 SUBROUTINES
 
-$Id: cust_main_county.pm,v 1.1 1999-08-04 09:03:53 ivan Exp $
+=over 4
+
+=item regionselector [ COUNTY STATE COUNTRY [ PREFIX [ ONCHANGE ] ] ]
+
+=cut
+
+sub regionselector {
+  my ( $selected_county, $selected_state, $selected_country,
+       $prefix, $onchange ) = @_;
+  $countyflag=1 if $selected_county;
+
+  unless ( @cust_main_county ) { #cache 
+    @cust_main_county = qsearch('cust_main_county', {} );
+    foreach my $c ( @cust_main_county ) {
+      $countyflag=1 if $c->county;
+      push @{$cust_main_county{$c->country}{$c->state}}, $c->county;
+    }
+  }
+
+  my $script_html = <<END;
+    <SCRIPT>
+    function opt(what,value,text) {
+      var optionName = new Option(text, value, false, false);
+      var length = what.length;
+      what.options[length] = optionName;
+    }
+    function ${prefix}country_changed(what) {
+      country = what.options[what.selectedIndex].text;
+      for ( var i = what.form.${prefix}state.length; i >= 0; i-- )
+          what.form.${prefix}state.options[i] = null;
+END
+      #what.form.${prefix}state.options[0] = new Option('', '', false, true);
+
+  foreach my $country ( sort keys %cust_main_county ) {
+    $script_html .= "\nif ( country == \"$country\" ) {\n";
+    foreach my $state ( sort keys %{$cust_main_county{$country}} ) {
+      my $text = $state || '(n/a)';
+      $script_html .= qq!opt(what.form.${prefix}state, "$state", "$text");\n!;
+    }
+    $script_html .= "}\n";
+  }
+
+  $script_html .= <<END;
+    }
+    function ${prefix}state_changed(what) {
+END
+
+  if ( $countyflag ) {
+    $script_html .= <<END;
+      state = what.options[what.selectedIndex].text;
+      country = what.form.${prefix}country.options[what.form.${prefix}country.selectedIndex].text;
+      for ( var i = what.form.${prefix}county.length; i >= 0; i-- )
+          what.form.${prefix}county.options[i] = null;
+END
+
+    foreach my $country ( sort keys %cust_main_county ) {
+      $script_html .= "\nif ( country == \"$country\" ) {\n";
+      foreach my $state ( sort keys %{$cust_main_county{$country}} ) {
+        $script_html .= "\nif ( state == \"$state\" ) {\n";
+          foreach my $county ( sort @{$cust_main_county{$country}{$state}} ) {
+            my $text = $county || '(n/a)';
+            $script_html .=
+              qq!opt(what.form.${prefix}county, "$county", "$text");\n!;
+          }
+        $script_html .= "}\n";
+      }
+      $script_html .= "}\n";
+    }
+  }
+
+  $script_html .= <<END;
+    }
+    </SCRIPT>
+END
+
+  my $county_html = $script_html;
+  if ( $countyflag ) {
+    $county_html .= qq!<SELECT NAME="${prefix}county" onChange="$onchange">!;
+    $county_html .= '</SELECT>';
+  } else {
+    $county_html .=
+      qq!<INPUT TYPE="hidden" NAME="${prefix}county" VALUE="$selected_county">!;
+  }
+
+  my $state_html = qq!<SELECT NAME="${prefix}state" !.
+                   qq!onChange="${prefix}state_changed(this); $onchange">!;
+  foreach my $state ( sort keys %{ $cust_main_county{$selected_country} } ) {
+    my $text = $state || '(n/a)';
+    my $selected = $state eq $selected_state ? 'SELECTED' : '';
+    $state_html .= "\n<OPTION $selected VALUE=$state>$text</OPTION>"
+  }
+  $state_html .= '</SELECT>';
+
+  $state_html .= '</SELECT>';
+
+  my $country_html = qq!<SELECT NAME="${prefix}country" !.
+                     qq!onChange="${prefix}country_changed(this); $onchange">!;
+  my $countrydefault = $conf->config('countrydefault') || 'US';
+  foreach my $country (
+    sort { ($b eq $countrydefault) <=> ($a eq $countrydefault) or $a cmp $b }
+      keys %cust_main_county
+  ) {
+    my $selected = $country eq $selected_country ? ' SELECTED' : '';
+    $country_html .= "\n<OPTION$selected>$country</OPTION>"
+  }
+  $country_html .= '</SELECT>';
+
+  ($county_html, $state_html, $country_html);
+
+}
+
+=back
 
 =head1 BUGS
 
+regionseletor?  putting web ui components in here?  they should probably live
+somewhere else...
+
 =head1 SEE ALSO
 
 L<FS::Record>, L<FS::cust_main>, L<FS::cust_bill>, schema.html from the base
index 89a946b..39b6ca5 100755 (executable)
@@ -69,7 +69,7 @@ print qq!<FORM ACTION="${p1}process/cust_main.cgi" METHOD=POST NAME="form1">!,
 
 # agent
 
-my $r = qq!<font color="#ff0000">*</font>!;
+my $r = qq!<font color="#ff0000">*</font>&nbsp;!;
 
 my @agents = qsearch( 'agent', {} );
 #die "No agents created!" unless @agents;
@@ -147,7 +147,7 @@ my($last,$first,$ss,$company,$address1,$address2,$city,$zip)=(
 );
 
 print "<BR><BR>Billing address", &itable("#cccccc"), <<END;
-<TR><TH ALIGN="right">${r}Contact name<BR>(last, first)</TH><TD COLSPAN=3>
+<TR><TH ALIGN="right">${r}Contact&nbsp;name<BR>(last,&nbsp;first)</TH><TD COLSPAN=3>
 END
 
 print <<END;
@@ -167,30 +167,24 @@ print <<END;
 <TR><TD ALIGN="right">Company</TD><TD COLSPAN=5><INPUT TYPE="text" NAME="company" VALUE="$company" SIZE=70></TD></TR>
 <TR><TH ALIGN="right">${r}Address</TH><TD COLSPAN=5><INPUT TYPE="text" NAME="address1" VALUE="$address1" SIZE=70></TD></TR>
 <TR><TD ALIGN="right">&nbsp;</TD><TD COLSPAN=5><INPUT TYPE="text" NAME="address2" VALUE="$address2" SIZE=70></TD></TR>
-<TR><TH ALIGN="right">${r}City</TH><TD><INPUT TYPE="text" NAME="city" VALUE="$city"></TD><TH ALIGN="right">${r}State/Country</TH><TD><SELECT NAME="state" SIZE="1">
+<TR><TH ALIGN="right">${r}City</TH><TD><INPUT TYPE="text" NAME="city" VALUE="$city"></TD><TH ALIGN="right">${r}State</TH><TD>
 END
 
-#false laziness with ship_state
+#false laziness with ship state
 my $countrydefault = $conf->config('countrydefault') || 'US';
 $cust_main->country( $countrydefault ) unless $cust_main->country;
+
 $cust_main->state( $conf->config('statedefault') || 'CA' )
   unless $cust_main->state || $cust_main->country ne 'US';
-foreach ( sort {
-     ( $b->country eq $countrydefault ) <=> ( $a->country eq $countrydefault )
-  or $a->country                        cmp $b->country
-  or $a->state                          cmp $b->state
-  or $a->county                         cmp $b->county
-} qsearch('cust_main_county',{}) ) {
-  print "<OPTION";
-  print " SELECTED" if ( $cust_main->state eq $_->state
-                         && $cust_main->county eq $_->county 
-                         && $cust_main->country eq $_->country
-                       );
-  print ">",$_->state;
-  print " (",$_->county,")" if $_->county;
-  print " / ", $_->country;
-}
-print qq!</SELECT></TD><TH>${r}Zip</TH><TD><INPUT TYPE="text" NAME="zip" VALUE="$zip" SIZE=10></TD></TR>!;
+
+my($county_html, $state_html, $country_html) =
+  FS::cust_main_county::regionselector( $cust_main->county,
+                                        $cust_main->state,
+                                        $cust_main->country );
+
+print "$county_html $state_html";
+
+print qq!</TD><TH>${r}Zip</TH><TD><INPUT TYPE="text" NAME="zip" VALUE="$zip" SIZE=10></TD></TR>!;
 
 my($daytime,$night,$fax)=(
   $cust_main->daytime,
@@ -199,12 +193,13 @@ my($daytime,$night,$fax)=(
 );
 
 print <<END;
+<TR><TH ALIGN="right">${r}Country</TH><TD>$country_html</TD></TR>
 <TR><TD ALIGN="right">Day Phone</TD><TD COLSPAN=5><INPUT TYPE="text" NAME="daytime" VALUE="$daytime" SIZE=18></TD></TR>
 <TR><TD ALIGN="right">Night Phone</TD><TD COLSPAN=5><INPUT TYPE="text" NAME="night" VALUE="$night" SIZE=18></TD></TR>
 <TR><TD ALIGN="right">Fax</TD><TD COLSPAN=5><INPUT TYPE="text" NAME="fax" VALUE="$fax" SIZE=12></TD></TR>
 END
 
-print "</TABLE>$required fields<BR>";
+print "</TABLE>${r}required fields<BR>";
 
 # service address
 
@@ -221,7 +216,11 @@ END
 print "      what.form.ship_$_.value = what.form.$_.value;\n"
   for (qw( last first company address1 address2 city zip daytime night fax ));
 print <<END;
+      what.form.ship_country.selectedIndex = what.form.country.selectedIndex;
+      ship_country_changed(what.form.ship_country);
       what.form.ship_state.selectedIndex = what.form.state.selectedIndex;
+      ship_state_changed(what.form.ship_state);
+      what.form.ship_county.selectedIndex = what.form.county.selectedIndex;
     }
   }
   </SCRIPT>
@@ -251,7 +250,7 @@ END
   );
 
   print &itable("#cccccc"), <<END;
-  <TR><TH ALIGN="right">${r}Contact name<BR>(last, first)</TH><TD COLSPAN=5>
+  <TR><TH ALIGN="right">${r}Contact&nbsp;name<BR>(last,&nbsp;first)</TH><TD COLSPAN=5>
 END
 
   print <<END;
@@ -264,30 +263,25 @@ END
   <TR><TD ALIGN="right">Company</TD><TD COLSPAN=5><INPUT TYPE="text" NAME="ship_company" VALUE="$ship_company" SIZE=70 onChange="changed(this)"></TD></TR>
   <TR><TH ALIGN="right">${r}Address</TH><TD COLSPAN=5><INPUT TYPE="text" NAME="ship_address1" VALUE="$ship_address1" SIZE=70 onChange="changed(this)"></TD></TR>
   <TR><TD ALIGN="right">&nbsp;</TD><TD COLSPAN=5><INPUT TYPE="text" NAME="ship_address2" VALUE="$ship_address2" SIZE=70 onChange="changed(this)"></TD></TR>
-  <TR><TH ALIGN="right">${r}City</TH><TD><INPUT TYPE="text" NAME="ship_city" VALUE="$ship_city" onChange="changed(this)"></TD><TH ALIGN="right">${r}State/Country</TH><TD><SELECT NAME="ship_state" SIZE="1" onChange="changed(this)">
+  <TR><TH ALIGN="right">${r}City</TH><TD><INPUT TYPE="text" NAME="ship_city" VALUE="$ship_city" onChange="changed(this)"></TD><TH ALIGN="right">${r}State</TH><TD>
 END
 
   #false laziness with regular state
-  $cust_main->ship_country( $conf->config('countrydefault') || 'US' )
-    unless $cust_main->ship_country;
+  $cust_main->ship_country( $countrydefault ) unless $cust_main->ship_country;
+
   $cust_main->ship_state( $conf->config('statedefault') || 'CA' )
     unless $cust_main->ship_state || $cust_main->ship_country ne 'US';
-  foreach ( sort {
-       ( $b->country eq $countrydefault ) <=> ( $a->country eq $countrydefault )
-    or $a->country                        cmp $b->country
-    or $a->state                          cmp $b->state
-    or $a->county                         cmp $b->county
-  } qsearch('cust_main_county',{}) ) {
-    print "<OPTION";
-    print " SELECTED" if ( $cust_main->ship_state eq $_->state
-                           && $cust_main->ship_county eq $_->county 
-                           && $cust_main->ship_country eq $_->country
-                         );
-    print ">",$_->state;
-    print " (",$_->county,")" if $_->county;
-    print " / ", $_->country;
-  }
-  print qq!</SELECT></TD><TH>${r}Zip</TH><TD><INPUT TYPE="text" NAME="ship_zip" VALUE="$ship_zip" SIZE=10 onChange="changed(this)"></TD></TR>!;
+
+  my($ship_county_html, $ship_state_html, $ship_country_html) =
+    FS::cust_main_county::regionselector( $cust_main->ship_county,
+                                          $cust_main->ship_state,
+                                          $cust_main->ship_country,
+                                          'ship_',
+                                          'changed(this)', );
+
+  print "$ship_county_html $ship_state_html";
+
+  print qq!</TD><TH>${r}Zip</TH><TD><INPUT TYPE="text" NAME="ship_zip" VALUE="$ship_zip" SIZE=10 onChange="changed(this)"></TD></TR>!;
 
   my($ship_daytime,$ship_night,$ship_fax)=(
     $cust_main->ship_daytime,
@@ -296,12 +290,13 @@ END
   );
 
   print <<END;
+  <TR><TH ALIGN="right">${r}Country</TH><TD>$ship_country_html</TD></TR>
   <TR><TD ALIGN="right">Day Phone</TD><TD COLSPAN=5><INPUT TYPE="text" NAME="ship_daytime" VALUE="$ship_daytime" SIZE=18 onChange="changed(this)"></TD></TR>
   <TR><TD ALIGN="right">Night Phone</TD><TD COLSPAN=5><INPUT TYPE="text" NAME="ship_night" VALUE="$ship_night" SIZE=18 onChange="changed(this)"></TD></TR>
   <TR><TD ALIGN="right">Fax</TD><TD COLSPAN=5><INPUT TYPE="text" NAME="ship_fax" VALUE="$ship_fax" SIZE=12 onChange="changed(this)"></TD></TR>
 END
 
-  print "</TABLE>$required fields<BR>";
+  print "</TABLE>${r}required fields<BR>";
 
 }
 
index c8038ec..6ce60d1 100755 (executable)
@@ -8,19 +8,6 @@ $cgi->param('tax','') unless defined $cgi->param('tax');
 
 $cgi->param('refnum', (split(/:/, ($cgi->param('refnum'))[0] ))[0] );
 
-$cgi->param('state') =~ /^(\w*)( \(([\w ]+)\))? ?\/ ?(\w+)$/
-  or die "Oops, illegal \"state\" param: ". $cgi->param('state');
-$cgi->param('state', $1);
-$cgi->param('county', $3 || '');
-$cgi->param('country', $4);
-
-$cgi->param('ship_state') =~ /^(\w*)( \(([\w ]+)\))? ?\/ ?(\w+)$/
-  or $cgi->param('ship_state') =~ /^(((())))$/
-  or die "Oops, illegal \"ship_state\" param: ". $cgi->param('ship_state');
-$cgi->param('ship_state', $1);
-$cgi->param('ship_county', $3 || '');
-$cgi->param('ship_country', $4);
-
 my $payby = $cgi->param('payby');
 if ( $payby ) {
   $cgi->param('payinfo', $cgi->param( $payby. '_payinfo' ) );