X-Git-Url: http://git.freeside.biz/gitweb/?a=blobdiff_plain;f=httemplate%2Felements%2Fselect-table.html;h=1632b596adc1f7e77622666889d37802454c4cec;hb=8ad827f9de1dd45c354381a58a2609f0d59fd8e7;hp=96f0177fbde9a127ba22f648bba4ed9dc0d48577;hpb=e65c6a26ca778166aec2b2d1dd3012ab84fa611a;p=freeside.git diff --git a/httemplate/elements/select-table.html b/httemplate/elements/select-table.html index 96f0177fb..1632b596a 100644 --- a/httemplate/elements/select-table.html +++ b/httemplate/elements/select-table.html @@ -1,54 +1,179 @@ -<% - - ##required - # 'table' => 'table_name', - # 'name_col' => 'name_column', - # - ##strongly recommended (you want your forms to be "sticky" on errors, right?) - # 'value' => 'current_value', - # - ##opt - # 'empty_label' => '', #better specify it though, the default might change - # 'hashref' => {}, - # 'records' => \@records, #instead of hashref - # 'pre_options' => [ 'value' => 'option' ], #before normal options - - my( %opt ) = @_; - - #warn "***** select-table: \n". Dumper(%opt); - - my $key = dbdef->table($opt{'table'})->primary_key; #? $opt{'primary_key'} || - - my $name_col = $opt{'name_col'}; - - my @records = (); - if ( $opt{'records'} ) { - @records = @{ $opt{'records'} }; - } else { - @records = qsearch( $opt{'table'}, ( $opt{'hashref'} || {} ) ); - } - - my @pre_options = $opt{'pre_options'} ? @{ $opt{'pre_options'} } : (); - -%> - - + 'pre_options' => [ 'value' => 'option' ], #before normal options + 'empty_label' => '', #better specify it though, the default might change + 'multiple' => 0, # bool + 'disable_empty' => 0, # bool (implied by multiple) + 'label_showkey' => 0, # bool + 'label_callback' => sub { my $record = shift; return "label"; }, + + #more params controlling HTML stuff about the element + 'onchange' => '', #javascript code + + #special return options + 'js_only' => 0, #set true to return only the JS portions (i.e. nothing) + 'html_only' => 0, #set true to return only the HTML portions (no-op, i.e. return everything) + + #debugging + 'debug' => 0, #set true to enable + + ) + + +% unless ( $opt{'js_only'} ) { + + +%} +<%init> + +my( %opt ) = @_; + +warn "elements/select-table.html: \n". Dumper(%opt) + if exists $opt{debug} && $opt{debug}; + +my $onchange = ''; +if ( $opt{'onchange'} ) { + $onchange = $opt{'onchange'}; + $onchange .= '(this)' unless $onchange =~ /\(\w*\);?$/; + $onchange =~ s/\(what\);/\(this\);/g; #ugh, terrible hack. all onchange + #callbacks should act the same + $onchange = 'onChange="'. $onchange. '"'; +} + +my $dbdef_table = dbdef->table($opt{'table'}) + or die "can't find dbdef for ". $opt{'table'}. " table\n"; + +my $key = $opt{'value_col'} || $dbdef_table->primary_key; + +my $name_col = $opt{'name_col'}; + +my $value = $opt{'curr_value'} || $opt{'value'}; +$value = [ split(/\s*,\s*/, $value) ] if $opt{'multiple'} && $value =~ /,/; + +#my $addl_from = $opt{'addl_from'} || ''; +my $extra_sql = $opt{'extra_sql'} || ''; +my $hashref = $opt{'hashref'} || {}; + +if ( $opt{'agent_virt'} ) { + $extra_sql .= + ( $extra_sql =~ /WHERE/i || scalar(keys %$hashref ) ? ' AND ' : ' WHERE ' ). + $FS::CurrentUser::CurrentUser->agentnums_sql( + 'null' => $opt{'agent_null'}, + 'null_right' => $opt{'agent_null_right'}, + ); +} + +my @records = (); +if ( $opt{'records'} ) { + @records = @{ $opt{'records'} }; +} else { + @records = qsearch( { + 'table' => $opt{'table'}, + 'addl_from' => $opt{'addl_from'}, + 'hashref' => $hashref, + 'extra_sql' => $extra_sql, + 'order_by' => ( $opt{'order_by'} || "ORDER BY $name_col" ), + }); +} + +unless ( $value < 1 # !$value #ignore negatives too + or ref($value) + or ! exists( $opt{hashref}->{disabled} ) #?? + or grep { $value == $_->$key() } @records + ) { + delete $opt{hashref}->{disabled}; + $opt{hashref}->{$key} = $value; + my $record = qsearchs( { + 'table' => $opt{table}, + 'addl_from' => $opt{'addl_from'}, + 'hashref' => $hashref, + 'extra_sql' => $extra_sql, + }); + push @records, $record if $record; +} + +if ( ref( $value ) eq 'ARRAY' ) { + $value = { map { $_ => 1 } @$value }; +} + +my @pre_options = $opt{pre_options} ? @{ $opt{pre_options} } : (); + +