# sort
map { s/[\n\r]//g; $_; }
map { $_->state; }
- qsearch( 'cust_main_county',
- { 'country' => $country },
- 'DISTINCT ON ( state ) *',
- )
+ qsearch({
+ 'select' => 'state',
+ 'table' => 'cust_main_county',
+ 'hashref' => { 'country' => $country },
+ 'extra_sql' => 'GROUP BY state',
+ })
;
#it could throw a fatal "Invalid country code" error (for example "AX")
my %result;
tie %result, "Tie::IxHash";
my @stuff = @{ $sth->fetchall_arrayref( {} ) };
- if($pkey) {
+ if ( $pkey && $stuff[0]->{$pkey} ) {
%result = map { $_->{$pkey}, $_ } @stuff;
} else {
@result{@stuff} = @stuff;
-%
-%
-% my %opt = @_;
-% foreach my $opt (qw( county state country prefix onchange disabled )) {
-% $opt{$_} = '' unless exists($opt{$_}) && defined($opt{$_});
-% }
-%
-% my $conf = new FS::Conf;
-% my $countrydefault = $conf->config('countrydefault') || 'US';
-%
-%
-
<% include('/elements/xmlhttp.html',
'url' => $p.'misc/states.cgi',
</SCRIPT>
<SELECT NAME="<% $opt{'prefix'} %>country" onChange="<% $opt{'prefix'} %>country_changed(this); <% $opt{'onchange'} %>" <% $opt{'disabled'} %>>
+
% foreach my $country (
% sort { ($b eq $countrydefault) <=> ($a eq $countrydefault)
% or code2country($a) cmp code2country($b) }
% map { $_->country }
-% qsearch( 'cust_main_county',{}, 'DISTINCT ON ( country ) *', )
-% ) {
-%
-
+% qsearch({
+% 'select' => 'country',
+% 'table' => 'cust_main_county',
+% 'hashref' => {},
+% 'extra_sql' => 'GROUP BY country',
+% })
+% ) {
<OPTION VALUE="<% $country %>"<% $country eq $opt{'country'} ? ' SELECTED' : '' %>><% code2country($country). " ($country)" %>
-% }
+% }
</SELECT>
+<%init>
+my %opt = @_;
+foreach my $opt (qw( county state country prefix onchange disabled )) {
+ $opt{$_} = '' unless exists($opt{$_}) && defined($opt{$_});
+}
+
+my $conf = new FS::Conf;
+my $countrydefault = $conf->config('countrydefault') || 'US';
+</%init>
+
-%
-%
-% my %opt = @_;
-% foreach my $opt (qw( county state country prefix onchange disabled )) {
-% $opt{$_} = '' unless exists($opt{$_}) && defined($opt{$_});
-% }
-%
-% my $sql = "SELECT COUNT(*) FROM cust_main_county".
-% " WHERE county IS NOT NULL AND county != ''";
-% my $sth = dbh->prepare($sql) or die dbh->errstr;
-% $sth->execute or die $sth->errstr;
-% my $countyflag = $sth->fetchrow_arrayref->[0];
-%
-%
% if ( $countyflag ) {
-
<% include('/elements/xmlhttp.html',
'url' => $p.'misc/counties.cgi',
'subs' => [ $opt{'prefix'}. 'get_counties' ],
)
-%>
+ %>
<SCRIPT TYPE="text/javascript">
% 'country' => $opt{'country'},
% }
% )
-% ) {
-%
-
+% ) {
<OPTION VALUE="<% $county %>"<% $county eq $opt{'county'} ? ' SELECTED' : '' %>><% $county %>
-% }
+% }
</SELECT>
+
% } else {
</SCRIPT>
<INPUT TYPE="hidden" NAME="<% $opt{'prefix'} %>county" VALUE="<% $opt{'county'} %>">
+
% }
+<%init>
+my %opt = @_;
+foreach my $opt (qw( county state country prefix onchange disabled )) {
+ $opt{$_} = '' unless exists($opt{$_}) && defined($opt{$_});
+}
+
+my $sql = "SELECT COUNT(*) FROM cust_main_county".
+ " WHERE county IS NOT NULL AND county != ''";
+my $sth = dbh->prepare($sql) or die dbh->errstr;
+$sth->execute or die $sth->errstr;
+my $countyflag = $sth->fetchrow_arrayref->[0];
+</%init>
-%
-%
-% my %opt = @_;
-% foreach my $opt (qw( county state country prefix onchange disabled )) {
-% $opt{$_} = '' unless exists($opt{$_}) && defined($opt{$_});
-% }
-%
-%
-
-
<SELECT NAME="<% $opt{'prefix'} %>state" onChange="<% $opt{'prefix'} %>state_changed(this); <% $opt{'onchange'} %>" <% $opt{'disabled'} %>>
-% tie my %states, 'Tie::IxHash', states_hash( $opt{'country'} );
-% foreach my $state ( keys %states ) {
+% foreach my $state ( keys %states ) {
<OPTION VALUE="<% $state %>"<% $state eq $opt{'state'} ? ' SELECTED' : '' %>><% $states{$state} || '(n/a)' %>
+
% }
</SELECT>
+<%init>
+my %opt = @_;
+foreach my $opt (qw( county state country prefix onchange disabled )) {
+ $opt{$_} = '' unless exists($opt{$_}) && defined($opt{$_});
+}
+
+tie my %states, 'Tie::IxHash', states_hash( $opt{'country'} );
+</%init>
+