1 package Koha
::Plugins
::Handler
;
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 File
::Path
qw(remove_tree);
24 use Module
::Load
::Conditional
qw(can_load);
29 my $pluginsdir = C4
::Context
->config("pluginsdir");
30 my @pluginsdir = ref($pluginsdir) eq 'ARRAY' ? @
$pluginsdir : $pluginsdir;
31 push( @INC, @pluginsdir );
32 pop @INC if $INC[-1] eq '.' ;
37 C4::Plugins::Handler - Handler Module for running plugins
41 Koha::Plugins::Handler->run({ class => $class, method => $method, cgi => $cgi });
55 my ( $class, $args ) = @_;
57 return unless ( C4
::Context
->config("enable_plugins") || $args->{'enable_plugins'} );
59 my $plugin_class = $args->{'class'};
60 my $plugin_method = $args->{'method'};
61 my $cgi = $args->{'cgi'};
62 my $params = $args->{'params'};
64 if ( can_load
( modules
=> { $plugin_class => undef } ) ) {
65 my $plugin = $plugin_class->new( { cgi
=> $cgi, enable_plugins
=> $args->{'enable_plugins'} } );
66 if ( $plugin->can($plugin_method) ) {
67 return $plugin->$plugin_method( $params );
69 warn "Plugin does not have method $plugin_method";
72 warn "Plugin $plugin_class cannot be loaded";
83 my ( $class, $args ) = @_;
85 return unless ( C4
::Context
->config("enable_plugins") || $args->{'enable_plugins'} );
87 my $plugin_class = $args->{'class'};
89 my $plugin_path = $plugin_class;
90 $plugin_path =~ s/::/\//g; # Take class name, transform :: to / to get path
91 $plugin_path =~ s/$/.pm/; # Add .pm to the end
92 require $plugin_path; # Require the plugin to have it's path listed in INC
94 $INC{$plugin_path}; # Get the full true path to the plugin from INC
95 $plugin_path =~ s/.pm//; # Remove the .pm from the end
97 Koha
::Plugins
::Handler
->run({
98 class => $plugin_class,
99 method
=> 'uninstall',
100 enable_plugins
=> $args->{enable_plugins
},
103 C4
::Context
->dbh->do( "DELETE FROM plugin_data WHERE plugin_class = ?", undef, ($plugin_class) );
105 unlink "$plugin_path.pm" or warn "Could not unlink $plugin_path.pm: $!";
106 remove_tree
($plugin_path);
116 Kyle M Hall <kyle.m.hall@gmail.com>