From 0ab51a509a2a9d0ced3b2ccd9ff154f670f27560 Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Tue, 27 Mar 2018 15:56:57 -0300 Subject: [PATCH] Bug 19223: Add methods to correctly handle plugin-generated output This patch introduces two methods to be used by plugin authors: ->output ->output_html They are basically wrappers for the helper methods from C4::Output (output_html_with_http_headers and output_with_http_headers). Plugin authors can use them, or keep the current flexibility of handling the headers themselves in their code. The KitchenSink plugin should be updated to highlight this. To test: - Run: $ kshell k$ prove t/db_dependent/Plugins.t => FAIL: The methods are not implemented - Apply this patch - Run: k$ prove t/db_dependent/Plugins.t => SUCCESS: Tests pass, and they are meaningful - Sign off :-D Sponsored-by: ByWater Solutions Signed-off-by: Katrin Fischer Signed-off-by: Kyle M Hall Signed-off-by: Jonathan Druart --- Koha/Plugins/Base.pm | 43 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/Koha/Plugins/Base.pm b/Koha/Plugins/Base.pm index bf401b11ce..c39a8c91bf 100644 --- a/Koha/Plugins/Base.pm +++ b/Koha/Plugins/Base.pm @@ -25,6 +25,7 @@ use Cwd qw(abs_path); use base qw{Module::Bundled::Files}; use C4::Context; +use C4::Output qw(output_with_http_headers output_html_with_http_headers); =head1 NAME @@ -38,7 +39,7 @@ sub new { return unless ( C4::Context->config("enable_plugins") || $args->{'enable_plugins'} ); $args->{'class'} = $class; - $args->{'template'} = Template->new( { ABSOLUTE => 1 } ); + $args->{'template'} = Template->new( { ABSOLUTE => 1, ENCODING => 'UTF-8' } ); my $self = bless( $args, $class ); @@ -177,6 +178,46 @@ sub go_home { print $self->{'cgi'}->redirect("/cgi-bin/koha/plugins/plugins-home.pl"); } +=head2 output_html + + $self->output_html( $data, $status, $extra_options ); + +Outputs $data setting the right headers for HTML content. + +Note: this is a wrapper function for C4::Output::output_with_http_headers + +=cut + +sub output_html { + my ( $self, $data, $status, $extra_options ) = @_; + output_with_http_headers( $self->{cgi}, undef, $data, 'html', $status, $extra_options ); +} + +=head2 output + + $self->output( $data, $content_type[, $status[, $extra_options]]); + +Outputs $data with the appropriate HTTP headers, +the authentication cookie and a Content-Type specified in +$content_type. + +$content_type is one of the following: 'html', 'js', 'json', 'xml', 'rss', or 'atom'. + +$status is an HTTP status message, like '403 Authentication Required'. It defaults to '200 OK'. + +$extra_options is hashref. If the key 'force_no_caching' is present and has +a true value, the HTTP headers include directives to force there to be no +caching whatsoever. + +Note: this is a wrapper function for C4::Output::output_with_http_headers + +=cut + +sub output { + my ( $self, $data, $content_type, $status, $extra_options ) = @_; + output_with_http_headers( $self->{cgi}, undef, $data, $content_type, $status, $extra_options ); +} + 1; __END__ -- 2.39.5