2 % # (everything not commented required is optional)
4 % # # basic options, required
5 % # 'title' => 'Page title',
7 % # 'name_singular' => 'item', #singular name for the records returned
8 % # #OR# # (preferred, will be pluralized automatically)
9 % # 'name' => 'items', #plural name for the records returned
10 % # # (deprecated, will be singularlized
13 % # # some HTML callbacks...
14 % # 'menubar' => '', #menubar arrayref
15 % # 'html_init' => '', #after the header/menubar and before the pager
16 % # 'html_foot' => '', #at the bottom
17 % # 'html_posttotal' => '', #at the bottom
18 % # # (these three can be strings or coderefs)
21 % # #literal SQL query string or qsearch hashref, required
23 % # 'table' => 'tablename',
24 % # #everything else is optional...
25 % # 'hashref' => { 'field' => 'value',
26 % # 'field' => { 'op' => '<',
31 % # 'addl_from' => '', #'LEFT JOIN othertable USING ( key )',
32 % # 'extra_sql' => '', #'AND otherstuff', #'WHERE onlystuff',
36 % # # "select * from tablename";
38 % # #required unless 'query' is an SQL query string (shouldn't be...)
39 % # 'count_query' => 'SELECT COUNT(*) FROM tablename',
41 % # 'count_addl' => [], #additional count fields listref of sprintf strings
42 % # # [ $money_char.'%.2f total paid', ],
44 % # #listref of column labels, <TH>
45 % # #required unless 'query' is an SQL query string
46 % # # (if not specified the database column names will be used)
47 % # 'header' => [ '#', 'Item' ],
49 % # 'disable_download' => '', # set true to hide the CSV/Excel download links
50 % # 'disable_nonefound' => '', # set true to disable the "No matching Xs found"
53 % # #listref - each item is a literal column name (or method) or coderef
54 % # #if not specified all columns will be shown
57 % # sub { my $row = shift; $row->column; },
60 % # #listref of column footers
63 % # #listref - each item is the empty string, or a listref of ...
67 % # 'align' => 'lrc.', #one letter for each column, left/right/center/none
68 % # # can also pass a listref with full values:
69 % # # [ 'left', 'right', 'center', '' ]
72 % # #currently only HTML, maybe eventually Excel too
77 % # #redirect if there's only one item...
78 % # # listref of URL base and column name (or method)
79 % # # or a coderef that returns the same
85 % #warn join(' / ', map { "$_ => $opt{$_}" } keys %opt ). "\n";
94 % $opt{align} = [ map $align{$_}, split(//, $opt{align}) ],
95 % unless !$opt{align} || ref($opt{align});
99 % my($confmax, $maxrecords, $total, $offset, $count_arrayref);
101 % if ( $cgi->param('_type') =~ /^(csv|\w*\.xls)$/ ) {
105 % } else { #setup some pagination things if we're in html mode
107 % unless (exists($opt{'count_query'}) && length($opt{'count_query'})) {
108 % ( $opt{'count_query'} = $opt{'query'} ) =~
109 % s/^\s*SELECT\s*(.*?)\s+FROM\s/SELECT COUNT(*) FROM /i; #silly vim:/
112 % my $conf = new FS::Conf;
113 % $confmax = $conf->config('maxsearchrecordsperpage');
114 % if ( $cgi->param('maxrecords') =~ /^(\d+)$/ ) {
117 % $maxrecords ||= $confmax;
120 % $limit = $maxrecords ? "LIMIT $maxrecords" : '';
122 % $offset = $cgi->param('offset') || 0;
123 % $limit .= " OFFSET $offset" if $offset;
125 % my $count_sth = dbh->prepare($opt{'count_query'})
126 % or die "Error preparing $opt{'count_query'}: ". dbh->errstr;
127 % $count_sth->execute
128 % or die "Error executing $opt{'count_query'}: ". $count_sth->errstr;
129 % $count_arrayref = $count_sth->fetchrow_arrayref;
130 % $total = $count_arrayref->[0];
136 % my $header = $opt{'header'};
138 % if ( ref($opt{'query'}) ) {
140 % #eval "use FS::$opt{'query'};";
142 % $opt{'query'}->{'table'},
143 % $opt{'query'}->{'hashref'} || {},
144 % $opt{'query'}->{'select'},
145 % $opt{'query'}->{'extra_sql'}. " $limit",
147 % (exists($opt{'query'}->{'addl_from'}) ? $opt{'query'}->{'addl_from'} : '')
152 % my $sth = dbh->prepare("$opt{'query'} $limit")
153 % or die "Error preparing $opt{'query'}: ". dbh->errstr;
155 % or die "Error executing $opt{'query'}: ". $sth->errstr;
157 % #can get # of rows without fetching them all?
158 % $rows = $sth->fetchall_arrayref;
160 % $header ||= $sth->{NAME};
164 % warn scalar(@$rows). ' rows returned from '.
165 % ( ref($opt{'query'}) ? 'qsearch query' : 'literal SQL query' )
166 % if $DEBUG || $opt{'debug'};
168 % # display the results - csv, xls or html
170 % if ( $type eq 'csv' ) {
172 % #http_header('Content-Type' => 'text/comma-separated-values' ); #IE chokes
173 % http_header('Content-Type' => 'text/plain' );
175 % my $csv = new Text::CSV_XS { 'always_quote' => 1,
176 % 'eol' => "\n", #"\015\012", #"\012"
179 % $csv->combine(@$header); #or die $csv->status;
184 % foreach my $row ( @$rows ) {
186 % if ( $opt{'fields'} ) {
190 % foreach my $field ( @{$opt{'fields'}} ) {
191 % if ( ref($field) eq 'CODE' ) {
194 % ? '(N/A)' #unimplemented
199 % push @line, $row->$field();
203 % $csv->combine(@line); #or die $csv->status;
206 % $csv->combine(@$row); #or die $csv->status;
215 % #} elsif ( $type eq 'excel' ) {
216 % } elsif ( $type =~ /\.xls$/ ) {
218 % #http_header('Content-Type' => 'application/excel' ); #eww
219 % http_header('Content-Type' => 'application/vnd.ms-excel' );
220 % #http_header('Content-Type' => 'application/msexcel' ); #alas
223 % my $XLS = new IO::Scalar \$data;
224 % my $workbook = Spreadsheet::WriteExcel->new($XLS)
225 % or die "Error opening .xls file: $!";
227 % my $worksheet = $workbook->add_worksheet(substr($opt{'title'},0,31));
231 % $worksheet->write($r, $c++, $_) foreach @$header;
233 % foreach my $row ( @$rows ) {
237 % if ( $opt{'fields'} ) {
239 % #my $links = $opt{'links'} ? [ @{$opt{'links'}} ] : '';
240 % #my $aligns = $opt{'align'} ? [ @{$opt{'align'}} ] : '';
242 % foreach my $field ( @{$opt{'fields'}} ) {
243 % #my $align = $aligns ? shift @$aligns : '';
244 % #$align = " ALIGN=$align" if $align;
247 % # my $link = shift @$links;
248 % # $link = &{$link}($row) if ref($link) eq 'CODE';
250 % # my( $url, $method ) = @{$link};
251 % # if ( ref($method) eq 'CODE' ) {
252 % # $a = $url. &{$method}($row);
254 % # $a = $url. $row->$method();
256 % # $a = qq(<A HREF="$a">);
259 % if ( ref($field) eq 'CODE' ) {
260 % foreach my $value ( &{$field}($row) ) {
261 % if ( ref($value) eq 'ARRAY' ) {
262 % $worksheet->write($r, $c++, '(N/A)' ); #unimplemented
264 % $worksheet->write($r, $c++, $value );
268 % $worksheet->write($r, $c++, $row->$field() );
273 % $worksheet->write($r, $c++, $_) foreach @$row;
278 % $workbook->close();# or die "Error creating .xls file: $!";
280 % http_header('Content-Length' => length($data) );
285 % } else { # regular HTML
287 % if ( exists($opt{'redirect'}) && scalar(@$rows) == 1 && $total == 1 ) {
288 % my $redirect = $opt{'redirect'};
289 % $redirect = &{$redirect}($rows->[0]) if ref($redirect) eq 'CODE';
290 % my( $url, $method ) = @$redirect;
291 % redirect( $url. $rows->[0]->$method() );
293 % if ( $opt{'name_singular'} ) {
294 % $opt{'name'} = PL($opt{'name_singular'});
296 % ( my $xlsname = $opt{'name'} ) =~ s/\W//g;
297 % if ( $total == 1 ) {
298 % if ( $opt{'name_singular'} ) {
299 % $opt{'name'} = $opt{'name_singular'}
301 % #$opt{'name'} =~ s/s$// if $total == 1;
302 % $opt{'name'} =~ s/((s)e)?s$/$2/ if $total == 1;
307 % if ( $opt{'menubar'} ) {
308 % @menubar = @{ $opt{'menubar'} };
310 % @menubar = ( 'Main menu' => $p );
316 <% include( '/elements/header.html', $opt{'title'},
317 include( '/elements/menubar.html', @menubar )
320 <% defined($opt{'html_init'})
321 ? ( ref($opt{'html_init'})
322 ? &{$opt{'html_init'}}()
328 % unless ( $total ) {
329 % unless ( $opt{'disable_nonefound'} ) {
331 No matching <% $opt{'name'} %> found.<BR>
342 <% $total %> total <% $opt{'name'} %>
344 % if ( $confmax && $total > $confmax ) {
345 % $cgi->delete('maxrecords');
346 % $cgi->param('_dummy', 1);
348 %# ( show <SELECT NAME="maxrecords" onChange="this.form.submit();">
349 ( show <SELECT NAME="maxrecords" onChange="window.location = '<% $cgi->self_url %>;maxrecords=' + this.options[this.selectedIndex].value;">
351 % foreach my $max ( map { $_ * $confmax } qw( 1 5 10 25 ) ) {
352 <OPTION VALUE="<% $max %>" <% ( $maxrecords == $max ) ? 'SELECTED' : '' %>><% $max %></OPTION>
357 % $cgi->param('maxrecords', $maxrecords);
361 <% defined($opt{'html_posttotal'})
362 ? ( ref($opt{'html_posttotal'})
363 ? &{$opt{'html_posttotal'}}()
364 : $opt{'html_posttotal'}
370 % if ( $opt{'count_addl'} ) {
371 % my $n=0; foreach my $count ( @{$opt{'count_addl'}} ) {
373 <% sprintf( $count, $count_arrayref->[++$n] ) %><BR>
381 % unless ( $opt{'disable_download'} ) {
384 % $cgi->param('_type', "$xlsname.xls" );
386 Download full results<BR>
387 as <A HREF="<% $cgi->self_url %>">Excel spreadsheet</A><BR>
388 % $cgi->param('_type', 'csv');
390 as <A HREF="<% $cgi->self_url %>">CSV file</A>
392 % $cgi->param('_type', "html" );
399 <% my $pager = include ( '/elements/pager.html',
401 'num_rows' => scalar(@$rows),
403 'maxrecords' => $maxrecords,
406 <% include('/elements/table-grid.html') %>
410 % foreach my $header ( @$header ) {
412 <TH CLASS="grid" BGCOLOR="#cccccc"><% $header %></TH>
416 % my $bgcolor1 = '#eeeeee';
417 % my $bgcolor2 = '#ffffff';
419 % foreach my $row ( @$rows ) {
420 % if ( $bgcolor eq $bgcolor1 ) {
421 % $bgcolor = $bgcolor2;
423 % $bgcolor = $bgcolor1;
428 % if ( $opt{'fields'} ) {
430 % my $links = $opt{'links'} ? [ @{$opt{'links'}} ] : '';
431 % my $aligns = $opt{'align'} ? [ @{$opt{'align'}} ] : '';
432 % my $colors = $opt{'color'} ? [ @{$opt{'color'}} ] : [];
433 % my $sizes = $opt{'size'} ? [ @{$opt{'size'}} ] : [];
434 % my $styles = $opt{'style'} ? [ @{$opt{'style'}} ] : [];
436 % foreach my $field (
439 % if ( ref($_) eq 'ARRAY' ) {
443 % '<TABLE CLASS="inv" CELLSPACING=0 CELLPADDING=0>'.
456 % ( $element->{'align'}
457 % ? ' ALIGN="'. $element->{'align'}. '"'
460 % ( $element->{'link'}
461 % ? '<A HREF="'. $element->{'link'}.'">'
464 % $element->{'data'}.
465 % ( $element->{'link'}
484 % if ( ref($_) eq 'CODE' ) {
494 % my $class = ( $field =~ /^<TABLE/i ) ? 'inv' : 'grid';
496 % my $align = $aligns ? shift @$aligns : '';
497 % $align = " ALIGN=$align" if $align;
501 % my $link = shift @$links;
502 % $link = &{$link}($row) if ref($link) eq 'CODE';
504 % my( $url, $method ) = @{$link};
505 % if ( ref($method) eq 'CODE' ) {
506 % $a = $url. &{$method}($row);
508 % $a = $url. $row->$method();
510 % $a = qq(<A HREF="$a">);
515 % my $color = shift @$colors;
516 % $color = &{$color}($row) if ref($color) eq 'CODE';
517 % my $size = shift @$sizes;
518 % $size = &{$size}($row) if ref($size) eq 'CODE';
519 % if ( $color || $size ) {
521 % ( $color ? "COLOR=#$color " : '' ).
522 % ( $size ? qq(SIZE="$size" ) : '' ).
526 % my($s, $es) = ( '', '' );
527 % my $style = shift @$styles;
528 % $style = &{$style}($row) if ref($style) eq 'CODE';
530 % $s = join( '', map "<$_>", split('', $style) );
531 % $es = join( '', map "</$_>", split('', $style) );
536 <TD CLASS="<% $class %>" BGCOLOR="<% $bgcolor %>"<% $align %>><% $font %><% $a %><% $s %><% $field %><% $es %><% $a ? '</A>' : '' %><% $font ? '</FONT>' : '' %></TD>
539 % foreach ( @$row ) {
541 <TD CLASS="grid" BGCOLOR="<% $bgcolor %>"><% $_ %></TD>
547 % if ( $opt{'footer'} ) {
550 % foreach my $footer ( @{ $opt{'footer'} } ) {
552 <TD CLASS="grid" BGCOLOR="#dddddd" STYLE="border-top: dashed 1px black;"><i><% $footer %></i></TH>
567 <% defined($opt{'html_foot'})
568 ? ( ref($opt{'html_foot'})
569 ? &{$opt{'html_foot'}}()
574 <% include( '/elements/footer.html' ) %>