summaryrefslogtreecommitdiff
path: root/httemplate/elements/select-country.html
blob: 286826752f279f07b5bdfb5ab921e6dddd4b708f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<%doc>

Example:

  <& /elements/select-country.html,
    #recommended
    country    => $current_country,

    #optional
    prefix        => $optional_unique_prefix,
    onchange      => $javascript,
    disabled      => 0, #bool
    disable_empty => 1, #defaults to 1, disable the empty option
    empty_label   => 'all', #label for empty option
    disable_stateupdate => 0, #bool - disabled update of the select-state.html
    style         => [ 'attribute:value', 'another:value' ],

    state_disable_empty => 1, #defaults to 1, disable the state empty option
    state_empty_label   => 'all', #label for state empty option
  &>

</%doc>
% #maybe this makes more sense in select-state.html?
% # (county update is in select-county... and we wouldn't have to pass "state_"
% #  options)
% unless ( $opt{'disable_stateupdate'} ) {

  <% include('/elements/xmlhttp.html',
                'url'  => $p.'misc/states.cgi',
                'subs' => [ $pre. 'get_states' ],
             )
  %>
  
  <SCRIPT TYPE="text/javascript">
  
    function opt(what,value,text) {
      var optionName = new Option(text, value, false, false);
      var length = what.length;
      what.options[length] = optionName;
    }
  
    function <% $pre %>country_changed(what, callback) {

      what.form.<% $pre %>state.disabled = 'disabled';
  
      country = what.options[what.selectedIndex].value;
  
      function <% $pre %>update_states(states) {
  
        // blank the current state list
        for ( var i = what.form.<% $pre %>state.length; i >= 0; i-- )
            what.form.<% $pre %>state.options[i] = null;
  
        // add the new states
        var statesArray = eval('(' + states + ')' );
%       unless ( $opt{'disable_empty'} ) {
          statesArray.unshift('', '');
%       }

        for ( var s = 0; s < statesArray.length; s=s+2 ) {
            var stateLabel = statesArray[s+1];
            if ( stateLabel == "" )
                stateLabel = <% $opt{state_empty_label} || '(n/a)' |js_string %>;
            opt(what.form.<% $pre %>state, statesArray[s], stateLabel);
        }

        what.form.<% $pre %>state.disabled = '';
  
        //run the callback
        if ( callback != null ) {
          callback();
        } else {
          <% $pre %>state_changed(what.form.<% $pre %>state);
        }
      }
  
      // go get the new states
      <% $pre %>get_states( country, <% $pre %>update_states );
  
    }
  
  </SCRIPT>

% }

<SELECT NAME     = "<% $pre %>country"
        ID       = "<% $pre %>country"
        onChange = "<% $onchange %>"
        <% $opt{'disabled'} %>
        <% $style %>
>

% unless ( $opt{'disable_empty'} ) {
    <OPTION VALUE=""><% $opt{'empty_label'} || '(all)' %></OPTION>
% }

% foreach my $country ( @all_countries ) {
  <OPTION VALUE="<% $country |h %>"<% $country eq $opt{'country'} ? ' SELECTED' : '' %>>
    <% FS::geocode_Mixin->code2country($country). " ($country)" |h %>
  </OPTION>
% } 

</SELECT>

<%init>

my %opt = @_;
foreach my $opt (qw( country prefix onchange disabled disable_stateupdate )) {
  $opt{$opt} = '' unless exists($opt{$opt}) && defined($opt{$opt});
}

$opt{'disable_empty'} = 1 unless exists($opt{'disable_empty'});

my $pre = $opt{'prefix'};

my $onchange =
  ( $opt{'disable_stateupdate'} ? '' : $pre.'country_changed(this); ' ).
  $opt{'onchange'};

$opt{'style'} ||= [];
my $style =
  scalar(@{$opt{style}})
    ? 'STYLE="'. join(';', @{$opt{style}}). '"'
    : '';

my $conf = new FS::Conf;
my $default = $conf->config('countrydefault') || 'US';

my @all_countries = ( 
                      sort {    ($b eq $default) <=> ($a eq $default)
                             or FS::geocode_Mixin->code2country($a) cmp FS::geocode_Mixin->code2country($b)
                           }
                      map  { $_->country } 
                           qsearch({
                                     'select'    => 'country',
                                     'table'     => 'cust_main_county',
                                     'hashref'   => {},
                                     'extra_sql' => 'GROUP BY country',
                                  })
                    );

</%init>