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