Bug 19223: Add methods to correctly handle plugin-generated output
[koha.git] / Koha / Plugins / Base.pm
blobc39a8c91bf0feda7f79b70b4864bfbf34a8ec623
1 package Koha::Plugins::Base;
3 # Copyright 2012 Kyle Hall
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20 use Modern::Perl;
22 use Module::Pluggable require => 1;
23 use Cwd qw(abs_path);
25 use base qw{Module::Bundled::Files};
27 use C4::Context;
28 use C4::Output qw(output_with_http_headers output_html_with_http_headers);
30 =head1 NAME
32 Koha::Plugins::Base - Base Module for plugins
34 =cut
36 sub new {
37 my ( $class, $args ) = @_;
39 return unless ( C4::Context->config("enable_plugins") || $args->{'enable_plugins'} );
41 $args->{'class'} = $class;
42 $args->{'template'} = Template->new( { ABSOLUTE => 1, ENCODING => 'UTF-8' } );
44 my $self = bless( $args, $class );
46 ## Run the installation method if it exists and hasn't been run before
47 if ( $self->can('install') && !$self->retrieve_data('__INSTALLED__') ) {
48 if ( $self->install() ) {
49 $self->store_data( { '__INSTALLED__' => 1 } );
50 } else {
51 warn "Plugin $class failed during installation!";
55 return $self;
58 =head2 store_data
60 store_data allows a plugin to store key value pairs in the database for future use.
62 usage: $self->store_data({ param1 => 'param1val', param2 => 'param2value' })
64 =cut
66 sub store_data {
67 my ( $self, $data ) = @_;
69 my $dbh = C4::Context->dbh;
70 my $sql = "REPLACE INTO plugin_data SET plugin_class = ?, plugin_key = ?, plugin_value = ?";
71 my $sth = $dbh->prepare($sql);
73 foreach my $key ( keys %$data ) {
74 $sth->execute( $self->{'class'}, $key, $data->{$key} );
78 =head2 retrieve_data
80 retrieve_data allows a plugin to read the values that were previously saved with store_data
82 usage: my $value = $self->retrieve_data( $key );
84 =cut
86 sub retrieve_data {
87 my ( $self, $key ) = @_;
89 my $dbh = C4::Context->dbh;
90 my $sql = "SELECT plugin_value FROM plugin_data WHERE plugin_class = ? AND plugin_key = ?";
91 my $sth = $dbh->prepare($sql);
92 $sth->execute( $self->{'class'}, $key );
93 my $row = $sth->fetchrow_hashref();
95 return $row->{'plugin_value'};
98 =head2 get_template
100 get_template returns a Template object. Eventually this will probably be calling
101 C4:Template, but at the moment, it does not.
103 =cut
105 sub get_template {
106 my ( $self, $args ) = @_;
108 require C4::Auth;
110 my $template_name = $args->{'file'} // '';
111 # if not absolute, call mbf_path, which dies if file does not exist
112 $template_name = $self->mbf_path( $template_name )
113 if $template_name !~ m/^\//;
114 my ( $template, $loggedinuser, $cookie ) = C4::Auth::get_template_and_user(
115 { template_name => $template_name,
116 query => $self->{'cgi'},
117 type => "intranet",
118 authnotrequired => 1,
122 $template->param(
123 CLASS => $self->{'class'},
124 METHOD => scalar $self->{'cgi'}->param('method'),
125 PLUGIN_PATH => $self->get_plugin_http_path(),
128 return $template;
131 sub get_metadata {
132 my ( $self, $args ) = @_;
134 return $self->{'metadata'};
137 =head2 get_qualified_table_name
139 To avoid naming conflict, each plugins tables should use a fully qualified namespace.
140 To avoid hardcoding and make plugins more flexible, this method will return the proper
141 fully qualified table name.
143 usage: my $table = $self->get_qualified_table_name( 'myTable' );
145 =cut
147 sub get_qualified_table_name {
148 my ( $self, $table_name ) = @_;
150 return lc( join( '_', split( '::', $self->{'class'} ), $table_name ) );
153 =head2 get_plugin_http_path
155 To access a plugin's own resources ( images, js files, css files, etc... )
156 a plugin will need to know what path to use in the template files. This
157 method returns that path.
159 usage: my $path = $self->get_plugin_http_path();
161 =cut
163 sub get_plugin_http_path {
164 my ($self) = @_;
166 return "/plugin/" . join( '/', split( '::', $self->{'class'} ) );
169 =head2 go_home
171 go_home is a quick redirect to the Koha plugins home page
173 =cut
175 sub go_home {
176 my ( $self, $params ) = @_;
178 print $self->{'cgi'}->redirect("/cgi-bin/koha/plugins/plugins-home.pl");
181 =head2 output_html
183 $self->output_html( $data, $status, $extra_options );
185 Outputs $data setting the right headers for HTML content.
187 Note: this is a wrapper function for C4::Output::output_with_http_headers
189 =cut
191 sub output_html {
192 my ( $self, $data, $status, $extra_options ) = @_;
193 output_with_http_headers( $self->{cgi}, undef, $data, 'html', $status, $extra_options );
196 =head2 output
198 $self->output( $data, $content_type[, $status[, $extra_options]]);
200 Outputs $data with the appropriate HTTP headers,
201 the authentication cookie and a Content-Type specified in
202 $content_type.
204 $content_type is one of the following: 'html', 'js', 'json', 'xml', 'rss', or 'atom'.
206 $status is an HTTP status message, like '403 Authentication Required'. It defaults to '200 OK'.
208 $extra_options is hashref. If the key 'force_no_caching' is present and has
209 a true value, the HTTP headers include directives to force there to be no
210 caching whatsoever.
212 Note: this is a wrapper function for C4::Output::output_with_http_headers
214 =cut
216 sub output {
217 my ( $self, $data, $content_type, $status, $extra_options ) = @_;
218 output_with_http_headers( $self->{cgi}, undef, $data, $content_type, $status, $extra_options );
222 __END__
224 =head1 AUTHOR
226 Kyle M Hall <kyle.m.hall@gmail.com>
228 =cut