summaryrefslogtreecommitdiff
path: root/httemplate/elements
diff options
context:
space:
mode:
Diffstat (limited to 'httemplate/elements')
-rw-r--r--httemplate/elements/select-country.html120
-rw-r--r--httemplate/elements/select-county.html152
-rw-r--r--httemplate/elements/select-did.html5
-rw-r--r--httemplate/elements/select-state.html45
4 files changed, 314 insertions, 8 deletions
diff --git a/httemplate/elements/select-country.html b/httemplate/elements/select-country.html
new file mode 100644
index 000000000..c4368c271
--- /dev/null
+++ b/httemplate/elements/select-country.html
@@ -0,0 +1,120 @@
+<%doc>
+
+Example:
+
+ include( '/elements/select-country.html',
+ #recommended
+ country => $current_country,
+
+ #optional
+ prefix => $optional_unique_prefix,
+ onchange => $javascript,
+ disabled => 0, #bool
+ disable_empty => 1, #defaults to 1, disable the empty option
+ empty_label => 'all', #label for empty option
+ disable_stateupdate => 0, #bool - disabled update of the select-state.html
+
+ );
+
+</%doc>
+% unless ( $opt{'disable_stateupdate'} ) {
+
+ <% include('/elements/xmlhttp.html',
+ 'url' => $p.'misc/states.cgi',
+ 'subs' => [ $pre. 'get_states' ],
+ )
+ %>
+
+ <SCRIPT TYPE="text/javascript">
+
+ function opt(what,value,text) {
+ var optionName = new Option(text, value, false, false);
+ var length = what.length;
+ what.options[length] = optionName;
+ }
+
+ function <% $pre %>country_changed(what, callback) {
+
+ country = what.options[what.selectedIndex].value;
+
+ function <% $pre %>update_states(states) {
+
+ // blank the current state list
+ for ( var i = what.form.<% $pre %>state.length; i >= 0; i-- )
+ what.form.<% $pre %>state.options[i] = null;
+
+ // add the new states
+ var statesArray = eval('(' + states + ')' );
+ for ( var s = 0; s < statesArray.length; s=s+2 ) {
+ var stateLabel = statesArray[s+1];
+ if ( stateLabel == "" )
+ stateLabel = '(n/a)';
+ opt(what.form.<% $pre %>state, statesArray[s], stateLabel);
+ }
+
+ //run the callback
+ if ( callback != null )
+ callback();
+ }
+
+ // go get the new states
+ <% $pre %>get_states( country, <% $pre %>update_states );
+
+ }
+
+ </SCRIPT>
+
+% }
+
+<SELECT NAME = "<% $pre %>country"
+ ID = "<% $pre %>country"
+ onChange = "<% $onchange %>"
+ <% $opt{'disabled'} %>
+>
+
+% unless ( $opt{'disable_empty'} ) {
+ <OPTION VALUE=""><% $opt{'empty_label'} || '(all)' %>
+% }
+
+% foreach my $country ( @all_countries ) {
+
+ <OPTION VALUE="<% $country |h %>"
+ <% $country eq $opt{'country'} ? ' SELECTED' : '' %>
+ ><% code2country($country). " ($country)" %>
+
+% }
+
+</SELECT>
+
+<%init>
+
+my %opt = @_;
+foreach my $opt (qw( country prefix onchange disabled disable_stateupdate )) {
+ $opt{$opt} = '' unless exists($opt{$opt}) && defined($opt{$opt});
+}
+
+$opt{'disable_empty'} = 1 unless exists($opt{'disable_empty'});
+
+my $pre = $opt{'prefix'};
+
+my $onchange =
+ ( $opt{'disable_stateupdate'} ? '' : $pre.'country_changed(this); ' ).
+ $opt{'onchange'};
+
+my $conf = new FS::Conf;
+my $default = $conf->config('countrydefault') || 'US';
+
+my @all_countries = (
+ sort { ($b eq $default) <=> ($a eq $default)
+ or code2country($a) cmp code2country($b)
+ }
+ map { $_->country }
+ qsearch({
+ 'select' => 'country',
+ 'table' => 'cust_main_county',
+ 'hashref' => {},
+ 'extra_sql' => 'GROUP BY country',
+ })
+ );
+
+</%init>
diff --git a/httemplate/elements/select-county.html b/httemplate/elements/select-county.html
new file mode 100644
index 000000000..8ef34dbe6
--- /dev/null
+++ b/httemplate/elements/select-county.html
@@ -0,0 +1,152 @@
+<%doc>
+
+Example:
+
+ include( '/elements/select-county.html',
+ #recommended
+ country => $current_country,
+ state => $current_state,
+ county => $current_county,
+
+ #optional
+ prefix => $optional_unique_prefix,
+ onchange => $javascript,
+ disabled => 0, #bool
+ disable_empty => 1, #defaults to 1, disable the empty option
+ empty_label => 'all', #label for empty option
+ );
+
+</%doc>
+% if ( $countyflag ) {
+
+ <% include('/elements/xmlhttp.html',
+ 'url' => $p.'misc/counties.cgi',
+ 'subs' => [ $pre. 'get_counties' ],
+ )
+ %>
+
+ <SCRIPT TYPE="text/javascript">
+
+ function opt(what,value,text) {
+ var optionName = new Option(text, value, false, false);
+ var length = what.length;
+ what.options[length] = optionName;
+ }
+
+ function <% $pre %>state_changed(what, callback) {
+
+ state = what.options[what.selectedIndex].value;
+ country = what.form.<% $pre %>country.options[what.form.<% $pre %>country.selectedIndex].value;
+
+ function <% $pre %>update_counties(counties) {
+
+ // blank the current county list
+ for ( var i = what.form.<% $pre %>county.length; i >= 0; i-- )
+ what.form.<% $pre %>county.options[i] = null;
+
+ // add the new counties
+ var countiesArray = eval('(' + counties + ')' );
+ for ( var s = 0; s < countiesArray.length; s++ ) {
+ var countyLabel = countiesArray[s];
+ if ( countyLabel == "" )
+ countyLabel = '(n/a)';
+ opt(what.form.<% $pre %>county, countiesArray[s], countyLabel);
+ }
+
+ var countyFormLabel = document.getElementById('<% $pre %>countylabel');
+
+ if ( countiesArray.length > 1 ) {
+ what.form.<% $pre %>county.style.display = '';
+ countyFormLabel.style.visibility = 'visible';
+ } else {
+ what.form.<% $pre %>county.style.display = 'none';
+ countyFormLabel.style.visibility = 'hidden';
+ }
+
+ //run the callback
+ if ( callback != null )
+ callback();
+ }
+
+ // go get the new counties
+ <% $pre %>get_counties( state, country, <% $pre %>update_counties );
+
+ }
+
+ </SCRIPT>
+
+ <SELECT NAME = "<% $pre %>county"
+ ID = "<% $pre %>county"
+ onChange= "<% $opt{'onchange'} %>"
+ <% $opt{'disabled'} %>
+ >
+
+% unless ( $opt{'disable_empty'} ) {
+ <OPTION VALUE="" <% $opt{county} eq '' ? 'SELECTED' : '' %>><% $opt{empty_label} %>
+% }
+
+% foreach my $county ( @counties ) {
+
+ <OPTION VALUE="<% $county |h %>"
+ <% $county eq $opt{'county'} ? 'SELECTED' : '' %>
+ ><% $county eq $opt{'empty_data_value'} ? $opt{'empty_data_label'} : $county %>
+
+% }
+
+ </SELECT>
+
+% } else {
+
+ <SCRIPT TYPE="text/javascript">
+ function <% $pre %>state_changed(what) {
+ }
+ </SCRIPT>
+
+ <SELECT NAME = "<% $pre %>county"
+ ID = "<% $pre %>county"
+ STYLE = "display:none"
+ >
+ <OPTION SELECTED VALUE="<% $opt{'county'} |h %>">
+ </SELECT>
+
+% }
+
+<%init>
+
+my %opt = @_;
+foreach my $opt (qw( county state country prefix onchange disabled
+ empty_value )) {
+ $opt{$opt} = '' unless exists($opt{$opt}) && defined($opt{$opt});
+}
+
+$opt{'disable_empty'} = 1 unless exists($opt{'disable_empty'});
+
+my $pre = $opt{'prefix'};
+
+my @counties = ();
+if ( $countyflag ) {
+
+ @counties = map { length($_) ? $_ : $opt{'empty_data_value'} }
+ counties( $opt{'state'}, $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];
+
+</%once>
diff --git a/httemplate/elements/select-did.html b/httemplate/elements/select-did.html
index a8ef09d61..069516476 100644
--- a/httemplate/elements/select-did.html
+++ b/httemplate/elements/select-did.html
@@ -18,8 +18,9 @@ Example:
<TR>
<TD>
<% include('/elements/select-state.html',
- 'country' => $country,
- 'empty' => 'Select state',
+ 'country' => $country,
+ 'disable_empty' => 0,
+ 'empty_label' => 'Select state',
)
%>
</TD>
diff --git a/httemplate/elements/select-state.html b/httemplate/elements/select-state.html
index 4f1c056b5..f7ac2c7bf 100644
--- a/httemplate/elements/select-state.html
+++ b/httemplate/elements/select-state.html
@@ -1,12 +1,35 @@
-<SELECT NAME="<% $opt{'prefix'} %>state" onChange="<% $opt{'prefix'} %>state_changed(this); <% $opt{'onchange'} %>" <% $opt{'disabled'} %>>
+<%doc>
-% if ($opt{empty}) {
- <OPTION VALUE=""<% $opt{state} eq '' ? ' SELECTED' : '' %>><% $opt{empty} %>
+Example:
+
+ include( '/elements/select-state.html',
+ #recommended
+ country => $current_country,
+ state => $current_state,
+
+ #optional
+ prefix => $optional_unique_prefix,
+ onchange => $javascript,
+ disabled => 0, #bool
+ disable_empty => 1, #defaults to 1, disable the empty option
+ empty_label => 'all', #label for empty option
+ disable_countyupdate => 0, #bool - disabled update of the select-state.html
+ );
+
+</%doc>
+
+<SELECT NAME = "<% $pre %>state"
+ ID = "<% $pre %>state"
+ onChange = "<% $onchange %>"
+>
+
+% unless ( $opt{'disable_empty'} ) {
+ <OPTION VALUE=""<% $opt{state} eq '' ? ' SELECTED' : '' %>><% $opt{empty_label} %>
% }
% foreach my $state ( keys %states ) {
- <OPTION VALUE="<% $state %>"<% $state eq $opt{'state'} ? ' SELECTED' : '' %>><% $states{$state} || '(n/a)' %>
+ <OPTION VALUE="<% $state |h %>"<% $state eq $opt{'state'} ? ' SELECTED' : '' %>><% $states{$state} || '(n/a)' %>
% }
@@ -14,11 +37,21 @@
</SELECT>
<%init>
+
my %opt = @_;
-foreach my $opt (qw( county state country prefix onchange disabled empty )) {
- $opt{$_} = '' unless exists($opt{$_}) && defined($opt{$_});
+foreach my $opt (qw( state country prefix onchange disabled empty_label )) {
+ $opt{$opt} = '' unless exists($opt{$opt}) && defined($opt{$opt});
}
+$opt{'disable_empty'} = 1 unless exists($opt{'disable_empty'});
+
+my $pre = $opt{'prefix'};
+
+my $onchange =
+ ( $opt{'disable_countyupdate'} ? '' : $pre.'state_changed(this); ' ).
+ $opt{'onchange'};
+
tie my %states, 'Tie::IxHash', states_hash( $opt{'country'} );
+
</%init>