combine ticket notification scrips, #15353
[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         } else {
59           <% $pre %>state_changed(what.form.<% $pre %>state);
60         }
61       }
62   
63       // go get the new states
64       <% $pre %>get_states( country, <% $pre %>update_states );
65   
66     }
67   
68   </SCRIPT>
69
70 % }
71
72 <SELECT NAME     = "<% $pre %>country"
73         ID       = "<% $pre %>country"
74         onChange = "<% $onchange %>"
75         <% $opt{'disabled'} %>
76         <% $style %>
77 >
78
79 % unless ( $opt{'disable_empty'} ) {
80     <OPTION VALUE=""><% $opt{'empty_label'} || '(all)' %>
81 % }
82
83 % foreach my $country ( @all_countries ) {
84
85   <OPTION VALUE="<% $country |h %>"
86           <% $country eq $opt{'country'} ? ' SELECTED' : '' %>
87   ><% code2country($country). " ($country)" %>
88
89 % } 
90
91 </SELECT>
92
93 <%init>
94
95 my %opt = @_;
96 foreach my $opt (qw( country prefix onchange disabled disable_stateupdate )) {
97   $opt{$opt} = '' unless exists($opt{$opt}) && defined($opt{$opt});
98 }
99
100 $opt{'disable_empty'} = 1 unless exists($opt{'disable_empty'});
101
102 my $pre = $opt{'prefix'};
103
104 my $onchange =
105   ( $opt{'disable_stateupdate'} ? '' : $pre.'country_changed(this); ' ).
106   $opt{'onchange'};
107
108 $opt{'style'} ||= [];
109 my $style =
110   scalar(@{$opt{style}})
111     ? 'STYLE="'. join(';', @{$opt{style}}). '"'
112     : '';
113
114 my $conf = new FS::Conf;
115 my $default = $conf->config('countrydefault') || 'US';
116
117 my @all_countries = ( 
118                       sort {    ($b eq $default) <=> ($a eq $default)
119                              or code2country($a) cmp code2country($b)
120                            }
121                       map  { $_->country } 
122                            qsearch({
123                                      'select'    => 'country',
124                                      'table'     => 'cust_main_county',
125                                      'hashref'   => {},
126                                      'extra_sql' => 'GROUP BY country',
127                                   })
128                     );
129
130 </%init>