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