4.x style
[freeside.git] / httemplate / elements / select-city.html
1 <%doc>
2
3 Example:
4
5  <& /elements/select-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, set to 0 to disable the empty option
17     empty_label   => 'all', #label for empty option
18     style         => [ 'attribute:value', 'another:value' ],
19   &>
20
21 </%doc>
22 % if ( $cityflag ) { 
23
24   <% include('/elements/xmlhttp.html',
25                 'url'  => $p.'misc/cities.cgi',
26                 'subs' => [ $pre. 'get_cities' ],
27              )
28   %>
29   
30   <SCRIPT TYPE="text/javascript">
31   
32     function opt(what,value,text) {
33       var optionName = new Option(text, value, false, false);
34       var length = what.length;
35       what.options[length] = optionName;
36     }
37   
38     function <% $pre %>county_changed(what, callback) {
39
40       what.form.<% $pre %>city.disabled = 'disabled';
41
42       county = what.form.<% $pre %>county.options[what.form.<% $pre %>county.selectedIndex].value;
43       state = what.options[what.selectedIndex].value;
44       country = what.form.<% $pre %>country.options[what.form.<% $pre %>country.selectedIndex].value;
45   
46       function <% $pre %>update_cities(cities) {
47
48         // blank the current city list
49         for ( var i = what.form.<% $pre %>city.length; i >= 0; i-- )
50             what.form.<% $pre %>city.options[i] = null;
51
52 %       unless ( $opt{disable_empty} ) {
53           opt( what.form.<% $pre %>city, '', <% $opt{empty_label} |js_string %> );
54 %       }
55   
56         // add the new cities
57         var citiesArray = eval('(' + cities + ')' );
58         for ( var s = 0; s < citiesArray.length; s++ ) {
59             var cityLabel = citiesArray[s];
60             if ( cityLabel == "" )
61                 cityLabel = '(n/a)';
62             opt(what.form.<% $pre %>city, citiesArray[s], cityLabel);
63         }
64
65         var cityFormLabel = document.getElementById('<% $pre %>citylabel');
66
67         if ( citiesArray.length > 1 ) { 
68           what.form.<% $pre %>city.style.display = '';
69           if ( cityFormLabel )  {
70             //cityFormLabel.style.visibility = 'visible';
71             cityFormLabel.style.display = '';
72           }
73         } else {
74           what.form.<% $pre %>city.style.display = 'none';
75           if ( cityFormLabel ) {
76             //cityFormLabel.style.visibility = 'hidden';
77             cityFormLabel.style.display = 'none';
78           }
79         }
80
81         what.form.<% $pre %>city.disabled = '';
82
83         //run the callback
84         if ( callback != null )  {
85           callback();
86         } else {
87           <% $pre %>city_changed(what.form.<% $pre %>city);
88         }
89       }
90   
91       // go get the new cities
92       <% $pre %>get_cities( state, country, <% $pre %>update_cities );
93   
94     }
95   
96   </SCRIPT>
97
98   <SELECT NAME    = "<% $pre %>city"
99           ID      = "<% $pre %>city"
100           onChange= "<% $onchange %>"
101           <% $opt{'disabled'} %>
102           <% $style %>
103   >
104
105 % unless ( $opt{'disable_empty'} ) {
106   <OPTION VALUE="" <% $opt{county} eq '' ? 'SELECTED' : '' %>><% $opt{empty_label} %>
107 % }
108
109 % foreach my $city ( @cities ) {
110
111     <OPTION VALUE="<% $city |h %>"
112             <% $city eq $opt{'city'} ? 'SELECTED' : '' %>
113     ><% $city eq $opt{'empty_data_value'} ? $opt{'empty_data_label'} : $city %>
114
115 % } 
116
117   </SELECT>
118
119 % } else { 
120
121   <SCRIPT TYPE="text/javascript">
122     function <% $pre %>city_changed(what) {
123     }
124   </SCRIPT>
125
126   <SELECT NAME  = "<% $pre %>city"
127            ID   = "<% $pre %>city"
128           STYLE = "display:none"
129   >
130     <OPTION SELECTED VALUE="<% $opt{'city'} |h %>">
131   </SELECT>
132
133 % } 
134
135 <%init>
136
137 my %opt = @_;
138 foreach my $opt (qw( city county state country prefix onchange disabled
139                      empty_value )) {
140   $opt{$opt} = '' unless exists($opt{$opt}) && defined($opt{$opt});
141 }
142
143 $opt{'disable_empty'} = 1 unless exists($opt{'disable_empty'});
144
145 my $pre = $opt{'prefix'};
146
147 my $onchange = $opt{'onchange'};
148
149 my $city_style = $opt{'style'} ? [ @{ $opt{'style'} } ] : [];
150
151 my @cities = ();
152 if ( $cityflag ) {
153
154   @cities = map { length($_) ? $_ : $opt{'empty_data_value'} }
155                   cities( $opt{'county'}, $opt{'state'}, $opt{'country'} );
156
157   push @$city_style, 'display:none'
158     unless scalar(@cities) > 1;
159
160 }
161
162 my $style =
163   scalar(@$city_style)
164     ? 'STYLE="'. join(';', @$city_style). '"'
165     : '';
166
167 </%init>
168 <%once>
169
170 my $sql = "SELECT COUNT(*) FROM cust_main_county".
171           " WHERE city IS NOT NULL AND city != ''";
172 my $sth = dbh->prepare($sql) or die dbh->errstr;
173 $sth->execute or die $sth->errstr;
174 my $cityflag = $sth->fetchrow_arrayref->[0];
175
176 </%once>