pick/enter a location when ordering a package, RT#4499
[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     style         => [ 'attribute:value', 'another:value' ],
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         <% $style %>
74 >
75
76 % unless ( $opt{'disable_empty'} ) {
77     <OPTION VALUE=""><% $opt{'empty_label'} || '(all)' %>
78 % }
79
80 % foreach my $country ( @all_countries ) {
81
82   <OPTION VALUE="<% $country |h %>"
83           <% $country eq $opt{'country'} ? ' SELECTED' : '' %>
84   ><% code2country($country). " ($country)" %>
85
86 % } 
87
88 </SELECT>
89
90 <%init>
91
92 my %opt = @_;
93 foreach my $opt (qw( country prefix onchange disabled disable_stateupdate )) {
94   $opt{$opt} = '' unless exists($opt{$opt}) && defined($opt{$opt});
95 }
96
97 $opt{'disable_empty'} = 1 unless exists($opt{'disable_empty'});
98
99 my $pre = $opt{'prefix'};
100
101 my $onchange =
102   ( $opt{'disable_stateupdate'} ? '' : $pre.'country_changed(this); ' ).
103   $opt{'onchange'};
104
105 $opt{'style'} ||= [];
106 my $style =
107   scalar(@{$opt{style}})
108     ? 'STYLE="'. join(';', @{$opt{style}}). '"'
109     : '';
110
111 my $conf = new FS::Conf;
112 my $default = $conf->config('countrydefault') || 'US';
113
114 my @all_countries = ( 
115                       sort {    ($b eq $default) <=> ($a eq $default)
116                              or code2country($a) cmp code2country($b)
117                            }
118                       map  { $_->country } 
119                            qsearch({
120                                      'select'    => 'country',
121                                      'table'     => 'cust_main_county',
122                                      'hashref'   => {},
123                                      'extra_sql' => 'GROUP BY country',
124                                   })
125                     );
126
127 </%init>