escape labels
[freeside.git] / httemplate / elements / select.html
1 <%doc>
2 <& select.html,
3   # required
4     field       => 'myfield', # NAME property
5     curr_value  => 'foo',
6     labels      => { # or 'option_labels'
7                      # note: these will be escaped for you, don't escape them
8                      'AL' => 'Alabama',
9                      'AK' => 'Alaska',
10                      'AR' => 'Arkansas',
11                    },
12     options     => [ 'AL', 'AK', 'AR' ],
13     curr_value  => $cgi->param('myfield'),
14
15   # recommended    
16     id          => 'myid',    # DOM id
17
18   # optional
19     size        => 1,         # to show multiple rows at once
20     style       => '',        # STYLE property
21     multiple    => 0,
22     disabled    => 0,
23     onchange    => 'do_something()',
24     js_only     => 0,         # disables the whole thing
25     element_etc => '',        # anything else to put in the <select> tag
26 &>
27 </%doc>
28     
29 % unless ( $opt{'js_only'} ) {
30
31 <SELECT NAME          = "<% $opt{field} %>"
32         ID            = "<% $opt{id} %>"
33         previousValue = "<% $curr_value %>"
34         previousText  = "<% $labels->{$curr_value} || $curr_value |h %>"
35         <% $multiple %>
36         <% $size %>
37         <% $style %>
38         <% $opt{disabled} %>
39         <% $onchange %>
40         <% $opt{'element_etc'} %>
41 >
42
43 % if ( $opt{options} ) {
44 %
45 %   foreach my $option ( @{ $opt{options} } ) { #just arrayref for now
46
47       <OPTION VALUE="<% $option %>"
48               <% $opt{curr_value} eq $option ? 'SELECTED' : '' %>
49       >
50         <% $labels->{$option} || $option |h %>
51       </OPTION>
52
53 %   }
54 %
55 % } else { #deprecated weird value hashref used only by reason.html
56 %
57 %   my $aref = $opt{'value'}->{'values'};
58 %   my $vkey = $opt{'value'}->{'vcolumn'};
59 %   my $ckey = $opt{'value'}->{'ccolumn'};
60 %   foreach my $v (@$aref) {
61
62       <OPTION VALUE="<% $v->$vkey %>"
63               <% ($opt{curr_value} eq $v->$vkey) ? 'SELECTED' : '' %>
64       >
65         <% $v->$ckey %>
66       </OPTION>
67
68 %   }
69 %
70 % }
71
72 </SELECT>
73
74 % }
75 <%init>
76
77 my %opt = @_;
78
79 my $labels = $opt{'option_labels'} || $opt{'labels'};
80
81 my $curr_value = $opt{'curr_value'};
82
83 my $onchange = '';
84 if ( $opt{'onchange'} ) {
85   $onchange = $opt{'onchange'};
86   $onchange .= '(this)' unless $onchange =~ /\(\w*\);?$/;
87   $onchange =~ s/\(what\);/\(this\);/g; #ugh, terrible hack.  all onchange
88                                         #callbacks should act the same
89   $onchange = 'onChange="'. $onchange. '"' unless $onchange =~ /^onChange=/i;
90 }
91
92 $opt{'disabled'} = &{ $opt{'disabled'} }( \%opt )
93   if ref($opt{'disabled'}) eq 'CODE';
94 $opt{'disabled'} = 'DISABLED'
95   if $opt{'disabled'} && $opt{'disabled'} !~ /disabled/i; # uuh... yeah?
96
97 my @style = ref($opt{'style'})
98               ? @{ $opt{'style'} }
99               : $opt{'style'}
100                 ? ( $opt{'style'} )
101                 : ();
102
103 my $style = scalar(@style) ? 'STYLE="'. join(';', @style). '"' : '';
104
105 my $size = $opt{'size'} ? 'SIZE='.$opt{'size'} : '';
106
107 my $multiple = $opt{'multiple'} ? 'MULTIPLE' : '';
108
109 </%init>