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>.
22 use Module::Pluggable require => 1;
24 use List::Util qw(max);
26 use base qw{Module::Bundled::Files};
29 use C4::Output qw(output_with_http_headers output_html_with_http_headers);
33 Koha::Plugins::Base - Base Module for plugins
38 my ( $class, $args ) = @_;
40 return unless ( C4::Context->config("enable_plugins") || $args->{'enable_plugins'} );
42 $args->{'class'} = $class;
43 $args->{'template'} = Template->new( { ABSOLUTE => 1, ENCODING => 'UTF-8' } );
45 my $self = bless( $args, $class );
47 my $plugin_version = $self->get_metadata->{version};
48 my $database_version = $self->retrieve_data('__INSTALLED_VERSION__') || 0;
50 ## Run the installation method if it exists and hasn't been run before
51 if ( $self->can('install') && !$self->retrieve_data('__INSTALLED__') ) {
52 if ( $self->install() ) {
53 $self->store_data( { '__INSTALLED__' => 1, '__ENABLED__' => 1 } );
54 if ( my $version = $plugin_version ) {
55 $self->store_data({ '__INSTALLED_VERSION__' => $version });
58 warn "Plugin $class failed during installation!";
60 } elsif ( $self->can('upgrade') ) {
61 if ( _version_compare( $plugin_version, $database_version ) == 1 ) {
62 if ( $self->upgrade() ) {
63 $self->store_data({ '__INSTALLED_VERSION__' => $plugin_version });
65 warn "Plugin $class failed during upgrade!";
68 } elsif ( $plugin_version ne $database_version ) {
69 $self->store_data({ '__INSTALLED_VERSION__' => $plugin_version });
72 $self->{_bundle_path} = abs_path($self->mbf_dir);
79 store_data allows a plugin to store key value pairs in the database for future use.
81 usage: $self->store_data({ param1 => 'param1val', param2 => 'param2value' })
86 my ( $self, $data ) = @_;
88 my $dbh = C4::Context->dbh;
89 my $sql = "REPLACE INTO plugin_data SET plugin_class = ?, plugin_key = ?, plugin_value = ?";
90 my $sth = $dbh->prepare($sql);
92 foreach my $key ( keys %$data ) {
93 $sth->execute( $self->{'class'}, $key, $data->{$key} );
99 retrieve_data allows a plugin to read the values that were previously saved with store_data
101 usage: my $value = $self->retrieve_data( $key );
106 my ( $self, $key ) = @_;
108 my $dbh = C4::Context->dbh;
109 my $sql = "SELECT plugin_value FROM plugin_data WHERE plugin_class = ? AND plugin_key = ?";
110 my $sth = $dbh->prepare($sql);
111 $sth->execute( $self->{'class'}, $key );
112 my $row = $sth->fetchrow_hashref();
114 return $row->{'plugin_value'};
119 get_template returns a Template object. Eventually this will probably be calling
120 C4:Template, but at the moment, it does not.
125 my ( $self, $args ) = @_;
129 my $template_name = $args->{'file'} // '';
130 # if not absolute, call mbf_path, which dies if file does not exist
131 $template_name = $self->mbf_path( $template_name )
132 if $template_name !~ m/^\//;
133 my ( $template, $loggedinuser, $cookie ) = C4::Auth::get_template_and_user(
134 { template_name => $template_name,
135 query => $self->{'cgi'},
137 authnotrequired => 1,
142 CLASS => $self->{'class'},
143 METHOD => scalar $self->{'cgi'}->param('method'),
144 PLUGIN_PATH => $self->get_plugin_http_path(),
151 my ( $self, $args ) = @_;
153 return $self->{'metadata'};
156 =head2 get_qualified_table_name
158 To avoid naming conflict, each plugins tables should use a fully qualified namespace.
159 To avoid hardcoding and make plugins more flexible, this method will return the proper
160 fully qualified table name.
162 usage: my $table = $self->get_qualified_table_name( 'myTable' );
166 sub get_qualified_table_name {
167 my ( $self, $table_name ) = @_;
169 return lc( join( '_', split( '::', $self->{'class'} ), $table_name ) );
172 =head2 get_plugin_http_path
174 To access a plugin's own resources ( images, js files, css files, etc... )
175 a plugin will need to know what path to use in the template files. This
176 method returns that path.
178 usage: my $path = $self->get_plugin_http_path();
182 sub get_plugin_http_path {
185 return "/plugin/" . join( '/', split( '::', $self->{'class'} ) );
190 go_home is a quick redirect to the Koha plugins home page
195 my ( $self, $params ) = @_;
197 print $self->{'cgi'}->redirect("/cgi-bin/koha/plugins/plugins-home.pl");
202 $self->output_html( $data, $status, $extra_options );
204 Outputs $data setting the right headers for HTML content.
206 Note: this is a wrapper function for C4::Output::output_with_http_headers
211 my ( $self, $data, $status, $extra_options ) = @_;
212 output_with_http_headers( $self->{cgi}, undef, $data, 'html', $status, $extra_options );
217 my $bundle_path = $self->bundle_path
219 Returns the directory in which bundled files are.
226 return $self->{_bundle_path};
231 $self->output( $data, $content_type[, $status[, $extra_options]]);
233 Outputs $data with the appropriate HTTP headers,
234 the authentication cookie and a Content-Type specified in
237 $content_type is one of the following: 'html', 'js', 'json', 'xml', 'rss', or 'atom'.
239 $status is an HTTP status message, like '403 Authentication Required'. It defaults to '200 OK'.
241 $extra_options is hashref. If the key 'force_no_caching' is present and has
242 a true value, the HTTP headers include directives to force there to be no
245 Note: this is a wrapper function for C4::Output::output_with_http_headers
250 my ( $self, $data, $content_type, $status, $extra_options ) = @_;
251 output_with_http_headers( $self->{cgi}, undef, $data, $content_type, $status, $extra_options );
254 =head2 _version_compare
256 Utility method to compare two version numbers.
257 Returns 1 if the first argument is the higher version
258 Returns -1 if the first argument is the lower version
259 Returns 0 if both versions are equal
261 if ( _version_compare( '2.6.26', '2.6.0' ) == 1 ) {
262 print "2.6.26 is greater than 2.6.0\n";
267 sub _version_compare {
268 my $ver1 = shift || 0;
269 my $ver2 = shift || 0;
271 my @v1 = split /[.+:~-]/, $ver1;
272 my @v2 = split /[.+:~-]/, $ver2;
274 for ( my $i = 0 ; $i < max( scalar(@v1), scalar(@v2) ) ; $i++ ) {
276 # Add missing version parts if one string is shorter than the other
277 # i.e. 0 should be lt 0.2.1 and not equal, so we append .0
278 # 0.0.0 <=> 0.2.1 = -1
279 push( @v1, 0 ) unless defined( $v1[$i] );
280 push( @v2, 0 ) unless defined( $v2[$i] );
281 if ( int( $v1[$i] ) > int( $v2[$i] ) ) {
284 elsif ( int( $v1[$i] ) < int( $v2[$i] ) ) {
293 Method that returns wether the plugin is enabled or not
302 return $self->retrieve_data( '__ENABLED__' );
307 Method for enabling plugin
316 $self->store_data( {'__ENABLED__' => 1} );
323 Method for disabling plugin
332 $self->store_data( {'__ENABLED__' => 0} );
342 Kyle M Hall <kyle.m.hall@gmail.com>