use GROUP BY instead of DISTINCT ON in the state and country queries for better cross...
[freeside.git] / httemplate / edit / cust_main / select-county.html
1 % if ( $countyflag ) { 
2
3   <% include('/elements/xmlhttp.html',
4                 'url'  => $p.'misc/counties.cgi',
5                 'subs' => [ $opt{'prefix'}. 'get_counties' ],
6              )
7   %>
8   
9   <SCRIPT TYPE="text/javascript">
10   
11     function opt(what,value,text) {
12       var optionName = new Option(text, value, false, false);
13       var length = what.length;
14       what.options[length] = optionName;
15     }
16   
17     function <% $opt{'prefix'} %>state_changed(what, callback) {
18
19       state = what.options[what.selectedIndex].text;
20       country = what.form.<% $opt{'prefix'} %>country.options[what.form.<% $opt{'prefix'} %>country.selectedIndex].text;
21   
22       function <% $opt{'prefix'} %>update_counties(counties) {
23
24         // blank the current county list
25         for ( var i = what.form.<% $opt{'prefix'} %>county.length; i >= 0; i-- )
26             what.form.<% $opt{'prefix'} %>county.options[i] = null;
27   
28         // add the new counties
29         var countiesArray = eval('(' + counties + ')' );
30         for ( var s = 0; s < countiesArray.length; s++ ) {
31             var countyLabel = countiesArray[s];
32             if ( countyLabel == "" )
33                 countyLabel = '(n/a)';
34             opt(what.form.<% $opt{'prefix'} %>county, countiesArray[s], countyLabel);
35         }
36
37         //run the callback
38         if ( callback != null ) 
39           callback();
40       }
41   
42       // go get the new counties
43       <% $opt{'prefix'} %>get_counties( state, country, <% $opt{'prefix'} %>update_counties );
44   
45     }
46   
47   </SCRIPT>
48
49   <SELECT NAME="<% $opt{'prefix'} %>county" onChange="<% $opt{'onchange'} %>" <% $opt{'disabled'} %>>
50 % foreach my $county (
51 %       sort
52 %       map { $_->county }
53 %       qsearch('cust_main_county', { 'state'   => $opt{'state'},
54 %                                     'country' => $opt{'country'},
55 %                                   }
56 %              )
57 % ) {
58
59     <OPTION VALUE="<% $county %>"<% $county eq $opt{'county'} ? ' SELECTED' : '' %>><% $county %>
60
61 % } 
62
63   </SELECT>
64
65 % } else { 
66
67
68   <SCRIPT TYPE="text/javascript">
69     function <% $opt{'prefix'} %>state_changed(what) {
70     }
71   </SCRIPT>
72
73   <INPUT TYPE="hidden" NAME="<% $opt{'prefix'} %>county" VALUE="<% $opt{'county'} %>">
74
75 % } 
76
77 <%init>
78 my %opt = @_;
79 foreach my $opt (qw( county state country prefix onchange disabled )) {
80   $opt{$_} = '' unless exists($opt{$_}) && defined($opt{$_});
81 }
82
83 my $sql = "SELECT COUNT(*) FROM cust_main_county".
84           " WHERE county IS NOT NULL AND county != ''";
85 my $sth = dbh->prepare($sql) or die dbh->errstr;
86 $sth->execute or die $sth->errstr;
87 my $countyflag = $sth->fetchrow_arrayref->[0];
88 </%init>