agent type on package add/edit (ticket 1446)
[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 %   my $matches = 0;
13 %   ref($opt{'value'}) ? (exists($opt{'value'}->{$record->$key()}) and $matches=1)
14 %                      : ($opt{'value'} == $record->$key() and $matches=1);
15
16     <OPTION VALUE="<% $record->$key() %>"<% $matches ? ' SELECTED' : '' %>><% $record->$name_col() %>
17
18 % } 
19
20 </SELECT>
21 <%init>
22
23 ##required
24 # 'table'    => 'table_name',
25 # 'name_col' => 'name_column',
26 #
27 ##strongly recommended (you want your forms to be "sticky" on errors, right?)
28 # 'value'    => 'current_value',
29 #
30 ##opt
31 # 'empty_label'  => '', #better specify it though, the default might change
32 # 'hashref'      => {},
33 # 'extra_sql'    => '',
34 # 'records'      => \@records, #instead of hashref
35 # 'pre_options'  => [ 'value' => 'option' ], #before normal options
36 # 'element_name' => '', #HTML element name, defaults to the name of
37 #                       # the primary key column
38 # 'element_etc'  => '', #additional attributes (i.e. "DISABLED") for the
39 #                       #<SELECT> element
40 # 'debug'        => 0, #set true to enable
41
42 my( %opt ) = @_;
43
44 warn "elements/select-table.html: \n". Dumper(%opt)
45   if exists $opt{debug} && $opt{debug};
46
47 my $key = dbdef->table($opt{table})->primary_key; #? $opt{primary_key} ||
48
49 my $name_col = $opt{name_col};
50
51 $opt{hashref} ||= {};
52
53 my @records = ();
54 if ( $opt{records} ) {
55   @records = @{ $opt{records} };
56 } else {
57   @records = qsearch( {
58     'table'     => $opt{table},
59     'hashref'   => $opt{hashref},
60     'extra_sql' => ( $opt{extra_sql} || '' ),
61   });
62 }
63
64 unless (    ! $opt{value}
65          or ref($opt{value})
66          or ! exists( $opt{hashref}->{disabled} ) #??
67          or grep { $opt{value} == $_->$key() } @records
68        ) {
69   delete $opt{hashref}->{disabled};
70   $opt{hashref}->{$key} = $opt{value};
71   my $record = qsearchs( {
72     'table'     => $opt{table},
73     'hashref'   => $opt{hashref},
74     'extra_sql' => ( $opt{extra_sql} || '' ),
75   });
76   push @records, $record if $record;
77 }
78
79 if ( ref( $opt{value} ) eq 'ARRAY' ) {
80   $opt{value} = { map { $_ => 1 } @{$opt{value}} };
81 }
82
83 my @pre_options = $opt{pre_options} ? @{ $opt{pre_options} } : ();
84
85 </%init>