This commit was generated by cvs2svn to compensate for changes in r4888,
[freeside.git] / httemplate / elements / select-table.html
1 %
2 %
3 %  ##required
4 %  # 'table'    => 'table_name',
5 %  # 'name_col' => 'name_column',
6 %  #
7 %  ##strongly recommended (you want your forms to be "sticky" on errors, right?)
8 %  # 'value'    => 'current_value',
9 %  #
10 %  ##opt
11 %  # 'empty_label'  => '', #better specify it though, the default might change
12 %  # 'hashref'      => {},
13 %  # 'extra_sql'    => '',
14 %  # 'records'      => \@records, #instead of hashref
15 %  # 'pre_options'  => [ 'value' => 'option' ], #before normal options
16 %  # 'element_name' => '', #HTML element name, defaults to the name of
17 %  #                       # the primary key column
18 %  # 'element_etc'  => '', #additional attributes (i.e. "DISABLED") for the
19 %  #                       #<SELECT> element
20 %
21 %  my( %opt ) = @_;
22 %
23 %  #warn "***** select-table: \n". Dumper(%opt);
24 %
25 %  my $key = dbdef->table($opt{'table'})->primary_key; #? $opt{'primary_key'} ||
26 %
27 %  my $name_col = $opt{'name_col'};
28 %
29 %  my @records = ();
30 %  if ( $opt{'records'} ) {
31 %    @records = @{ $opt{'records'} };
32 %  } else {
33 %    @records = qsearch( {
34 %      'table'     => $opt{'table'},
35 %      'hashref'   => ( $opt{'hashref'} || {} ),
36 %      'extra_sql' => ( $opt{'extra_sql'} || '' ),
37 %    });
38 %  }
39 %
40 %  my @pre_options = $opt{'pre_options'} ? @{ $opt{'pre_options'} } : ();
41 %
42 %
43
44
45 <SELECT NAME="<% $opt{'element_name'} || $key %>" <% $opt{'element_etc'} %>>
46 % while ( @pre_options ) { 
47
48     <OPTION VALUE="<% shift(@pre_options) %>"><% shift(@pre_options) %>
49 % } 
50
51
52   <OPTION VALUE=""><% $opt{'empty_label'} || 'all' %>
53 % foreach my $record ( sort { $a->$name_col() cmp $b->$name_col() }
54 %                               @records
55 %                        )
56 %     {
57 %  
58
59
60     <OPTION VALUE="<% $record->$key() %>"<% $opt{'value'} == $record->$key() ? ' SELECTED' : '' %>><% $record->$name_col() %>
61 % } 
62
63
64 </SELECT>
65