5 include( '/elements/select-table.html',
10 'table' => 'table_name',
11 'name_col' => 'name_column',
13 #strongly recommended (you want your forms to be "sticky" on errors, right?)
14 'curr_value' => 'current_value',
15 #'value' => #deprecated form of 'curr_value',
25 'agent_virt' => 0, #set true and make sure the result is JOINed to
26 #something with agentnum (usually cust_main)
27 'agent_null_right' => '', #right to see un-agented entries
29 'records' => \@records, #instead of search params
31 #basic params controlling the resulting <SELECT>
32 'pre_options' => [ 'value' => 'option' ], #before normal options
33 'empty_label' => '', #better specify it though, the default might change
34 'multiple' => 0, # bool
35 'disable_empty' => 0, # bool (implied by multiple)
36 'label_callback' => sub { my $record = shift; return "label"; },
38 #more params controlling HTML stuff about the <SELECT>
39 'element_name' => '', #HTML element name, defaults to the name of
40 # the primary key column
41 'field' => '', #synonym for element_name
42 'element_etc' => '', #additional attributes (i.e. "DISABLED") for the
44 'onchange' => '', #javascript code
46 #special return options
47 'js_only' => 0, #set true to return only the JS portions (i.e. nothing)
48 'html_only' => 0, #set true to return only the HTML portions (no-op, i.e. return everything)
51 'debug' => 0, #set true to enable
56 % unless ( $opt{'js_only'} ) {
58 <SELECT <% $opt{'multiple'} ? 'MULTIPLE' : '' %>
59 NAME = "<% $opt{'element_name'} || $opt{'field'} || $key %>"
60 ID = "<% $opt{'id'} || $key %>"
62 <% $opt{'element_etc'} %>
65 % while ( @pre_options ) {
66 <OPTION VALUE="<% shift(@pre_options) %>"><% shift(@pre_options) %>
70 % unless ( $opt{'multiple'} || $opt{'disable_empty'} ) {
71 <OPTION VALUE=""><% $opt{'empty_label'} || 'all' %>
74 % foreach my $record ( sort { $a->$name_col() cmp $b->$name_col() } @records ) {
75 % my $recvalue = $record->$key();
76 <OPTION VALUE="<% $recvalue %>"
77 <% ref($value) && $value->{$recvalue} || $value == $recvalue
80 ><% $opt{'label_callback'}
81 ? &{ $opt{'label_callback'} }( $record )
82 : $record->$name_col()
93 warn "elements/select-table.html: \n". Dumper(%opt)
94 if exists $opt{debug} && $opt{debug};
97 if ( $opt{'onchange'} ) {
98 $onchange = $opt{'onchange'};
99 $onchange .= '(this)' unless $onchange =~ /\(\w*\);?$/;
100 $onchange =~ s/\(what\);/\(this\);/g; #ugh, terrible hack. all onchange
101 #callbacks should act the same
102 $onchange = 'onChange="'. $onchange. '"';
105 my $dbdef_table = dbdef->table($opt{'table'})
106 or die "can't find dbdef for ". $opt{'table'}. " table\n";
108 my $key = $dbdef_table->primary_key; #? $opt{'primary_key'} ||
110 my $name_col = $opt{'name_col'};
112 my $value = $opt{'curr_value'} || $opt{'value'};
113 $value = [ split(/\s*,\s*/, $value) ] if $opt{'multiple'} && $value =~ /,/;
115 #my $addl_from = $opt{'addl_from'} || '';
116 my $extra_sql = $opt{'extra_sql'} || '';
117 my $hashref = $opt{'hashref'} || {};
119 if ( $opt{'agent_virt'} ) {
121 ( $extra_sql =~ /WHERE/i || scalar(keys %$hashref ) ? ' AND ' : ' WHERE ' ).
122 $FS::CurrentUser::CurrentUser->agentnums_sql(
123 'null_right' => $opt{'agent_null_right'}
128 if ( $opt{'records'} ) {
129 @records = @{ $opt{'records'} };
131 @records = qsearch( {
132 'table' => $opt{'table'},
133 'addl_from' => $opt{'addl_from'},
134 'hashref' => $hashref,
135 'extra_sql' => $extra_sql,
136 'order_by' => ( $opt{'order_by'} || "ORDER BY $name_col" ),
142 or ! exists( $opt{hashref}->{disabled} ) #??
143 or grep { $value == $_->$key() } @records
145 delete $opt{hashref}->{disabled};
146 $opt{hashref}->{$key} = $value;
147 my $record = qsearchs( {
148 'table' => $opt{table},
149 'addl_from' => $opt{'addl_from'},
150 'hashref' => $hashref,
151 'extra_sql' => $extra_sql,
153 push @records, $record if $record;
156 if ( ref( $value ) eq 'ARRAY' ) {
157 $value = { map { $_ => 1 } @$value };
160 my @pre_options = $opt{pre_options} ? @{ $opt{pre_options} } : ();