fix browse results for selecting counties (resulting from separating tax classes...
[freeside.git] / httemplate / elements / select-country.html
1 <%doc>
2
3 Example:
4
5   include( '/elements/select-country.html',
6     #recommended
7     country    => $current_country,
8
9     #optional
10     prefix        => $optional_unique_prefix,
11     onchange      => $javascript,
12     disabled      => 0, #bool
13     disable_empty => 1, #defaults to 1, disable the empty option
14     empty_label   => 'all', #label for empty option
15     disable_stateupdate => 0, #bool - disabled update of the select-state.html
16     
17   );
18
19 </%doc>
20 % unless ( $opt{'disable_stateupdate'} ) {
21
22   <% include('/elements/xmlhttp.html',
23                 'url'  => $p.'misc/states.cgi',
24                 'subs' => [ $pre. 'get_states' ],
25              )
26   %>
27   
28   <SCRIPT TYPE="text/javascript">
29   
30     function opt(what,value,text) {
31       var optionName = new Option(text, value, false, false);
32       var length = what.length;
33       what.options[length] = optionName;
34     }
35   
36     function <% $pre %>country_changed(what, callback) {
37   
38       country = what.options[what.selectedIndex].value;
39   
40       function <% $pre %>update_states(states) {
41   
42         // blank the current state list
43         for ( var i = what.form.<% $pre %>state.length; i >= 0; i-- )
44             what.form.<% $pre %>state.options[i] = null;
45   
46         // add the new states
47         var statesArray = eval('(' + states + ')' );
48         for ( var s = 0; s < statesArray.length; s=s+2 ) {
49             var stateLabel = statesArray[s+1];
50             if ( stateLabel == "" )
51                 stateLabel = '(n/a)';
52             opt(what.form.<% $pre %>state, statesArray[s], stateLabel);
53         }
54   
55         //run the callback
56         if ( callback != null ) 
57           callback();
58       }
59   
60       // go get the new states
61       <% $pre %>get_states( country, <% $pre %>update_states );
62   
63     }
64   
65   </SCRIPT>
66
67 % }
68
69 <SELECT NAME     = "<% $pre %>country"
70         ID       = "<% $pre %>country"
71         onChange = "<% $onchange %>"
72         <% $opt{'disabled'} %>
73 >
74
75 % unless ( $opt{'disable_empty'} ) {
76     <OPTION VALUE=""><% $opt{'empty_label'} || '(all)' %>
77 % }
78
79 % foreach my $country ( @all_countries ) {
80
81   <OPTION VALUE="<% $country |h %>"
82           <% $country eq $opt{'country'} ? ' SELECTED' : '' %>
83   ><% code2country($country). " ($country)" %>
84
85 % } 
86
87 </SELECT>
88
89 <%init>
90
91 my %opt = @_;
92 foreach my $opt (qw( country prefix onchange disabled disable_stateupdate )) {
93   $opt{$opt} = '' unless exists($opt{$opt}) && defined($opt{$opt});
94 }
95
96 $opt{'disable_empty'} = 1 unless exists($opt{'disable_empty'});
97
98 my $pre = $opt{'prefix'};
99
100 my $onchange =
101   ( $opt{'disable_stateupdate'} ? '' : $pre.'country_changed(this); ' ).
102   $opt{'onchange'};
103
104 my $conf = new FS::Conf;
105 my $default = $conf->config('countrydefault') || 'US';
106
107 my @all_countries = ( 
108                       sort {    ($b eq $default) <=> ($a eq $default)
109                              or code2country($a) cmp code2country($b)
110                            }
111                       map  { $_->country } 
112                            qsearch({
113                                      'select'    => 'country',
114                                      'table'     => 'cust_main_county',
115                                      'hashref'   => {},
116                                      'extra_sql' => 'GROUP BY country',
117                                   })
118                     );
119
120 </%init>