combine ticket notification scrips, #15353
[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= '<% $saved_city |h %>';
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         <%$pre%>city_select_changed(what.form.<% $pre %>city_select);
65         what.form.<% $pre %>city.style.display = 'none';
66         what.form.<% $pre %>city_select.style.display = '';
67       } else if ( what.form.<% $pre %>city.style.display == 'none' ) {
68         // turn on the text city, turn off the select
69         what.form.<%$ pre %>city.value = saved_<%$pre%>city;
70         what.form.<% $pre %>city.style.display = '';
71         what.form.<% $pre %>city_select.style.display = 'none';
72       }
73
74       //run the callback
75       if ( callback != null )
76         callback();
77     }
78
79     // go get the new cities
80     <% $pre %>get_cities( county, state, country, <% $pre %>update_cities );
81
82   }
83
84   function <%$pre%>city_select_changed(what) {
85     what.form.<%$pre%>city.value = what.options[what.selectedIndex].value;
86   }
87
88 </SCRIPT>
89
90 <INPUT TYPE     = "text"
91        NAME     = "<%$pre%>city"
92        ID       = "<%$pre%>city"
93        VALUE    = "<% $opt{'city'} |h %>"
94        onChange = "<% $opt{'onchange'} %>"
95        <% $opt{'disabled'} %>
96        <% $text_style %>
97 >
98
99 <SELECT NAME     = "<%$pre%>city_select"
100         ID       = "<%$pre%>city_select"
101         onChange = "<%$pre%>city_select_changed(this); <% $opt{'onchange'} %>"
102         <% $opt{'disabled'} %>
103         <% $select_style %>
104 >
105
106 % foreach my $city ( @cities ) {
107
108     <OPTION VALUE="<% $city |h %>"
109             <% $city eq $opt{'city'} ? 'SELECTED' : '' %>
110     ><% $city eq $opt{'empty_data_value'} ? $opt{'empty_data_label'} : $city %>
111
112 % }
113
114 </SELECT>
115
116 %#           VALUE    = "<% $curr_value |h %>"
117 <%init>
118
119 my %opt = @_;
120
121 my $pre = $opt{'prefix'};
122
123 my $text_style   = $opt{'style'} ? [ @{ $opt{'style'} } ] : [];
124 my $select_style = $opt{'style'} ? [ @{ $opt{'style'} } ] : [];
125
126 my @cities = cities( $opt{'county'}, $opt{'state'}, $opt{'country'} );
127 my $saved_city = '';
128 if ( scalar(@cities) > 1 || $cities[0] ) {
129   push @$text_style, 'display:none';
130 } else {
131   push @$select_style, 'display:none';
132   $saved_city = $opt{'city'};
133 }
134
135 $text_style =
136   scalar(@$text_style)
137     ? 'STYLE="'. join(';', @$text_style). '"'
138     : '';
139
140 $select_style =
141   scalar(@$select_style)
142     ? 'STYLE="'. join(';', @$select_style). '"'
143     : '';
144
145 </%init>