c0bb721f7c6dc7b7594043f4fd7ed49ef639c5c6
[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               <% $opt{'extra_choices_callback'}
148                  ? &{$opt{'extra_choices_callback'}}($cgi->query_string)
149                  : ''
150               %>
151
152               </TD>
153 %             $cgi->param('_type', "html" ); 
154 %           } 
155
156           </TR>
157           <TR>
158             <TD COLSPAN=2>
159
160 %             my $pager = '';
161 %             unless ( $type eq 'html_print' ) {
162
163                 <% $pager = include( '/elements/pager.html',
164                                        'offset'     => $offset,
165                                        'num_rows'   => scalar(@$rows),
166                                        'total'      => $total,
167                                        'maxrecords' => $maxrecords,
168                                    )
169                 %>
170
171                 <% defined($opt{'html_form'}) 
172                      ? ( ref($opt{'html_form'})
173                            ? &{$opt{'html_form'}}()
174                            : $opt{'html_form'}
175                        )
176                      : ''
177                 %>
178
179 %             }
180
181               <% include('/elements/table-grid.html') %>
182
183                 <TR>
184 %                 my $h2 = 0;
185 %                 foreach my $header ( @{ $opt{header} } ) { 
186 %                   my $label = ref($header) ? $header->{label} : $header;
187 %                   my $rowspan = 1;
188 %                   my $style = '';
189 %                   if ( $opt{header2} ) {
190 %                     if ( !length($opt{header2}->[$h2]) ) {
191 %                       $rowspan = 2;
192 %                       splice @{ $opt{header2} }, $h2, 1;
193 %                     } else {
194 %                       $h2++;
195 %                       $style = 'STYLE="border-bottom: none"'
196 %                     }
197 %                   }
198                     <TH CLASS   = "grid"
199                         BGCOLOR = "#cccccc"
200                         ROWSPAN = "<% $rowspan %>"
201                         <% $style %>
202
203                     >
204                       <% $label %>
205                     </TH>
206 %                 } 
207                 </TR>
208
209 %               if ( $opt{header2} ) {
210                   <TR>
211 %                   foreach my $header ( @{ $opt{header2} } ) { 
212 %                     my $label = ref($header) ? $header->{label} : $header;
213                       <TH CLASS="grid" BGCOLOR="#cccccc">
214                         <FONT SIZE="-1"><% $label %></FONT>
215                       </TH>
216 %                   } 
217                   </TR>
218 %               }
219
220 %               my $bgcolor1 = '#eeeeee';
221 %               my $bgcolor2 = '#ffffff';
222 %               my $bgcolor;
223 %
224 %               foreach my $row ( @$rows ) {
225 %
226 %                 if ( $bgcolor eq $bgcolor1 ) {
227 %                   $bgcolor = $bgcolor2;
228 %                 } else {
229 %                   $bgcolor = $bgcolor1;
230 %                 }
231
232                   <TR>
233
234 %                   if ( $opt{'fields'} ) {
235 %
236 %                     my $links    = $opt{'links'} ? [ @{$opt{'links'}} ] : '';
237 %                     my $onclicks = $opt{'link_onclicks'} ? [ @{$opt{'link_onclicks'}} ] : [];
238 %                     my $aligns   = $opt{'align'} ? [ @{$opt{'align'}} ] : '';
239 %                     my $colors   = $opt{'color'} ? [ @{$opt{'color'}} ] : [];
240 %                     my $sizes    = $opt{'size'}  ? [ @{$opt{'size'}}  ] : [];
241 %                     my $styles   = $opt{'style'} ? [ @{$opt{'style'}} ] : [];
242 %                     my $cstyles  = $opt{'cell_style'} ? [ @{$opt{'cell_style'}} ] : [];
243 %
244 %                     foreach my $field (
245 %
246 %                       map {
247 %                             if ( ref($_) eq 'ARRAY' ) {
248 %
249 %                               my $tableref = $_;
250 %
251 %                               '<TABLE CLASS="inv" CELLSPACING=0 CELLPADDING=0 WIDTH="100%">'.
252 %
253 %                               join('', map {
254 %
255 %                                 my $rowref = $_;
256 %
257 %                                 '<tr>'.
258 %
259 %                                 join('', map {
260 %
261 %                                   my $e = $_;
262 %
263 %                                   '<TD '.
264 %                                     join(' ', map {
265 %                                       uc($_).'="'. $e->{$_}. '"';
266 %                                     }
267 %                                     grep exists($e->{$_}),
268 %                                          qw( align bgcolor colspan rowspan
269 %                                              style valign width )
270 %                                     ).
271 %                                   '>'.
272 %
273 %                                   ( $e->{'link'}
274 %                                       ? '<A HREF="'. $e->{'link'}. '">'
275 %                                       : ''
276 %                                   ).
277 %                                   ( $e->{'size'}
278 %                                      ? '<FONT SIZE="'.uc($e->{'size'}).'">'
279 %                                      : ''
280 %                                   ).
281 %                                   ( $e->{'data_style'}
282 %                                       ? '<'. uc($e->{'data_style'}). '>'
283 %                                       : ''
284 %                                   ).
285 %                                   $e->{'data'}.
286 %                                   ( $e->{'data_style'}
287 %                                       ? '</'. uc($e->{'data_style'}). '>'
288 %                                       : ''
289 %                                   ).
290 %                                   ( $e->{'size'} ? '</FONT>' : '' ).
291 %                                   ( $e->{'link'} ? '</A>'    : '' ).
292 %                                   '</td>';
293 %
294 %                                 } @$rowref ).
295 %
296 %                                 '</tr>';
297 %                               } @$tableref ).
298 %
299 %                               '</table>';
300 %
301 %                             } else {
302 %                               $_;
303 %                             }
304 %                           }
305 %
306 %                       map {
307 %                             if ( ref($_) eq 'CODE' ) {
308 %                               &{$_}($row);
309 %                             } else {
310 %                               $row->$_();
311 %                             }
312 %                           }
313 %                       @{$opt{'fields'}}
314 %
315 %                     ) {
316 %
317 %                       my $class = ( $field =~ /^<TABLE/i ) ? 'inv' : 'grid';
318 %
319 %                       my $align = $aligns ? shift @$aligns : '';
320 %                       $align = " ALIGN=$align" if $align;
321 %
322 %                       my $a = '';
323 %                       if ( $links ) {
324 %                         my $link = shift @$links;
325 %                         my $onclick = shift @$onclicks;
326 %
327 %                         if (    ! $opt{'agent_virt'}
328 %                              || ( $null_link && ! $row->agentnum )
329 %                              || grep { $row->agentnum == $_ }
330 %                                      @link_agentnums
331 %                            ) {
332 %        
333 %                           $link = &{$link}($row)
334 %                             if ref($link) eq 'CODE';
335 %
336 %                           $onclick = &{$onclick}($row)
337 %                             if ref($onclick) eq 'CODE';
338 %                           $onclick = qq( onClick="$onclick") if $onclick;
339 %
340 %                           if ( $link ) {
341 %                             my( $url, $method ) = @{$link};
342 %                             if ( ref($method) eq 'CODE' ) {
343 %                               $a = $url. &{$method}($row);
344 %                             } else {
345 %                               $a = $url. $row->$method();
346 %                             }
347 %                             $a = qq(<A HREF="$a"$onclick>);
348 %                           }
349 %                           elsif ( $onclick ) {
350 %                             $a = qq(<A HREF="javascript:void(0);"$onclick>);
351 %                           }
352 %                         }
353 %
354 %                       }
355 %
356 %                       my $font = '';
357 %                       my $color = shift @$colors;
358 %                       $color = &{$color}($row) if ref($color) eq 'CODE';
359 %                       my $size = shift @$sizes;
360 %                       $size = &{$size}($row) if ref($size) eq 'CODE';
361 %                       if ( $color || $size ) {
362 %                         $font = '<FONT '.
363 %                                 ( $color ? "COLOR=#$color "   : '' ).
364 %                                 ( $size  ? qq(SIZE="$size" )  : '' ).
365 %                                 '>';
366 %                       }
367 %
368 %                       my($s, $es) = ( '', '' );
369 %                       my $style = shift @$styles;
370 %                       $style = &{$style}($row) if ref($style) eq 'CODE';
371 %                       if ( $style ) {
372 %                         $s = join( '', map "<$_>", split('', $style) );
373 %                         $es = join( '', map "</$_>", split('', $style) );
374 %                       }
375 %
376 %                       my $cstyle = shift @$cstyles;
377 %                       $cstyle = &{$cstyle}($row) if ref($cstyle) eq 'CODE';
378 %                       $cstyle = qq(STYLE="$cstyle")
379 %                         if $cstyle;
380
381                         <TD CLASS="<% $class %>" BGCOLOR="<% $bgcolor %>" <% $align %> <% $cstyle %>><% $font %><% $a %><% $s %><% $field %><% $es %><% $a ? '</A>' : '' %><% $font ? '</FONT>' : '' %></TD>
382
383 %                     } 
384 %
385 %                   } else { 
386 %
387 %                     foreach ( @$row ) { 
388                         <TD CLASS="grid" BGCOLOR="<% $bgcolor %>"><% $_ %></TD>
389 %                     }
390 %
391 %                   }
392
393                   </TR>
394
395 %               } 
396
397 %               if ( $opt{'footer'} ) { 
398
399                   <TR>
400
401 %                   foreach my $footer ( @{ $opt{'footer'} } ) { 
402                       <TD CLASS="grid" BGCOLOR="#dddddd" STYLE="border-top: dashed 1px black;"><i><% $footer %></i></TD>
403 %                   } 
404
405                   </TR>
406 %               } 
407             
408               </TABLE>
409
410               <% $pager %>
411   
412             </TD>
413           </TR>
414         </TABLE>
415 %     }
416
417 %     if ( $type eq 'html-print' ) {
418
419         </BODY></HTML>
420       
421 %     } else {
422
423         <% defined($opt{'html_foot'}) 
424               ? ( ref($opt{'html_foot'})
425                     ? &{$opt{'html_foot'}}()
426                     : $opt{'html_foot'}
427                 )
428               : ''
429         %>
430
431         <% include( '/elements/footer.html' ) %>
432
433 %     }
434
435 %   } 
436 <%init>
437
438 my %args = @_;
439 my $type           = $args{'type'};
440 my $header         = $args{'header'};
441 my $rows           = $args{'rows'};
442 my @link_agentnums = @{ $args{'link_agentnums'} };
443 my $null_link      = $args{'null_link'};
444 my $confmax        = $args{'confmax'};
445 my $maxrecords     = $args{'maxrecords'};
446 my $offset         = $args{'offset'};
447 my %opt            = %{ $args{'opt'} };
448
449 my $count_sth = dbh->prepare($opt{'count_query'})
450   or die "Error preparing $opt{'count_query'}: ". dbh->errstr;
451 $count_sth->execute
452   or die "Error executing $opt{'count_query'}: ". $count_sth->errstr;
453 my $count_arrayref = $count_sth->fetchrow_arrayref;
454 my $total = $count_arrayref->[0];
455
456 </%init>