5 include( '/elements/select-table.html',
10 'table' => 'table_name',
11 'name_col' => 'name_column', #or method if you pass an order_by
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 #instead of the primary key... only for special cases
33 'value_col' => 'columnname',
35 #basic params controlling the resulting <SELECT>
36 'pre_options' => [ 'value' => 'option' ], #before normal options
37 'post_options' => [ 'value' => 'option' ], #after normal options
38 'empty_label' => '', #better specify it though, the default might change
39 'multiple' => 0, # bool
40 'disable_empty' => 0, # bool (implied by multiple)
41 'label_showkey' => 0, # bool
42 'label_callback' => sub { my $record = shift; return "label"; },
44 #more params controlling HTML stuff about the <SELECT>
45 'element_name' => '', #HTML element name, defaults to the name of
46 # the primary key column
47 'field' => '', #synonym for element_name
48 'element_etc' => '', #additional attributes (i.e. "DISABLED") for the
50 'onchange' => '', #javascript code
52 #params (well, a param) controlling the <OPTION>s
53 'extra_option_attributes' => [ 'field' ], #field or method in $table objects
54 #(are prefixed w/data- per HTML5)
56 #special return options
57 'js_only' => 0, #set true to return only the JS portions (i.e. nothing)
58 'html_only' => 0, #set true to return only the HTML portions (no-op, i.e. return everything)
61 'debug' => 0, #set true to enable
66 % unless ( $opt{'js_only'} ) {
68 <SELECT <% $opt{'multiple'} ? 'MULTIPLE' : '' %>
69 NAME = "<% $opt{'element_name'} || $opt{'field'} || $key %>"
70 ID = "<% $opt{'id'} || $key %>"
72 <% $opt{'element_etc'} %>
75 % while ( @pre_options ) {
76 % my $pre_opt = shift(@pre_options);
77 % my $pre_label = shift(@pre_options);
78 % my $selected = ( ref($value) && $value->{$pre_opt} )
79 % || ( $value eq $pre_opt );
80 <OPTION VALUE="<% $pre_opt %>"
81 <% $selected ? 'SELECTED' : '' %>
85 % unless ( $opt{'multiple'} || $opt{'disable_empty'} ) {
86 <OPTION VALUE=""><% $opt{'empty_label'} || 'all' %>
89 % foreach my $record ( sort { $a->$name_col() cmp $b->$name_col()
90 % || $a->$key() <=> $b->$key()
95 % my $recvalue = $record->$key();
97 % if ( $opt{'all_selected'} ) {
99 % } elsif ( $opt{'compare_sub'} && !ref($value) ) {
100 % $selected = &{ $opt{'compare_sub'} }( $value, $recvalue );
102 % $selected = ( ref($value) && $value->{$recvalue} )
103 % || ( $value && $value eq $recvalue ); #not == because of value_col
105 <OPTION VALUE="<% $recvalue %>"
106 <% $selected ? ' SELECTED' : '' %>
107 % foreach my $att ( @{ $opt{'extra_option_attributes'} } ) {
108 data-<% $att %>="<% $record->$att() |h %>"
110 ><% $opt{'label_showkey'} ? "$recvalue: " : '' %>
111 <% $opt{'label_callback'}
112 ? &{ $opt{'label_callback'} }( $record )
113 : $record->$name_col()
118 % while ( @post_options ) {
119 % my $post_opt = shift(@post_options);
120 % my $post_label = shift(@post_options);
121 % my $selected = ( ref($value) && $value->{$post_opt} )
122 % || ( $value eq $post_opt );
123 <OPTION VALUE="<% $post_opt %>"
124 <% $selected ? 'SELECTED' : '' %>
135 warn "elements/select-table.html: \n". Dumper(\%opt)
136 if exists $opt{debug} && $opt{debug};
138 $opt{'extra_option_attributes'} ||= [];
141 if ( $opt{'onchange'} ) {
142 $onchange = $opt{'onchange'};
143 $onchange .= '(this)' unless $onchange =~ /\(\w*\);?$/;
144 $onchange =~ s/\(what\);/\(this\);/g; #ugh, terrible hack. all onchange
145 #callbacks should act the same
146 $onchange = 'onChange="'. $onchange. '"';
149 my $dbdef_table = dbdef->table($opt{'table'})
150 or die "can't find dbdef for ". $opt{'table'}. " table\n";
152 my $key = $opt{'value_col'} || $dbdef_table->primary_key;
154 my $name_col = $opt{'name_col'};
156 my $value = $opt{'curr_value'} || $opt{'value'};
157 $value = [ split(/\s*,\s*/, $value) ] if $opt{'multiple'} && $value =~ /,/;
159 #my $addl_from = $opt{'addl_from'} || '';
160 my $extra_sql = $opt{'extra_sql'} || '';
161 my $hashref = $opt{'hashref'} || {};
163 if ( $opt{'agent_virt'} ) {
165 ( $extra_sql =~ /WHERE/i || scalar(keys %$hashref ) ? ' AND ' : ' WHERE ' ).
166 $FS::CurrentUser::CurrentUser->agentnums_sql(
167 'null' => $opt{'agent_null'},
168 'null_right' => $opt{'agent_null_right'},
173 if ( $opt{'records'} ) {
174 @records = @{ $opt{'records'} };
176 @records = qsearch( {
177 'table' => $opt{'table'},
178 'addl_from' => $opt{'addl_from'},
179 'hashref' => $hashref,
180 'extra_sql' => $extra_sql,
181 'order_by' => ( $opt{'order_by'} || "ORDER BY $name_col" ),
185 if ( ref( $value ) eq 'ARRAY' ) {
186 $value = { map { $_ => 1 } @$value };
189 unless ( !ref($value) && $value < 1 # !$value #ignore negatives too
190 or ! exists( $opt{hashref}->{disabled} ) #??
191 #or grep { $value == $_->$key() } @records
193 delete $opt{hashref}->{disabled};
195 foreach my $v ( ref($value) ? keys %$value : ($value) ) {
196 next if grep { $v == $_->$key() } @records;
198 $opt{hashref}->{$key} = $v;
199 my $record = qsearchs( {
200 'table' => $opt{table},
201 'addl_from' => $opt{'addl_from'},
202 'hashref' => $hashref,
203 'extra_sql' => $extra_sql,
205 push @records, $record if $record;
210 my @pre_options = $opt{pre_options} ? @{ $opt{pre_options} } : ();
211 my @post_options = $opt{post_options} ? @{ $opt{post_options} } : ();