UI: get rid of undocumented extra_choices_callback in search.html: a terrible place...
[freeside.git] / httemplate / search / elements / search-html.html
1 %
2 %   if ( exists($opt{'redirect'}) && $opt{'redirect'}
3 %        && scalar(@$rows) == 1 && $total == 1
4 %        && $type ne 'html-print'
5 %      ) {
6 %     my $redirect = $opt{'redirect'};
7 %     $redirect = &{$redirect}($rows->[0], $cgi) if ref($redirect) eq 'CODE';
8 %     my( $url, $method ) = @$redirect;
9 %     redirect( $url. $rows->[0]->$method() );
10 %   } elsif ( exists($opt{'redirect_empty'}) && ! scalar(@$rows) && $total == 0
11 %             && $type ne 'html-print'
12 %             && $opt{'redirect_empty'}
13 %             && ( ref($opt{'redirect_empty'}) ne 'CODE'
14 %                  || &{$opt{'redirect_empty'}}($cgi)    )
15 %      ) {
16 %     my $redirect = $opt{'redirect_empty'};
17 %     $redirect = &{$redirect}($cgi) if ref($redirect) eq 'CODE';
18 %     redirect( $redirect );
19 %   } else {
20 %     if ( $opt{'name_singular'} ) {
21 %       $opt{'name'} = PL($opt{'name_singular'});
22 %     }
23 %     ( my $xlsname = $opt{'name'} ) =~ s/\W//g;
24 %     if ( $total == 1 ) {
25 %       if ( $opt{'name_singular'} ) {
26 %         $opt{'name'} = $opt{'name_singular'}
27 %       } else {
28 %         #$opt{'name'} =~ s/s$// if $total == 1;
29 %         $opt{'name'} =~ s/((s)e)?s$/$2/ if $total == 1;
30 %       }
31 %     }
32 %
33 %     if ( $type eq 'html-print' ) {
34
35         <% include( '/elements/header-popup.html', $opt{'title'} ) %>
36
37 %     } elsif ( $type eq 'select' ) {
38
39         <% include( '/elements/header-popup.html', $opt{'title'} ) %>
40         <% defined($opt{'html_init'}) 
41               ? ( ref($opt{'html_init'})
42                     ? &{$opt{'html_init'}}()
43                     : $opt{'html_init'}
44                 )
45               : ''
46         %>
47
48 %     } else {
49 %
50 %       my @menubar = ();
51 %       if ( $opt{'menubar'} ) {
52 %         @menubar = @{ $opt{'menubar'} };
53 %       #} else {
54 %       #  @menubar = ( 'Main menu' => $p );
55 %       }
56
57         <% include( '/elements/header.html', $opt{'title'},
58                       include( '/elements/menubar.html', @menubar )
59                   )
60         %>
61
62         <% defined($opt{'html_init'}) 
63               ? ( ref($opt{'html_init'})
64                     ? &{$opt{'html_init'}}()
65                     : $opt{'html_init'}
66                 )
67               : ''
68         %>
69
70 %     }
71
72 %     unless ( $total ) { 
73 %       unless ( $opt{'disable_nonefound'} ) { 
74           No matching <% $opt{'name'} %> found.<BR>
75 %       } 
76 %     }
77 %
78 %     if ( $total || $opt{'disableable'} ) { #hmm... and there *are* ones to show??
79
80         <TABLE>
81           <TR>
82
83             <TD VALIGN="bottom">
84
85               <FORM>
86
87 %               if (! $opt{'disable_total'}) {
88                   <% $total %> total <% $opt{'name'} %>
89 %               }
90
91 %               if ( $confmax && $total > $confmax
92 %                    && ! $opt{'disable_maxselect'}
93 %                    && $type ne 'html-print' )
94 %               {
95 %                 $cgi->delete('maxrecords');
96 %                 $cgi->param('_dummy', 1);
97
98                   ( show <SELECT NAME="maxrecords" onChange="window.location = '<% $cgi->self_url %>;maxrecords=' + this.options[this.selectedIndex].value;">
99
100 %                   foreach my $max ( map { $_ * $confmax } qw( 1 5 10 25 ) ) {
101                   <OPTION VALUE="<% $max %>" <% ( $maxrecords == $max ) ? 'SELECTED' : '' %>><% $max %></OPTION>
102 %                   }
103
104                   </SELECT> per page )
105
106 %                 $cgi->param('maxrecords', $maxrecords);
107 %               }
108
109 %               if ( defined($opt{'html_posttotal'}) && $type ne 'html-print' ) {
110                     <% ref($opt{'html_posttotal'})
111                          ? &{$opt{'html_posttotal'}}()
112                          : $opt{'html_posttotal'}
113                     %>
114 %               }
115                 <BR>
116
117 %               if ( $opt{'count_addl'} ) { 
118 %                 my $n=0;
119 %                 foreach my $count ( @{$opt{'count_addl'}} ) { 
120 %                   my $data = $count_arrayref->[++$n];
121 %                   if ( ref($count) ) {
122                       <% &{ $count }( $data ) %>
123 %                   } else {
124                       <% sprintf( $count, $data ) %><BR>
125 %                   }
126 %                 } 
127 %               } 
128               </FORM>
129
130             </TD>
131
132 %           unless ( $opt{'disable_download'} || $type eq 'html-print' ) { 
133
134               <TD ALIGN="right">
135
136                 Download full results<BR>
137
138 %               $cgi->param('_type', "$xlsname.xls" ); 
139                 as <A HREF="<% $cgi->self_url %>">Excel spreadsheet</A><BR>
140
141 %               $cgi->param('_type', 'csv'); 
142                 as <A HREF="<% $cgi->self_url %>">CSV file</A><BR>
143
144 %               $cgi->param('_type', 'html-print'); 
145                 as <A HREF="<% $cgi->self_url %>">printable copy</A>
146
147               </TD>
148 %             $cgi->param('_type', "html" ); 
149 %           } 
150
151           </TR>
152           <TR>
153             <TD COLSPAN=2>
154
155 %             my $pager = '';
156 %             unless ( $type eq 'html_print' ) {
157
158                 <% $pager = include( '/elements/pager.html',
159                                        'offset'     => $offset,
160                                        'num_rows'   => scalar(@$rows),
161                                        'total'      => $total,
162                                        'maxrecords' => $maxrecords,
163                                    )
164                 %>
165
166                 <% defined($opt{'html_form'}) 
167                      ? ( ref($opt{'html_form'})
168                            ? &{$opt{'html_form'}}()
169                            : $opt{'html_form'}
170                        )
171                      : ''
172                 %>
173
174 %             }
175
176               <% include('/elements/table-grid.html') %>
177
178                 <TR>
179 %                 my $h2 = 0;
180 %                 foreach my $header ( @{ $opt{header} } ) { 
181 %                   my $label = ref($header) ? $header->{label} : $header;
182 %                   my $rowspan = 1;
183 %                   my $style = '';
184 %                   if ( $opt{header2} ) {
185 %                     if ( !length($opt{header2}->[$h2]) ) {
186 %                       $rowspan = 2;
187 %                       splice @{ $opt{header2} }, $h2, 1;
188 %                     } else {
189 %                       $h2++;
190 %                       $style = 'STYLE="border-bottom: none"'
191 %                     }
192 %                   }
193                     <TH CLASS   = "grid"
194                         BGCOLOR = "#cccccc"
195                         ROWSPAN = "<% $rowspan %>"
196                         <% $style %>
197
198                     >
199                       <% $label %>
200                     </TH>
201 %                 } 
202                 </TR>
203
204 %               if ( $opt{header2} ) {
205                   <TR>
206 %                   foreach my $header ( @{ $opt{header2} } ) { 
207 %                     my $label = ref($header) ? $header->{label} : $header;
208                       <TH CLASS="grid" BGCOLOR="#cccccc">
209                         <FONT SIZE="-1"><% $label %></FONT>
210                       </TH>
211 %                   } 
212                   </TR>
213 %               }
214
215 %               my $bgcolor1 = '#eeeeee';
216 %               my $bgcolor2 = '#ffffff';
217 %               my $bgcolor;
218 %
219 %               foreach my $row ( @$rows ) {
220 %
221 %                 if ( $bgcolor eq $bgcolor1 ) {
222 %                   $bgcolor = $bgcolor2;
223 %                 } else {
224 %                   $bgcolor = $bgcolor1;
225 %                 }
226
227                   <TR>
228
229 %                   if ( $opt{'fields'} ) {
230 %
231 %                     my $links    = $opt{'links'} ? [ @{$opt{'links'}} ] : '';
232 %                     my $onclicks = $opt{'link_onclicks'} ? [ @{$opt{'link_onclicks'}} ] : [];
233 %                     my $aligns   = $opt{'align'} ? [ @{$opt{'align'}} ] : '';
234 %                     my $colors   = $opt{'color'} ? [ @{$opt{'color'}} ] : [];
235 %                     my $sizes    = $opt{'size'}  ? [ @{$opt{'size'}}  ] : [];
236 %                     my $styles   = $opt{'style'} ? [ @{$opt{'style'}} ] : [];
237 %                     my $cstyles  = $opt{'cell_style'} ? [ @{$opt{'cell_style'}} ] : [];
238 %
239 %                     foreach my $field (
240 %
241 %                       map {
242 %                             if ( ref($_) eq 'ARRAY' ) {
243 %
244 %                               my $tableref = $_;
245 %
246 %                               '<TABLE CLASS="inv" CELLSPACING=0 CELLPADDING=0 WIDTH="100%">'.
247 %
248 %                               join('', map {
249 %
250 %                                 my $rowref = $_;
251 %
252 %                                 '<tr>'.
253 %
254 %                                 join('', map {
255 %
256 %                                   my $e = $_;
257 %
258 %                                   '<TD '.
259 %                                     join(' ', map {
260 %                                       uc($_).'="'. $e->{$_}. '"';
261 %                                     }
262 %                                     grep exists($e->{$_}),
263 %                                          qw( align bgcolor colspan rowspan
264 %                                              style valign width )
265 %                                     ).
266 %                                   '>'.
267 %
268 %                                   ( $e->{'link'}
269 %                                       ? '<A HREF="'. $e->{'link'}. '">'
270 %                                       : ''
271 %                                   ).
272 %                                   ( $e->{'size'}
273 %                                      ? '<FONT SIZE="'.uc($e->{'size'}).'">'
274 %                                      : ''
275 %                                   ).
276 %                                   ( $e->{'data_style'}
277 %                                       ? '<'. uc($e->{'data_style'}). '>'
278 %                                       : ''
279 %                                   ).
280 %                                   $e->{'data'}.
281 %                                   ( $e->{'data_style'}
282 %                                       ? '</'. uc($e->{'data_style'}). '>'
283 %                                       : ''
284 %                                   ).
285 %                                   ( $e->{'size'} ? '</FONT>' : '' ).
286 %                                   ( $e->{'link'} ? '</A>'    : '' ).
287 %                                   '</td>';
288 %
289 %                                 } @$rowref ).
290 %
291 %                                 '</tr>';
292 %                               } @$tableref ).
293 %
294 %                               '</table>';
295 %
296 %                             } else {
297 %                               $_;
298 %                             }
299 %                           }
300 %
301 %                       map {
302 %                             if ( ref($_) eq 'CODE' ) {
303 %                               &{$_}($row);
304 %                             } else {
305 %                               $row->$_();
306 %                             }
307 %                           }
308 %                       @{$opt{'fields'}}
309 %
310 %                     ) {
311 %
312 %                       my $class = ( $field =~ /^<TABLE/i ) ? 'inv' : 'grid';
313 %
314 %                       my $align = $aligns ? shift @$aligns : '';
315 %                       $align = " ALIGN=$align" if $align;
316 %
317 %                       my $a = '';
318 %                       if ( $links ) {
319 %                         my $link = shift @$links;
320 %                         my $onclick = shift @$onclicks;
321 %
322 %                         if (    ! $opt{'agent_virt'}
323 %                              || ( $null_link && ! $row->agentnum )
324 %                              || grep { $row->agentnum == $_ }
325 %                                      @link_agentnums
326 %                            ) {
327 %        
328 %                           $link = &{$link}($row)
329 %                             if ref($link) eq 'CODE';
330 %
331 %                           $onclick = &{$onclick}($row)
332 %                             if ref($onclick) eq 'CODE';
333 %                           $onclick = qq( onClick="$onclick") if $onclick;
334 %
335 %                           if ( $link ) {
336 %                             my( $url, $method ) = @{$link};
337 %                             if ( ref($method) eq 'CODE' ) {
338 %                               $a = $url. &{$method}($row);
339 %                             } else {
340 %                               $a = $url. $row->$method();
341 %                             }
342 %                             $a = qq(<A HREF="$a"$onclick>);
343 %                           }
344 %                           elsif ( $onclick ) {
345 %                             $a = qq(<A HREF="javascript:void(0);"$onclick>);
346 %                           }
347 %                         }
348 %
349 %                       }
350 %
351 %                       my $font = '';
352 %                       my $color = shift @$colors;
353 %                       $color = &{$color}($row) if ref($color) eq 'CODE';
354 %                       my $size = shift @$sizes;
355 %                       $size = &{$size}($row) if ref($size) eq 'CODE';
356 %                       if ( $color || $size ) {
357 %                         $font = '<FONT '.
358 %                                 ( $color ? "COLOR=#$color "   : '' ).
359 %                                 ( $size  ? qq(SIZE="$size" )  : '' ).
360 %                                 '>';
361 %                       }
362 %
363 %                       my($s, $es) = ( '', '' );
364 %                       my $style = shift @$styles;
365 %                       $style = &{$style}($row) if ref($style) eq 'CODE';
366 %                       if ( $style ) {
367 %                         $s = join( '', map "<$_>", split('', $style) );
368 %                         $es = join( '', map "</$_>", split('', $style) );
369 %                       }
370 %
371 %                       my $cstyle = shift @$cstyles;
372 %                       $cstyle = &{$cstyle}($row) if ref($cstyle) eq 'CODE';
373 %                       $cstyle = qq(STYLE="$cstyle")
374 %                         if $cstyle;
375
376                         <TD CLASS="<% $class %>" BGCOLOR="<% $bgcolor %>" <% $align %> <% $cstyle %>><% $font %><% $a %><% $s %><% $field %><% $es %><% $a ? '</A>' : '' %><% $font ? '</FONT>' : '' %></TD>
377
378 %                     } 
379 %
380 %                   } else { 
381 %
382 %                     foreach ( @$row ) { 
383                         <TD CLASS="grid" BGCOLOR="<% $bgcolor %>"><% $_ %></TD>
384 %                     }
385 %
386 %                   }
387
388                   </TR>
389
390 %               } 
391
392 %               if ( $opt{'footer'} ) {
393
394                   <TR>
395
396 %                   foreach my $footer ( @{ $opt{'footer'} } ) { 
397 %                     $footer = &{$footer}() if ref($footer) eq 'CODE';
398                       <TD CLASS="grid" BGCOLOR="#dddddd" STYLE="border-top: dashed 1px black;"><i><% $footer %></i></TD>
399 %                   } 
400
401                   </TR>
402 %               } 
403             
404               </TABLE>
405
406               <% $pager %>
407   
408             </TD>
409           </TR>
410         </TABLE>
411 %     }
412
413 %     if ( $type eq 'html-print' ) {
414
415         </BODY></HTML>
416       
417 %     } else {
418
419         <% defined($opt{'html_foot'}) 
420               ? ( ref($opt{'html_foot'})
421                     ? &{$opt{'html_foot'}}()
422                     : $opt{'html_foot'}
423                 )
424               : ''
425         %>
426
427         <% include( '/elements/footer.html' ) %>
428
429 %     }
430
431 %   } 
432 <%init>
433
434 my %args = @_;
435 my $type           = $args{'type'};
436 my $header         = $args{'header'};
437 my $rows           = $args{'rows'};
438 my @link_agentnums = @{ $args{'link_agentnums'} };
439 my $null_link      = $args{'null_link'};
440 my $confmax        = $args{'confmax'};
441 my $maxrecords     = $args{'maxrecords'};
442 my $offset         = $args{'offset'};
443 my %opt            = %{ $args{'opt'} };
444
445 my $count_sth = dbh->prepare($opt{'count_query'})
446   or die "Error preparing $opt{'count_query'}: ". dbh->errstr;
447 $count_sth->execute
448   or die "Error executing $opt{'count_query'}: ". $count_sth->errstr;
449 my $count_arrayref = $count_sth->fetchrow_arrayref;
450 my $total = $count_arrayref->[0];
451
452 </%init>