some stuff to allow select-table to be used as as edit/elements/edit.html m2* element...
[freeside.git] / httemplate / elements / select-table.html
1 <%doc>
2
3 Example:
4
5   include( '/elements/select-table.html',
6
7     #required
8     'table'          => 'table_name',
9     'name_col'       => 'name_column',
10    
11     #strongly recommended (you want your forms to be "sticky" on errors, right?)
12     'curr_value'     => 'current_value',
13     #'value' => #deprecated form of 'curr_value',
14    
15     #opt
16     'empty_label'    => '', #better specify it though, the default might change
17     'hashref'        => {},
18     'extra_sql'      => '',
19     'records'        => \@records, #instead of hashref
20     'pre_options'    => [ 'value' => 'option' ], #before normal options
21     'element_name'   => '', #HTML element name, defaults to the name of
22                             # the primary key column
23     'field'          => '', #synonym for element_name
24     'element_etc'    => '', #additional attributes (i.e. "DISABLED") for the
25                             #<SELECT> element
26     'onchange'       => '', #javascript code
27     'multiple'       => 0, # bool
28     'disable_empty'  => 0, # bool (implied by multiple)
29     'debug'          => 0, #set true to enable
30     'label_callback' => sub { my $record = shift; return "label"; },
31     'js_only'      => 0, #set true to return only the JS portions (i.e. nothing)
32     'html_only'    => 0, #set true to return only the HTML portions (no-op, i.e. return everything)
33   )
34
35 </%doc>
36 % unless ( $opt{'js_only'} ) {
37
38 <SELECT <% $opt{'multiple'} ? 'MULTIPLE' : '' %>
39         NAME = "<% $opt{'element_name'} || $opt{'field'} || $key %>"
40         ID   = "<% $opt{'id'} || $key %>"
41         <% $onchange %>
42         <% $opt{'element_etc'} %>
43 >
44
45 % while ( @pre_options ) { 
46     <OPTION VALUE="<% shift(@pre_options) %>"><% shift(@pre_options) %>
47
48 % } 
49
50 % unless ( $opt{'multiple'} || $opt{'disable_empty'} ) {
51     <OPTION VALUE=""><% $opt{'empty_label'} || 'all' %>
52 % }
53
54 % foreach my $record ( sort { $a->$name_col() cmp $b->$name_col() } @records ) {
55 %   my $recvalue = $record->$key();
56     <OPTION VALUE="<% $recvalue %>"
57             <% ref($value) && $value->{$recvalue} || $value == $recvalue
58                ? ' SELECTED' : ''
59             %>
60     ><% $opt{'label_callback'}
61           ? &{ $opt{'label_callback'} }( $record )
62           : $record->$name_col()
63      %>
64 % } 
65
66 </SELECT>
67
68 %}
69 <%init>
70
71 my( %opt ) = @_;
72
73 warn "elements/select-table.html: \n". Dumper(%opt)
74   if exists $opt{debug} && $opt{debug};
75
76 my $onchange = '';
77 if ( $opt{'onchange'} ) {
78   $onchange = $opt{'onchange'};
79   $onchange .= '(this)' unless $onchange =~ /\(\w*\);?$/;
80   $onchange =~ s/\(what\);/\(this\);/g; #ugh, terrible hack.  all onchange
81                                         #callbacks should act the same
82   $onchange = 'onChange="'. $onchange. '"';
83 }
84
85 my $dbdef_table = dbdef->table($opt{'table'})
86   or die "can't find dbdef for ". $opt{'table'}. " table\n";
87
88 my $key = $dbdef_table->primary_key; #? $opt{'primary_key'} ||
89
90 my $name_col = $opt{'name_col'};
91
92 my $value = $opt{'curr_value'} || $opt{'value'};
93 $value = [ split(/\s*,\s*/, $value) ] if $opt{'multiple'} && $value =~ /,/;
94
95 my @records = ();
96 if ( $opt{'records'} ) {
97   @records = @{ $opt{'records'} };
98 } else {
99   @records = qsearch( {
100     'table'     => $opt{'table'},
101     'hashref'   => ( $opt{'hashref'} || {} ),
102     'extra_sql' => ( $opt{'extra_sql'} || '' ),
103   });
104 }
105
106 unless (    ! $value
107          or ref($value)
108          or ! exists( $opt{hashref}->{disabled} ) #??
109          or grep { $value == $_->$key() } @records
110        ) {
111   delete $opt{hashref}->{disabled};
112   $opt{hashref}->{$key} = $value;
113   my $record = qsearchs( {
114     'table'     => $opt{table},
115     'hashref'   => $opt{hashref},
116     'extra_sql' => ( $opt{extra_sql} || '' ),
117   });
118   push @records, $record if $record;
119 }
120
121 if ( ref( $value ) eq 'ARRAY' ) {
122   $value = { map { $_ => 1 } @$value };
123 }
124
125 my @pre_options = $opt{pre_options} ? @{ $opt{pre_options} } : ();
126
127 </%init>