add ability to disable package classes
[freeside.git] / httemplate / elements / select-table.html
1 <SELECT NAME="<% $opt{'element_name'} || $key %>" <% $opt{'element_etc'} %>>
2
3 % while ( @pre_options ) { 
4
5     <OPTION VALUE="<% shift(@pre_options) %>"><% shift(@pre_options) %>
6
7 % } 
8
9   <OPTION VALUE=""><% $opt{'empty_label'} || 'all' %>
10
11 % foreach my $record ( sort { $a->$name_col() cmp $b->$name_col() } @records ) {
12
13     <OPTION VALUE="<% $record->$key() %>"<% $opt{'value'} == $record->$key() ? ' SELECTED' : '' %>><% $record->$name_col() %>
14
15 % } 
16
17 </SELECT>
18 <%init>
19
20 ##required
21 # 'table'    => 'table_name',
22 # 'name_col' => 'name_column',
23 #
24 ##strongly recommended (you want your forms to be "sticky" on errors, right?)
25 # 'value'    => 'current_value',
26 #
27 ##opt
28 # 'empty_label'  => '', #better specify it though, the default might change
29 # 'hashref'      => {},
30 # 'extra_sql'    => '',
31 # 'records'      => \@records, #instead of hashref
32 # 'pre_options'  => [ 'value' => 'option' ], #before normal options
33 # 'element_name' => '', #HTML element name, defaults to the name of
34 #                       # the primary key column
35 # 'element_etc'  => '', #additional attributes (i.e. "DISABLED") for the
36 #                       #<SELECT> element
37 # 'debug'        => 0, #set true to enable
38
39 my( %opt ) = @_;
40
41 warn "elements/select-table.html: \n". Dumper(%opt)
42   if exists $opt{debug} && $opt{debug};
43
44 my $key = dbdef->table($opt{table})->primary_key; #? $opt{primary_key} ||
45
46 my $name_col = $opt{name_col};
47
48 $opt{hashref} ||= {};
49
50 my @records = ();
51 if ( $opt{records} ) {
52   @records = @{ $opt{records} };
53 } else {
54   @records = qsearch( {
55     'table'     => $opt{table},
56     'hashref'   => $opt{hashref},
57     'extra_sql' => ( $opt{extra_sql} || '' ),
58   });
59 }
60
61 unless (    ! $opt{value}
62          or ! exists( $opt{hashref}->{disabled} ) #??
63          or grep { $opt{value} == $_->$key() } @records
64        ) {
65   delete $opt{hashref}->{disabled};
66   $opt{hashref}->{$key} = $opt{value};
67   my $record = qsearchs( {
68     'table'     => $opt{table},
69     'hashref'   => $opt{hashref},
70     'extra_sql' => ( $opt{extra_sql} || '' ),
71   });
72   push @records, $record if $record;
73 }
74
75 my @pre_options = $opt{pre_options} ? @{ $opt{pre_options} } : ();
76
77 </%init>