agent virtualization, take one (stuff from "inactive" changeset snuck into cust_main...
[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
17   my( %opt ) = @_;
18
19   #warn "***** select-table: \n". Dumper(%opt);
20
21   my $key = dbdef->table($opt{'table'})->primary_key; #? $opt{'primary_key'} ||
22
23   my $name_col = $opt{'name_col'};
24
25   my @records = ();
26   if ( $opt{'records'} ) {
27     @records = @{ $opt{'records'} };
28   } else {
29     @records = qsearch( {
30       'table'     => $opt{'table'},
31       'hashref'   => ( $opt{'hashref'} || {} ),
32       'extra_sql' => ( $opt{'extra_sql'} || '' ),
33     });
34   }
35
36   my @pre_options = $opt{'pre_options'} ? @{ $opt{'pre_options'} } : ();
37
38 %>
39
40 <SELECT NAME="<%= $key %>">
41
42   <% while ( @pre_options ) { %>
43     <OPTION VALUE="<%= shift(@pre_options) %>"><%= shift(@pre_options) %>
44   <% } %>
45
46   <OPTION VALUE=""><%= $opt{'empty_label'} || 'all' %>
47
48   <% foreach my $record ( sort { $a->$name_col() cmp $b->$name_col() }
49                                @records
50                         )
51      {
52   %>
53
54     <OPTION VALUE="<%= $record->$key() %>"<%= $opt{'value'} == $record->$key() ? ' SELECTED' : '' %>><%= $record->$name_col() %>
55
56   <% } %>
57
58 </SELECT>
59