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
72 #warn join(' / ', map { "$_ => $opt{$_}" } keys %opt ). "\n";
81 $opt{align} = [ map $align{$_}, split(//, $opt{align}) ],
82 unless !$opt{align} || ref($opt{align});
86 my($maxrecords, $total, $offset, $count_arrayref);
88 if ( $cgi->param('_type') =~ /^(csv|\w*\.xls)$/ ) {
92 } else { #setup some pagination things if we're in html mode
94 unless (exists($opt{'count_query'}) && length($opt{'count_query'})) {
95 ( $opt{'count_query'} = $opt{'query'} ) =~
96 s/^\s*SELECT\s*(.*?)\s+FROM\s/SELECT COUNT(*) FROM /i;
99 my $conf = new FS::Conf;
100 $maxrecords = $conf->config('maxsearchrecordsperpage');
102 $limit = $maxrecords ? "LIMIT $maxrecords" : '';
104 $offset = $cgi->param('offset') || 0;
105 $limit .= " OFFSET $offset" if $offset;
107 my $count_sth = dbh->prepare($opt{'count_query'})
108 or die "Error preparing $opt{'count_query'}: ". dbh->errstr;
110 or die "Error executing $opt{'count_query'}: ". $count_sth->errstr;
111 $count_arrayref = $count_sth->fetchrow_arrayref;
112 $total = $count_arrayref->[0];
118 my $header = $opt{'header'};
120 if ( ref($opt{'query'}) ) {
121 #eval "use FS::$opt{'query'};";
123 $opt{'query'}->{'table'},
124 $opt{'query'}->{'hashref'} || {},
125 $opt{'query'}->{'select'},
126 $opt{'query'}->{'extra_sql'}. " $limit",
128 (exists($opt{'query'}->{'addl_from'}) ? $opt{'query'}->{'addl_from'} : '')
131 my $sth = dbh->prepare("$opt{'query'} $limit")
132 or die "Error preparing $opt{'query'}: ". dbh->errstr;
134 or die "Error executing $opt{'query'}: ". $sth->errstr;
136 #can get # of rows without fetching them all?
137 $rows = $sth->fetchall_arrayref;
139 $header ||= $sth->{NAME};
142 if ( $type eq 'csv' ) {
144 #http_header('Content-Type' => 'text/comma-separated-values' ); #IE chokes
145 http_header('Content-Type' => 'text/plain' );
147 my $csv = new Text::CSV_XS { 'always_quote' => 1,
148 'eol' => "\n", #"\015\012", #"\012"
151 $csv->combine(@$header); #or die $csv->status;
152 %><%= $csv->string %><%
154 foreach my $row ( @$rows ) {
156 if ( $opt{'fields'} ) {
160 foreach my $field ( @{$opt{'fields'}} ) {
161 if ( ref($field) eq 'CODE' ) {
164 ? '(N/A)' #unimplemented
169 push @line, $row->$field();
173 $csv->combine(@line); #or die $csv->status;
176 $csv->combine(@$row); #or die $csv->status;
179 %><%= $csv->string %><%
183 #} elsif ( $type eq 'excel' ) {
184 } elsif ( $type =~ /\.xls$/ ) {
186 #http_header('Content-Type' => 'application/excel' ); #eww
187 http_header('Content-Type' => 'application/vnd.ms-excel' );
188 #http_header('Content-Type' => 'application/msexcel' ); #alas
191 my $XLS = new IO::Scalar \$data;
192 my $workbook = Spreadsheet::WriteExcel->new($XLS)
193 or die "Error opening .xls file: $!";
195 my $worksheet = $workbook->add_worksheet(substr($opt{'title'},0,31));
199 $worksheet->write($r, $c++, $_) foreach @$header;
201 foreach my $row ( @$rows ) {
205 if ( $opt{'fields'} ) {
207 #my $links = $opt{'links'} ? [ @{$opt{'links'}} ] : '';
208 #my $aligns = $opt{'align'} ? [ @{$opt{'align'}} ] : '';
210 foreach my $field ( @{$opt{'fields'}} ) {
211 #my $align = $aligns ? shift @$aligns : '';
212 #$align = " ALIGN=$align" if $align;
215 # my $link = shift @$links;
216 # $link = &{$link}($row) if ref($link) eq 'CODE';
218 # my( $url, $method ) = @{$link};
219 # if ( ref($method) eq 'CODE' ) {
220 # $a = $url. &{$method}($row);
222 # $a = $url. $row->$method();
224 # $a = qq(<A HREF="$a">);
227 if ( ref($field) eq 'CODE' ) {
228 foreach my $value ( &{$field}($row) ) {
229 if ( ref($value) eq 'ARRAY' ) {
230 $worksheet->write($r, $c++, '(N/A)' ); #unimplemented
232 $worksheet->write($r, $c++, $value );
236 $worksheet->write($r, $c++, $row->$field() );
241 $worksheet->write($r, $c++, $_) foreach @$row;
246 $workbook->close();# or die "Error creating .xls file: $!";
248 http_header('Content-Length' => length($data) );
251 } else { # regular HTML
253 if ( exists($opt{'redirect'}) && scalar(@$rows) == 1 && $total == 1 ) {
254 my $redirect = $opt{'redirect'};
255 $redirect = &{$redirect}($rows->[0]) if ref($redirect) eq 'CODE';
256 my( $url, $method ) = @$redirect;
257 redirect( $url. $rows->[0]->$method() );
259 ( my $xlsname = $opt{'name'} ) =~ s/\W//g;
260 #$opt{'name'} =~ s/s$// if $total == 1;
261 $opt{'name'} =~ s/((s)e)?s$/$2/ if $total == 1; #should use Lingua::bs
265 if ( $opt{'menubar'} ) {
266 @menubar = @{ $opt{'menubar'} };
268 @menubar = ( 'Main menu' => $p );
273 <%= include( '/elements/header.html', $opt{'title'},
274 include( '/elements/menubar.html', @menubar )
277 <%= defined($opt{'html_init'}) ? $opt{'html_init'} : '' %>
278 <% my $pager = include ( '/elements/pager.html',
280 'num_rows' => scalar(@$rows),
282 'maxrecords' => $maxrecords,
285 <% unless ( $total ) { %>
286 No matching <%= $opt{'name'} %> found.<BR>
292 <%= $total %> total <%= $opt{'name'} %><BR>
293 <% if ( $opt{'count_addl'} ) { %>
294 <% my $n=0; foreach my $count ( @{$opt{'count_addl'}} ) { %>
295 <%= sprintf( $count, $count_arrayref->[++$n] ) %><BR>
300 <% $cgi->param('_type', "$xlsname.xls" ); %>
301 Download full results<BR>
302 as <A HREF="<%= $cgi->self_url %>">Excel spreadsheet</A><BR>
303 <% $cgi->param('_type', 'csv'); %>
304 as <A HREF="<%= $cgi->self_url %>">CSV file</A>
311 <%= include('/elements/table-grid.html') %>
315 foreach my $header ( @$header ) { %>
316 <TH CLASS="grid" BGCOLOR="#cccccc"><%= $header %></TH>
319 <% my $bgcolor1 = '#eeeeee';
320 my $bgcolor2 = '#ffffff';
322 foreach my $row ( @$rows ) {
323 if ( $bgcolor eq $bgcolor1 ) {
324 $bgcolor = $bgcolor2;
326 $bgcolor = $bgcolor1;
330 <% if ( $opt{'fields'} ) {
332 my $links = $opt{'links'} ? [ @{$opt{'links'}} ] : '';
333 my $aligns = $opt{'align'} ? [ @{$opt{'align'}} ] : '';
334 my $colors = $opt{'color'} ? [ @{$opt{'color'}} ] : [];
335 my $sizes = $opt{'size'} ? [ @{$opt{'size'}} ] : [];
336 my $styles = $opt{'style'} ? [ @{$opt{'style'}} ] : [];
341 if ( ref($_) eq 'ARRAY' ) {
345 '<TABLE BORDER=0 CELLSPACING=0 CELLPADDING=0'.
346 ' STYLE="border:none">'.
358 '<TD STYLE="border:none"'.
359 ( $element->{'align'}
360 ? ' ALIGN="'. $element->{'align'}. '"'
364 ? '<A HREF="'. $element->{'link'}.'">'
387 if ( ref($_) eq 'CODE' ) {
397 my $align = $aligns ? shift @$aligns : '';
398 $align = " ALIGN=$align" if $align;
402 my $link = shift @$links;
403 $link = &{$link}($row) if ref($link) eq 'CODE';
405 my( $url, $method ) = @{$link};
406 if ( ref($method) eq 'CODE' ) {
407 $a = $url. &{$method}($row);
409 $a = $url. $row->$method();
411 $a = qq(<A HREF="$a">);
416 my $color = shift @$colors;
417 $color = &{$color}($row) if ref($color) eq 'CODE';
418 my $size = shift @$sizes;
419 $size = &{$size}($row) if ref($size) eq 'CODE';
420 if ( $color || $size ) {
422 ( $color ? "COLOR=#$color " : '' ).
423 ( $size ? qq(SIZE="$size" ) : '' ).
427 my($s, $es) = ( '', '' );
428 my $style = shift @$styles;
429 $style = &{$style}($row) if ref($style) eq 'CODE';
431 $s = join( '', map "<$_>", split('', $style) );
432 $es = join( '', map "</$_>", split('', $style) );
436 <TD CLASS="grid" BGCOLOR="<%= $bgcolor %>"<%= $align %>><%= $font %><%= $a %><%= $s %><%= $field %><%= $es %><%= $a ? '</A>' : '' %><%= $font ? '</FONT>' : '' %></TD>
439 <% foreach ( @$row ) { %>
440 <TD CLASS="grid" BGCOLOR="$bgcolor"><%= $_ %></TD>
446 <% if ( $opt{'footer'} ) { %>
448 <% foreach my $footer ( @{ $opt{'footer'} } ) { %>
449 <TD CLASS="grid" BGCOLOR="#dddddd" STYLE="border-top: dashed 1px black;"><i><%= $footer %></i></TH>
462 <%= include( '/elements/footer.html' ) %>