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