4 % # (everything not commented required is optional)
6 % # # basic options, required
7 % # 'title' => 'Page title',
9 % # 'name_singular' => 'item', #singular name for the records returned
10 % # #OR# # (preferred, will be pluralized automatically)
11 % # 'name' => 'items', #plural name for the records returned
12 % # # (deprecated, will be singularlized
15 % # # some HTML callbacks...
16 % # 'menubar' => '', #menubar arrayref
17 % # 'html_init' => '', #after the header/menubar and before the pager
18 % # 'html_foot' => '', #at the bottom
19 % # 'html_posttotal' => '', #at the bottom
20 % # # (these three can be strings or coderefs)
23 % # #literal SQL query string or qsearch hashref, required
25 % # 'table' => 'tablename',
26 % # #everything else is optional...
27 % # 'hashref' => { 'field' => 'value',
28 % # 'field' => { 'op' => '<',
33 % # 'addl_from' => '', #'LEFT JOIN othertable USING ( key )',
34 % # 'extra_sql' => '', #'AND otherstuff', #'WHERE onlystuff',
38 % # # "select * from tablename";
40 % # #required unless 'query' is an SQL query string (shouldn't be...)
41 % # 'count_query' => 'SELECT COUNT(*) FROM tablename',
43 % # 'count_addl' => [], #additional count fields listref of sprintf strings
44 % # # [ $money_char.'%.2f total paid', ],
46 % # #listref of column labels, <TH>
47 % # #required unless 'query' is an SQL query string
48 % # # (if not specified the database column names will be used)
49 % # 'header' => [ '#', 'Item' ],
51 % # 'disable_download' => '', # set true to hide the CSV/Excel download links
52 % # 'disable_nonefound' => '', # set true to disable the "No matching Xs found"
55 % # #listref - each item is a literal column name (or method) or coderef
56 % # #if not specified all columns will be shown
59 % # sub { my $row = shift; $row->column; },
62 % # #listref of column footers
65 % # #listref - each item is the empty string, or a listref of ...
69 % # 'align' => 'lrc.', #one letter for each column, left/right/center/none
70 % # # can also pass a listref with full values:
71 % # # [ 'left', 'right', 'center', '' ]
74 % # #currently only HTML, maybe eventually Excel too
79 % # #redirect if there's only one item...
80 % # # listref of URL base and column name (or method)
81 % # # or a coderef that returns the same
87 % #warn join(' / ', map { "$_ => $opt{$_}" } keys %opt ). "\n";
96 % $opt{align} = [ map $align{$_}, split(//, $opt{align}) ],
97 % unless !$opt{align} || ref($opt{align});
101 % my($maxrecords, $total, $offset, $count_arrayref);
103 % if ( $cgi->param('_type') =~ /^(csv|\w*\.xls)$/ ) {
107 % } else { #setup some pagination things if we're in html mode
109 % unless (exists($opt{'count_query'}) && length($opt{'count_query'})) {
110 % ( $opt{'count_query'} = $opt{'query'} ) =~
111 % s/^\s*SELECT\s*(.*?)\s+FROM\s/SELECT COUNT(*) FROM /i;
114 % my $conf = new FS::Conf;
115 % $maxrecords = $conf->config('maxsearchrecordsperpage');
117 % $limit = $maxrecords ? "LIMIT $maxrecords" : '';
119 % $offset = $cgi->param('offset') || 0;
120 % $limit .= " OFFSET $offset" if $offset;
122 % my $count_sth = dbh->prepare($opt{'count_query'})
123 % or die "Error preparing $opt{'count_query'}: ". dbh->errstr;
124 % $count_sth->execute
125 % or die "Error executing $opt{'count_query'}: ". $count_sth->errstr;
126 % $count_arrayref = $count_sth->fetchrow_arrayref;
127 % $total = $count_arrayref->[0];
133 % my $header = $opt{'header'};
135 % if ( ref($opt{'query'}) ) {
137 % #eval "use FS::$opt{'query'};";
139 % $opt{'query'}->{'table'},
140 % $opt{'query'}->{'hashref'} || {},
141 % $opt{'query'}->{'select'},
142 % $opt{'query'}->{'extra_sql'}. " $limit",
144 % (exists($opt{'query'}->{'addl_from'}) ? $opt{'query'}->{'addl_from'} : '')
149 % my $sth = dbh->prepare("$opt{'query'} $limit")
150 % or die "Error preparing $opt{'query'}: ". dbh->errstr;
152 % or die "Error executing $opt{'query'}: ". $sth->errstr;
154 % #can get # of rows without fetching them all?
155 % $rows = $sth->fetchall_arrayref;
157 % $header ||= $sth->{NAME};
161 % warn scalar(@$rows). ' rows returned from '.
162 % ( ref($opt{'query'}) ? 'qsearch query' : 'literal SQL query' )
163 % if $DEBUG || $opt{'debug'};
165 % # display the results - csv, xls or html
167 % if ( $type eq 'csv' ) {
169 % #http_header('Content-Type' => 'text/comma-separated-values' ); #IE chokes
170 % http_header('Content-Type' => 'text/plain' );
172 % my $csv = new Text::CSV_XS { 'always_quote' => 1,
173 % 'eol' => "\n", #"\015\012", #"\012"
176 % $csv->combine(@$header); #or die $csv->status;
181 % foreach my $row ( @$rows ) {
183 % if ( $opt{'fields'} ) {
187 % foreach my $field ( @{$opt{'fields'}} ) {
188 % if ( ref($field) eq 'CODE' ) {
191 % ? '(N/A)' #unimplemented
196 % push @line, $row->$field();
200 % $csv->combine(@line); #or die $csv->status;
203 % $csv->combine(@$row); #or die $csv->status;
212 % #} elsif ( $type eq 'excel' ) {
213 % } elsif ( $type =~ /\.xls$/ ) {
215 % #http_header('Content-Type' => 'application/excel' ); #eww
216 % http_header('Content-Type' => 'application/vnd.ms-excel' );
217 % #http_header('Content-Type' => 'application/msexcel' ); #alas
220 % my $XLS = new IO::Scalar \$data;
221 % my $workbook = Spreadsheet::WriteExcel->new($XLS)
222 % or die "Error opening .xls file: $!";
224 % my $worksheet = $workbook->add_worksheet(substr($opt{'title'},0,31));
228 % $worksheet->write($r, $c++, $_) foreach @$header;
230 % foreach my $row ( @$rows ) {
234 % if ( $opt{'fields'} ) {
236 % #my $links = $opt{'links'} ? [ @{$opt{'links'}} ] : '';
237 % #my $aligns = $opt{'align'} ? [ @{$opt{'align'}} ] : '';
239 % foreach my $field ( @{$opt{'fields'}} ) {
240 % #my $align = $aligns ? shift @$aligns : '';
241 % #$align = " ALIGN=$align" if $align;
244 % # my $link = shift @$links;
245 % # $link = &{$link}($row) if ref($link) eq 'CODE';
247 % # my( $url, $method ) = @{$link};
248 % # if ( ref($method) eq 'CODE' ) {
249 % # $a = $url. &{$method}($row);
251 % # $a = $url. $row->$method();
253 % # $a = qq(<A HREF="$a">);
256 % if ( ref($field) eq 'CODE' ) {
257 % foreach my $value ( &{$field}($row) ) {
258 % if ( ref($value) eq 'ARRAY' ) {
259 % $worksheet->write($r, $c++, '(N/A)' ); #unimplemented
261 % $worksheet->write($r, $c++, $value );
265 % $worksheet->write($r, $c++, $row->$field() );
270 % $worksheet->write($r, $c++, $_) foreach @$row;
275 % $workbook->close();# or die "Error creating .xls file: $!";
277 % http_header('Content-Length' => length($data) );
282 % } else { # regular HTML
284 % if ( exists($opt{'redirect'}) && scalar(@$rows) == 1 && $total == 1 ) {
285 % my $redirect = $opt{'redirect'};
286 % $redirect = &{$redirect}($rows->[0]) if ref($redirect) eq 'CODE';
287 % my( $url, $method ) = @$redirect;
288 % redirect( $url. $rows->[0]->$method() );
290 % if ( $opt{'name_singular'} ) {
291 % $opt{'name'} = PL($opt{'name_singular'});
293 % ( my $xlsname = $opt{'name'} ) =~ s/\W//g;
294 % if ( $total == 1 ) {
295 % if ( $opt{'name_singular'} ) {
296 % $opt{'name'} = $opt{'name_singular'}
298 % #$opt{'name'} =~ s/s$// if $total == 1;
299 % $opt{'name'} =~ s/((s)e)?s$/$2/ if $total == 1;
304 % if ( $opt{'menubar'} ) {
305 % @menubar = @{ $opt{'menubar'} };
307 % @menubar = ( 'Main menu' => $p );
313 <% include( '/elements/header.html', $opt{'title'},
314 include( '/elements/menubar.html', @menubar )
317 <% defined($opt{'html_init'})
318 ? ( ref($opt{'html_init'})
319 ? &{$opt{'html_init'}}()
324 % my $pager = include ( '/elements/pager.html',
325 % 'offset' => $offset,
326 % 'num_rows' => scalar(@$rows),
328 % 'maxrecords' => $maxrecords,
331 % unless ( $total ) {
332 % unless ( $opt{'disable_nonefound'} ) {
334 No matching <% $opt{'name'} %> found.<BR>
342 <% $total %> total <% $opt{'name'} %>
343 <% defined($opt{'html_posttotal'})
344 ? ( ref($opt{'html_posttotal'})
345 ? &{$opt{'html_posttotal'}}()
346 : $opt{'html_posttotal'}
351 % if ( $opt{'count_addl'} ) {
352 % my $n=0; foreach my $count ( @{$opt{'count_addl'}} ) {
354 <% sprintf( $count, $count_arrayref->[++$n] ) %><BR>
359 % unless ( $opt{'disable_download'} ) {
362 % $cgi->param('_type', "$xlsname.xls" );
364 Download full results<BR>
365 as <A HREF="<% $cgi->self_url %>">Excel spreadsheet</A><BR>
366 % $cgi->param('_type', 'csv');
368 as <A HREF="<% $cgi->self_url %>">CSV file</A>
377 <% include('/elements/table-grid.html') %>
381 % foreach my $header ( @$header ) {
383 <TH CLASS="grid" BGCOLOR="#cccccc"><% $header %></TH>
387 % my $bgcolor1 = '#eeeeee';
388 % my $bgcolor2 = '#ffffff';
390 % foreach my $row ( @$rows ) {
391 % if ( $bgcolor eq $bgcolor1 ) {
392 % $bgcolor = $bgcolor2;
394 % $bgcolor = $bgcolor1;
399 % if ( $opt{'fields'} ) {
401 % my $links = $opt{'links'} ? [ @{$opt{'links'}} ] : '';
402 % my $aligns = $opt{'align'} ? [ @{$opt{'align'}} ] : '';
403 % my $colors = $opt{'color'} ? [ @{$opt{'color'}} ] : [];
404 % my $sizes = $opt{'size'} ? [ @{$opt{'size'}} ] : [];
405 % my $styles = $opt{'style'} ? [ @{$opt{'style'}} ] : [];
407 % foreach my $field (
410 % if ( ref($_) eq 'ARRAY' ) {
414 % '<TABLE CLASS="inv" CELLSPACING=0 CELLPADDING=0>'.
427 % ( $element->{'align'}
428 % ? ' ALIGN="'. $element->{'align'}. '"'
431 % ( $element->{'link'}
432 % ? '<A HREF="'. $element->{'link'}.'">'
435 % $element->{'data'}.
436 % ( $element->{'link'}
455 % if ( ref($_) eq 'CODE' ) {
465 % my $class = ( $field =~ /^<TABLE/i ) ? 'inv' : 'grid';
467 % my $align = $aligns ? shift @$aligns : '';
468 % $align = " ALIGN=$align" if $align;
472 % my $link = shift @$links;
473 % $link = &{$link}($row) if ref($link) eq 'CODE';
475 % my( $url, $method ) = @{$link};
476 % if ( ref($method) eq 'CODE' ) {
477 % $a = $url. &{$method}($row);
479 % $a = $url. $row->$method();
481 % $a = qq(<A HREF="$a">);
486 % my $color = shift @$colors;
487 % $color = &{$color}($row) if ref($color) eq 'CODE';
488 % my $size = shift @$sizes;
489 % $size = &{$size}($row) if ref($size) eq 'CODE';
490 % if ( $color || $size ) {
492 % ( $color ? "COLOR=#$color " : '' ).
493 % ( $size ? qq(SIZE="$size" ) : '' ).
497 % my($s, $es) = ( '', '' );
498 % my $style = shift @$styles;
499 % $style = &{$style}($row) if ref($style) eq 'CODE';
501 % $s = join( '', map "<$_>", split('', $style) );
502 % $es = join( '', map "</$_>", split('', $style) );
507 <TD CLASS="<% $class %>" BGCOLOR="<% $bgcolor %>"<% $align %>><% $font %><% $a %><% $s %><% $field %><% $es %><% $a ? '</A>' : '' %><% $font ? '</FONT>' : '' %></TD>
510 % foreach ( @$row ) {
512 <TD CLASS="grid" BGCOLOR="<% $bgcolor %>"><% $_ %></TD>
518 % if ( $opt{'footer'} ) {
521 % foreach my $footer ( @{ $opt{'footer'} } ) {
523 <TD CLASS="grid" BGCOLOR="#dddddd" STYLE="border-top: dashed 1px black;"><i><% $footer %></i></TH>
538 <% defined($opt{'html_foot'})
539 ? ( ref($opt{'html_foot'})
540 ? &{$opt{'html_foot'}}()
545 <% include( '/elements/footer.html' ) %>