From 9e342300c380e29af1b9678f1a9604609e0061b6 Mon Sep 17 00:00:00 2001 From: ivan Date: Sat, 10 Sep 2005 14:50:57 +0000 Subject: [PATCH] ajax-style xmlhttprequest state/county/country selector! --- CREDITS | 5 ++ httemplate/edit/cust_main.cgi | 14 ++- httemplate/edit/cust_main/contact.html | 32 ++++--- httemplate/edit/cust_main/select-country.html | 68 +++++++++++++++ httemplate/edit/cust_main/select-county.html | 87 +++++++++++++++++++ httemplate/edit/cust_main/select-state.html | 27 ++++++ httemplate/elements/xmlhttp.html | 65 ++++++++++++++ httemplate/misc/counties.cgi | 17 ++++ httemplate/misc/states.cgi | 16 ++++ 9 files changed, 317 insertions(+), 14 deletions(-) create mode 100644 httemplate/edit/cust_main/select-country.html create mode 100644 httemplate/edit/cust_main/select-county.html create mode 100644 httemplate/edit/cust_main/select-state.html create mode 100644 httemplate/elements/xmlhttp.html create mode 100644 httemplate/misc/counties.cgi create mode 100644 httemplate/misc/states.cgi diff --git a/CREDITS b/CREDITS index 0e6059909..450f26704 100644 --- a/CREDITS +++ b/CREDITS @@ -153,5 +153,10 @@ by Erik Bosrup), licensed under the terms of the Artistic license Ricardo SIGNES has contributed a bunch of patches to clean up and refactor various stuff in the module layer. Thanks! +XMLHttpRequest implementation based on the SAJAX toolkit, licensed under the +terms of the BSD license. +(c) copyright 2005 modernmethod, inc +Perl backend version (c) copyright 2005 Nathan Schmidt + Everything else is my (Ivan Kohler ) fault. diff --git a/httemplate/edit/cust_main.cgi b/httemplate/edit/cust_main.cgi index a8f202fbb..d10b21a4a 100755 --- a/httemplate/edit/cust_main.cgi +++ b/httemplate/edit/cust_main.cgi @@ -173,11 +173,17 @@ function bill_changed(what) { <% for (qw( last first company address1 address2 city zip daytime night fax )) { %> what.form.ship_<%=$_%>.value = what.form.<%=$_%>.value; <% } %> + 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; + function fix_ship_state() { + what.form.ship_state.selectedIndex = what.form.state.selectedIndex; + } + 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) { diff --git a/httemplate/edit/cust_main/contact.html b/httemplate/edit/cust_main/contact.html index d3b7b167f..e0cd06f56 100644 --- a/httemplate/edit/cust_main/contact.html +++ b/httemplate/edit/cust_main/contact.html @@ -14,14 +14,23 @@ $cust_main->set($pre.'state', $statedefault ) unless $cust_main->get($pre.'state') || $cust_main->get($pre.'country') ne $countrydefault; -my($county_html, $state_html, $country_html) = - FS::cust_main_county::regionselector( $cust_main->get($pre.'county'), - $cust_main->get($pre.'state'), - $cust_main->get($pre.'country'), - $pre, - $onchange, - $disabled, - ); +#my($county_html, $state_html, $country_html) = +# FS::cust_main_county::regionselector( $cust_main->get($pre.'county'), +# $cust_main->get($pre.'state'), +# $cust_main->get($pre.'country'), +# $pre, +# $onchange, +# $disabled, +# ); + +my %select_hash = ( + 'county' => $cust_main->get($pre.'county'), + 'state' => $cust_main->get($pre.'state'), + 'country' => $cust_main->get($pre.'country'), + 'prefix' => $pre, + 'onchange' => $onchange, + 'disabled' => $disabled, +); my $daytime_label = FS::Msgcat::_gettext('daytime') || 'Day Phone'; my $night_label = FS::Msgcat::_gettext('night') || 'Night Phone'; @@ -75,7 +84,10 @@ my $r = qq!* !; > <%=$r%>State - <%= $county_html. $state_html %> + + <%= include('select-county.html', %select_hash ) %> + <%= include('select-state.html', %select_hash ) %> + <%=$r%>Zip > @@ -84,7 +96,7 @@ my $r = qq!* !; <%=$r%>Country - <%= $country_html %> + <%= include('select-country.html', %select_hash ) %> diff --git a/httemplate/edit/cust_main/select-country.html b/httemplate/edit/cust_main/select-country.html new file mode 100644 index 000000000..44f4f0a55 --- /dev/null +++ b/httemplate/edit/cust_main/select-country.html @@ -0,0 +1,68 @@ +<% + + my %opt = @_; + foreach my $opt (qw( county state country prefix onchange disabled )) { + $opt{$_} = '' unless exists($opt{$_}) && defined($opt{$_}); + } + + my $conf = new FS::Conf; + my $countrydefault = $conf->config('countrydefault') || 'US'; + +%> + +<%= include('/elements/xmlhttp.html', $p.'misc/states.cgi', $opt{'prefix'}. 'get_states') %> + + + + + diff --git a/httemplate/edit/cust_main/select-county.html b/httemplate/edit/cust_main/select-county.html new file mode 100644 index 000000000..70a8f9477 --- /dev/null +++ b/httemplate/edit/cust_main/select-county.html @@ -0,0 +1,87 @@ +<% + + my %opt = @_; + foreach my $opt (qw( county state country prefix onchange disabled )) { + $opt{$_} = '' unless exists($opt{$_}) && defined($opt{$_}); + } + + 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]; + +%> + +<% if ( $countyflag ) { %> + + <%= include('/elements/xmlhttp.html', $p.'misc/counties.cgi', $opt{'prefix'}. 'get_counties' ) %> + + + + + +<% } else { %> + + + + + +<% } %> diff --git a/httemplate/edit/cust_main/select-state.html b/httemplate/edit/cust_main/select-state.html new file mode 100644 index 000000000..98e685ab8 --- /dev/null +++ b/httemplate/edit/cust_main/select-state.html @@ -0,0 +1,27 @@ +<% + + my %opt = @_; + foreach my $opt (qw( county state country prefix onchange disabled )) { + $opt{$_} = '' unless exists($opt{$_}) && defined($opt{$_}); + } + +%> + + + diff --git a/httemplate/elements/xmlhttp.html b/httemplate/elements/xmlhttp.html new file mode 100644 index 000000000..425e28e3d --- /dev/null +++ b/httemplate/elements/xmlhttp.html @@ -0,0 +1,65 @@ +<% + my ( $url, @subs ) = @_; + + $url .= ( ($url =~ /\?/) ? '&' : '?' ). + 'sub='; + +%> + + diff --git a/httemplate/misc/counties.cgi b/httemplate/misc/counties.cgi new file mode 100644 index 000000000..80ae616c9 --- /dev/null +++ b/httemplate/misc/counties.cgi @@ -0,0 +1,17 @@ +<% + + 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) %> ] diff --git a/httemplate/misc/states.cgi b/httemplate/misc/states.cgi new file mode 100644 index 000000000..cff2c9774 --- /dev/null +++ b/httemplate/misc/states.cgi @@ -0,0 +1,16 @@ +<% + + my $country = $cgi->param('arg'); + + my @states = + sort + map { s/[\n\r]//g; $_; } + map { $_->state; } + qsearch( 'cust_main_county', + { 'country' => $country }, + 'DISTINCT ON ( state ) *', + ) + ; + + +%>[ <%= join(', ', map { qq("$_") } @states) %> ] -- 2.20.1