e7363880165ab6f8a7ab15d2f464c88d686cee97
[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    
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',
16    
17     ##
18     # optional
19     ##
20
21     #search params
22     'hashref'          => {},
23     'addl_from'        => '',
24     'extra_sql'        => '',
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
29     #or
30     'records'        => \@records, #instead of search params
31
32     #instead of the primary key... only for special cases
33     'value_col'      => 'columnname',
34
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     'all_selected'   => 0, # useful with multiple
41     'disable_empty'  => 0, # bool (implied by multiple)
42     'label_showkey'  => 0, # bool
43     'label_callback' => sub { my $record = shift; return "label"; },
44
45     #more params controlling HTML stuff about the <SELECT>
46     'element_name'   => '', #HTML element name, defaults to the name of
47                             # the primary key column
48     'field'          => '', #synonym for element_name
49     'element_etc'    => '', #additional attributes (i.e. "DISABLED") for the
50                             #<SELECT> element
51     'onchange'       => '', #javascript code
52
53     #params (well, a param) controlling the <OPTION>s
54     'extra_option_attributes' => [ 'field' ], #field or method in $table objects
55                                               #(are prefixed w/data- per HTML5)
56
57     #special return options
58     'js_only'      => 0, #set true to return only the JS portions (i.e. nothing)
59     'html_only'    => 0, #set true to return only the HTML portions (no-op, i.e. return everything)
60
61     #debugging
62     'debug'          => 0, #set true to enable
63
64   )
65
66 </%doc>
67 % unless ( $opt{'js_only'} ) {
68
69 <SELECT <% $opt{'multiple'} ? 'MULTIPLE' : '' %>
70         NAME = "<% $opt{'element_name'} || $opt{'field'} || $key %>"
71         ID   = "<% $opt{'id'} || $key %>"
72         <% $onchange %>
73         <% $size %>
74         <% $opt{'element_etc'} %>
75 >
76
77 % while ( @pre_options ) { 
78 %   my $pre_opt   = shift(@pre_options);
79 %   my $pre_label = shift(@pre_options);
80 %   my $selected = $opt{'all_selected'}
81 %                   || ( ref($value) && $value->{$pre_opt} )
82 %                   || ( $value eq $pre_opt );
83     <OPTION VALUE="<% $pre_opt %>"
84             <% $selected ? 'SELECTED' : '' %>
85     ><% $pre_label %>
86 % } 
87
88 % unless ( $opt{'multiple'} || $opt{'disable_empty'} ) {
89     <OPTION VALUE=""><% $opt{'empty_label'} || 'all' %>
90 % }
91
92 % foreach my $record ( sort {    $a->$name_col() cmp $b->$name_col()
93 %                             || $a->$key()      <=> $b->$key()
94 %                           }
95 %                           @records
96 %                    )
97 % {
98 %   my $recvalue = $record->$key();
99 %   my $selected;
100 %   if ( $opt{'all_selected'} ) {
101 %     $selected = 1;
102 %   } elsif ( $opt{'compare_sub'} && !ref($value) ) {
103 %     $selected = &{ $opt{'compare_sub'} }( $value, $recvalue );
104 %   } else {
105 %     $selected =    ( ref($value) && $value->{$recvalue} )
106 %                 || ( $value && $value eq $recvalue ); #not == because of value_col
107 %   }
108     <OPTION VALUE="<% $recvalue %>"
109             <% $selected ? ' SELECTED' : '' %>
110 %           foreach my $att ( @{ $opt{'extra_option_attributes'} } ) {
111               data-<% $att %>="<% $record->$att() |h %>"
112 %           }
113     ><% $opt{'label_showkey'} ? "$recvalue: " : '' %>
114      <% $opt{'label_callback'}
115           ? &{ $opt{'label_callback'} }( $record )
116           : $record->$name_col()
117         |h
118      %>
119 % } 
120
121 % while ( @post_options ) { 
122 %   my $post_opt   = shift(@post_options);
123 %   my $post_label = shift(@post_options);
124 %   my $selected =    ( ref($value) && $value->{$post_opt} )
125 %                  || ( $value eq $post_opt );
126     <OPTION VALUE="<% $post_opt %>"
127             <% $selected ? 'SELECTED' : '' %>
128     ><% $post_label %>
129 % } 
130
131 </SELECT>
132
133 %}
134 <%init>
135
136 my( %opt ) = @_;
137
138 warn "elements/select-table.html: \n". Dumper(\%opt)
139   if exists $opt{debug} && $opt{debug};
140
141 $opt{'extra_option_attributes'} ||= [];
142
143 my $onchange = '';
144 if ( $opt{'onchange'} ) {
145   $onchange = $opt{'onchange'};
146   $onchange .= '(this)' unless $onchange =~ /\(\w*\);?$/;
147   $onchange =~ s/\(what\);/\(this\);/g; #ugh, terrible hack.  all onchange
148                                         #callbacks should act the same
149   $onchange = 'onChange="'. $onchange. '"';
150 }
151
152 my $dbdef_table = dbdef->table($opt{'table'})
153   or die "can't find dbdef for ". $opt{'table'}. " table\n";
154
155 my $key = $opt{'value_col'} || $dbdef_table->primary_key;
156
157 my $name_col = $opt{'name_col'};
158
159 my $value = $opt{'curr_value'} || $opt{'value'};
160 $value = [ split(/\s*,\s*/, $value) ] if $opt{'multiple'} && $value =~ /,/;
161
162 #my $addl_from = $opt{'addl_from'} || '';
163 my $extra_sql = $opt{'extra_sql'} || '';
164 my $hashref   = $opt{'hashref'} || {};
165
166 if ( $opt{'agent_virt'} ) {
167   $extra_sql .=
168     ( $extra_sql =~ /WHERE/i || scalar(keys %$hashref ) ? ' AND ' : ' WHERE ' ).
169     $FS::CurrentUser::CurrentUser->agentnums_sql(
170                                     'null'       => $opt{'agent_null'},
171                                     'null_right' => $opt{'agent_null_right'},
172                                    );
173 }
174
175 my @records = ();
176 if ( $opt{'records'} ) {
177   @records = @{ $opt{'records'} };
178 } else {
179   @records = qsearch( {
180     'table'     => $opt{'table'},
181     'addl_from' => $opt{'addl_from'},
182     'hashref'   => $hashref,
183     'extra_sql' => $extra_sql,
184     'order_by'  => ( $opt{'order_by'} || "ORDER BY $name_col" ),
185   });
186 }
187
188 if ( ref( $value ) eq 'ARRAY' ) {
189   $value = { map { $_ => 1 } @$value };
190 }
191
192 unless (    !ref($value) && $value < 1 # !$value #ignore negatives too
193          or ! exists( $opt{hashref}->{disabled} ) #??
194          #or grep { $value == $_->$key() } @records
195        ) {
196   delete $opt{hashref}->{disabled};
197
198   foreach my $v ( ref($value) ? keys %$value : ($value) ) {
199     next if grep { $v == $_->$key() } @records;
200
201     $opt{hashref}->{$key} = $v;
202     my $record = qsearchs( {
203       'table'     => $opt{table},
204       'addl_from' => $opt{'addl_from'},
205       'hashref'   => $hashref,
206       'extra_sql' => $extra_sql,
207     });
208     push @records, $record if $record;
209
210   }
211 }
212
213 my @pre_options  = $opt{pre_options}  ? @{ $opt{pre_options} } : ();
214 my @post_options = $opt{post_options} ? @{ $opt{post_options} } : ();
215
216 my $size = $opt{'size'} ? 'SIZE=' . $opt{'size'} : '';
217
218 </%init>