4 #warn join(' / ', map { "$_ => $opt{$_}" } keys %opt ). "\n";
13 $opt{align} = [ map $align{$_}, split(//, $opt{align}) ],
14 unless !$opt{align} || ref($opt{align});
18 my($maxrecords, $total, $offset, $count_arrayref);
20 if ( $cgi->param('_type') =~ /^(csv|\w*\.xls)$/ ) {
24 } else { #setup some pagination things if we're in html mode
26 unless (exists($opt{'count_query'}) && length($opt{'count_query'})) {
27 ( $opt{'count_query'} = $opt{'query'} ) =~
28 s/^\s*SELECT\s*(.*?)\s+FROM\s/SELECT COUNT(*) FROM /i;
31 my $conf = new FS::Conf;
32 $maxrecords = $conf->config('maxsearchrecordsperpage');
34 $limit = $maxrecords ? "LIMIT $maxrecords" : '';
36 $offset = $cgi->param('offset') || 0;
37 $limit .= " OFFSET $offset" if $offset;
39 my $count_sth = dbh->prepare($opt{'count_query'})
40 or die "Error preparing $opt{'count_query'}: ". dbh->errstr;
42 or die "Error executing $opt{'count_query'}: ". $count_sth->errstr;
43 $count_arrayref = $count_sth->fetchrow_arrayref;
44 $total = $count_arrayref->[0];
50 my $header = $opt{'header'};
52 if ( ref($opt{'query'}) ) {
53 #eval "use FS::$opt{'query'};";
55 $opt{'query'}->{'table'},
56 $opt{'query'}->{'hashref'} || {},
57 $opt{'query'}->{'select'},
58 $opt{'query'}->{'extra_sql'}. " $limit",
60 (exists($opt{'query'}->{'addl_from'}) ? $opt{'query'}->{'addl_from'} : '')
63 my $sth = dbh->prepare("$opt{'query'} $limit")
64 or die "Error preparing $opt{'query'}: ". dbh->errstr;
66 or die "Error executing $opt{'query'}: ". $sth->errstr;
68 #can get # of rows without fetching them all?
69 $rows = $sth->fetchall_arrayref;
71 $header ||= $sth->{NAME};
74 if ( $type eq 'csv' ) {
76 #http_header('Content-Type' => 'text/comma-separated-values' ); #IE chokes
77 http_header('Content-Type' => 'text/plain' );
79 my $csv = new Text::CSV_XS { 'always_quote' => 1,
80 'eol' => "\n", #"\015\012", #"\012"
83 $csv->combine(@$header); #or die $csv->status;
84 %><%= $csv->string %><%
86 foreach my $row ( @$rows ) {
88 if ( $opt{'fields'} ) {
92 foreach my $field ( @{$opt{'fields'}} ) {
93 if ( ref($field) eq 'CODE' ) {
96 ? '(N/A)' #unimplemented
101 push @line, $row->$field();
105 $csv->combine(@line); #or die $csv->status;
108 $csv->combine(@$row); #or die $csv->status;
111 %><%= $csv->string %><%
115 #} elsif ( $type eq 'excel' ) {
116 } elsif ( $type =~ /\.xls$/ ) {
118 #http_header('Content-Type' => 'application/excel' ); #eww
119 http_header('Content-Type' => 'application/vnd.ms-excel' );
120 #http_header('Content-Type' => 'application/msexcel' ); #alas
123 my $XLS = new IO::Scalar \$data;
124 my $workbook = Spreadsheet::WriteExcel->new($XLS)
125 or die "Error opening .xls file: $!";
127 my $worksheet = $workbook->add_worksheet(substr($opt{'title'},0,31));
131 $worksheet->write($r, $c++, $_) foreach @$header;
133 foreach my $row ( @$rows ) {
137 if ( $opt{'fields'} ) {
139 #my $links = $opt{'links'} ? [ @{$opt{'links'}} ] : '';
140 #my $aligns = $opt{'align'} ? [ @{$opt{'align'}} ] : '';
142 foreach my $field ( @{$opt{'fields'}} ) {
143 #my $align = $aligns ? shift @$aligns : '';
144 #$align = " ALIGN=$align" if $align;
147 # my $link = shift @$links;
148 # $link = &{$link}($row) if ref($link) eq 'CODE';
150 # my( $url, $method ) = @{$link};
151 # if ( ref($method) eq 'CODE' ) {
152 # $a = $url. &{$method}($row);
154 # $a = $url. $row->$method();
156 # $a = qq(<A HREF="$a">);
159 if ( ref($field) eq 'CODE' ) {
160 foreach my $value ( &{$field}($row) ) {
161 if ( ref($value) eq 'ARRAY' ) {
162 $worksheet->write($r, $c++, '(N/A)' ); #unimplemented
164 $worksheet->write($r, $c++, $value );
168 $worksheet->write($r, $c++, $row->$field() );
173 $worksheet->write($r, $c++, $_) foreach @$row;
178 $workbook->close();# or die "Error creating .xls file: $!";
180 http_header('Content-Length' => length($data) );
183 } else { # regular HTML
185 if ( exists($opt{'redirect'}) && scalar(@$rows) == 1 && $total == 1 ) {
186 my $redirect = $opt{'redirect'};
187 $redirect = &{$redirect}($rows->[0]) if ref($redirect) eq 'CODE';
188 my( $url, $method ) = @$redirect;
189 redirect( $url. $rows->[0]->$method() );
191 ( my $xlsname = $opt{'name'} ) =~ s/\W//g;
192 $opt{'name'} =~ s/s$// if $total == 1;
195 if ( $opt{'menubar'} ) {
196 @menubar = @{ $opt{'menubar'} };
198 @menubar = ( 'Main menu' => $p );
201 <%= include( '/elements/header.html', $opt{'title'},
202 include( '/elements/menubar.html', @menubar )
205 <%= defined($opt{'html_init'}) ? $opt{'html_init'} : '' %>
206 <% my $pager = include ( '/elements/pager.html',
208 'num_rows' => scalar(@$rows),
210 'maxrecords' => $maxrecords,
213 <% unless ( $total ) { %>
214 No matching <%= $opt{'name'} %> found.<BR>
220 <%= $total %> total <%= $opt{'name'} %><BR>
221 <% if ( $opt{'count_addl'} ) { %>
222 <% my $n=0; foreach my $count ( @{$opt{'count_addl'}} ) { %>
223 <%= sprintf( $count, $count_arrayref->[++$n] ) %><BR>
228 <% $cgi->param('_type', "$xlsname.xls" ); %>
229 Download full results<BR>
230 as <A HREF="<%= $cgi->self_url %>">Excel spreadsheet</A><BR>
231 <% $cgi->param('_type', 'csv'); %>
232 as <A HREF="<%= $cgi->self_url %>">CSV file</A>
239 <%= include('/elements/table-grid.html') %>
242 <% foreach my $header ( @$header ) { %>
243 <TH CLASS="grid" BGCOLOR="#cccccc"><%= $header %></TH>
246 <% my $bgcolor1 = '#eeeeee';
247 my $bgcolor2 = '#ffffff';
249 foreach my $row ( @$rows ) {
250 if ( $bgcolor eq $bgcolor1 ) {
251 $bgcolor = $bgcolor2;
253 $bgcolor = $bgcolor1;
257 <% if ( $opt{'fields'} ) {
259 my $links = $opt{'links'} ? [ @{$opt{'links'}} ] : '';
260 my $aligns = $opt{'align'} ? [ @{$opt{'align'}} ] : '';
261 my $colors = $opt{'color'} ? [ @{$opt{'color'}} ] : [];
262 my $sizes = $opt{'size'} ? [ @{$opt{'size'}} ] : [];
263 my $styles = $opt{'style'} ? [ @{$opt{'style'}} ] : [];
268 if ( ref($_) eq 'ARRAY' ) {
272 '<TABLE BORDER=0 CELLSPACING=0 CELLPADDING=0'.
273 ' STYLE="border:none">'.
285 '<TD STYLE="border:none"'.
286 ( $element->{'align'}
287 ? ' ALIGN="'. $element->{'align'}. '"'
291 ? '<A HREF="'. $element->{'link'}.'">'
314 if ( ref($_) eq 'CODE' ) {
324 my $align = $aligns ? shift @$aligns : '';
325 $align = " ALIGN=$align" if $align;
329 my $link = shift @$links;
330 $link = &{$link}($row) if ref($link) eq 'CODE';
332 my( $url, $method ) = @{$link};
333 if ( ref($method) eq 'CODE' ) {
334 $a = $url. &{$method}($row);
336 $a = $url. $row->$method();
338 $a = qq(<A HREF="$a">);
343 my $color = shift @$colors;
344 $color = &{$color}($row) if ref($color) eq 'CODE';
345 my $size = shift @$sizes;
346 $size = &{$size}($row) if ref($size) eq 'CODE';
347 if ( $color || $size ) {
349 ( $color ? "COLOR=#$color " : '' ).
350 ( $size ? qq(SIZE="$size" ) : '' ).
354 my($s, $es) = ( '', '' );
355 my $style = shift @$styles;
356 $style = &{$style}($row) if ref($style) eq 'CODE';
358 $s = join( '', map "<$_>", split('', $style) );
359 $es = join( '', map "</$_>", split('', $style) );
363 <TD CLASS="grid" BGCOLOR="<%= $bgcolor %>"<%= $align %>><%= $font %><%= $a %><%= $s %><%= $field %><%= $es %><%= $a ? '</A>' : '' %><%= $font ? '</FONT>' : '' %></TD>
366 <% foreach ( @$row ) { %>
367 <TD CLASS="grid" BGCOLOR="$bgcolor"><%= $_ %></TD>
373 <% if ( $opt{'footer'} ) { %>
375 <% foreach my $footer ( @{ $opt{'footer'} } ) { %>
376 <TD CLASS="grid" BGCOLOR="#dddddd" STYLE="border-top: dashed 1px black;"><i><%= $footer %></i></TH>