fix browse results for selecting counties (resulting from separating tax classes...
[freeside.git] / httemplate / elements / select-county.html
1 <%doc>
2
3 Example:
4
5   include( '/elements/select-county.html',
6     #recommended
7     country    => $current_country,
8     state      => $current_state,
9     county     => $current_county,
10
11     #optional
12     prefix        => $optional_unique_prefix,
13     onchange      => $javascript,
14     disabled      => 0, #bool
15     disable_empty => 1, #defaults to 1, disable the empty option
16     empty_label   => 'all', #label for empty option
17   );
18
19 </%doc>
20 % if ( $countyflag ) { 
21
22   <% include('/elements/xmlhttp.html',
23                 'url'  => $p.'misc/counties.cgi',
24                 'subs' => [ $pre. 'get_counties' ],
25              )
26   %>
27   
28   <SCRIPT TYPE="text/javascript">
29   
30     function opt(what,value,text) {
31       var optionName = new Option(text, value, false, false);
32       var length = what.length;
33       what.options[length] = optionName;
34     }
35   
36     function <% $pre %>state_changed(what, callback) {
37
38       state = what.options[what.selectedIndex].value;
39       country = what.form.<% $pre %>country.options[what.form.<% $pre %>country.selectedIndex].value;
40   
41       function <% $pre %>update_counties(counties) {
42
43         // blank the current county list
44         for ( var i = what.form.<% $pre %>county.length; i >= 0; i-- )
45             what.form.<% $pre %>county.options[i] = null;
46   
47         // add the new counties
48         var countiesArray = eval('(' + counties + ')' );
49         for ( var s = 0; s < countiesArray.length; s++ ) {
50             var countyLabel = countiesArray[s];
51             if ( countyLabel == "" )
52                 countyLabel = '(n/a)';
53             opt(what.form.<% $pre %>county, countiesArray[s], countyLabel);
54         }
55
56         var countyFormLabel = document.getElementById('<% $pre %>countylabel');
57
58         if ( countiesArray.length > 1 ) { 
59           what.form.<% $pre %>county.style.display = '';
60           countyFormLabel.style.visibility = 'visible';
61         } else {
62           what.form.<% $pre %>county.style.display = 'none';
63           countyFormLabel.style.visibility = 'hidden';
64         }
65
66         //run the callback
67         if ( callback != null ) 
68           callback();
69       }
70   
71       // go get the new counties
72       <% $pre %>get_counties( state, country, <% $pre %>update_counties );
73   
74     }
75   
76   </SCRIPT>
77
78   <SELECT NAME    = "<% $pre %>county"
79           ID      = "<% $pre %>county"
80           onChange= "<% $opt{'onchange'} %>"
81           <% $opt{'disabled'} %>
82   >
83
84 % unless ( $opt{'disable_empty'} ) {
85   <OPTION VALUE="" <% $opt{county} eq '' ? 'SELECTED' : '' %>><% $opt{empty_label} %>
86 % }
87
88 % foreach my $county ( @counties ) {
89
90     <OPTION VALUE="<% $county |h %>"
91             <% $county eq $opt{'county'} ? 'SELECTED' : '' %>
92     ><% $county eq $opt{'empty_data_value'} ? $opt{'empty_data_label'} : $county %>
93
94 % } 
95
96   </SELECT>
97
98 % } else { 
99
100   <SCRIPT TYPE="text/javascript">
101     function <% $pre %>state_changed(what) {
102     }
103   </SCRIPT>
104
105   <SELECT NAME  = "<% $pre %>county"
106            ID   = "<% $pre %>county"
107           STYLE = "display:none"
108   >
109     <OPTION SELECTED VALUE="<% $opt{'county'} |h %>">
110   </SELECT>
111
112 % } 
113
114 <%init>
115
116 my %opt = @_;
117 foreach my $opt (qw( county state country prefix onchange disabled
118                      empty_value )) {
119   $opt{$opt} = '' unless exists($opt{$opt}) && defined($opt{$opt});
120 }
121
122 $opt{'disable_empty'} = 1 unless exists($opt{'disable_empty'});
123
124 my $pre = $opt{'prefix'};
125
126 my @counties = ();
127 if ( $countyflag ) {
128
129   @counties = map { length($_) ? $_ : $opt{'empty_data_value'} }
130                   counties( $opt{'state'}, $opt{'country'} );
131
132   # this is very hacky
133   unless ( scalar(@counties) > 1 ) {
134     if ( $opt{'disabled'} =~ /STYLE=/i ) {
135       $opt{'disabled'} =~ s/STYLE="([^"]+)"/STYLE="$1; display:none"/i;
136     } else {
137       $opt{'disabled'} .= ' STYLE="display:none"';
138     }
139   }
140
141 }
142
143 </%init>
144 <%once>
145
146 my $sql = "SELECT COUNT(*) FROM cust_main_county".
147           " WHERE county IS NOT NULL AND county != ''";
148 my $sth = dbh->prepare($sql) or die dbh->errstr;
149 $sth->execute or die $sth->errstr;
150 my $countyflag = $sth->fetchrow_arrayref->[0];
151
152 </%once>