Bug 19223: Add methods to correctly handle plugin-generated output
authorTomas Cohen Arazi <tomascohen@theke.io>
Tue, 27 Mar 2018 18:56:57 +0000 (15:56 -0300)
committerJonathan Druart <jonathan.druart@bugs.koha-community.org>
Wed, 4 Apr 2018 18:40:07 +0000 (15:40 -0300)
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 <katrin.fischer.83@web.de>
Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
Koha/Plugins/Base.pm

index bf401b1..c39a8c9 100644 (file)
@@ -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__