4ac0887fd51bf95fa0d488cbf6bd580da078d53d
[freeside.git] / httemplate / elements / select-table.html
1 <%doc>
2
3 Example:
4
5   include( '/elements/select-table.html',
6
7     ##
8     # required
9     ##
10     'table'          => 'table_name',
11     'name_col'       => 'name_column', #or method if you pass an order_by
12                                        # order_by is currently broken, though
13    
14     #strongly recommended (you want your forms to be "sticky" on errors, right?)
15     'curr_value'     => 'current_value',
16     #'value' => #deprecated form of 'curr_value',
17    
18     ##
19     # optional
20     ##
21
22     #search params
23     'hashref'          => {},
24     'addl_from'        => '',
25     'extra_sql'        => '',
26     'agent_virt'       => 0, #set true and make sure the result is JOINed to
27                              #something with agentnum (usually cust_main)
28     'agent_null'       => 0, #set true to always show un-agented entries
29     'agent_null_right' => '', #right to see un-agented entries
30     #or
31     'records'        => \@records, #instead of search params
32
33     #instead of the primary key... only for special cases
34     'value_col'      => 'columnname',
35
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"; },
45
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
51                             #<SELECT> element
52     'onchange'       => '', #javascript code
53
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)
57
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)
61
62     #debugging
63     'debug'          => 0, #set true to enable
64
65   )
66
67 </%doc>
68 % unless ( $opt{'js_only'} ) {
69
70 <SELECT <% $opt{'multiple'} ? 'MULTIPLE' : '' %>
71         NAME = "<% $opt{'element_name'} || $opt{'field'} || $key %>"
72         ID   = "<% $opt{'id'} || $key %>"
73         <% $onchange %>
74         <% $size %>
75         <% $opt{'element_etc'} %>
76 >
77
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' : '' %>
86     ><% $pre_label %></OPTION>
87 % } 
88
89 % unless ( $opt{'multiple'} || $opt{'disable_empty'} ) {
90     <OPTION VALUE=""><% $opt{'empty_label'} || 'all' %></OPTION>
91 % }
92
93 % # XXX fix this eventually, when we have time to test it
94 % foreach my $record ( sort {    $a->$name_col() cmp $b->$name_col()
95 %                             || $a->$key()      <=> $b->$key()
96 %                           }
97 %                           @records
98 %                    )
99 % {
100 %   my $recvalue = $record->$key();
101 %   my $selected;
102 %   if ( $opt{'all_selected'} ) {
103 %     $selected = 1;
104 %   } elsif ( $opt{'compare_sub'} && !ref($value) ) {
105 %     $selected = &{ $opt{'compare_sub'} }( $value, $recvalue );
106 %   } else {
107 %     $selected =    ( ref($value) && $value->{$recvalue} )
108 %                 || ( $value && $value eq $recvalue ); #not == because of value_col
109 %   }
110     <OPTION VALUE="<% $recvalue %>"
111             <% $selected ? ' SELECTED' : '' %>
112 %           foreach my $att ( @{ $opt{'extra_option_attributes'} } ) {
113               data-<% $att %>="<% $record->$att() |h %>"
114 %           }
115     ><% $opt{'label_showkey'} ? "$recvalue: " : '' %>
116      <% $opt{'label_callback'}
117           ? &{ $opt{'label_callback'} }( $record )
118           : $record->$name_col()
119         |h
120      %></OPTION>
121 % } 
122
123 % while ( @post_options ) { 
124 %   my $post_opt   = shift(@post_options);
125 %   my $post_label = shift(@post_options);
126 %   my $selected =    ( ref($value) && $value->{$post_opt} )
127 %                  || ( $value eq $post_opt );
128     <OPTION VALUE="<% $post_opt %>"
129             <% $selected ? 'SELECTED' : '' %>
130     ><% $post_label %></OPTION>
131 % } 
132
133 </SELECT>
134
135 %}
136 <%init>
137
138 my( %opt ) = @_;
139
140 warn "elements/select-table.html: \n". Dumper(\%opt)
141   if exists $opt{debug} && $opt{debug};
142
143 $opt{'extra_option_attributes'} ||= [];
144
145 my $onchange = '';
146 if ( $opt{'onchange'} ) {
147   $onchange = $opt{'onchange'};
148   $onchange .= '(this)' unless $onchange =~ /\(\w*\);?$/;
149   $onchange =~ s/\(what\);/\(this\);/g; #ugh, terrible hack.  all onchange
150                                         #callbacks should act the same
151   $onchange = 'onChange="'. $onchange. '"';
152 }
153
154 my $dbdef_table = dbdef->table($opt{'table'})
155   or die "can't find dbdef for ". $opt{'table'}. " table\n";
156
157 my $key = $opt{'value_col'} || $dbdef_table->primary_key;
158
159 my $name_col = $opt{'name_col'};
160
161 my $value = $opt{'curr_value'} || $opt{'value'};
162 $value = [ split(/\s*,\s*/, $value) ] if $opt{'multiple'} && $value =~ /,/;
163
164 #my $addl_from = $opt{'addl_from'} || '';
165 my $extra_sql = $opt{'extra_sql'} || '';
166 my $hashref   = $opt{'hashref'} || {};
167
168 if ( $opt{'agent_virt'} ) {
169   $extra_sql .=
170     ( $extra_sql =~ /WHERE/i || scalar(keys %$hashref ) ? ' AND ' : ' WHERE ' ).
171     $FS::CurrentUser::CurrentUser->agentnums_sql(
172                                     'null'       => $opt{'agent_null'},
173                                     'null_right' => $opt{'agent_null_right'},
174                                    );
175 }
176
177 my @records = ();
178 if ( $opt{'records'} ) {
179   @records = @{ $opt{'records'} };
180 } else {
181   @records = qsearch( {
182     'table'     => $opt{'table'},
183     'addl_from' => $opt{'addl_from'},
184     'hashref'   => $hashref,
185     'extra_sql' => $extra_sql,
186     'order_by'  => ( $opt{'order_by'} || "ORDER BY $name_col" ),
187   });
188 }
189
190 if ( ref( $value ) eq 'ARRAY' ) {
191   $value = { map { $_ => 1 } @$value };
192 }
193
194 unless (    !ref($value) && $value < 1 # !$value #ignore negatives too
195          or ! exists( $opt{hashref}->{disabled} ) #??
196          #or grep { $value == $_->$key() } @records
197        ) {
198   delete $opt{hashref}->{disabled};
199
200   foreach my $v ( ref($value) ? keys %$value : ($value) ) {
201     next if grep { $v == $_->$key() } @records;
202
203     $opt{hashref}->{$key} = $v;
204     my $record = qsearchs( {
205       'table'     => $opt{table},
206       'addl_from' => $opt{'addl_from'},
207       'hashref'   => $hashref,
208       'extra_sql' => $extra_sql,
209     });
210     push @records, $record if $record;
211
212   }
213 }
214
215 my @pre_options  = $opt{pre_options}  ? @{ $opt{pre_options} } : ();
216 my @post_options = $opt{post_options} ? @{ $opt{post_options} } : ();
217
218 my $size = $opt{'size'} ? 'SIZE=' . $opt{'size'} : '';
219
220 </%init>