4 % # (everything not commented required is optional)
6 % # # basic options, required
7 % # 'title' => 'Page title',
8 % # 'name' => 'items', #name for the records returned
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)
18 % # #literal SQL query string or qsearch hashref, required
20 % # 'table' => 'tablename',
21 % # #everything else is optional...
22 % # 'hashref' => { 'field' => 'value',
23 % # 'field' => { 'op' => '<',
28 % # 'addl_from' => '', #'LEFT JOIN othertable USING ( key )',
29 % # 'extra_sql' => '', #'AND otherstuff', #'WHERE onlystuff',
33 % # # "select * from tablename";
35 % # #required unless 'query' is an SQL query string (shouldn't be...)
36 % # 'count_query' => 'SELECT COUNT(*) FROM tablename',
38 % # 'count_addl' => [], #additional count fields listref of sprintf strings
39 % # # [ $money_char.'%.2f total paid', ],
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' ],
46 % # 'disable_download' => '', # set true to hide the CSV/Excel download links
47 % # 'disable_nonefound' => '', # set true to disable the "No matching Xs found"
50 % # #listref - each item is a literal column name (or method) or coderef
51 % # #if not specified all columns will be shown
54 % # sub { my $row = shift; $row->column; },
57 % # #listref of column footers
60 % # #listref - each item is the empty string, or a listref of ...
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', '' ]
69 % # #currently only HTML, maybe eventually Excel too
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
82 % #warn join(' / ', map { "$_ => $opt{$_}" } keys %opt ). "\n";
91 % $opt{align} = [ map $align{$_}, split(//, $opt{align}) ],
92 % unless !$opt{align} || ref($opt{align});
96 % my($maxrecords, $total, $offset, $count_arrayref);
98 % if ( $cgi->param('_type') =~ /^(csv|\w*\.xls)$/ ) {
102 % } else { #setup some pagination things if we're in html mode
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;
109 % my $conf = new FS::Conf;
110 % $maxrecords = $conf->config('maxsearchrecordsperpage');
112 % $limit = $maxrecords ? "LIMIT $maxrecords" : '';
114 % $offset = $cgi->param('offset') || 0;
115 % $limit .= " OFFSET $offset" if $offset;
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];
128 % my $header = $opt{'header'};
130 % if ( ref($opt{'query'}) ) {
132 % #eval "use FS::$opt{'query'};";
134 % $opt{'query'}->{'table'},
135 % $opt{'query'}->{'hashref'} || {},
136 % $opt{'query'}->{'select'},
137 % $opt{'query'}->{'extra_sql'}. " $limit",
139 % (exists($opt{'query'}->{'addl_from'}) ? $opt{'query'}->{'addl_from'} : '')
144 % my $sth = dbh->prepare("$opt{'query'} $limit")
145 % or die "Error preparing $opt{'query'}: ". dbh->errstr;
147 % or die "Error executing $opt{'query'}: ". $sth->errstr;
149 % #can get # of rows without fetching them all?
150 % $rows = $sth->fetchall_arrayref;
152 % $header ||= $sth->{NAME};
156 % warn scalar(@$rows). ' rows returned from '.
157 % ( ref($opt{'query'}) ? 'qsearch query' : 'literal SQL query' )
158 % if $DEBUG || $opt{'debug'};
160 % # display the results - csv, xls or html
162 % if ( $type eq 'csv' ) {
164 % #http_header('Content-Type' => 'text/comma-separated-values' ); #IE chokes
165 % http_header('Content-Type' => 'text/plain' );
167 % my $csv = new Text::CSV_XS { 'always_quote' => 1,
168 % 'eol' => "\n", #"\015\012", #"\012"
171 % $csv->combine(@$header); #or die $csv->status;
176 % foreach my $row ( @$rows ) {
178 % if ( $opt{'fields'} ) {
182 % foreach my $field ( @{$opt{'fields'}} ) {
183 % if ( ref($field) eq 'CODE' ) {
186 % ? '(N/A)' #unimplemented
191 % push @line, $row->$field();
195 % $csv->combine(@line); #or die $csv->status;
198 % $csv->combine(@$row); #or die $csv->status;
207 % #} elsif ( $type eq 'excel' ) {
208 % } elsif ( $type =~ /\.xls$/ ) {
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
215 % my $XLS = new IO::Scalar \$data;
216 % my $workbook = Spreadsheet::WriteExcel->new($XLS)
217 % or die "Error opening .xls file: $!";
219 % my $worksheet = $workbook->add_worksheet(substr($opt{'title'},0,31));
223 % $worksheet->write($r, $c++, $_) foreach @$header;
225 % foreach my $row ( @$rows ) {
229 % if ( $opt{'fields'} ) {
231 % #my $links = $opt{'links'} ? [ @{$opt{'links'}} ] : '';
232 % #my $aligns = $opt{'align'} ? [ @{$opt{'align'}} ] : '';
234 % foreach my $field ( @{$opt{'fields'}} ) {
235 % #my $align = $aligns ? shift @$aligns : '';
236 % #$align = " ALIGN=$align" if $align;
239 % # my $link = shift @$links;
240 % # $link = &{$link}($row) if ref($link) eq 'CODE';
242 % # my( $url, $method ) = @{$link};
243 % # if ( ref($method) eq 'CODE' ) {
244 % # $a = $url. &{$method}($row);
246 % # $a = $url. $row->$method();
248 % # $a = qq(<A HREF="$a">);
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
256 % $worksheet->write($r, $c++, $value );
260 % $worksheet->write($r, $c++, $row->$field() );
265 % $worksheet->write($r, $c++, $_) foreach @$row;
270 % $workbook->close();# or die "Error creating .xls file: $!";
272 % http_header('Content-Length' => length($data) );
277 % } else { # regular HTML
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() );
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
291 % if ( $opt{'menubar'} ) {
292 % @menubar = @{ $opt{'menubar'} };
294 % @menubar = ( 'Main menu' => $p );
300 <% include( '/elements/header.html', $opt{'title'},
301 include( '/elements/menubar.html', @menubar )
304 <% defined($opt{'html_init'})
305 ? ( ref($opt{'html_init'})
306 ? &{$opt{'html_init'}}()
311 % my $pager = include ( '/elements/pager.html',
312 % 'offset' => $offset,
313 % 'num_rows' => scalar(@$rows),
315 % 'maxrecords' => $maxrecords,
318 % unless ( $total ) {
319 % unless ( $opt{'disable_nonefound'} ) {
321 No matching <% $opt{'name'} %> found.<BR>
329 <% $total %> total <% $opt{'name'} %>
330 <% defined($opt{'html_posttotal'})
331 ? ( ref($opt{'html_posttotal'})
332 ? &{$opt{'html_posttotal'}}()
333 : $opt{'html_posttotal'}
338 % if ( $opt{'count_addl'} ) {
339 % my $n=0; foreach my $count ( @{$opt{'count_addl'}} ) {
341 <% sprintf( $count, $count_arrayref->[++$n] ) %><BR>
346 % unless ( $opt{'disable_download'} ) {
349 % $cgi->param('_type', "$xlsname.xls" );
351 Download full results<BR>
352 as <A HREF="<% $cgi->self_url %>">Excel spreadsheet</A><BR>
353 % $cgi->param('_type', 'csv');
355 as <A HREF="<% $cgi->self_url %>">CSV file</A>
364 <% include('/elements/table-grid.html') %>
368 % foreach my $header ( @$header ) {
370 <TH CLASS="grid" BGCOLOR="#cccccc"><% $header %></TH>
374 % my $bgcolor1 = '#eeeeee';
375 % my $bgcolor2 = '#ffffff';
377 % foreach my $row ( @$rows ) {
378 % if ( $bgcolor eq $bgcolor1 ) {
379 % $bgcolor = $bgcolor2;
381 % $bgcolor = $bgcolor1;
386 % if ( $opt{'fields'} ) {
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'}} ] : [];
394 % foreach my $field (
397 % if ( ref($_) eq 'ARRAY' ) {
401 % '<TABLE CLASS="inv" CELLSPACING=0 CELLPADDING=0>'.
414 % ( $element->{'align'}
415 % ? ' ALIGN="'. $element->{'align'}. '"'
418 % ( $element->{'link'}
419 % ? '<A HREF="'. $element->{'link'}.'">'
422 % $element->{'data'}.
423 % ( $element->{'link'}
442 % if ( ref($_) eq 'CODE' ) {
452 % my $class = ( $field =~ /^<TABLE/i ) ? 'inv' : 'grid';
454 % my $align = $aligns ? shift @$aligns : '';
455 % $align = " ALIGN=$align" if $align;
459 % my $link = shift @$links;
460 % $link = &{$link}($row) if ref($link) eq 'CODE';
462 % my( $url, $method ) = @{$link};
463 % if ( ref($method) eq 'CODE' ) {
464 % $a = $url. &{$method}($row);
466 % $a = $url. $row->$method();
468 % $a = qq(<A HREF="$a">);
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 ) {
479 % ( $color ? "COLOR=#$color " : '' ).
480 % ( $size ? qq(SIZE="$size" ) : '' ).
484 % my($s, $es) = ( '', '' );
485 % my $style = shift @$styles;
486 % $style = &{$style}($row) if ref($style) eq 'CODE';
488 % $s = join( '', map "<$_>", split('', $style) );
489 % $es = join( '', map "</$_>", split('', $style) );
494 <TD CLASS="<% $class %>" BGCOLOR="<% $bgcolor %>"<% $align %>><% $font %><% $a %><% $s %><% $field %><% $es %><% $a ? '</A>' : '' %><% $font ? '</FONT>' : '' %></TD>
497 % foreach ( @$row ) {
499 <TD CLASS="grid" BGCOLOR="<% $bgcolor %>"><% $_ %></TD>
505 % if ( $opt{'footer'} ) {
508 % foreach my $footer ( @{ $opt{'footer'} } ) {
510 <TD CLASS="grid" BGCOLOR="#dddddd" STYLE="border-top: dashed 1px black;"><i><% $footer %></i></TH>
525 <% defined($opt{'html_foot'})
526 ? ( ref($opt{'html_foot'})
527 ? &{$opt{'html_foot'}}()
532 <% include( '/elements/footer.html' ) %>