Merge branch 'bug_7804' into 3.12-master
[koha.git] / Koha / Plugins / Handler.pm
1 package Koha::Plugins::Handler;
2
3 # Copyright 2012 Kyle Hall
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20 use Modern::Perl;
21
22 use File::Path qw(remove_tree);
23
24 use Module::Load::Conditional qw(can_load);
25
26 use C4::Context;
27
28 BEGIN {
29     push @INC, C4::Context->config("pluginsdir");
30 }
31
32 =head1 NAME
33
34 C4::Plugins::Handler - Handler Module for running plugins
35
36 =head1 SYNOPSIS
37
38   Koha::Plugins::Handler->run({ class => $class, method => $method, cgi => $cgi });
39   $p->run();
40
41 =over 2
42
43 =cut
44
45 =item run
46
47 Runs a plugin
48
49 =cut
50
51 sub run {
52     my ( $class, $args ) = @_;
53
54     return unless ( C4::Context->config("enable_plugins") || $args->{'enable_plugins'} );
55
56     my $plugin_class  = $args->{'class'};
57     my $plugin_method = $args->{'method'};
58     my $cgi           = $args->{'cgi'};
59
60     if ( can_load( modules => { $plugin_class => undef } ) ) {
61         my $plugin = $plugin_class->new( { cgi => $cgi } );
62         if ( $plugin->can($plugin_method) ) {
63             return $plugin->$plugin_method();
64         } else {
65             warn "Plugin does not have method $plugin_method";
66         }
67     } else {
68         warn "Plugin $plugin_class cannot be loaded";
69     }
70 }
71
72 =item delete
73
74 Deletes a plugin
75
76 =cut
77
78 sub delete {
79     my ( $class, $args ) = @_;
80     my $plugin_class = $args->{'class'};
81     my $plugin_dir   = C4::Context->config("pluginsdir");
82     my $plugin_path  = "$plugin_dir/" . join( '/', split( '::', $args->{'class'} ) );
83
84     Koha::Plugins::Handler->run( { class => $plugin_class, method => 'uninstall' } );
85
86     C4::Context->dbh->do( "DELETE FROM plugin_data WHERE plugin_class = ?", undef, ($plugin_class) );
87
88     unlink("$plugin_path.pm");
89     remove_tree($plugin_path);
90 }
91
92 1;
93 __END__
94
95 =back
96
97 =head1 AUTHOR
98
99 Kyle M Hall <kyle.m.hall@gmail.com>
100
101 =cut