From: Mark Wells Date: Fri, 13 Jul 2012 23:55:24 +0000 (-0700) Subject: support XLSX in other places, #17971 X-Git-Url: http://git.freeside.biz/gitweb/?p=freeside.git;a=commitdiff_plain;h=1baa2c61623bab583557554996d43551f784307e support XLSX in other places, #17971 --- diff --git a/FS/FS/access_user.pm b/FS/FS/access_user.pm index 4d72c2ee7..509cc0950 100644 --- a/FS/FS/access_user.pm +++ b/FS/FS/access_user.pm @@ -511,14 +511,16 @@ sub default_customer_view { } -=item spreadsheet_format +=item spreadsheet_format [ OVERRIDE ] Returns a hashref of this user's Excel spreadsheet download settings: 'extension' (xls or xlsx), 'class' (Spreadsheet::WriteExcel or -Excel::Writer::XLSX), and 'mime_type'. +Excel::Writer::XLSX), and 'mime_type'. If OVERRIDE is 'XLS' or 'XLSX', +use that instead of the user's setting. =cut +# is there a better place to put this? my %formats = ( XLS => { extension => '.xls', @@ -535,10 +537,12 @@ my %formats = ( sub spreadsheet_format { my $self = shift; + my $override = shift; - my $f = $self->option('spreadsheet_format') - || $conf->config('spreadsheet_format') - || 'XLS'; + my $f = $override + || $self->option('spreadsheet_format') + || $conf->config('spreadsheet_format') + || 'XLS'; $formats{$f}; } diff --git a/httemplate/graph/elements/report.html b/httemplate/graph/elements/report.html index 3600f2c66..f7746165a 100644 --- a/httemplate/graph/elements/report.html +++ b/httemplate/graph/elements/report.html @@ -77,15 +77,16 @@ any delimiter and linked from the elements in @data. % } % % } elsif ( $cgi->param('_type') =~ /(xls)$/ ) { -% -% #http_header('Content-Type' => 'application/excel' ); #eww -% http_header('Content-Type' => 'application/vnd.ms-excel' ); -% #http_header('Content-Type' => 'application/msexcel' ); #alas -% http_header('Content-Disposition' => "attachment;filename=$filename.xls"); +% #false laziness w/ search/elements/search-xls +% my $format = $FS::CurrentUser::CurrentUser->spreadsheet_format; +% $filename .= $format->{extension}; +% +% http_header('Content-Type' => $format->{mime_type} ); +% http_header('Content-Disposition' => qq!attachment;filename="$filename"! ); % % my $output = ''; % my $XLS = new IO::Scalar \$output; -% my $workbook = Spreadsheet::WriteExcel->new($XLS) +% my $workbook = $format->{class}->new($XLS) % or die "Error opening .xls file: $!"; % % my $worksheet = $workbook->add_worksheet(substr($opt{'title'},0,31)); @@ -304,9 +305,6 @@ td.cell { <% include('/elements/footer.html') %> % } -<%once> - - <%init> my(%opt) = @_; diff --git a/httemplate/search/elements/search-xls.html b/httemplate/search/elements/search-xls.html index c862dfbbe..09dbe46e0 100644 --- a/httemplate/search/elements/search-xls.html +++ b/httemplate/search/elements/search-xls.html @@ -7,7 +7,10 @@ my $header = $args{'header'}; my $rows = $args{'rows'}; my %opt = %{ $args{'opt'} }; -my $format = $FS::CurrentUser::CurrentUser->spreadsheet_format; +my $override = scalar(@$rows) >= 65536 ? 'XLSX' : ''; + +my $format = $FS::CurrentUser::CurrentUser->spreadsheet_format($override); + my $filename = $opt{'name'} || PL($opt{'name_singular'}); $filename .= $format->{extension}; diff --git a/httemplate/search/report_tax-xls.cgi b/httemplate/search/report_tax-xls.cgi index 1c278dfd1..f19f85aaa 100755 --- a/httemplate/search/report_tax-xls.cgi +++ b/httemplate/search/report_tax-xls.cgi @@ -1,9 +1,25 @@ <% $data %> <%init> +my $htmldoc = include('report_tax.cgi'); + +my ($title) = ($htmldoc =~ /\s*(.*)\s*<\/title>/i); + +# do this first so we can override the format if it's too many rows +# attribs option: how to locate the table? It's the only one with class="grid". +my $te = HTML::TableExtract->new(attribs => {class => 'grid'}); +$te->parse($htmldoc); +my $table = $te->first_table_found; + +my $override = ($table->row_count >= 65536 ? 'XLSX' : ''); +my $format = $FS::CurrentUser::CurrentUser->spreadsheet_format($override); +my $filename = 'report_tax'.$format->{extension}; + +http_header('Content-Type' => $format->{mime_type}); +http_header('Content-Disposition' => qq!attachment;filename="$filename"! ); my $data = ''; my $XLS = new IO::Scalar \$data; -my $workbook = Spreadsheet::WriteExcel->new($XLS) +my $workbook = $format->{class}->new($XLS) or die "Error opening .xls file: $!"; # hardcoded formats, this could be handled better @@ -66,15 +82,6 @@ foreach (keys(%format)) { } my $ws = $workbook->add_worksheet('taxreport'); -my $htmldoc = include('report_tax.cgi'); - -my ($title) = ($htmldoc =~ /<title>\s*(.*)\s*<\/title>/i); - -# attribs option: how to locate the table? It's the only one with class="grid". -my $te = HTML::TableExtract->new(attribs => {class => 'grid'}); -$te->parse($htmldoc); -my $table = $te->first_table_found; - my @sheet; $sheet[0][0] = { text => $title, @@ -148,6 +155,4 @@ for my $x (0..scalar(@widths)-1) { $workbook->close; -http_header('Content-Type' => 'application/vnd.ms-excel'); -http_header('Content-Disposition' => 'attachment;filename="report_tax.xls"'); </%init>