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' => 0, #set true to always show un-agented entries
28 'agent_null_right' => '', #right to see un-agented entries
30 'records' => \@records, #instead of search params
32 #basic params controlling the resulting <SELECT>
33 'pre_options' => [ 'value' => 'option' ], #before normal options
34 'empty_label' => '', #better specify it though, the default might change
35 'multiple' => 0, # bool
36 'disable_empty' => 0, # bool (implied by multiple)
37 'label_callback' => sub { my $record = shift; return "label"; },
39 #more params controlling HTML stuff about the <SELECT>
40 'element_name' => '', #HTML element name, defaults to the name of
41 # the primary key column
42 'field' => '', #synonym for element_name
43 'element_etc' => '', #additional attributes (i.e. "DISABLED") for the
45 'onchange' => '', #javascript code
47 #special return options
48 'js_only' => 0, #set true to return only the JS portions (i.e. nothing)
49 'html_only' => 0, #set true to return only the HTML portions (no-op, i.e. return everything)
52 'debug' => 0, #set true to enable
57 % unless ( $opt{'js_only'} ) {
59 <SELECT <% $opt{'multiple'} ? 'MULTIPLE' : '' %>
60 NAME = "<% $opt{'element_name'} || $opt{'field'} || $key %>"
61 ID = "<% $opt{'id'} || $key %>"
63 <% $opt{'element_etc'} %>
66 % while ( @pre_options ) {
67 <OPTION VALUE="<% shift(@pre_options) %>"><% shift(@pre_options) %>
71 % unless ( $opt{'multiple'} || $opt{'disable_empty'} ) {
72 <OPTION VALUE=""><% $opt{'empty_label'} || 'all' %>
75 % foreach my $record ( sort { $a->$name_col() cmp $b->$name_col() } @records ) {
76 % my $recvalue = $record->$key();
77 <OPTION VALUE="<% $recvalue %>"
78 <% ref($value) && $value->{$recvalue} || $value == $recvalue
81 ><% $opt{'label_callback'}
82 ? &{ $opt{'label_callback'} }( $record )
83 : $record->$name_col()
94 warn "elements/select-table.html: \n". Dumper(%opt)
95 if exists $opt{debug} && $opt{debug};
98 if ( $opt{'onchange'} ) {
99 $onchange = $opt{'onchange'};
100 $onchange .= '(this)' unless $onchange =~ /\(\w*\);?$/;
101 $onchange =~ s/\(what\);/\(this\);/g; #ugh, terrible hack. all onchange
102 #callbacks should act the same
103 $onchange = 'onChange="'. $onchange. '"';
106 my $dbdef_table = dbdef->table($opt{'table'})
107 or die "can't find dbdef for ". $opt{'table'}. " table\n";
109 my $key = $dbdef_table->primary_key; #? $opt{'primary_key'} ||
111 my $name_col = $opt{'name_col'};
113 my $value = $opt{'curr_value'} || $opt{'value'};
114 $value = [ split(/\s*,\s*/, $value) ] if $opt{'multiple'} && $value =~ /,/;
116 #my $addl_from = $opt{'addl_from'} || '';
117 my $extra_sql = $opt{'extra_sql'} || '';
118 my $hashref = $opt{'hashref'} || {};
120 if ( $opt{'agent_virt'} ) {
122 ( $extra_sql =~ /WHERE/i || scalar(keys %$hashref ) ? ' AND ' : ' WHERE ' ).
123 $FS::CurrentUser::CurrentUser->agentnums_sql(
124 'null' => $opt{'agent_null'},
125 'null_right' => $opt{'agent_null_right'},
130 if ( $opt{'records'} ) {
131 @records = @{ $opt{'records'} };
133 @records = qsearch( {
134 'table' => $opt{'table'},
135 'addl_from' => $opt{'addl_from'},
136 'hashref' => $hashref,
137 'extra_sql' => $extra_sql,
138 'order_by' => ( $opt{'order_by'} || "ORDER BY $name_col" ),
142 unless ( $value < 1 # !$value #ignore negatives too
144 or ! exists( $opt{hashref}->{disabled} ) #??
145 or grep { $value == $_->$key() } @records
147 delete $opt{hashref}->{disabled};
148 $opt{hashref}->{$key} = $value;
149 my $record = qsearchs( {
150 'table' => $opt{table},
151 'addl_from' => $opt{'addl_from'},
152 'hashref' => $hashref,
153 'extra_sql' => $extra_sql,
155 push @records, $record if $record;
158 if ( ref( $value ) eq 'ARRAY' ) {
159 $value = { map { $_ => 1 } @$value };
162 my @pre_options = $opt{pre_options} ? @{ $opt{pre_options} } : ();