Add the ability to link customer service definition fields to inventory
[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 <SELECT NAME="<%= $opt{'element_name'} || $key %>" <%= $opt{'element_etc'} %>>
45
46   <% while ( @pre_options ) { %>
47     <OPTION VALUE="<%= shift(@pre_options) %>"><%= shift(@pre_options) %>
48   <% } %>
49
50   <OPTION VALUE=""><%= $opt{'empty_label'} || 'all' %>
51
52   <% foreach my $record ( sort { $a->$name_col() cmp $b->$name_col() }
53                                @records
54                         )
55      {
56   %>
57
58     <OPTION VALUE="<%= $record->$key() %>"<%= $opt{'value'} == $record->$key() ? ' SELECTED' : '' %>><%= $record->$name_col() %>
59
60   <% } %>
61
62 </SELECT>
63