Merge branch 'master' of git.freeside.biz:/home/git/freeside
[freeside.git] / httemplate / elements / city.html
1 <%doc>
2
3 Example:
4
5   include( '/elements/city.html',
6     #recommended
7     country    => $current_country,
8     state      => $current_state,
9     county     => $current_county,
10     city       => $current_city,
11
12     #optional
13     prefix        => $optional_unique_prefix,
14     onchange      => $javascript,
15     disabled      => 0, #bool
16 #    disable_empty => 1, #defaults to 1, disable the empty option
17 #    empty_label   => 'all', #label for empty option
18 #    disable_select => 1, # disable the selector (just show a text input)
19     style         => [ 'attribute:value', 'another:value' ],
20   );
21
22 </%doc>
23
24 % if ( $disable_select ) {
25 <SCRIPT TYPE="text/javascript">
26 function <% $pre %>county_changed(what, callback) {}
27 </SCRIPT>
28 % }
29 % else {
30
31
32 <% include('/elements/xmlhttp.html',
33               'url'  => $p.'misc/cities.cgi',
34               'subs' => [ $pre. 'get_cities' ],
35            )
36 %>
37
38 <SCRIPT TYPE="text/javascript">
39
40   function opt(what,value,text) {
41     var optionName = new Option(text, value, false, false);
42     var length = what.length;
43     what.options[length] = optionName;
44   }
45
46   var saved_<%$pre%>city= '<% $saved_city |h %>';
47
48   function <% $pre %>county_changed(what, callback) {
49
50     county  = what.options[what.selectedIndex].value;
51     state   = what.form.<% $pre %>state.options[what.form.<% $pre %>state.selectedIndex].value;
52     country = what.form.<% $pre %>country.options[what.form.<% $pre %>country.selectedIndex].value;
53
54     function <% $pre %>update_cities(cities) {
55      
56       // blank the current city list
57       for ( var i = what.form.<% $pre %>city_select.length; i >= 0; i-- )
58           what.form.<% $pre %>city_select.options[i] = null;
59
60       // add the new cities
61       var citiesArray = eval('(' + cities + ')' );
62
63       for ( var s = 0; s < citiesArray.length; s++ ) {
64           var cityLabel = citiesArray[s];
65           if ( cityLabel == "" )
66               cityLabel = '(n/a)';
67           opt(what.form.<% $pre %>city_select, citiesArray[s], cityLabel);
68       }
69
70      if ( citiesArray.length > 1 || citiesArray[0].length ) { 
71         // turn off the text city, turn on the select
72         saved_<%$pre%>city = what.form.<%$ pre %>city.value;
73         <%$pre%>city_select_changed(what.form.<% $pre %>city_select);
74         what.form.<% $pre %>city.style.display = 'none';
75         what.form.<% $pre %>city_select.style.display = '';
76       } else if ( what.form.<% $pre %>city.style.display == 'none' ) {
77         // turn on the text city, turn off the select
78         what.form.<%$ pre %>city.value = saved_<%$pre%>city;
79         what.form.<% $pre %>city.style.display = '';
80         what.form.<% $pre %>city_select.style.display = 'none';
81       }
82
83       //run the callback
84       if ( callback != null )
85         callback();
86     }
87
88     // go get the new cities
89     <% $pre %>get_cities( county, state, country, <% $pre %>update_cities );
90
91   }
92
93   function <%$pre%>city_select_changed(what) {
94     what.form.<%$pre%>city.value = what.options[what.selectedIndex].value;
95   }
96
97 </SCRIPT>
98
99 % } #!disable_select
100
101 <INPUT TYPE     = "text"
102        NAME     = "<%$pre%>city"
103        ID       = "<%$pre%>city"
104        VALUE    = "<% $opt{'city'} |h %>"
105        onChange = "<% $opt{'onchange'} %>"
106        <% $opt{'disabled'} %>
107        <% $text_style %>
108 >
109
110 % if ( $disable_select ) {
111 %# avoid JS errors
112 <INPUT TYPE="hidden" ID="city_select">
113 % }
114 % else {
115
116 <SELECT NAME     = "<%$pre%>city_select"
117         ID       = "<%$pre%>city_select"
118         onChange = "<%$pre%>city_select_changed(this); <% $opt{'onchange'} %>"
119         <% $opt{'disabled'} %>
120         <% $select_style %>
121 >
122
123 % foreach my $city ( @cities ) {
124
125     <OPTION VALUE="<% $city |h %>"
126             <% $city eq $opt{'city'} ? 'SELECTED' : '' %>
127     ><% $city eq $opt{'empty_data_value'} ? $opt{'empty_data_label'} : $city %>
128
129 % }
130
131 </SELECT>
132 % }
133 %#           VALUE    = "<% $curr_value |h %>"
134 <%init>
135
136 my %opt = @_;
137
138 my $pre = $opt{'prefix'};
139
140 my $conf = new FS::Conf;
141 # Using tax_district_method implies that there's not a preloaded city/county
142 # tax district table.
143 my $disable_select = 1 if $conf->config('tax_district_method');
144
145 my $text_style   = $opt{'style'} ? [ @{ $opt{'style'} } ] : [];
146 my $select_style = $opt{'style'} ? [ @{ $opt{'style'} } ] : [];
147
148 push @$text_style,   @{ $opt{'text_style'} }   if $opt{'text_style'};
149 push @$select_style, @{ $opt{'select_style'} } if $opt{'select_style'};
150
151 my @cities = cities( $opt{'county'}, $opt{'state'}, $opt{'country'} );
152 my $saved_city = '';
153 if ( scalar(@cities) > 1 || $cities[0] and !$disable_select ) {
154   push @$text_style, 'display:none';
155 } else {
156   push @$select_style, 'display:none';
157   $saved_city = $opt{'city'};
158 }
159
160 $text_style =
161   scalar(@$text_style)
162     ? 'STYLE="'. join(';', @$text_style). '"'
163     : '';
164
165 $select_style =
166   scalar(@$select_style)
167     ? 'STYLE="'. join(';', @$select_style). '"'
168     : '';
169
170 </%init>