147a2d984feaa8a70d53149b85b10e68f09ba6c4
[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'}}($opt{html_init_data})
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 results:' %>
140
141 %               $cgi->param('_type', "$xlsname.xls" ); 
142                 <A HREF="<% "$self_url?". $cgi->query_string %>">Spreadsheet</A>&nbsp;|&nbsp;
143
144 %               $cgi->param('_type', 'csv'); 
145                 <A HREF="<% "$self_url?". $cgi->query_string %>">CSV</A>&nbsp;|&nbsp;
146
147 %             if ( defined($opt{xml_elements}) ) {
148 %               $cgi->param('_type', 'xml'); 
149                 <A HREF="<% "$self_url?". $cgi->query_string %>">XML</A>&nbsp;|&nbsp;
150 %             }
151
152 %               $cgi->param('_type', 'html-print'); 
153                 <A HREF="<% "$self_url?". $cgi->query_string %>">webpage</A>
154
155 %# "save search" -- for now, obey disable_download and the 'Download
156 %# report data' ACL, because saving a search allows the user to receive
157 %# copies of the data.
158                 <BR>
159 %# XXX should do a check here on whether the user already has this
160 %# search saved...
161                 <& /elements/popup_link.html,
162                   'action'        => $fsurl.'/edit/saved_search.html?title='.
163                                        uri_escape($opt{title}),
164                   'label'         => 'Save this search',
165                   'actionlabel'   => 'Save this search',
166                   'width'         => 650,
167                   'height'        => 500,
168                 &>
169               </TD>
170 %             $cgi->param('_type', "html" ); 
171 %           } 
172
173           </TR>
174           <TR>
175             <TD COLSPAN=2>
176
177 %             my $pager = '';
178 %             unless ( $type eq 'html_print' ) {
179
180                 <% $pager = include( '/elements/pager.html',
181                                        'offset'     => $offset,
182                                        'num_rows'   => scalar(@$rows),
183                                        'total'      => $total,
184                                        'maxrecords' => $maxrecords,
185                                    )
186                 %>
187
188                 <% defined($opt{'html_form'}) 
189                      ? ( ref($opt{'html_form'})
190                            ? &{$opt{'html_form'}}()
191                            : $opt{'html_form'}
192                        )
193                      : ''
194                 %>
195
196 %             }
197
198               <& SELF:data_table,
199                   rows            => $rows,
200                   null_link       => $null_link,
201                   link_agentnums  => \@link_agentnums,
202                   self_url        => $self_url,
203                   %opt
204               &>
205
206               <% $pager %>
207   
208             </TD>
209           </TR>
210         </TABLE>
211 %     }
212
213 %     if ( $type eq 'html-print' ) {
214 %       unless ( $opt{nohtmlheader} ) {
215
216         </BODY></HTML>
217       
218 %       }
219 %     } else {
220
221         <% defined($opt{'html_foot'}) 
222               ? ( ref($opt{'html_foot'})
223                     ? &{$opt{'html_foot'}}()
224                     : $opt{'html_foot'}
225                 )
226               : ''
227         %>
228
229         <% $opt{nohtmlheader}
230              ? ''
231              : include( '/elements/footer.html' )
232         %>
233
234 %     }
235
236 %   } 
237 <%init>
238
239 my $curuser = $FS::CurrentUser::CurrentUser;
240
241 my %args = @_;
242 my $type           = $args{'type'};
243 my $header         = $args{'header'};
244 my $rows           = $args{'rows'};
245 my @link_agentnums = @{ $args{'link_agentnums'} };
246 my $null_link      = $args{'null_link'};
247 my $confmax        = $args{'confmax'};
248 my $maxrecords     = $args{'maxrecords'};
249 my $offset         = $args{'offset'};
250 my %opt            = %{ $args{'opt'} };
251
252 # must be an arrayref of the row count, followed by any other totals
253 my $count_arrayref = $args{'totals'};
254 my $total = $count_arrayref->[0];
255
256 # there used to be an option to override this, for highly dubious reasons
257 my $self_url = $cgi->url('-path_info' => 1, '-full' =>1);
258
259 </%init>
260 <%method data_table>
261 % my %opt = @_;
262 % my $rows = delete $opt{rows};
263 % my $self_url = delete $opt{self_url};
264 <& /elements/table-grid.html &>
265
266 <THEAD>
267 <& SELF:header_row,
268   'header'      => $opt{'header'},
269   'header2'     => $opt{'header2'},
270   'sort_fields' => ($opt{'sort_fields'} || $opt{'fields'}),
271 &>
272 </THEAD>
273
274 <TBODY>
275 <& SELF:data_rows, rows => $rows, opt => \%opt &>
276 </TBODY>
277
278 % if ( $opt{'footer'} ) {
279 <TFOOT>
280 <& SELF:footer_row, row => $opt{'footer'}, opt => \%opt &>
281 </TFOOT> 
282 % } 
283 </TABLE>
284 </%method>
285 <%method header_row>
286 <%args>
287 @sort_fields
288 @header
289 @header2 => ()
290 </%args>
291   <TR>
292 % my $h2 = 0;
293 % my $colspan = 0;
294 % my $order_by = $cgi->param('order_by');
295 % my $self_url = $cgi->url('-path_info' => 1, '-full' =>1);
296 % foreach my $header ( @header ) { 
297 %
298 %   my $field = shift @sort_fields;
299 %
300 %   $colspan-- if $colspan > 0;
301 %   next if $colspan;
302 %
303 %   my $label = ref($header) ? $header->{label} : $header;
304 %   unless ( ref($field) || !$field ) {
305 %     if ( $order_by eq $field ) {
306 %       $cgi->param('order_by', "$field DESC");
307 %     } else {
308 %       $cgi->param('order_by', $field);
309 %     }
310 %     $label = qq(<A HREF="$self_url?). $cgi->query_string.
311 %              qq(">$label</A>);
312 %   }
313 %
314 %   $colspan = ref($header) ? $header->{colspan} : 0;
315 %   my $rowspan = 1;
316 %   my $style = '';
317 %   if ( @header2 ) {
318 %     if ( !length($header2[$h2]) ) {
319 %       $rowspan = 2;
320 %       splice @header2, $h2, 1;
321 %     } else {
322 %       $h2++;
323 %       $style = 'STYLE="border-bottom: none"'
324 %     }
325 %   }
326     <TH CLASS   = "grid"
327         BGCOLOR = "#cccccc"
328         ROWSPAN = "<% $rowspan %>"
329         <% $colspan ? 'COLSPAN = "'.$colspan.'"' : '' %>
330         <% $style %>
331
332     >
333       <% $label %>
334     </TH>
335 % } 
336   </TR>
337
338 % if ( @header2 ) {
339   <TR>
340 %   foreach my $header ( @header2 ) { 
341 %     my $label = ref($header) ? $header->{label} : $header;
342       <TH CLASS="grid" BGCOLOR="#cccccc">
343         <FONT SIZE="-1"><% $label %></FONT>
344       </TH>
345 %   } 
346   </TR>
347 % }
348 </%method>
349 <%method data_rows>
350 <%args>
351 $rows => []
352 %opt
353 </%args>
354 % my %align = (
355 %   'l' => 'left',
356 %   'r' => 'right',
357 %   'c' => 'center',
358 %   ' ' => '',
359 %   '.' => '',
360 % );
361 % if ( $opt{align} and !ref($opt{align}) ) {
362 %   $opt{align} = [ map $align{$_}, split(//, $opt{align}) ];
363 % }
364
365 % my $i = 0; # for row striping # XXX CSS - nth-child
366 % my $id = 0;
367 % foreach my $row ( @$rows ) {
368 %
369 %   my $rowstyle = '';
370 %   if ( $row eq $opt{'footer_data'} ) { # XXX CSS - tfoot
371 %     $rowstyle = ' STYLE="border-top: dashed 1px black; font-style: italic background-color=#dddddd"';
372 %   }
373 %
374 %   my $trid = '';
375 %   if ( $opt{'link_field' } ) {
376 %     my $link_field = $opt{'link_field'};
377 %     if ( ref($link_field) eq 'CODE' ) {
378 %       $trid = &{$link_field}($row);
379 %     } else {
380 %       $trid = $row->$link_field();
381 %     }
382 %   }
383     <TR ID="<%$trid |h%>" CLASS="row<% $i % 2 %>"<%$rowstyle%>>
384
385 %   if ( $opt{'fields'} ) {
386 %
387 %     my $links    = $opt{'links'} ? [ @{$opt{'links'}} ] : '';
388 %     my $onclicks = $opt{'link_onclicks'} ? [ @{$opt{'link_onclicks'}} ] : [];
389 %     my $tooltips = $opt{'tooltips'} ? [ @{$opt{'tooltips'}} ] : [];
390 %     my $aligns   = $opt{'align'} ? [ @{$opt{'align'}} ] : '';
391 %     my $colors   = $opt{'color'} ? [ @{$opt{'color'}} ] : [];
392 %     my $sizes    = $opt{'size'}  ? [ @{$opt{'size'}}  ] : [];
393 %     my $styles   = $opt{'style'} ? [ @{$opt{'style'}} ] : [];
394 %     my $cstyles  = $opt{'cell_style'} ? [ @{$opt{'cell_style'}} ] : [];
395 %     my $formats  = $opt{'format'} ? [ @{$opt{'format'}} ] : [];
396 %
397 %     foreach my $field (
398 %
399 %       # if the value of the field is an arrayref, then construct a table in
400 %       # the cell.
401 %       # if it's a (non-empty) scalar, and a format has been specified, then
402 %       # format the scalar with that.
403 %       # otherwise, just output the value.
404 %       # XXX we should also do date formats like this
405 %       map {
406 %             if ( ref($_) eq 'ARRAY' ) {
407 %
408 %               my $tableref = $_;
409 %
410 %               '<TABLE CLASS="inv" CELLSPACING=0 CELLPADDING=0 WIDTH="100%">'.
411 %
412 %               join('', map {
413 %
414 %                 my $rowref = $_;
415 %
416 %                 '<tr>'.
417 %
418 %                 join('', map {
419 %
420 %                   my $e = $_;
421 %
422 %                   '<TD '.
423 %                     join(' ', map {
424 %                       uc($_).'="'. $e->{$_}. '"';
425 %                     }
426 %                     grep exists($e->{$_}),
427 %                          qw( align bgcolor colspan rowspan
428 %                              style valign width )
429 %                     ).
430 %                   '>'.
431 %
432 %                   ( $e->{'link'}
433 %                       ? '<A HREF="'. $e->{'link'}. '">'
434 %                       : ''
435 %                   ).
436 %                   ( $e->{'onclick'} # don't use with 'link'
437 %                       ? '<A HREF="#" onclick="' .
438 %                         $e->{'onclick'}.'">'
439 %                       : ''
440 %                   ).
441 %                   ( $e->{'size'}
442 %                      ? '<FONT SIZE="'.uc($e->{'size'}).'">'
443 %                      : ''
444 %                   ).
445 %                   ( $e->{'data_style'}
446 %                       ? '<'. uc($e->{'data_style'}). '>'
447 %                       : ''
448 %                   ).
449 %                   $e->{'data'}.
450 %                   ( $e->{'data_style'}
451 %                       ? '</'. uc($e->{'data_style'}). '>'
452 %                       : ''
453 %                   ).
454 %                   ( $e->{'size'} ? '</FONT>' : '' ).
455 %                   ( $e->{'link'} || $e->{'onclick'} 
456 %                       ? '</A>'
457 %                       : '' ).
458 %                   '</td>';
459 %
460 %                 } @$rowref ).
461 %
462 %                 '</tr>';
463 %               } @$tableref ).
464 %
465 %               '</table>';
466 %
467 %             } else {
468 %               if ( length($_) > 0 and my $format = shift @$formats ) {
469 %                 $_ = sprintf($format, $_);
470 %               }
471 %               $_;
472 %             }
473 %           }
474 %
475 %       # get the value of the field spec:
476 %       # - if the spec is a coderef, evaluate the coderef
477 %       # - if the spec is a string, call that string as a method
478 %       # - if the spec is an integer, get the field in that position
479 %       map {
480 %             if ( ref($_) eq 'CODE' ) {
481 %               &{$_}($row);
482 %             } elsif ( ref($row) eq 'ARRAY' and 
483 %                       $_ =~ /^\d+$/ ) {
484 %             # for the 'straight SQL' case: specify fields
485 %             # by position
486 %               encode_entities($row->[$_]);
487 %             } else {
488 %               encode_entities($row->$_());
489 %             }
490 %           }
491 %       @{$opt{'fields'}}
492 %
493 %     ) {
494 %
495 %       my $class = ( $field =~ /^<TABLE/i ) ? 'inv' : 'grid';
496 %       my $class = 'grid';
497 %
498 %       my $align = $aligns ? shift @$aligns : '';
499 %       $align = " ALIGN=$align" if $align;
500 %
501 %       my $a = '';
502 %       if ( $links ) {
503 %         my $link = shift @$links;
504 %         my $onclick = shift @$onclicks;
505 %         my $tooltip = shift @$tooltips;
506 %
507 %         if (    ! $opt{'agent_virt'}
508 %              || ( $opt{'null_link'} && ! $row->agentnum )
509 %              || grep { $row->agentnum == $_ }
510 %                      @{ $opt{link_agentnums} }
511 %            ) {
512 %
513 %           $link = &{$link}($row)
514 %             if ref($link) eq 'CODE';
515 %
516 %           $onclick = &{$onclick}($row)
517 %             if ref($onclick) eq 'CODE';
518 %           $onclick = qq( onClick="$onclick") if $onclick;
519 %
520 %           $tooltip = &{$tooltip}($row)
521 %             if ref($tooltip) eq 'CODE';
522 %           $tooltip = qq! id="a$id" !.
523 %             qq! onmouseover="return overlib(!.
524 %             $m->interp->apply_escapes($tooltip, 'h', 'js_string').
525 %             qq!, FGCLASS, 'tooltip', REF, 'a$id', !.
526 %             qq!REFC, 'LL', REFP, 'UL')"! if $tooltip;
527 %
528 %           if ( $link ) {
529 %             my( $url, $method ) = @{$link};
530 %             if ( ref($method) eq 'CODE' ) {
531 %               $a = $url. &{$method}($row);
532 %             } else {
533 %               $a = $url. $row->$method();
534 %             }
535 %             $a = qq(<A HREF="$a"$onclick$tooltip>);
536 %           }
537 %           elsif ( $onclick ) {
538 %             $a = qq(<A HREF="javascript:void(0);"$onclick>);
539 %           }
540 %           elsif ( $tooltip ) {
541 %             $a = qq(<A $tooltip>);
542 %           }
543 %           $id++;
544
545 %         }
546 %
547 %       }
548 %
549 %       my $font = '';
550 %       my $color = shift @$colors;
551 %       $color = &{$color}($row) if ref($color) eq 'CODE';
552 %       my $size = shift @$sizes;
553 %       $size = &{$size}($row) if ref($size) eq 'CODE';
554 %       if ( $color || $size ) {
555 %         $font = '<FONT '.
556 %                 ( $color ? "COLOR=#$color "   : '' ).
557 %                 ( $size  ? qq(SIZE="$size" )  : '' ).
558 %                 '>';
559 %       }
560 %
561 %       my($s, $es) = ( '', '' );
562 %       my $style = shift @$styles;
563 %       $style = &{$style}($row) if ref($style) eq 'CODE';
564 %       if ( $style ) {
565 %         $s = join( '', map "<$_>", split('', $style) );
566 %         $es = join( '', map "</$_>", split('', $style) );
567 %       }
568 %
569 %       my $cstyle = shift @$cstyles;
570 %       $cstyle = &{$cstyle}($row) if ref($cstyle) eq 'CODE';
571 %       $cstyle = qq(STYLE="$cstyle")
572 %         if $cstyle;
573
574         <TD CLASS="<% $class %>" <% $align %> <% $cstyle %>><% $a %><% $font %><% $s %><% $field %><% $es %><% $font ? '</FONT>' : '' %><% $a ? '</A>' : '' %></TD>
575
576 %     } 
577 %
578 %   } else { # not $opt{'fields'}
579 %
580 %     foreach ( @$row ) { 
581         <TD CLASS="grid"><% $_ %></TD>
582 %     }
583 %
584 %   }
585
586     </TR>
587
588 %   $i++;
589 %
590 % } # foreach $row
591 </%method>
592 <%method footer_row>
593 <%args>
594 $row
595 %opt
596 </%args>
597 %# don't try to respect all the styling options, just the ones that are
598 %# hard to replicate with CSS
599 % my %align = (
600 %   'l' => 'left',
601 %   'r' => 'right',
602 %   'c' => 'center',
603 %   ' ' => '',
604 %   '.' => '',
605 % );
606 % if ( $opt{align} and !ref($opt{align}) ) {
607 %   $opt{align} = [ map $align{$_}, split(//, $opt{align}) ];
608 % }
609 % my @aligns = @{ $opt{align} };
610
611 <TR>
612 % foreach my $footer ( @$row ) {
613 %   $footer = &{$footer}() if ref($footer) eq 'CODE';
614 %   my $align = shift @aligns;
615 %   my $style = '';
616 %   $style .= "text-align: $align;" if $align;
617     <TD CLASS="grid" STYLE="<% $style %>"><% $footer %></TD>
618 % } 
619 </TR>
620 </%method>
621