5 include( '/elements/select-table.html',
8 'table' => 'table_name',
9 'name_col' => 'name_column',
11 #strongly recommended (you want your forms to be "sticky" on errors, right?)
12 'curr_value' => 'current_value',
13 #'value' => #deprecated form of 'curr_value',
16 'empty_label' => '', #better specify it though, the default might change
19 'records' => \@records, #instead of hashref
20 'pre_options' => [ 'value' => 'option' ], #before normal options
21 'element_name' => '', #HTML element name, defaults to the name of
22 # the primary key column
23 'field' => '', #synonym for element_name
24 'element_etc' => '', #additional attributes (i.e. "DISABLED") for the
26 'onchange' => '', #javascript code
27 'multiple' => 0, # bool
28 'disable_empty' => 0, # bool (implied by multiple)
29 'debug' => 0, #set true to enable
30 'label_callback' => sub { my $record = shift; return "label"; },
31 'js_only' => 0, #set true to return only the JS portions (i.e. nothing)
32 'html_only' => 0, #set true to return only the HTML portions (no-op, i.e. return everything)
36 % unless ( $opt{'js_only'} ) {
38 <SELECT <% $opt{'multiple'} ? 'MULTIPLE' : '' %>
39 NAME = "<% $opt{'element_name'} || $opt{'field'} || $key %>"
40 ID = "<% $opt{'id'} || $key %>"
42 <% $opt{'element_etc'} %>
45 % while ( @pre_options ) {
46 <OPTION VALUE="<% shift(@pre_options) %>"><% shift(@pre_options) %>
50 % unless ( $opt{'multiple'} || $opt{'disable_empty'} ) {
51 <OPTION VALUE=""><% $opt{'empty_label'} || 'all' %>
54 % foreach my $record ( sort { $a->$name_col() cmp $b->$name_col() } @records ) {
55 % my $recvalue = $record->$key();
56 <OPTION VALUE="<% $recvalue %>"
57 <% ref($value) && $value->{$recvalue} || $value == $recvalue
60 ><% $opt{'label_callback'}
61 ? &{ $opt{'label_callback'} }( $record )
62 : $record->$name_col()
73 warn "elements/select-table.html: \n". Dumper(%opt)
74 if exists $opt{debug} && $opt{debug};
77 if ( $opt{'onchange'} ) {
78 $onchange = $opt{'onchange'};
79 $onchange .= '(this)' unless $onchange =~ /\(\w*\);?$/;
80 $onchange =~ s/\(what\);/\(this\);/g; #ugh, terrible hack. all onchange
81 #callbacks should act the same
82 $onchange = 'onChange="'. $onchange. '"';
85 my $dbdef_table = dbdef->table($opt{'table'})
86 or die "can't find dbdef for ". $opt{'table'}. " table\n";
88 my $key = $dbdef_table->primary_key; #? $opt{'primary_key'} ||
90 my $name_col = $opt{'name_col'};
92 my $value = $opt{'curr_value'} || $opt{'value'};
93 $value = [ split(/\s*,\s*/, $value) ] if $opt{'multiple'} && $value =~ /,/;
95 my $extra_sql = $opt{'extra_sql'} || '';
96 my $hashref = $opt{'hashref'} || {};
98 if ( $opt{'agent_virt'} ) {
100 ( $extra_sql =~ /WHERE/i || scalar(keys %$hashref ) ? ' AND ' : ' WHERE ' ).
101 $FS::CurrentUser::CurrentUser->agentnums_sql(
102 'null_right' => $opt{'agent_null_right'}
107 if ( $opt{'records'} ) {
108 @records = @{ $opt{'records'} };
110 @records = qsearch( {
111 'table' => $opt{'table'},
112 'hashref' => $hashref,
113 'extra_sql' => $extra_sql,
114 'order_by' => ( $opt{'order_by'} || "ORDER BY $name_col" ),
120 or ! exists( $opt{hashref}->{disabled} ) #??
121 or grep { $value == $_->$key() } @records
123 delete $opt{hashref}->{disabled};
124 $opt{hashref}->{$key} = $value;
125 my $record = qsearchs( {
126 'table' => $opt{table},
127 'hashref' => $hashref,
128 'extra_sql' => ( $opt{extra_sql} || '' ),
130 push @records, $record if $record;
133 if ( ref( $value ) eq 'ARRAY' ) {
134 $value = { map { $_ => 1 } @$value };
137 my @pre_options = $opt{pre_options} ? @{ $opt{pre_options} } : ();