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;
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;
172 %><%= $csv->string %><%
174 foreach my $row ( @$rows ) {
176 if ( $opt{'fields'} ) {
180 foreach my $field ( @{$opt{'fields'}} ) {
181 if ( ref($field) eq 'CODE' ) {
184 ? '(N/A)' #unimplemented
189 push @line, $row->$field();
193 $csv->combine(@line); #or die $csv->status;
196 $csv->combine(@$row); #or die $csv->status;
199 %><%= $csv->string %><%
203 #} elsif ( $type eq 'excel' ) {
204 } elsif ( $type =~ /\.xls$/ ) {
206 #http_header('Content-Type' => 'application/excel' ); #eww
207 http_header('Content-Type' => 'application/vnd.ms-excel' );
208 #http_header('Content-Type' => 'application/msexcel' ); #alas
211 my $XLS = new IO::Scalar \$data;
212 my $workbook = Spreadsheet::WriteExcel->new($XLS)
213 or die "Error opening .xls file: $!";
215 my $worksheet = $workbook->add_worksheet(substr($opt{'title'},0,31));
219 $worksheet->write($r, $c++, $_) foreach @$header;
221 foreach my $row ( @$rows ) {
225 if ( $opt{'fields'} ) {
227 #my $links = $opt{'links'} ? [ @{$opt{'links'}} ] : '';
228 #my $aligns = $opt{'align'} ? [ @{$opt{'align'}} ] : '';
230 foreach my $field ( @{$opt{'fields'}} ) {
231 #my $align = $aligns ? shift @$aligns : '';
232 #$align = " ALIGN=$align" if $align;
235 # my $link = shift @$links;
236 # $link = &{$link}($row) if ref($link) eq 'CODE';
238 # my( $url, $method ) = @{$link};
239 # if ( ref($method) eq 'CODE' ) {
240 # $a = $url. &{$method}($row);
242 # $a = $url. $row->$method();
244 # $a = qq(<A HREF="$a">);
247 if ( ref($field) eq 'CODE' ) {
248 foreach my $value ( &{$field}($row) ) {
249 if ( ref($value) eq 'ARRAY' ) {
250 $worksheet->write($r, $c++, '(N/A)' ); #unimplemented
252 $worksheet->write($r, $c++, $value );
256 $worksheet->write($r, $c++, $row->$field() );
261 $worksheet->write($r, $c++, $_) foreach @$row;
266 $workbook->close();# or die "Error creating .xls file: $!";
268 http_header('Content-Length' => length($data) );
271 } else { # regular HTML
273 if ( exists($opt{'redirect'}) && scalar(@$rows) == 1 && $total == 1 ) {
274 my $redirect = $opt{'redirect'};
275 $redirect = &{$redirect}($rows->[0]) if ref($redirect) eq 'CODE';
276 my( $url, $method ) = @$redirect;
277 redirect( $url. $rows->[0]->$method() );
279 ( my $xlsname = $opt{'name'} ) =~ s/\W//g;
280 #$opt{'name'} =~ s/s$// if $total == 1;
281 $opt{'name'} =~ s/((s)e)?s$/$2/ if $total == 1; #should use Lingua::bs
285 if ( $opt{'menubar'} ) {
286 @menubar = @{ $opt{'menubar'} };
288 @menubar = ( 'Main menu' => $p );
293 <%= include( '/elements/header.html', $opt{'title'},
294 include( '/elements/menubar.html', @menubar )
297 <%= defined($opt{'html_init'})
298 ? ( ref($opt{'html_init'})
299 ? &{$opt{'html_init'}}()
304 <% my $pager = include ( '/elements/pager.html',
306 'num_rows' => scalar(@$rows),
308 'maxrecords' => $maxrecords,
311 <% unless ( $total ) { %>
312 <% unless ( $opt{'disable_nonefound'} ) { %>
313 No matching <%= $opt{'name'} %> found.<BR>
320 <%= $total %> total <%= $opt{'name'} %>
321 <%= defined($opt{'html_posttotal'})
322 ? ( ref($opt{'html_posttotal'})
323 ? &{$opt{'html_posttotal'}}()
324 : $opt{'html_posttotal'}
329 <% if ( $opt{'count_addl'} ) { %>
330 <% my $n=0; foreach my $count ( @{$opt{'count_addl'}} ) { %>
331 <%= sprintf( $count, $count_arrayref->[++$n] ) %><BR>
335 <% unless ( $opt{'disable_download'} ) { %>
337 <% $cgi->param('_type', "$xlsname.xls" ); %>
338 Download full results<BR>
339 as <A HREF="<%= $cgi->self_url %>">Excel spreadsheet</A><BR>
340 <% $cgi->param('_type', 'csv'); %>
341 as <A HREF="<%= $cgi->self_url %>">CSV file</A>
349 <%= include('/elements/table-grid.html') %>
353 foreach my $header ( @$header ) { %>
354 <TH CLASS="grid" BGCOLOR="#cccccc"><%= $header %></TH>
357 <% my $bgcolor1 = '#eeeeee';
358 my $bgcolor2 = '#ffffff';
360 foreach my $row ( @$rows ) {
361 if ( $bgcolor eq $bgcolor1 ) {
362 $bgcolor = $bgcolor2;
364 $bgcolor = $bgcolor1;
368 <% if ( $opt{'fields'} ) {
370 my $links = $opt{'links'} ? [ @{$opt{'links'}} ] : '';
371 my $aligns = $opt{'align'} ? [ @{$opt{'align'}} ] : '';
372 my $colors = $opt{'color'} ? [ @{$opt{'color'}} ] : [];
373 my $sizes = $opt{'size'} ? [ @{$opt{'size'}} ] : [];
374 my $styles = $opt{'style'} ? [ @{$opt{'style'}} ] : [];
379 if ( ref($_) eq 'ARRAY' ) {
383 '<TABLE CLASS="inv" CELLSPACING=0 CELLPADDING=0>'.
396 ( $element->{'align'}
397 ? ' ALIGN="'. $element->{'align'}. '"'
401 ? '<A HREF="'. $element->{'link'}.'">'
424 if ( ref($_) eq 'CODE' ) {
434 my $class = ( $field =~ /^<TABLE/i ) ? 'inv' : 'grid';
436 my $align = $aligns ? shift @$aligns : '';
437 $align = " ALIGN=$align" if $align;
441 my $link = shift @$links;
442 $link = &{$link}($row) if ref($link) eq 'CODE';
444 my( $url, $method ) = @{$link};
445 if ( ref($method) eq 'CODE' ) {
446 $a = $url. &{$method}($row);
448 $a = $url. $row->$method();
450 $a = qq(<A HREF="$a">);
455 my $color = shift @$colors;
456 $color = &{$color}($row) if ref($color) eq 'CODE';
457 my $size = shift @$sizes;
458 $size = &{$size}($row) if ref($size) eq 'CODE';
459 if ( $color || $size ) {
461 ( $color ? "COLOR=#$color " : '' ).
462 ( $size ? qq(SIZE="$size" ) : '' ).
466 my($s, $es) = ( '', '' );
467 my $style = shift @$styles;
468 $style = &{$style}($row) if ref($style) eq 'CODE';
470 $s = join( '', map "<$_>", split('', $style) );
471 $es = join( '', map "</$_>", split('', $style) );
475 <TD CLASS="<%= $class %>" BGCOLOR="<%= $bgcolor %>"<%= $align %>><%= $font %><%= $a %><%= $s %><%= $field %><%= $es %><%= $a ? '</A>' : '' %><%= $font ? '</FONT>' : '' %></TD>
478 <% foreach ( @$row ) { %>
479 <TD CLASS="grid" BGCOLOR="<%= $bgcolor %>"><%= $_ %></TD>
485 <% if ( $opt{'footer'} ) { %>
487 <% foreach my $footer ( @{ $opt{'footer'} } ) { %>
488 <TD CLASS="grid" BGCOLOR="#dddddd" STYLE="border-top: dashed 1px black;"><i><%= $footer %></i></TH>
501 <%= defined($opt{'html_foot'})
502 ? ( ref($opt{'html_foot'})
503 ? &{$opt{'html_foot'}}()
508 <%= include( '/elements/footer.html' ) %>