Will things ever be the same again?
[freeside.git] / httemplate / search / elements / search.html
1 %
2 %
3 %  # options example...  
4 %  # (everything not commented required is optional)
5 %  #
6 %  # # basic options, required
7 %  # 'title'       => 'Page title',
8 %  # 'name'        => 'items', #name for the records returned
9 %  #
10 %  # # some HTML callbacks...
11 %  # 'menubar'          => '', #menubar arrayref
12 %  # 'html_init'        => '', #after the header/menubar and before the pager
13 %  # 'html_foot'        => '', #at the bottom
14 %  # 'html_posttotal'   => '', #at the bottom
15 %  #                           # (these three can be strings or coderefs)
16 %  # 
17 %  #
18 %  # #literal SQL query string or qsearch hashref, required
19 %  # 'query'       => {
20 %  #                    'table'     => 'tablename',
21 %  #                    #everything else is optional...
22 %  #                    'hashref'   => { 'field' => 'value',
23 %  #                                     'field' => { 'op'    => '<',
24 %  #                                                  'value' => '54',
25 %  #                                                },
26 %  #                                   },
27 %  #                    'select'    => '*',
28 %  #                    'addl_from' => '', #'LEFT JOIN othertable USING ( key )',
29 %  #                    'extra_sql' => '', #'AND otherstuff', #'WHERE onlystuff',
30 %  #                    
31 %  #
32 %  #                  },
33 %  #                  # "select * from tablename";
34 %  #
35 %  # #required unless 'query' is an SQL query string (shouldn't be...)
36 %  # 'count_query' => 'SELECT COUNT(*) FROM tablename',
37 %  #
38 %  # 'count_addl' => [], #additional count fields listref of sprintf strings
39 %  #                     # [ $money_char.'%.2f total paid', ],
40 %  #
41 %  # #listref of column labels, <TH>
42 %  # #required unless 'query' is an SQL query string
43 %  # # (if not specified the database column names will be used)
44 %  # 'header'      => [ '#', 'Item' ],
45 %  #
46 %  # 'disable_download' => '', # set true to hide the CSV/Excel download links
47 %  # 'disable_nonefound' => '', # set true to disable the "No matching Xs found"
48 %  #                            # message
49 %  #
50 %  # #listref - each item is a literal column name (or method) or coderef
51 %  # #if not specified all columns will be shown
52 %  # 'fields'      => [
53 %  #                    'column',
54 %  #                    sub { my $row = shift; $row->column; },
55 %  #                  ],
56 %  #
57 %  # #listref of column footers
58 %  # 'footer'      => [],
59 %  # 
60 %  # #listref - each item is the empty string, or a listref of ...
61 %  # 'links'       =>
62 %  #
63 %  #
64 %  # 'align'       => 'lrc.', #one letter for each column, left/right/center/none
65 %  #                          # can also pass a listref with full values:
66 %  #                          # [ 'left', 'right', 'center', '' ]
67 %  #
68 %  # #listrefs...
69 %  # #currently only HTML, maybe eventually Excel too
70 %  # 'color'       => [],
71 %  # 'size'        => [],
72 %  # 'style'       => [],
73 %  # 
74 %  # #redirect if there's only one item...
75 %  # # listref of URL base and column name (or method)
76 %  # # or a coderef that returns the same
77 %  # 'redirect' =>
78 %
79 %  my $DEBUG = 0;
80 %
81 %  my(%opt) = @_;
82 %  #warn join(' / ', map { "$_ => $opt{$_}" } keys %opt ). "\n";
83 %
84 %  my %align = (
85 %    'l' => 'left',
86 %    'r' => 'right',
87 %    'c' => 'center',
88 %    ' ' => '',
89 %    '.' => '',
90 %  );
91 %  $opt{align} = [ map $align{$_}, split(//, $opt{align}) ],
92 %    unless !$opt{align} || ref($opt{align});
93 %
94 %  my $type = '';
95 %  my $limit = '';
96 %  my($maxrecords, $total, $offset, $count_arrayref);
97 %
98 %  if ( $cgi->param('_type') =~ /^(csv|\w*\.xls)$/ ) {
99 %  
100 %    $type = $1;
101 %
102 %  } else { #setup some pagination things if we're in html mode
103 %
104 %    unless (exists($opt{'count_query'}) && length($opt{'count_query'})) {
105 %      ( $opt{'count_query'} = $opt{'query'} ) =~
106 %        s/^\s*SELECT\s*(.*?)\s+FROM\s/SELECT COUNT(*) FROM /i;
107 %    }
108 %
109 %    my $conf = new FS::Conf;
110 %    $maxrecords = $conf->config('maxsearchrecordsperpage');
111 %
112 %    $limit = $maxrecords ? "LIMIT $maxrecords" : '';
113 %
114 %    $offset = $cgi->param('offset') || 0;
115 %    $limit .= " OFFSET $offset" if $offset;
116 %
117 %    my $count_sth = dbh->prepare($opt{'count_query'})
118 %      or die "Error preparing $opt{'count_query'}: ". dbh->errstr;
119 %    $count_sth->execute
120 %      or die "Error executing $opt{'count_query'}: ". $count_sth->errstr;
121 %    $count_arrayref = $count_sth->fetchrow_arrayref;
122 %    $total = $count_arrayref->[0];
123 %
124 %  }
125 %
126 %  # run the query
127 %
128 %  my $header = $opt{'header'};
129 %  my $rows;
130 %  if ( ref($opt{'query'}) ) {
131 %
132 %    #eval "use FS::$opt{'query'};";
133 %    $rows = [ qsearch(
134 %      $opt{'query'}->{'table'}, 
135 %      $opt{'query'}->{'hashref'} || {}, 
136 %      $opt{'query'}->{'select'},
137 %      $opt{'query'}->{'extra_sql'}. " $limit",
138 %      '',
139 %      (exists($opt{'query'}->{'addl_from'}) ? $opt{'query'}->{'addl_from'} : '')
140 %    ) ];
141 %
142 %  } else {
143 %
144 %    my $sth = dbh->prepare("$opt{'query'} $limit")
145 %      or die "Error preparing $opt{'query'}: ". dbh->errstr;
146 %    $sth->execute
147 %      or die "Error executing $opt{'query'}: ". $sth->errstr;
148 %
149 %    #can get # of rows without fetching them all?
150 %    $rows = $sth->fetchall_arrayref;
151 %
152 %    $header ||= $sth->{NAME};
153 %
154 %  }
155 %
156 %  warn scalar(@$rows). ' rows returned from '.
157 %       ( ref($opt{'query'}) ? 'qsearch query' : 'literal SQL query' )
158 %    if $DEBUG || $opt{'debug'};
159 %
160 %  # display the results - csv, xls or html
161 %
162 %  if ( $type eq 'csv' ) {
163 %
164 %    #http_header('Content-Type' => 'text/comma-separated-values' ); #IE chokes
165 %    http_header('Content-Type' => 'text/plain' );
166 %
167 %    my $csv = new Text::CSV_XS { 'always_quote' => 1,
168 %                                 'eol'          => "\n", #"\015\012", #"\012"
169 %                               };
170 %
171 %    $csv->combine(@$header); #or die $csv->status;
172 %    
173 <% $csv->string %>
174 %
175 %
176 %    foreach my $row ( @$rows ) {
177 %
178 %      if ( $opt{'fields'} ) {
179 %
180 %        my @line = ();
181 %
182 %        foreach my $field ( @{$opt{'fields'}} ) {
183 %          if ( ref($field) eq 'CODE' ) {
184 %            push @line, map {
185 %                              ref($_) eq 'ARRAY'
186 %                                ? '(N/A)' #unimplemented
187 %                                : $_;
188 %                            }
189 %                            &{$field}($row);
190 %          } else {
191 %            push @line, $row->$field();
192 %          }
193 %        }
194 %
195 %        $csv->combine(@line); #or die $csv->status;
196 %
197 %      } else {
198 %        $csv->combine(@$row); #or die $csv->status;
199 %      }
200 %
201 %      
202 <% $csv->string %>
203 %
204 %
205 %    }
206 %
207 %  #} elsif ( $type eq 'excel' ) {
208 %  } elsif ( $type =~ /\.xls$/ ) {
209 %
210 %    #http_header('Content-Type' => 'application/excel' ); #eww
211 %    http_header('Content-Type' => 'application/vnd.ms-excel' );
212 %    #http_header('Content-Type' => 'application/msexcel' ); #alas
213 %
214 %    my $data = '';
215 %    my $XLS = new IO::Scalar \$data;
216 %    my $workbook = Spreadsheet::WriteExcel->new($XLS)
217 %      or die "Error opening .xls file: $!";
218 %
219 %    my $worksheet = $workbook->add_worksheet(substr($opt{'title'},0,31));
220 %
221 %    my($r,$c) = (0,0);
222 %
223 %    $worksheet->write($r, $c++, $_) foreach @$header;
224 %
225 %    foreach my $row ( @$rows ) {
226 %      $r++;
227 %      $c = 0;
228 %
229 %      if ( $opt{'fields'} ) {
230 %
231 %        #my $links = $opt{'links'} ? [ @{$opt{'links'}} ] : '';
232 %        #my $aligns = $opt{'align'} ? [ @{$opt{'align'}} ] : '';
233 %
234 %        foreach my $field ( @{$opt{'fields'}} ) {
235 %          #my $align = $aligns ? shift @$aligns : '';
236 %          #$align = " ALIGN=$align" if $align;
237 %          #my $a = '';
238 %          #if ( $links ) {
239 %          #  my $link = shift @$links;
240 %          #  $link = &{$link}($row) if ref($link) eq 'CODE';
241 %          #  if ( $link ) {
242 %          #    my( $url, $method ) = @{$link};
243 %          #    if ( ref($method) eq 'CODE' ) {
244 %          #      $a = $url. &{$method}($row);
245 %          #    } else {
246 %          #      $a = $url. $row->$method();
247 %          #    }
248 %          #    $a = qq(<A HREF="$a">);
249 %          #  }
250 %          #}
251 %          if ( ref($field) eq 'CODE' ) {
252 %            foreach my $value ( &{$field}($row) ) {
253 %              if ( ref($value) eq 'ARRAY' ) { 
254 %                $worksheet->write($r, $c++, '(N/A)' ); #unimplemented
255 %              } else {
256 %                $worksheet->write($r, $c++, $value );
257 %              }
258 %            }
259 %          } else {
260 %            $worksheet->write($r, $c++, $row->$field() );
261 %          }
262 %        }
263 %
264 %      } else {
265 %        $worksheet->write($r, $c++, $_) foreach @$row;
266 %      }
267 %
268 %    }
269 %
270 %    $workbook->close();# or die "Error creating .xls file: $!";
271 %
272 %    http_header('Content-Length' => length($data) );
273 %    
274 <% $data %>
275 %
276 %
277 %  } else { # regular HTML
278 %
279 %    if ( exists($opt{'redirect'}) && scalar(@$rows) == 1 && $total == 1 ) {
280 %      my $redirect = $opt{'redirect'};
281 %      $redirect = &{$redirect}($rows->[0]) if ref($redirect) eq 'CODE';
282 %      my( $url, $method ) = @$redirect;
283 %      redirect( $url. $rows->[0]->$method() );
284 %    } else {
285 %      ( my $xlsname = $opt{'name'} ) =~ s/\W//g;
286 %      #$opt{'name'} =~ s/s$// if $total == 1;
287 %      $opt{'name'} =~ s/((s)e)?s$/$2/ if $total == 1;  #should use Lingua::bs
288 %                                                       # to "depluralize"
289 %
290 %      my @menubar = ();
291 %      if ( $opt{'menubar'} ) {
292 %        @menubar = @{ $opt{'menubar'} };
293 %      } else {
294 %        @menubar = ( 'Main menu' => $p );
295 %      }
296 %
297 %
298 %  
299
300   <% include( '/elements/header.html', $opt{'title'},
301                  include( '/elements/menubar.html', @menubar )
302              )
303   %>
304   <% defined($opt{'html_init'}) 
305         ? ( ref($opt{'html_init'})
306               ? &{$opt{'html_init'}}()
307               : $opt{'html_init'}
308           )
309         : ''
310   %>
311 % my $pager = include ( '/elements/pager.html',
312 %                             'offset'     => $offset,
313 %                             'num_rows'   => scalar(@$rows),
314 %                             'total'      => $total,
315 %                             'maxrecords' => $maxrecords,
316 %                         );
317 %  
318 % unless ( $total ) { 
319 % unless ( $opt{'disable_nonefound'} ) { 
320
321       No matching <% $opt{'name'} %> found.<BR>
322 % } 
323 % } else { 
324
325   
326     <TABLE>
327       <TR>
328         <TD VALIGN="bottom">
329           <% $total %> total <% $opt{'name'} %>
330           <% defined($opt{'html_posttotal'}) 
331                 ? ( ref($opt{'html_posttotal'})
332                       ? &{$opt{'html_posttotal'}}()
333                       : $opt{'html_posttotal'}
334                   )
335                 : ''
336           %>
337           <BR>
338 % if ( $opt{'count_addl'} ) { 
339 % my $n=0; foreach my $count ( @{$opt{'count_addl'}} ) { 
340
341               <% sprintf( $count, $count_arrayref->[++$n] ) %><BR>
342 % } 
343 % } 
344
345         </TD>
346 % unless ( $opt{'disable_download'} ) { 
347
348           <TD ALIGN="right">
349 % $cgi->param('_type', "$xlsname.xls" ); 
350
351             Download full results<BR>
352             as <A HREF="<% $cgi->self_url %>">Excel spreadsheet</A><BR>
353 % $cgi->param('_type', 'csv'); 
354
355             as <A HREF="<% $cgi->self_url %>">CSV file</A>
356           </TD>
357 % } 
358
359       </TR>
360       <TR>
361         <TD COLSPAN=2>
362             <% $pager %>
363
364             <% include('/elements/table-grid.html') %>
365
366               <TR>
367
368 %                 foreach my $header ( @$header ) { 
369
370                    <TH CLASS="grid" BGCOLOR="#cccccc"><% $header %></TH>
371 % } 
372
373               </TR>
374 % my $bgcolor1 = '#eeeeee';
375 %                 my $bgcolor2 = '#ffffff';
376 %                 my $bgcolor;
377 %                 foreach my $row ( @$rows ) {
378 %                   if ( $bgcolor eq $bgcolor1 ) {
379 %                     $bgcolor = $bgcolor2;
380 %                   } else {
381 %                     $bgcolor = $bgcolor1;
382 %                   }
383 %              
384
385                    <TR>
386 % if ( $opt{'fields'} ) {
387 %
388 %                        my $links  = $opt{'links'} ? [ @{$opt{'links'}} ] : '';
389 %                        my $aligns = $opt{'align'} ? [ @{$opt{'align'}} ] : '';
390 %                        my $colors = $opt{'color'} ? [ @{$opt{'color'}} ] : [];
391 %                        my $sizes  = $opt{'size'}  ? [ @{$opt{'size'}}  ] : [];
392 %                        my $styles = $opt{'style'} ? [ @{$opt{'style'}} ] : [];
393 %
394 %                        foreach my $field (
395 %
396 %                          map {
397 %                                if ( ref($_) eq 'ARRAY' ) {
398 %
399 %                                  my $tableref = $_;
400 %
401 %                                  '<TABLE CLASS="inv" CELLSPACING=0 CELLPADDING=0>'.
402 %
403 %                                  join('', map {
404 %
405 %                                    my $rowref = $_;
406 %
407 %                                    '<tr>'.
408 %
409 %                                    join('', map {
410 %
411 %                                      my $element = $_;
412 %
413 %                                      '<TD'.
414 %                                      ( $element->{'align'}
415 %                                          ? ' ALIGN="'. $element->{'align'}. '"'
416 %                                          : ''
417 %                                      ). '>'.
418 %                                      ( $element->{'link'}
419 %                                          ? '<A HREF="'. $element->{'link'}.'">'
420 %                                          : ''
421 %                                      ).
422 %                                      $element->{'data'}.
423 %                                      ( $element->{'link'}
424 %                                          ? '</A>'
425 %                                          : ''
426 %                                      ).
427 %                                      '</td>';
428 %
429 %                                    } @$rowref ).
430 %
431 %                                    '</tr>';
432 %                                  } @$tableref ).
433 %
434 %                                  '</table>';
435 %
436 %                                } else {
437 %                                  $_;
438 %                                }
439 %                              }
440 %
441 %                          map {
442 %                                if ( ref($_) eq 'CODE' ) {
443 %                                  &{$_}($row);
444 %                                } else {
445 %                                  $row->$_();
446 %                                }
447 %                              }
448 %                          @{$opt{'fields'}}
449 %
450 %                        ) {
451 %
452 %                          my $class = ( $field =~ /^<TABLE/i ) ? 'inv' : 'grid';
453 %
454 %                          my $align = $aligns ? shift @$aligns : '';
455 %                          $align = " ALIGN=$align" if $align;
456 %
457 %                          my $a = '';
458 %                          if ( $links ) {
459 %                            my $link = shift @$links;
460 %                            $link = &{$link}($row) if ref($link) eq 'CODE';
461 %                            if ( $link ) {
462 %                              my( $url, $method ) = @{$link};
463 %                              if ( ref($method) eq 'CODE' ) {
464 %                                $a = $url. &{$method}($row);
465 %                              } else {
466 %                                $a = $url. $row->$method();
467 %                              }
468 %                              $a = qq(<A HREF="$a">);
469 %                            }
470 %                          }
471 %
472 %                          my $font = '';
473 %                          my $color = shift @$colors;
474 %                          $color = &{$color}($row) if ref($color) eq 'CODE';
475 %                          my $size = shift @$sizes;
476 %                          $size = &{$size}($row) if ref($size) eq 'CODE';
477 %                          if ( $color || $size ) {
478 %                            $font = '<FONT '.
479 %                                    ( $color ? "COLOR=#$color "   : '' ).
480 %                                    ( $size  ? qq(SIZE="$size" )  : '' ).
481 %                                    '>';
482 %                          }
483 %
484 %                          my($s, $es) = ( '', '' );
485 %                          my $style = shift @$styles;
486 %                          $style = &{$style}($row) if ref($style) eq 'CODE';
487 %                          if ( $style ) {
488 %                            $s = join( '', map "<$_>", split('', $style) );
489 %                            $es = join( '', map "</$_>", split('', $style) );
490 %                          }
491 %
492 %                       
493
494                        <TD CLASS="<% $class %>" BGCOLOR="<% $bgcolor %>"<% $align %>><% $font %><% $a %><% $s %><% $field %><% $es %><% $a ? '</A>' : '' %><% $font ? '</FONT>' : '' %></TD>
495 % } 
496 % } else { 
497 % foreach ( @$row ) { 
498
499                           <TD CLASS="grid" BGCOLOR="<% $bgcolor %>"><% $_ %></TD>
500 % } 
501 % } 
502
503                    </TR>
504 % } 
505 % if ( $opt{'footer'} ) { 
506
507                 <TR>
508 % foreach my $footer ( @{ $opt{'footer'} } ) { 
509
510                      <TD CLASS="grid" BGCOLOR="#dddddd" STYLE="border-top: dashed 1px black;"><i><% $footer %></i></TH>
511 % } 
512
513                 </TR>
514 % } 
515
516             
517             </TABLE>
518             <% $pager %>
519   
520           </TD>
521         </TR>
522       </TABLE>
523 % } 
524
525   <% defined($opt{'html_foot'}) 
526         ? ( ref($opt{'html_foot'})
527               ? &{$opt{'html_foot'}}()
528               : $opt{'html_foot'}
529           )
530         : ''
531   %>
532   <% include( '/elements/footer.html' ) %>
533 % } 
534 % } 
535