summaryrefslogtreecommitdiff
path: root/httemplate/elements/city.html
blob: 3c5e91782059b1c450aaf2ba405c518ef90f95f4 (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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<%doc>

Example:

  <& /elements/city.html,
       #recommended
       country        => $current_country,
       state          => $current_state,
       county         => $current_county,
       city           => $current_city,

       #optional
       prefix         => $optional_unique_prefix,
       onchange       => $javascript,
       disabled       => 0, #bool
       style          => [ 'attribute:value', 'another:value' ],
       disable_empty  => 1, #defaults to 1, set to 0 to disable the empty option
       empty_label    => 'all', #label for empty option
       disable_text   => 1, # disable the text input (just show the select)
       #doesn't work as an option yet, set when conf tax_district_method
       #disable_select => 1, # disable the selector (just show a text input)
  &>

</%doc>

% if ( $disable_select ) {
<SCRIPT TYPE="text/javascript">
function <% $pre %>county_changed(what, callback) {}
</SCRIPT>
% }
% else {


<% include('/elements/xmlhttp.html',
              'url'  => $p.'misc/cities.cgi',
              'subs' => [ $pre. 'get_cities' ],
           )
%>

<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;
  }

  var saved_<%$pre%>city= '<% $saved_city |h %>';

  function <% $pre %>county_changed(what, callback) {

    what.form.<% $pre %>city_select.disabled = 'disabled';

    county  = what.options[what.selectedIndex].value;
    state   = what.form.<% $pre %>state.options[what.form.<% $pre %>state.selectedIndex].value;
    country = what.form.<% $pre %>country.options[what.form.<% $pre %>country.selectedIndex].value;

    function <% $pre %>update_cities(cities) {

      // blank the current city list
      for ( var i = what.form.<% $pre %>city_select.length; i >= 0; i-- )
          what.form.<% $pre %>city_select.options[i] = null;

%     unless ( $opt{disable_empty} ) {
        opt( what.form.<% $pre %>city_select, '', <% $opt{empty_label} |js_string %> );
%     }

      // add the new cities
      var citiesArray = eval('(' + cities + ')' );

      for ( var s = 0; s < citiesArray.length; s++ ) {
          var cityLabel = citiesArray[s];
          if ( cityLabel == "" )
              cityLabel = '(n/a)';
          opt(what.form.<% $pre %>city_select, citiesArray[s], cityLabel);
      }

     if ( citiesArray.length > 1 || (citiesArray[0] && citiesArray[0].length) ){
        // turn off the text city, turn on the select
        saved_<%$pre%>city = what.form.<%$ pre %>city.value;
        <%$pre%>city_select_changed(what.form.<% $pre %>city_select);
        what.form.<% $pre %>city.style.display = 'none';
        what.form.<% $pre %>city_select.style.display = '';
      } else if ( what.form.<% $pre %>city.style.display == 'none' ) {
        // turn on the text city, turn off the select
        what.form.<%$ pre %>city.value = saved_<%$pre%>city;
        what.form.<% $pre %>city.style.display = '';
        what.form.<% $pre %>city_select.style.display = 'none';
      }

      what.form.<% $pre %>city_select.disabled = '';

      //run the callback
      if ( callback != null )
        callback();
    }

    // go get the new cities
    <% $pre %>get_cities( county, state, country, <% $pre %>update_cities );

  }

  function <%$pre%>city_select_changed(what) {
    what.form.<%$pre%>city.value = what.options[what.selectedIndex].value;
  }

</SCRIPT>

% } #!disable_select

<INPUT TYPE     = "<% $opt{disable_text} ? 'hidden' : 'text' %>"
       NAME     = "<%$pre%>city"
       ID       = "<%$pre%>city"
       VALUE    = "<% $opt{'city'} |h %>"
% unless ( $opt{disable_text} ) {
       onChange = "<% $opt{'onchange'} %>"
       <% $opt{'disabled'} %>
       <% $text_style %>
% }
>

% if ( $disable_select ) {
%# avoid JS errors
    <INPUT TYPE="hidden" ID="city_select">
% } else {

    <SELECT NAME     = "<%$pre%>city_select"
            ID       = "<%$pre%>city_select"
            onChange = "<%$pre%>city_select_changed(this); <% $opt{onchange} %>"
            <% $opt{disabled} %>
            <% $select_style %>
    >

%   unless ( $opt{'disable_empty'} ) {
      <OPTION VALUE="" <% $opt{city} eq '' ? 'SELECTED' : '' %>><% $opt{empty_label} %></OPTION>
%   }

%   foreach my $city ( @cities ) {

      <OPTION VALUE="<% $city |h %>"
              <% $city eq $opt{city} ? 'SELECTED' : '' %>
      ><% $city eq $opt{empty_data_value} ? $opt{empty_data_label} : $city %></OPTION>

%   }

    </SELECT>
% }

<%init>

my %opt = @_;

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

my $conf = new FS::Conf;
my $disable_select = 0;

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

if ($conf->exists('cust_main-no_city_in_address')) {
  $opt{'disable_text'} = 1;
  $disable_select = 1;
}

my $text_style   = $opt{'style'} ? [ @{ $opt{'style'} } ] : [];
my $select_style = $opt{'style'} ? [ @{ $opt{'style'} } ] : [];

push @$text_style,   @{ $opt{'text_style'} }   if $opt{'text_style'};
push @$select_style, @{ $opt{'select_style'} } if $opt{'select_style'};

my @cities = cities( $opt{'county'}, $opt{'state'}, $opt{'country'} );
my $saved_city = '';
if ( scalar(@cities) > 1 || $cities[0] and !$disable_select ) {
  push @$text_style, 'display:none';
} else {
  push @$select_style, 'display:none';
  $saved_city = $opt{'city'};
}

$text_style =
  scalar(@$text_style)
    ? 'STYLE="'. join(';', @$text_style). '"'
    : '';

$select_style =
  scalar(@$select_style)
    ? 'STYLE="'. join(';', @$select_style). '"'
    : '';

</%init>