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