summaryrefslogtreecommitdiff
path: root/httemplate/elements/select-county.html
blob: 59f235a233e86973e0f8c060f039f9af99fedb3e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<%doc>

Example:

  include( '/elements/select-county.html',
    #recommended
    country    => $current_country,
    state      => $current_state,
    county     => $current_county,

    #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' ],
  );

</%doc>
% if ( $countyflag ) { 

  <% include('/elements/xmlhttp.html',
                'url'  => $p.'misc/counties.cgi',
                'subs' => [ $pre. 'get_counties' ],
             )
  %>
  
  <SCRIPT TYPE="text/javascript">
  
    function opt(what,value,text) {
      var optionName = new Option(text, value, false, false);
      var length = what.length;
      what.options[length] = optionName;
    }
  
    function <% $pre %>state_changed(what, callback) {

      state = what.options[what.selectedIndex].value;
      country = what.form.<% $pre %>country.options[what.form.<% $pre %>country.selectedIndex].value;
  
      function <% $pre %>update_counties(counties) {

        // blank the current county list
        for ( var i = what.form.<% $pre %>county.length; i >= 0; i-- )
            what.form.<% $pre %>county.options[i] = null;
  
        // add the new counties
        var countiesArray = eval('(' + counties + ')' );
        for ( var s = 0; s < countiesArray.length; s++ ) {
            var countyLabel = countiesArray[s];
            if ( countyLabel == "" )
                countyLabel = '(n/a)';
            opt(what.form.<% $pre %>county, countiesArray[s], countyLabel);
        }

        var countyFormLabel = document.getElementById('<% $pre %>countylabel');

        if ( countiesArray.length > 1 ) { 
          what.form.<% $pre %>county.style.display = '';
          countyFormLabel.style.visibility = 'visible';
        } else {
          what.form.<% $pre %>county.style.display = 'none';
          countyFormLabel.style.visibility = 'hidden';
        }

        //run the callback
        if ( callback != null ) 
          callback();
      }
  
      // go get the new counties
      <% $pre %>get_counties( state, country, <% $pre %>update_counties );
  
    }
  
  </SCRIPT>

  <SELECT NAME    = "<% $pre %>county"
          ID      = "<% $pre %>county"
          onChange= "<% $opt{'onchange'} %>"
          <% $opt{'disabled'} %>
          <% $style %>
  >

% unless ( $opt{'disable_empty'} ) {
  <OPTION VALUE="" <% $opt{county} eq '' ? 'SELECTED' : '' %>><% $opt{empty_label} %>
% }

% foreach my $county ( @counties ) {

    <OPTION VALUE="<% $county |h %>"
            <% $county eq $opt{'county'} ? 'SELECTED' : '' %>
    ><% $county eq $opt{'empty_data_value'} ? $opt{'empty_data_label'} : $county %>

% } 

  </SELECT>

% } else { 

  <SCRIPT TYPE="text/javascript">
    function <% $pre %>state_changed(what) {
    }
  </SCRIPT>

  <SELECT NAME  = "<% $pre %>county"
           ID   = "<% $pre %>county"
          STYLE = "display:none"
  >
    <OPTION SELECTED VALUE="<% $opt{'county'} |h %>">
  </SELECT>

% } 

<%init>

my %opt = @_;
foreach my $opt (qw( county state country prefix onchange disabled
                     empty_value )) {
  $opt{$opt} = '' unless exists($opt{$opt}) && defined($opt{$opt});
}

$opt{'disable_empty'} = 1 unless exists($opt{'disable_empty'});

my $pre = $opt{'prefix'};

$opt{'style'} ||= [];
my $style =
  scalar(@{$opt{style}})
    ? 'STYLE="'. join(';', @{$opt{style}}). '"'
    : '';

my @counties = ();
if ( $countyflag ) {

  @counties = map { length($_) ? $_ : $opt{'empty_data_value'} }
                  counties( $opt{'state'}, $opt{'country'} );

  # this is very hacky
  unless ( scalar(@counties) > 1 ) {
    if ( $opt{'disabled'} =~ /STYLE=/i ) {
      $opt{'disabled'} =~ s/STYLE="([^"]+)"/STYLE="$1; display:none"/i;
    } else {
      $opt{'disabled'} .= ' STYLE="display:none"';
    }
  }

}

</%init>
<%once>

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];

</%once>