stray closing /TABLE in the no-ticket case
[freeside.git] / httemplate / elements / city.html
index 0e1bf8d..4e9a609 100644 (file)
@@ -2,24 +2,35 @@
 
 Example:
 
-  include( '/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
-#    disable_empty => 1, #defaults to 1, disable the empty option
-#    empty_label   => 'all', #label for empty option
-    style         => [ 'attribute:value', 'another:value' ],
-  );
+  <& /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' ],
@@ -38,16 +49,22 @@ Example:
 
   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 + ')' );
 
@@ -58,7 +75,7 @@ Example:
           opt(what.form.<% $pre %>city_select, citiesArray[s], cityLabel);
       }
 
-     if ( citiesArray.length > 1 || citiesArray[0].length ) { 
+     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);
@@ -66,11 +83,13 @@ Example:
         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.value = saved_<%$pre%>city;
       }
 
+      what.form.<% $pre %>city_select.disabled = '';
+
       //run the callback
       if ( callback != null )
         callback();
@@ -87,45 +106,73 @@ Example:
 
 </SCRIPT>
 
-<INPUT TYPE     = "text"
+% } #!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 %>
+% }
 >
 
-<SELECT NAME     = "<%$pre%>city_select"
-        ID       = "<%$pre%>city_select"
-        onChange = "<%$pre%>city_select_changed(this); <% $opt{'onchange'} %>"
-        <% $opt{'disabled'} %>
-        <% $select_style %>
->
+% if ( $disable_select ) {
+%# avoid JS errors
+    <INPUT TYPE="hidden" ID="city_select">
+% } else {
 
-% foreach my $city ( @cities ) {
+    <SELECT NAME     = "<%$pre%>city_select"
+            ID       = "<%$pre%>city_select"
+            onChange = "<%$pre%>city_select_changed(this); <% $opt{onchange} %>"
+            <% $opt{disabled} %>
+            <% $select_style %>
+    >
 
-    <OPTION VALUE="<% $city |h %>"
-            <% $city eq $opt{'city'} ? 'SELECTED' : '' %>
-    ><% $city eq $opt{'empty_data_value'} ? $opt{'empty_data_label'} : $city %>
+%   unless ( $opt{'disable_empty'} ) {
+      <OPTION VALUE="" <% $opt{city} eq '' ? 'SELECTED' : '' %>><% $opt{empty_label} %>
+%   }
 
-% }
+%   foreach my $city ( @cities ) {
 
-</SELECT>
+      <OPTION VALUE="<% $city |h %>"
+              <% $city eq $opt{city} ? 'SELECTED' : '' %>
+      ><% $city eq $opt{empty_data_value} ? $opt{empty_data_label} : $city %>
+
+%   }
+
+    </SELECT>
+% }
 
-%#           VALUE    = "<% $curr_value |h %>"
 <%init>
 
 my %opt = @_;
 
 my $pre = $opt{'prefix'};
 
+my $conf = new FS::Conf;
+# Using tax_district_method implies that there's not a preloaded city/county
+# tax district table.
+my $disable_select = 1 if $conf->config('tax_district_method');
+
+$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] ) {
+if ( scalar(@cities) > 1 || $cities[0] and !$disable_select ) {
   push @$text_style, 'display:none';
 } else {
   push @$select_style, 'display:none';