Option to ignore old CDRs, RT#81480
[freeside.git] / httemplate / elements / select-state.html
1 <%doc>
2
3 Example:
4
5   <& /elements/select-state.html,
6     #recommended
7     country    => $current_country,
8     state      => $current_state,
9
10     #optional
11     prefix        => $optional_unique_prefix,
12     onchange      => $javascript,
13     disabled      => 0, #bool
14     disable_empty => 1, #defaults to 1, disable the empty option
15     empty_label   => 'all', #label for empty option
16     disable_countyupdate => 0, #bool - disabled update of the select-state.html
17     style         => [ 'attribute:value', 'another:value' ],
18   &>
19
20 </%doc>
21
22 <SELECT NAME     = "<% $pre %>state"
23         ID       = "<% $pre %>state"
24         onChange = "<% $onchange %>"
25         <% $opt{'disabled'} %>
26         <% $style %>
27 >
28
29 % unless ( $opt{'disable_empty'} ) {
30   <OPTION VALUE=""<% $opt{state} eq '' ? ' SELECTED' : '' %>><% $opt{empty_label} %></OPTION>
31 % }
32
33 % foreach my $state ( keys %states ) { 
34   <OPTION VALUE="<% $state |h %>"<% $state eq $opt{'state'} ? ' SELECTED' : '' %>><% $states{$state} || '(n/a)' |h %></OPTION>
35 % } 
36
37 </SELECT>
38
39 <%init>
40
41 my %opt = @_;
42 foreach my $opt (qw(
43   state country prefix onchange disabled empty_label svcpart
44 )) {
45   $opt{$opt} = '' unless exists($opt{$opt}) && defined($opt{$opt});
46 }
47
48 $opt{'disable_empty'} = 1 unless exists($opt{'disable_empty'});
49
50 my $pre = $opt{'prefix'};
51
52 my $onchange =
53   ( $opt{'disable_countyupdate'} ? '' : $pre.'state_changed(this); ' ).
54   $opt{'onchange'};
55
56 $opt{'style'} ||= [];
57 my $style =
58   scalar(@{$opt{style}})
59     ? 'STYLE="'. join(';', @{$opt{style}}). '"'
60     : '';
61
62 tie my %states, 'Tie::IxHash', states_hash( $opt{'country'} ); 
63
64 if ( $opt{'svcpart'} ) {
65
66   my $part_svc = qsearchs('part_svc', { 'svcpart' => $opt{'svcpart'} } );
67   if ( $part_svc && $part_svc->exporttype eq 'internal_diddb' ) {
68
69     my $sth = dbh->prepare(
70       'SELECT DISTINCT state FROM phone_avail WHERE svcnum IS NULL'
71     ) or die dbh->errstr;
72     $sth->execute or die $sth->errstr;
73     my %avail_states = map { $_->[0] => 1 } @{ $sth->fetchall_arrayref };
74
75     if ( %avail_states ) {
76       delete $states{$_} foreach grep ! $avail_states{$_}, keys %states;
77     }
78
79   }
80 }
81
82 </%init>
83