This commit was generated by cvs2svn to compensate for changes in r6255,
[freeside.git] / httemplate / elements / select-table.html
1 <%doc>
2
3 Example:
4
5   include( '/elements/select-table.html',
6
7     #required
8     'table'          => 'table_name',
9     'name_col'       => 'name_column',
10    
11     #strongly recommended (you want your forms to be "sticky" on errors, right?)
12     'curr_value'     => 'current_value',
13     #'value' => #deprecated form of 'curr_value',
14    
15     #opt
16     'empty_label'    => '', #better specify it though, the default might change
17     'hashref'        => {},
18     'extra_sql'      => '',
19     'records'        => \@records, #instead of hashref
20     'pre_options'    => [ 'value' => 'option' ], #before normal options
21     'element_name'   => '', #HTML element name, defaults to the name of
22                             # the primary key column
23     'field'          => '', #synonym for element_name
24     'element_etc'    => '', #additional attributes (i.e. "DISABLED") for the
25                             #<SELECT> element
26     'onchange'       => '', #javascript code
27     'multiple'       => 0, # bool
28     'disable_empty'  => 0, # bool (implied by multiple)
29     'debug'          => 0, #set true to enable
30     'label_callback' => sub { my $record = shift; return "label"; },
31   )
32
33 </%doc>
34
35 <SELECT <% $opt{'multiple'} ? 'MULTIPLE' : '' %>
36         NAME = "<% $opt{'element_name'} || $opt{'field'} || $key %>"
37         <% $onchange %>
38         <% $opt{'element_etc'} %>
39 >
40
41 % while ( @pre_options ) { 
42     <OPTION VALUE="<% shift(@pre_options) %>"><% shift(@pre_options) %>
43
44 % } 
45
46 % unless ( $opt{'multiple'} || $opt{'disable_empty'} ) {
47     <OPTION VALUE=""><% $opt{'empty_label'} || 'all' %>
48 % }
49
50 % foreach my $record ( sort { $a->$name_col() cmp $b->$name_col() } @records ) {
51 %   my $recvalue = $record->$key();
52     <OPTION VALUE="<% $recvalue %>"
53             <% ref($value) && $value->{$recvalue} || $value == $recvalue
54                ? ' SELECTED' : ''
55             %>
56     ><% $opt{'label_callback'}
57           ? &{ $opt{'label_callback'} }( $record )
58           : $record->$name_col()
59      %>
60 % } 
61
62 </SELECT>
63
64 <%init>
65
66 my( %opt ) = @_;
67
68 warn "elements/select-table.html: \n". Dumper(%opt)
69   if exists $opt{debug} && $opt{debug};
70
71 my $onchange = $opt{'onchange'}
72                  ? 'onChange="'. $opt{'onchange'}. '(this)"'
73                  : '';
74
75 my $key = dbdef->table($opt{'table'})->primary_key; #? $opt{'primary_key'} ||
76
77 my $name_col = $opt{'name_col'};
78
79 my $value = $opt{'curr_value'} || $opt{'value'};
80 $value = [ split(/\s*,\s*/, $value) ] if $opt{'multiple'} && $value =~ /,/;
81
82 my @records = ();
83 if ( $opt{'records'} ) {
84   @records = @{ $opt{'records'} };
85 } else {
86   @records = qsearch( {
87     'table'     => $opt{'table'},
88     'hashref'   => ( $opt{'hashref'} || {} ),
89     'extra_sql' => ( $opt{'extra_sql'} || '' ),
90   });
91 }
92
93 unless (    ! $value
94          or ref($value)
95          or ! exists( $opt{hashref}->{disabled} ) #??
96          or grep { $value == $_->$key() } @records
97        ) {
98   delete $opt{hashref}->{disabled};
99   $opt{hashref}->{$key} = $value;
100   my $record = qsearchs( {
101     'table'     => $opt{table},
102     'hashref'   => $opt{hashref},
103     'extra_sql' => ( $opt{extra_sql} || '' ),
104   });
105   push @records, $record if $record;
106 }
107
108 if ( ref( $value ) eq 'ARRAY' ) {
109   $value = { map { $_ => 1 } @$value };
110 }
111
112 my @pre_options = $opt{pre_options} ? @{ $opt{pre_options} } : ();
113
114 </%init>