search by state (and county, country), RT#30312
[freeside.git] / httemplate / elements / select-country.html
1 <%doc>
2
3 Example:
4
5   <& /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     style         => [ 'attribute:value', 'another:value' ],
17
18     state_disable_empty => 1, #defaults to 1, disable the state empty option
19     state_empty_label   => 'all', #label for state empty option
20   &>
21
22 </%doc>
23 % #maybe this makes more sense in select-state.html?
24 % # (county update is in select-county... and we wouldn't have to pass "state_"
25 % #  options)
26 % unless ( $opt{'disable_stateupdate'} ) {
27
28   <% include('/elements/xmlhttp.html',
29                 'url'  => $p.'misc/states.cgi',
30                 'subs' => [ $pre. 'get_states' ],
31              )
32   %>
33   
34   <SCRIPT TYPE="text/javascript">
35   
36     function opt(what,value,text) {
37       var optionName = new Option(text, value, false, false);
38       var length = what.length;
39       what.options[length] = optionName;
40     }
41   
42     function <% $pre %>country_changed(what, callback) {
43   
44       country = what.options[what.selectedIndex].value;
45   
46       function <% $pre %>update_states(states) {
47   
48         // blank the current state list
49         for ( var i = what.form.<% $pre %>state.length; i >= 0; i-- )
50             what.form.<% $pre %>state.options[i] = null;
51   
52         // add the new states
53         var statesArray = eval('(' + states + ')' );
54 %       unless ( $opt{'disable_empty'} ) {
55           statesArray.unshift('', '');
56 %       }
57
58         for ( var s = 0; s < statesArray.length; s=s+2 ) {
59             var stateLabel = statesArray[s+1];
60             if ( stateLabel == "" )
61                 stateLabel = <% $opt{state_empty_label} || '(n/a)' |js_string %>;
62             opt(what.form.<% $pre %>state, statesArray[s], stateLabel);
63         }
64   
65         //run the callback
66         if ( callback != null ) {
67           callback();
68         } else {
69           <% $pre %>state_changed(what.form.<% $pre %>state);
70         }
71       }
72   
73       // go get the new states
74       <% $pre %>get_states( country, <% $pre %>update_states );
75   
76     }
77   
78   </SCRIPT>
79
80 % }
81
82 <SELECT NAME     = "<% $pre %>country"
83         ID       = "<% $pre %>country"
84         onChange = "<% $onchange %>"
85         <% $opt{'disabled'} %>
86         <% $style %>
87 >
88
89 % unless ( $opt{'disable_empty'} ) {
90     <OPTION VALUE=""><% $opt{'empty_label'} || '(all)' %>
91 % }
92
93 % foreach my $country ( @all_countries ) {
94
95   <OPTION VALUE="<% $country |h %>"
96           <% $country eq $opt{'country'} ? ' SELECTED' : '' %>
97   ><% code2country($country). " ($country)" %>
98
99 % } 
100
101 </SELECT>
102
103 <%init>
104
105 my %opt = @_;
106 foreach my $opt (qw( country prefix onchange disabled disable_stateupdate )) {
107   $opt{$opt} = '' unless exists($opt{$opt}) && defined($opt{$opt});
108 }
109
110 $opt{'disable_empty'} = 1 unless exists($opt{'disable_empty'});
111
112 my $pre = $opt{'prefix'};
113
114 my $onchange =
115   ( $opt{'disable_stateupdate'} ? '' : $pre.'country_changed(this); ' ).
116   $opt{'onchange'};
117
118 $opt{'style'} ||= [];
119 my $style =
120   scalar(@{$opt{style}})
121     ? 'STYLE="'. join(';', @{$opt{style}}). '"'
122     : '';
123
124 my $conf = new FS::Conf;
125 my $default = $conf->config('countrydefault') || 'US';
126
127 my @all_countries = ( 
128                       sort {    ($b eq $default) <=> ($a eq $default)
129                              or code2country($a) cmp code2country($b)
130                            }
131                       map  { $_->country } 
132                            qsearch({
133                                      'select'    => 'country',
134                                      'table'     => 'cust_main_county',
135                                      'hashref'   => {},
136                                      'extra_sql' => 'GROUP BY country',
137                                   })
138                     );
139
140 </%init>