combine ticket notification scrips, #15353
[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     style         => [ 'attribute:value', 'another:value' ],
18   );
19
20 </%doc>
21 % if ( $countyflag ) { 
22
23   <% include('/elements/xmlhttp.html',
24                 'url'  => $p.'misc/counties.cgi',
25                 'subs' => [ $pre. 'get_counties' ],
26              )
27   %>
28   
29   <SCRIPT TYPE="text/javascript">
30   
31     function opt(what,value,text) {
32       var optionName = new Option(text, value, false, false);
33       var length = what.length;
34       what.options[length] = optionName;
35     }
36   
37     function <% $pre %>state_changed(what, callback) {
38
39       state = what.options[what.selectedIndex].value;
40       country = what.form.<% $pre %>country.options[what.form.<% $pre %>country.selectedIndex].value;
41   
42       function <% $pre %>update_counties(counties) {
43
44         // blank the current county list
45         for ( var i = what.form.<% $pre %>county.length; i >= 0; i-- )
46             what.form.<% $pre %>county.options[i] = null;
47   
48         // add the new counties
49         var countiesArray = eval('(' + counties + ')' );
50         for ( var s = 0; s < countiesArray.length; s++ ) {
51             var countyLabel = countiesArray[s];
52             if ( countyLabel == "" )
53                 countyLabel = '(n/a)';
54             opt(what.form.<% $pre %>county, countiesArray[s], countyLabel);
55         }
56
57         var countyFormLabel = document.getElementById('<% $pre %>countylabel');
58
59         if ( countiesArray.length > 1 ) { 
60           what.form.<% $pre %>county.style.display = '';
61           //countyFormLabel.style.visibility = 'visible';
62           countyFormLabel.style.display = '';
63         } else {
64           what.form.<% $pre %>county.style.display = 'none';
65           //countyFormLabel.style.visibility = 'hidden';
66           countyFormLabel.style.display = 'none';
67         }
68
69         //run the callback
70         if ( callback != null )  {
71           callback();
72         } else {
73           <% $pre %>county_changed(what.form.<% $pre %>county);
74         }
75       }
76   
77       // go get the new counties
78       <% $pre %>get_counties( state, country, <% $pre %>update_counties );
79   
80     }
81   
82   </SCRIPT>
83
84   <SELECT NAME    = "<% $pre %>county"
85           ID      = "<% $pre %>county"
86           onChange= "<% $onchange %>"
87           <% $opt{'disabled'} %>
88           <% $style %>
89   >
90
91 % unless ( $opt{'disable_empty'} ) {
92   <OPTION VALUE="" <% $opt{county} eq '' ? 'SELECTED' : '' %>><% $opt{empty_label} %>
93 % }
94
95 % foreach my $county ( @counties ) {
96
97     <OPTION VALUE="<% $county |h %>"
98             <% $county eq $opt{'county'} ? 'SELECTED' : '' %>
99     ><% $county eq $opt{'empty_data_value'} ? $opt{'empty_data_label'} : $county %>
100
101 % } 
102
103   </SELECT>
104
105 % } else { 
106
107   <SCRIPT TYPE="text/javascript">
108     function <% $pre %>state_changed(what) {
109     }
110   </SCRIPT>
111
112   <SELECT NAME  = "<% $pre %>county"
113            ID   = "<% $pre %>county"
114           STYLE = "display:none"
115   >
116     <OPTION SELECTED VALUE="<% $opt{'county'} |h %>">
117   </SELECT>
118
119 % } 
120
121 <%init>
122
123 my %opt = @_;
124 foreach my $opt (qw( county state country prefix onchange disabled
125                      empty_value )) {
126   $opt{$opt} = '' unless exists($opt{$opt}) && defined($opt{$opt});
127 }
128
129 $opt{'disable_empty'} = 1 unless exists($opt{'disable_empty'});
130
131 my $pre = $opt{'prefix'};
132
133
134 # disable_cityupdate?
135 my $onchange =
136   ( $opt{'disable_cityupdate'} ? '' : $pre.'county_changed(this); ' ).
137   $opt{'onchange'};
138
139 my $county_style = $opt{'style'} ? [ @{ $opt{'style'} } ] : [];
140
141 my @counties = ();
142 if ( $countyflag ) {
143
144   @counties = map { length($_) ? $_ : $opt{'empty_data_value'} }
145                   counties( $opt{'state'}, $opt{'country'} );
146
147   push @$county_style, 'display:none'
148     unless scalar(@counties) > 1;
149
150 }
151
152 my $style =
153   scalar(@$county_style)
154     ? 'STYLE="'. join(';', @$county_style). '"'
155     : '';
156
157 </%init>
158 <%once>
159
160 my $sql = "SELECT COUNT(*) FROM cust_main_county".
161           " WHERE county IS NOT NULL AND county != ''";
162 my $sth = dbh->prepare($sql) or die dbh->errstr;
163 $sth->execute or die $sth->errstr;
164 my $countyflag = $sth->fetchrow_arrayref->[0];
165
166 </%once>