From 0e019fd90408634c49450b0bbbc84668c96175df Mon Sep 17 00:00:00 2001 From: Pasi Kallinen Date: Tue, 25 Feb 2020 12:40:27 +0200 Subject: [PATCH] Bug 24724: Add plugin hook for new SQL report export formats This adds plugin hooks for adding new export formats for SQL reports. The plugin should implement two new methods: - sql_report_get_export_formats Takes no parameters. Should return an array ref of hashes, with the following keys: shortname => 'dump', name => 'Name of this format', type => 'text/plain' - sql_report_process_data Takes 3 parameters: format, headers, and data. - format is string the shortname from one of the hashes in sql_report_get_export_formats. - headers is array ref of strings, column names of the data. - data is the data as returned by perl DBI fetchall_arrayref() Test plan: 1) Enable plugins, and install the example plugin for the bug 2) Create a new SQL report, eg. "SELECT * FROM BORROWERS LIMIT 30" 3) Run the report - you should see two new entries in the "Download" button: "Perl data dump (.dump)" and "HTML table (.htm)" 4) Download the new formats, and inspect the file contents that they look correct 5) Test the other entries in the download-button and that they work Signed-off-by: Pasi Kallinen --- .../prog/en/includes/reports-toolbar.inc | 3 ++ reports/guided_reports.pl | 40 +++++++++++++++++++++- 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/koha-tmpl/intranet-tmpl/prog/en/includes/reports-toolbar.inc b/koha-tmpl/intranet-tmpl/prog/en/includes/reports-toolbar.inc index fd7ce29ce9..716fcc06b6 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/includes/reports-toolbar.inc +++ b/koha-tmpl/intranet-tmpl/prog/en/includes/reports-toolbar.inc @@ -97,6 +97,9 @@
  • [% PROCESS 'delimiter_text.inc' %]
  • Tab separated text
  • Open Document Spreadsheet
  • + [% FOREACH pluginexport IN pluginexports %] +
  • [% pluginexport.name | html %] (.[% pluginexport.shortname | html %])
  • + [% END %] [% IF (results.json) %]
  • Chart (.svg)
  • [% END %] diff --git a/reports/guided_reports.pl b/reports/guided_reports.pl index fa2817f1c7..6cdb9f89e4 100755 --- a/reports/guided_reports.pl +++ b/reports/guided_reports.pl @@ -91,6 +91,20 @@ elsif ($session and not $input->param('clear_filters')) { $filter = $session->param('report_filter'); } +if (($phase eq 'Run this report' || $phase eq 'Export') && + C4::Context->preference('UseKohaPlugins') && C4::Context->config('enable_plugins')) { + my @plugins = Koha::Plugins->new()->GetPlugins({ method => 'sql_report_get_export_formats' }); + my @exportformats; + foreach my $plugin (@plugins) { + my @formats = @{ $plugin->sql_report_get_export_formats() }; + foreach my $format (@formats) { + $format->{'pluginclass'} = $plugin->{'class'}; + push(@exportformats, $format); + } + } + $template->param( 'pluginexports' => \@exportformats ); +} + my $op = $input->param('op') || q||; my @errors = (); @@ -891,12 +905,36 @@ elsif ($phase eq 'Export'){ my $format = $input->param('format'); my $reportname = $input->param('reportname'); my $reportfilename = $reportname ? "$reportname-reportresults.$format" : "reportresults.$format" ; + my $pluginclass = $input->param('plugin') || ''; ($sql, undef) = get_prepped_report( $sql, \@param_names, \@sql_params ); my ($sth, $q_errors) = execute_query($sql); unless ($q_errors and @$q_errors) { my ( $type, $content ); - if ($format eq 'tab') { + if ($pluginclass =~ /Koha::Plugin::/ && C4::Context->preference('UseKohaPlugins') && C4::Context->config('enable_plugins')) { + my $pluginexported = 0; + my @plugins = Koha::Plugins->new()->GetPlugins({ method => 'sql_report_get_export_formats', class => $pluginclass }); + foreach my $plugin (@plugins) { + my @exportformats = @{ $plugin->sql_report_get_export_formats() }; + foreach my $exportformat (@exportformats) { + next if ($exportformat->{'shortname'} ne $format); + my @headers = header_cell_values($sth); + my $sql_rows = $sth->fetchall_arrayref(); + my ($plugcontent, $error) = $plugin->sql_report_process_data($format, \@headers, $sql_rows); + if ($error) { + push(@$q_errors, { pluginerr => $error }); + } else { + $type = $exportformat->{'type'}; + $content = $plugcontent; + $pluginexported = 1; + } + } + } + if (!$pluginexported) { + # Should not happen unless you edit url params manually, or plugin is buggy + push(@$q_errors, { pluginerr => 'Unknown plugin or plugin output format' }); + } + } elsif ($format eq 'tab') { $type = 'application/octet-stream'; $content .= join("\t", header_cell_values($sth)) . "\n"; $content = Encode::decode('UTF-8', $content); -- 2.11.0