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
14 # #literal SQL query string or qsearch hashref, required
16 # 'table' => 'tablename',
17 # #everything else is optional...
18 # 'hashref' => { 'field' => 'value',
19 # 'field' => { 'op' => '<',
24 # 'addl_from' => '', #'LEFT JOIN othertable USING ( key )',
25 # 'extra_sql' => '', #'AND otherstuff', #'WHERE onlystuff',
29 # # "select * from tablename";
31 # #required unless 'query' is an SQL query string (shouldn't be...)
32 # 'count_query' => 'SELECT COUNT(*) FROM tablename',
34 # 'count_addl' => [], #additional count fields listref of sprintf strings
35 # # [ $money_char.'%.2f total paid', ],
37 # #listref of column labels, <TH>
38 # #required unless 'query' is an SQL query string
39 # # (if not specified the database column names will be used)
40 # 'header' => [ '#', 'Item' ],
42 # #listref - each item is a literal column name (or method) or coderef
43 # #if not specified all columns will be shown
46 # sub { my $row = shift; $row->column; },
49 # #listref of column footers
52 # #listref - each item is the empty string, or a listref of ...
56 # 'align' => 'lrc.', #one letter for each column, left/right/center/none
57 # # can also pass a listref with full values:
58 # # [ 'left', 'right', 'center', '' ]
61 # #currently only HTML, maybe eventually Excel too
66 # #redirect if there's only one item...
67 # # listref of URL base and column name (or method)
68 # # or a coderef that returns the same
74 #warn join(' / ', map { "$_ => $opt{$_}" } keys %opt ). "\n";
83 $opt{align} = [ map $align{$_}, split(//, $opt{align}) ],
84 unless !$opt{align} || ref($opt{align});
88 my($maxrecords, $total, $offset, $count_arrayref);
90 if ( $cgi->param('_type') =~ /^(csv|\w*\.xls)$/ ) {
94 } else { #setup some pagination things if we're in html mode
96 unless (exists($opt{'count_query'}) && length($opt{'count_query'})) {
97 ( $opt{'count_query'} = $opt{'query'} ) =~
98 s/^\s*SELECT\s*(.*?)\s+FROM\s/SELECT COUNT(*) FROM /i;
101 my $conf = new FS::Conf;
102 $maxrecords = $conf->config('maxsearchrecordsperpage');
104 $limit = $maxrecords ? "LIMIT $maxrecords" : '';
106 $offset = $cgi->param('offset') || 0;
107 $limit .= " OFFSET $offset" if $offset;
109 my $count_sth = dbh->prepare($opt{'count_query'})
110 or die "Error preparing $opt{'count_query'}: ". dbh->errstr;
112 or die "Error executing $opt{'count_query'}: ". $count_sth->errstr;
113 $count_arrayref = $count_sth->fetchrow_arrayref;
114 $total = $count_arrayref->[0];
120 my $header = $opt{'header'};
122 if ( ref($opt{'query'}) ) {
124 #eval "use FS::$opt{'query'};";
126 $opt{'query'}->{'table'},
127 $opt{'query'}->{'hashref'} || {},
128 $opt{'query'}->{'select'},
129 $opt{'query'}->{'extra_sql'}. " $limit",
131 (exists($opt{'query'}->{'addl_from'}) ? $opt{'query'}->{'addl_from'} : '')
136 my $sth = dbh->prepare("$opt{'query'} $limit")
137 or die "Error preparing $opt{'query'}: ". dbh->errstr;
139 or die "Error executing $opt{'query'}: ". $sth->errstr;
141 #can get # of rows without fetching them all?
142 $rows = $sth->fetchall_arrayref;
144 $header ||= $sth->{NAME};
148 warn scalar(@$rows). ' rows returned from '.
149 ( ref($opt{'query'}) ? 'qsearch query' : 'literal SQL query' )
150 if $DEBUG || $opt{'debug'};
152 # display the results - csv, xls or html
154 if ( $type eq 'csv' ) {
156 #http_header('Content-Type' => 'text/comma-separated-values' ); #IE chokes
157 http_header('Content-Type' => 'text/plain' );
159 my $csv = new Text::CSV_XS { 'always_quote' => 1,
160 'eol' => "\n", #"\015\012", #"\012"
163 $csv->combine(@$header); #or die $csv->status;
164 %><%= $csv->string %><%
166 foreach my $row ( @$rows ) {
168 if ( $opt{'fields'} ) {
172 foreach my $field ( @{$opt{'fields'}} ) {
173 if ( ref($field) eq 'CODE' ) {
176 ? '(N/A)' #unimplemented
181 push @line, $row->$field();
185 $csv->combine(@line); #or die $csv->status;
188 $csv->combine(@$row); #or die $csv->status;
191 %><%= $csv->string %><%
195 #} elsif ( $type eq 'excel' ) {
196 } elsif ( $type =~ /\.xls$/ ) {
198 #http_header('Content-Type' => 'application/excel' ); #eww
199 http_header('Content-Type' => 'application/vnd.ms-excel' );
200 #http_header('Content-Type' => 'application/msexcel' ); #alas
203 my $XLS = new IO::Scalar \$data;
204 my $workbook = Spreadsheet::WriteExcel->new($XLS)
205 or die "Error opening .xls file: $!";
207 my $worksheet = $workbook->add_worksheet(substr($opt{'title'},0,31));
211 $worksheet->write($r, $c++, $_) foreach @$header;
213 foreach my $row ( @$rows ) {
217 if ( $opt{'fields'} ) {
219 #my $links = $opt{'links'} ? [ @{$opt{'links'}} ] : '';
220 #my $aligns = $opt{'align'} ? [ @{$opt{'align'}} ] : '';
222 foreach my $field ( @{$opt{'fields'}} ) {
223 #my $align = $aligns ? shift @$aligns : '';
224 #$align = " ALIGN=$align" if $align;
227 # my $link = shift @$links;
228 # $link = &{$link}($row) if ref($link) eq 'CODE';
230 # my( $url, $method ) = @{$link};
231 # if ( ref($method) eq 'CODE' ) {
232 # $a = $url. &{$method}($row);
234 # $a = $url. $row->$method();
236 # $a = qq(<A HREF="$a">);
239 if ( ref($field) eq 'CODE' ) {
240 foreach my $value ( &{$field}($row) ) {
241 if ( ref($value) eq 'ARRAY' ) {
242 $worksheet->write($r, $c++, '(N/A)' ); #unimplemented
244 $worksheet->write($r, $c++, $value );
248 $worksheet->write($r, $c++, $row->$field() );
253 $worksheet->write($r, $c++, $_) foreach @$row;
258 $workbook->close();# or die "Error creating .xls file: $!";
260 http_header('Content-Length' => length($data) );
263 } else { # regular HTML
265 if ( exists($opt{'redirect'}) && scalar(@$rows) == 1 && $total == 1 ) {
266 my $redirect = $opt{'redirect'};
267 $redirect = &{$redirect}($rows->[0]) if ref($redirect) eq 'CODE';
268 my( $url, $method ) = @$redirect;
269 redirect( $url. $rows->[0]->$method() );
271 ( my $xlsname = $opt{'name'} ) =~ s/\W//g;
272 #$opt{'name'} =~ s/s$// if $total == 1;
273 $opt{'name'} =~ s/((s)e)?s$/$2/ if $total == 1; #should use Lingua::bs
277 if ( $opt{'menubar'} ) {
278 @menubar = @{ $opt{'menubar'} };
280 @menubar = ( 'Main menu' => $p );
285 <%= include( '/elements/header.html', $opt{'title'},
286 include( '/elements/menubar.html', @menubar )
289 <%= defined($opt{'html_init'}) ? $opt{'html_init'} : '' %>
290 <% my $pager = include ( '/elements/pager.html',
292 'num_rows' => scalar(@$rows),
294 'maxrecords' => $maxrecords,
297 <% unless ( $total ) { %>
298 No matching <%= $opt{'name'} %> found.<BR>
304 <%= $total %> total <%= $opt{'name'} %><BR>
305 <% if ( $opt{'count_addl'} ) { %>
306 <% my $n=0; foreach my $count ( @{$opt{'count_addl'}} ) { %>
307 <%= sprintf( $count, $count_arrayref->[++$n] ) %><BR>
312 <% $cgi->param('_type', "$xlsname.xls" ); %>
313 Download full results<BR>
314 as <A HREF="<%= $cgi->self_url %>">Excel spreadsheet</A><BR>
315 <% $cgi->param('_type', 'csv'); %>
316 as <A HREF="<%= $cgi->self_url %>">CSV file</A>
323 <%= include('/elements/table-grid.html') %>
327 foreach my $header ( @$header ) { %>
328 <TH CLASS="grid" BGCOLOR="#cccccc"><%= $header %></TH>
331 <% my $bgcolor1 = '#eeeeee';
332 my $bgcolor2 = '#ffffff';
334 foreach my $row ( @$rows ) {
335 if ( $bgcolor eq $bgcolor1 ) {
336 $bgcolor = $bgcolor2;
338 $bgcolor = $bgcolor1;
342 <% if ( $opt{'fields'} ) {
344 my $links = $opt{'links'} ? [ @{$opt{'links'}} ] : '';
345 my $aligns = $opt{'align'} ? [ @{$opt{'align'}} ] : '';
346 my $colors = $opt{'color'} ? [ @{$opt{'color'}} ] : [];
347 my $sizes = $opt{'size'} ? [ @{$opt{'size'}} ] : [];
348 my $styles = $opt{'style'} ? [ @{$opt{'style'}} ] : [];
353 if ( ref($_) eq 'ARRAY' ) {
357 '<TABLE BORDER=0 CELLSPACING=0 CELLPADDING=0'.
358 ' STYLE="border:none">'.
370 '<TD STYLE="border:none"'.
371 ( $element->{'align'}
372 ? ' ALIGN="'. $element->{'align'}. '"'
376 ? '<A HREF="'. $element->{'link'}.'">'
399 if ( ref($_) eq 'CODE' ) {
409 my $align = $aligns ? shift @$aligns : '';
410 $align = " ALIGN=$align" if $align;
414 my $link = shift @$links;
415 $link = &{$link}($row) if ref($link) eq 'CODE';
417 my( $url, $method ) = @{$link};
418 if ( ref($method) eq 'CODE' ) {
419 $a = $url. &{$method}($row);
421 $a = $url. $row->$method();
423 $a = qq(<A HREF="$a">);
428 my $color = shift @$colors;
429 $color = &{$color}($row) if ref($color) eq 'CODE';
430 my $size = shift @$sizes;
431 $size = &{$size}($row) if ref($size) eq 'CODE';
432 if ( $color || $size ) {
434 ( $color ? "COLOR=#$color " : '' ).
435 ( $size ? qq(SIZE="$size" ) : '' ).
439 my($s, $es) = ( '', '' );
440 my $style = shift @$styles;
441 $style = &{$style}($row) if ref($style) eq 'CODE';
443 $s = join( '', map "<$_>", split('', $style) );
444 $es = join( '', map "</$_>", split('', $style) );
448 <TD CLASS="grid" BGCOLOR="<%= $bgcolor %>"<%= $align %>><%= $font %><%= $a %><%= $s %><%= $field %><%= $es %><%= $a ? '</A>' : '' %><%= $font ? '</FONT>' : '' %></TD>
451 <% foreach ( @$row ) { %>
452 <TD CLASS="grid" BGCOLOR="<%= $bgcolor %>"><%= $_ %></TD>
458 <% if ( $opt{'footer'} ) { %>
460 <% foreach my $footer ( @{ $opt{'footer'} } ) { %>
461 <TD CLASS="grid" BGCOLOR="#dddddd" STYLE="border-top: dashed 1px black;"><i><%= $footer %></i></TH>
474 <%= include( '/elements/footer.html' ) %>