add northern cyprus, RT#39335
[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       what.form.<% $pre %>state.disabled = 'disabled';
45   
46       country = what.options[what.selectedIndex].value;
47   
48       function <% $pre %>update_states(states) {
49   
50         // blank the current state list
51         for ( var i = what.form.<% $pre %>state.length; i >= 0; i-- )
52             what.form.<% $pre %>state.options[i] = null;
53   
54         // add the new states
55         var statesArray = eval('(' + states + ')' );
56 %       unless ( $opt{'disable_empty'} ) {
57           statesArray.unshift('', '');
58 %       }
59
60         for ( var s = 0; s < statesArray.length; s=s+2 ) {
61             var stateLabel = statesArray[s+1];
62             if ( stateLabel == "" )
63                 stateLabel = <% $opt{state_empty_label} || '(n/a)' |js_string %>;
64             opt(what.form.<% $pre %>state, statesArray[s], stateLabel);
65         }
66
67         what.form.<% $pre %>state.disabled = '';
68   
69         //run the callback
70         if ( callback != null ) {
71           callback();
72         } else {
73           <% $pre %>state_changed(what.form.<% $pre %>state);
74         }
75       }
76   
77       // go get the new states
78       <% $pre %>get_states( country, <% $pre %>update_states );
79   
80     }
81   
82   </SCRIPT>
83
84 % }
85
86 <SELECT NAME     = "<% $pre %>country"
87         ID       = "<% $pre %>country"
88         onChange = "<% $onchange %>"
89         <% $opt{'disabled'} %>
90         <% $style %>
91 >
92
93 % unless ( $opt{'disable_empty'} ) {
94     <OPTION VALUE=""><% $opt{'empty_label'} || '(all)' %>
95 % }
96
97 % foreach my $country ( @all_countries ) {
98
99   <OPTION VALUE="<% $country |h %>"
100           <% $country eq $opt{'country'} ? ' SELECTED' : '' %>
101   ><% FS::geocode_Mixin->code2country($country). " ($country)" %>
102
103 % } 
104
105 </SELECT>
106
107 <%init>
108
109 my %opt = @_;
110 foreach my $opt (qw( country prefix onchange disabled disable_stateupdate )) {
111   $opt{$opt} = '' unless exists($opt{$opt}) && defined($opt{$opt});
112 }
113
114 $opt{'disable_empty'} = 1 unless exists($opt{'disable_empty'});
115
116 my $pre = $opt{'prefix'};
117
118 my $onchange =
119   ( $opt{'disable_stateupdate'} ? '' : $pre.'country_changed(this); ' ).
120   $opt{'onchange'};
121
122 $opt{'style'} ||= [];
123 my $style =
124   scalar(@{$opt{style}})
125     ? 'STYLE="'. join(';', @{$opt{style}}). '"'
126     : '';
127
128 my $conf = new FS::Conf;
129 my $default = $conf->config('countrydefault') || 'US';
130
131 my @all_countries = ( 
132                       sort {    ($b eq $default) <=> ($a eq $default)
133                              or FS::geocode_Mixin->code2country($a) cmp FS::geocode_Mixin->code2country($b)
134                            }
135                       map  { $_->country } 
136                            qsearch({
137                                      'select'    => 'country',
138                                      'table'     => 'cust_main_county',
139                                      'hashref'   => {},
140                                      'extra_sql' => 'GROUP BY country',
141                                   })
142                     );
143
144 </%init>