default to a session cookie instead of setting an explicit timeout, weird timezone...
[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     'presorted'      => 0, #set true to disable sorting the records on name_col
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 % my $curr_option_found;
94 % foreach my $record ( 
95 % # we have the order_by parameter for this
96 % #sort {    $a->$name_col() cmp $b->$name_col()
97 % #                           || $a->$key()      <=> $b->$key()
98 % #                         }
99 %                          @records
100 %                    )
101 % {
102 %   my $recvalue = $record->$key();
103 %   my $selected;
104 %   if ( $opt{'all_selected'} ) {
105 %     $selected = 1;
106 %   } elsif ( $opt{'compare_sub'} && !ref($value) ) {
107 %     $selected = &{ $opt{'compare_sub'} }( $value, $recvalue );
108 %   } else {
109 %     $selected =    ( ref($value) && $value->{$recvalue} )
110 %                 || ( $value && $value eq $recvalue ); #not == because of value_col
111 %   }
112 %   $curr_option_found = $selected unless $curr_option_found;
113     <OPTION VALUE="<% $recvalue %>"
114             <% $selected ? ' SELECTED' : '' %>
115 %           foreach my $att ( @{ $opt{'extra_option_attributes'} } ) {
116               data-<% $att %>="<% $record->$att() |h %>"
117 %           }
118     ><% $opt{'label_showkey'} ? "$recvalue: " : '' %>
119      <% $opt{'label_callback'}
120           ? &{ $opt{'label_callback'} }( $record )
121           : $record->$name_col()
122         |h
123      %></OPTION>
124 % } 
125
126 % while ( @post_options ) { 
127 %   my $post_opt   = shift(@post_options);
128 %   my $post_label = shift(@post_options);
129 %   my $selected =    ( ref($value) && $value->{$post_opt} )
130 %                  || ( $value eq $post_opt );
131     <OPTION VALUE="<% $post_opt %>"
132             <% $selected ? 'SELECTED' : '' %>
133     ><% $post_label %></OPTION>
134 % }
135
136 % my $non_option_label = $opt{'non_option_label'};
137 % if (!$curr_option_found && $non_option_label && $value) {
138     <OPTION VALUE="<% $value %>" SELECTED><% $non_option_label %></OPTION>
139 % } 
140
141 </SELECT>
142
143 %}
144 <%init>
145
146 my( %opt ) = @_;
147
148 warn "elements/select-table.html: \n". Dumper(\%opt)
149   if exists $opt{debug} && $opt{debug};
150
151 $opt{'extra_option_attributes'} ||= [];
152
153 my $onchange = '';
154 if ( $opt{'onchange'} ) {
155   $onchange = $opt{'onchange'};
156   $onchange .= '(this)' unless $onchange =~ /\(\w*\);?$/;
157   $onchange =~ s/\(what\);/\(this\);/g; #ugh, terrible hack.  all onchange
158                                         #callbacks should act the same
159   $onchange = 'onChange="'. $onchange. '"';
160 }
161
162 my $dbdef_table = dbdef->table($opt{'table'})
163   or die "can't find dbdef for ". $opt{'table'}. " table\n";
164
165 my $key = $opt{'value_col'} || $dbdef_table->primary_key;
166
167 my $name_col = $opt{'name_col'};
168
169 my $value = exists($opt{'curr_value'}) ? $opt{'curr_value'} : $opt{'value'};
170 $value = [ split(/\s*,\s*/, $value) ] if $opt{'multiple'} && $value =~ /,/;
171
172 #my $addl_from = $opt{'addl_from'} || '';
173 my $extra_sql = $opt{'extra_sql'} || '';
174 my $hashref   = $opt{'hashref'} || {};
175
176 if ( $opt{'agent_virt'} ) {
177   $extra_sql .=
178     ( $extra_sql =~ /WHERE/i || scalar(keys %$hashref ) ? ' AND ' : ' WHERE ' ).
179     $FS::CurrentUser::CurrentUser->agentnums_sql(
180                                     'null'       => $opt{'agent_null'},
181                                     'null_right' => $opt{'agent_null_right'},
182                                    );
183 }
184
185 my @records = ();
186 if ( $opt{'records'} ) {
187   @records = @{ $opt{'records'} };
188   @records = sort { $a->get($name_col) cmp $b->get($name_col) } @records
189     unless $opt{'presorted'};
190 } else {
191   @records = qsearch( {
192     'select'    => $opt{'select'} || '*',
193     'table'     => $opt{'table'},
194     'addl_from' => $opt{'addl_from'},
195     'hashref'   => $hashref,
196     'extra_sql' => $extra_sql,
197     'order_by'  => ( $opt{'order_by'} || "ORDER BY $name_col" ),
198   });
199 }
200
201 if ( ref( $value ) eq 'ARRAY' ) {
202   $value = { map { $_ => 1 } @$value };
203 }
204
205
206 if (
207      (ref $value && $value)
208   or (ref $opt{hashref} && exists $opt{hashref}->{disabled})
209   or $opt{also_show_option}
210 ) {
211
212   delete $opt{hashref}->{disabled};
213
214   # also_show_option: (hack for change_pkg form):
215   #   Include an absent select option, without selecting it
216   #   as the curr_value
217   my @also_show = ref($value) ? keys %$value : ($value);
218   push @also_show, $opt{also_show_option} if $opt{also_show_option};
219
220   foreach my $v ( @also_show ) {
221     next if grep { $v == $_->$key() } @records;
222
223     $opt{hashref}->{$key} = $v;
224     my $record = qsearchs( {
225       'table'     => $opt{table},
226       'addl_from' => $opt{'addl_from'},
227       'hashref'   => $hashref,
228       'extra_sql' => $extra_sql,
229     });
230     push @records, $record if $record;
231
232   }
233 }
234
235 my @pre_options  = $opt{pre_options}  ? @{ $opt{pre_options} } : ();
236 my @post_options = $opt{post_options} ? @{ $opt{post_options} } : ();
237
238 my $size = $opt{'size'} ? 'SIZE=' . $opt{'size'} : '';
239
240 </%init>