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