event refactor, landing on HEAD!
[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     'value'    => 'current_value',
13    
14     #opt
15     'empty_label'   => '', #better specify it though, the default might change
16     'hashref'       => {},
17     'extra_sql'     => '',
18     'records'       => \@records, #instead of hashref
19     'pre_options'   => [ 'value' => 'option' ], #before normal options
20     'element_name ' => '', #HTML element name, defaults to the name of
21                            # the primary key column
22     'field'         => '', #synonym for element_name
23     'element_etc'   => '', #additional attributes (i.e. "DISABLED") for the
24                            #<SELECT> element
25     'multiple'      => 0, # bool
26     'disable_empty' => 0, # bool (implied by multiple)
27     'debug'         => 0, #set true to enable
28
29   )
30
31 </%doc>
32
33 <SELECT <% $opt{'multiple'} ? 'MULTIPLE' : '' %> NAME="<% $opt{'element_name'} || $opt{'field'} || $key %>" <% $opt{'element_etc'} %>>
34
35 % while ( @pre_options ) { 
36     <OPTION VALUE="<% shift(@pre_options) %>"><% shift(@pre_options) %>
37
38 % } 
39
40 % unless ( $opt{'multiple'} || $opt{'disable_empty'} ) {
41     <OPTION VALUE=""><% $opt{'empty_label'} || 'all' %>
42 % }
43
44 % foreach my $record ( sort { $a->$name_col() cmp $b->$name_col() } @records ) {
45 %   my $recvalue = $record->$key();
46     <OPTION VALUE="<% $recvalue %>"
47             <% ref($value) && $value->{$recvalue} || $value == $recvalue
48                ? ' SELECTED' : ''
49             %>
50     ><% $record->$name_col() %>
51 % } 
52
53 </SELECT>
54
55 <%init>
56
57 my( %opt ) = @_;
58
59 warn "elements/select-table.html: \n". Dumper(%opt)
60   if exists $opt{debug} && $opt{debug};
61
62 my $key = dbdef->table($opt{'table'})->primary_key; #? $opt{'primary_key'} ||
63
64 my $name_col = $opt{'name_col'};
65
66 my $value = $opt{'curr_value'} || $opt{'value'};
67 $value = [ split(/\s*,\s*/, $value) ] if $opt{'multiple'} && $value =~ /,/;
68
69 my @records = ();
70 if ( $opt{'records'} ) {
71   @records = @{ $opt{'records'} };
72 } else {
73   @records = qsearch( {
74     'table'     => $opt{'table'},
75     'hashref'   => ( $opt{'hashref'} || {} ),
76     'extra_sql' => ( $opt{'extra_sql'} || '' ),
77   });
78 }
79
80 unless (    ! $value
81          or ref($value)
82          or ! exists( $opt{hashref}->{disabled} ) #??
83          or grep { $value == $_->$key() } @records
84        ) {
85   delete $opt{hashref}->{disabled};
86   $opt{hashref}->{$key} = $value;
87   my $record = qsearchs( {
88     'table'     => $opt{table},
89     'hashref'   => $opt{hashref},
90     'extra_sql' => ( $opt{extra_sql} || '' ),
91   });
92   push @records, $record if $record;
93 }
94
95 if ( ref( $value ) eq 'ARRAY' ) {
96   $value = { map { $_ => 1 } @$value };
97 }
98
99 my @pre_options = $opt{pre_options} ? @{ $opt{pre_options} } : ();
100
101 </%init>