UI for per-city taxes (setup and assigning to customers/package locations), RT#5852
[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     style         => [ 'attribute:value', 'another:value' ],
19   );
20
21 </%doc>
22
23 <% include('/elements/xmlhttp.html',
24               'url'  => $p.'misc/cities.cgi',
25               'subs' => [ $pre. 'get_cities' ],
26            )
27 %>
28
29 <SCRIPT TYPE="text/javascript">
30
31   function opt(what,value,text) {
32     var optionName = new Option(text, value, false, false);
33     var length = what.length;
34     what.options[length] = optionName;
35   }
36
37   var saved_<%$pre%>city= '';
38
39   function <% $pre %>county_changed(what, callback) {
40
41     county  = what.options[what.selectedIndex].value;
42     state   = what.form.<% $pre %>state.options[what.form.<% $pre %>state.selectedIndex].value;
43     country = what.form.<% $pre %>country.options[what.form.<% $pre %>country.selectedIndex].value;
44
45     function <% $pre %>update_cities(cities) {
46      
47       // blank the current city list
48       for ( var i = what.form.<% $pre %>city_select.length; i >= 0; i-- )
49           what.form.<% $pre %>city_select.options[i] = null;
50
51       // add the new cities
52       var citiesArray = eval('(' + cities + ')' );
53
54       for ( var s = 0; s < citiesArray.length; s++ ) {
55           var cityLabel = citiesArray[s];
56           if ( cityLabel == "" )
57               cityLabel = '(n/a)';
58           opt(what.form.<% $pre %>city_select, citiesArray[s], cityLabel);
59       }
60
61      if ( citiesArray.length > 1 || citiesArray[0].length ) { 
62         // turn off the text city, turn on the select
63         saved_<%$pre%>city = what.form.<%$ pre %>city.value;
64         what.form.<% $pre %>city.style.display = 'none';
65         what.form.<% $pre %>city_select.style.display = '';
66       } else {
67         // turn on the text city, turn off the select
68         what.form.<%$ pre %>city.value = saved_<%$pre%>city;
69         what.form.<% $pre %>city.style.display = '';
70         what.form.<% $pre %>city_select.style.display = 'none';
71       }
72
73       //run the callback
74       if ( callback != null )
75         callback();
76     }
77
78     // go get the new cities
79     <% $pre %>get_cities( county, state, country, <% $pre %>update_cities );
80
81   }
82
83   function <%$pre%>city_select_changed(what) {
84     what.form.<%$pre%>city.value = what.options[what.selectedIndex].value;
85   }
86
87 </SCRIPT>
88
89 <INPUT TYPE     = "text"
90        NAME     = "<%$pre%>city"
91        ID       = "<%$pre%>city"
92        VALUE    = "<% $opt{'city'} |h %>"
93        onChange = "<% $opt{'onchange'} %>"
94        <% $opt{'disabled'} %>
95        <% $text_style %>
96 >
97
98 <SELECT NAME     = "<%$pre%>city_select"
99         ID       = "<%$pre%>city_select"
100         onChange = "<%$pre%>city_select_changed(this); <% $opt{'onchange'} %>"
101         <% $opt{'disabled'} %>
102         <% $select_style %>
103 >
104
105 % foreach my $city ( @cities ) {
106
107     <OPTION VALUE="<% $city |h %>"
108             <% $city eq $opt{'city'} ? 'SELECTED' : '' %>
109     ><% $city eq $opt{'empty_data_value'} ? $opt{'empty_data_label'} : $city %>
110
111 % }
112
113 </SELECT>
114
115 %#           VALUE    = "<% $curr_value |h %>"
116 <%init>
117
118 my %opt = @_;
119
120 my $pre = $opt{'prefix'};
121
122 my $text_style   = $opt{'style'} ? [ @{ $opt{'style'} } ] : [];
123 my $select_style = $opt{'style'} ? [ @{ $opt{'style'} } ] : [];
124
125 my @cities = cities( $opt{'county'}, $opt{'state'}, $opt{'country'} );
126 if ( scalar(@cities) > 1 || $cities[0] ) {
127   push @$text_style, 'display:none';
128 } else {
129   push @$select_style, 'display:none';
130 }
131
132 $text_style =
133   scalar(@$text_style)
134     ? 'STYLE="'. join(';', @$text_style). '"'
135     : '';
136
137 $select_style =
138   scalar(@$select_style)
139     ? 'STYLE="'. join(';', @$select_style). '"'
140     : '';
141
142 </%init>