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' ) {
94 push @line, &{$field}($row);
96 push @line, $row->$field();
100 $csv->combine(@line); #or die $csv->status;
103 $csv->combine(@$row); #or die $csv->status;
106 %><%= $csv->string %><%
110 #} elsif ( $type eq 'excel' ) {
111 } elsif ( $type =~ /\.xls$/ ) {
113 #http_header('Content-Type' => 'application/excel' ); #eww
114 http_header('Content-Type' => 'application/vnd.ms-excel' );
115 #http_header('Content-Type' => 'application/msexcel' ); #alas
118 my $XLS = new IO::Scalar \$data;
119 my $workbook = Spreadsheet::WriteExcel->new($XLS)
120 or die "Error opening .xls file: $!";
122 my $worksheet = $workbook->add_worksheet(substr($opt{'title'},0,31));
126 $worksheet->write($r, $c++, $_) foreach @$header;
128 foreach my $row ( @$rows ) {
132 if ( $opt{'fields'} ) {
134 #my $links = $opt{'links'} ? [ @{$opt{'links'}} ] : '';
135 #my $aligns = $opt{'align'} ? [ @{$opt{'align'}} ] : '';
137 foreach my $field ( @{$opt{'fields'}} ) {
138 #my $align = $aligns ? shift @$aligns : '';
139 #$align = " ALIGN=$align" if $align;
142 # my $link = shift @$links;
143 # $link = &{$link}($row) if ref($link) eq 'CODE';
145 # my( $url, $method ) = @{$link};
146 # if ( ref($method) eq 'CODE' ) {
147 # $a = $url. &{$method}($row);
149 # $a = $url. $row->$method();
151 # $a = qq(<A HREF="$a">);
154 if ( ref($field) eq 'CODE' ) {
155 $worksheet->write($r, $c++, &{$field}($row) );
157 $worksheet->write($r, $c++, $row->$field() );
162 $worksheet->write($r, $c++, $_) foreach @$row;
167 $workbook->close();# or die "Error creating .xls file: $!";
169 http_header('Content-Length' => length($data) );
172 } else { # regular HTML
174 if ( exists($opt{'redirect'}) && scalar(@$rows) == 1 && $total == 1 ) {
175 my( $url, $method ) = @{$opt{'redirect'}};
176 redirect( $url. $rows->[0]->$method() );
178 ( my $xlsname = $opt{'name'} ) =~ s/\W//g;
179 $opt{'name'} =~ s/s$// if $total == 1;
182 if ( $opt{'menubar'} ) {
183 @menubar = @{ $opt{'menubar'} };
185 @menubar = ( 'Main menu' => $p );
188 <%= include( '/elements/header.html', $opt{'title'},
189 include( '/elements/menubar.html', @menubar )
192 <% my $pager = include ( '/elements/pager.html',
194 'num_rows' => scalar(@$rows),
196 'maxrecords' => $maxrecords,
199 <% unless ( $total ) { %>
200 No matching <%= $opt{'name'} %> found.<BR>
206 <%= $total %> total <%= $opt{'name'} %><BR>
207 <% if ( $opt{'count_addl'} ) { %>
208 <% my $n=0; foreach my $count ( @{$opt{'count_addl'}} ) { %>
209 <%= sprintf( $count, $count_arrayref->[++$n] ) %><BR>
214 <% $cgi->param('_type', "$xlsname.xls" ); %>
215 Download full results<BR>
216 as <A HREF="<%= $cgi->self_url %>">Excel spreadsheet</A><BR>
217 <% $cgi->param('_type', 'csv'); %>
218 as <A HREF="<%= $cgi->self_url %>">CSV file</A>
225 <STYLE TYPE="text/css">
226 .grid table { border: solid; empty-cells: show }
227 .grid TH { padding-left: 3px; padding-right: 3px; border: 1px solid #dddddd; border-bottom: dashed 1px black; border-right: none }
228 .grid TD { padding-left: 3px; padding-right: 3px; empty-cells: show; border: 1px solid #cccccc; border-bottom: none; border-right: none }
230 <TABLE CLASS="grid" CELLSPACING=0 CELLPADDING=0 BORDER=1 BORDERCOLOR="#000000" STYLE="border: solid 1px black; empty-cells: show">
232 <% foreach my $header ( @$header ) { %>
233 <TH CLASS="grid" BGCOLOR="#cccccc"><%= $header %></TH>
236 <% my $bgcolor1 = '#eeeeee';
237 my $bgcolor2 = '#ffffff';
239 foreach my $row ( @$rows ) {
240 if ( $bgcolor eq $bgcolor1 ) {
241 $bgcolor = $bgcolor2;
243 $bgcolor = $bgcolor1;
247 <% if ( $opt{'fields'} ) {
248 my $links = $opt{'links'} ? [ @{$opt{'links'}} ] : '';
249 my $aligns = $opt{'align'} ? [ @{$opt{'align'}} ] : '';
250 foreach my $field ( @{$opt{'fields'}} ) {
251 my $align = $aligns ? shift @$aligns : '';
252 $align = " ALIGN=$align" if $align;
255 my $link = shift @$links;
256 $link = &{$link}($row) if ref($link) eq 'CODE';
258 my( $url, $method ) = @{$link};
259 if ( ref($method) eq 'CODE' ) {
260 $a = $url. &{$method}($row);
262 $a = $url. $row->$method();
264 $a = qq(<A HREF="$a">);
268 <% if ( ref($field) eq 'CODE' ) { %>
269 <TD CLASS="grid" BGCOLOR="<%= $bgcolor %>"<%= $align %>><%= $a %><%= &{$field}($row) %><%= $a ? '</A>' : '' %></TD>
271 <TD CLASS="grid" BGCOLOR="<%= $bgcolor %>"<%= $align %>><%= $a %><%= $row->$field() %><%= $a ? '</A>' : '' %></TD>
275 <% foreach ( @$row ) { %>
276 <TD CLASS="grid" BGCOLOR="$bgcolor"><%= $_ %></TD>