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
31 'presorted' => 0, #set true to disable sorting the records on name_col
33 #instead of the primary key... only for special cases
34 'value_col' => 'columnname',
36 #basic params controlling the resulting <SELECT>
37 'pre_options' => [ 'value' => 'option' ], #before normal options
38 'post_options' => [ 'value' => 'option' ], #after normal options
39 'empty_label' => '', #better specify it though, the default might change
40 'multiple' => 0, # bool
41 'all_selected' => 0, # useful with multiple
42 'disable_empty' => 0, # bool (implied by multiple)
43 'label_showkey' => 0, # bool
44 'label_callback' => sub { my $record = shift; return "label"; },
46 #more params controlling HTML stuff about the <SELECT>
47 'element_name' => '', #HTML element name, defaults to the name of
48 # the primary key column
49 'field' => '', #synonym for element_name
50 'element_etc' => '', #additional attributes (i.e. "DISABLED") for the
52 'onchange' => '', #javascript code
54 #params (well, a param) controlling the <OPTION>s
55 'extra_option_attributes' => [ 'field' ], #field or method in $table objects
56 #(are prefixed w/data- per HTML5)
58 #special return options
59 'js_only' => 0, #set true to return only the JS portions (i.e. nothing)
60 'html_only' => 0, #set true to return only the HTML portions (no-op, i.e. return everything)
63 'debug' => 0, #set true to enable
68 % unless ( $opt{'js_only'} ) {
70 <SELECT <% $opt{'multiple'} ? 'MULTIPLE' : '' %>
71 NAME = "<% $opt{'element_name'} || $opt{'field'} || $key %>"
72 ID = "<% $opt{'id'} || $key %>"
75 <% $opt{'element_etc'} %>
78 % while ( @pre_options ) {
79 % my $pre_opt = shift(@pre_options);
80 % my $pre_label = shift(@pre_options);
81 % my $selected = $opt{'all_selected'}
82 % || ( ref($value) && $value->{$pre_opt} )
83 % || ( $value eq $pre_opt );
84 <OPTION VALUE="<% $pre_opt %>"
85 <% $selected ? 'SELECTED' : '' %>
89 % unless ( $opt{'multiple'} || $opt{'disable_empty'} ) {
90 <OPTION VALUE=""><% $opt{'empty_label'} || 'all' %>
93 % foreach my $record (
94 % # we have the order_by parameter for this
95 % #sort { $a->$name_col() cmp $b->$name_col()
96 % # || $a->$key() <=> $b->$key()
101 % my $recvalue = $record->$key();
103 % if ( $opt{'all_selected'} ) {
105 % } elsif ( $opt{'compare_sub'} && !ref($value) ) {
106 % $selected = &{ $opt{'compare_sub'} }( $value, $recvalue );
108 % $selected = ( ref($value) && $value->{$recvalue} )
109 % || ( $value && $value eq $recvalue ); #not == because of value_col
111 <OPTION VALUE="<% $recvalue %>"
112 <% $selected ? ' SELECTED' : '' %>
113 % foreach my $att ( @{ $opt{'extra_option_attributes'} } ) {
114 data-<% $att %>="<% $record->$att() |h %>"
116 ><% $opt{'label_showkey'} ? "$recvalue: " : '' %>
117 <% $opt{'label_callback'}
118 ? &{ $opt{'label_callback'} }( $record )
119 : $record->$name_col()
124 % while ( @post_options ) {
125 % my $post_opt = shift(@post_options);
126 % my $post_label = shift(@post_options);
127 % my $selected = ( ref($value) && $value->{$post_opt} )
128 % || ( $value eq $post_opt );
129 <OPTION VALUE="<% $post_opt %>"
130 <% $selected ? 'SELECTED' : '' %>
141 warn "elements/select-table.html: \n". Dumper(\%opt)
142 if exists $opt{debug} && $opt{debug};
144 $opt{'extra_option_attributes'} ||= [];
147 if ( $opt{'onchange'} ) {
148 $onchange = $opt{'onchange'};
149 $onchange .= '(this)' unless $onchange =~ /\(\w*\);?$/;
150 $onchange =~ s/\(what\);/\(this\);/g; #ugh, terrible hack. all onchange
151 #callbacks should act the same
152 $onchange = 'onChange="'. $onchange. '"';
155 my $dbdef_table = dbdef->table($opt{'table'})
156 or die "can't find dbdef for ". $opt{'table'}. " table\n";
158 my $key = $opt{'value_col'} || $dbdef_table->primary_key;
160 my $name_col = $opt{'name_col'};
162 my $value = exists($opt{'curr_value'}) ? $opt{'curr_value'} : $opt{'value'};
163 $value = [ split(/\s*,\s*/, $value) ] if $opt{'multiple'} && $value =~ /,/;
165 #my $addl_from = $opt{'addl_from'} || '';
166 my $extra_sql = $opt{'extra_sql'} || '';
167 my $hashref = $opt{'hashref'} || {};
169 if ( $opt{'agent_virt'} ) {
171 ( $extra_sql =~ /WHERE/i || scalar(keys %$hashref ) ? ' AND ' : ' WHERE ' ).
172 $FS::CurrentUser::CurrentUser->agentnums_sql(
173 'null' => $opt{'agent_null'},
174 'null_right' => $opt{'agent_null_right'},
179 if ( $opt{'records'} ) {
180 @records = @{ $opt{'records'} };
181 @records = sort { $a->get($name_col) cmp $b->get($name_col) } @records
182 unless $opt{'presorted'};
184 @records = qsearch( {
185 'select' => $opt{'select'} || '*',
186 'table' => $opt{'table'},
187 'addl_from' => $opt{'addl_from'},
188 'hashref' => $hashref,
189 'extra_sql' => $extra_sql,
190 'order_by' => ( $opt{'order_by'} || "ORDER BY $name_col" ),
194 if ( ref( $value ) eq 'ARRAY' ) {
195 $value = { map { $_ => 1 } @$value };
198 unless ( !ref($value) && $value < 1 # !$value #ignore negatives too
199 or ! exists( $opt{hashref}->{disabled} ) #??
200 #or grep { $value == $_->$key() } @records
202 delete $opt{hashref}->{disabled};
204 foreach my $v ( ref($value) ? keys %$value : ($value) ) {
205 next if grep { $v == $_->$key() } @records;
207 $opt{hashref}->{$key} = $v;
208 my $record = qsearchs( {
209 'table' => $opt{table},
210 'addl_from' => $opt{'addl_from'},
211 'hashref' => $hashref,
212 'extra_sql' => $extra_sql,
214 push @records, $record if $record;
219 my @pre_options = $opt{pre_options} ? @{ $opt{pre_options} } : ();
220 my @post_options = $opt{post_options} ? @{ $opt{post_options} } : ();
222 my $size = $opt{'size'} ? 'SIZE=' . $opt{'size'} : '';