Bug 7804 - Add Koha Plugin System
[koha-equinox.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     die('Plugins not enabled in config') unless ( C4::Context->config("enable_plugins") );
30
31     push @INC, C4::Context->config("pluginsdir");
32 }
33
34 =head1 NAME
35
36 C4::Plugins::Handler - Handler Module for running plugins
37
38 =head1 SYNOPSIS
39
40   Koha::Plugins::Handler->run({ class => $class, method => $method, cgi => $cgi });
41   $p->run();
42
43 =over 2
44
45 =cut
46
47 =item run
48
49 Runs a plugin
50
51 =cut
52
53 sub run {
54     my ( $class, $args ) = @_;
55     my $plugin_class  = $args->{'class'};
56     my $plugin_method = $args->{'method'};
57     my $cgi           = $args->{'cgi'};
58
59     if ( can_load( modules => { $plugin_class => undef } ) ) {
60         my $plugin = $plugin_class->new( { cgi => $cgi } );
61         if ( $plugin->can($plugin_method) ) {
62             $plugin->$plugin_method();
63         } else {
64             warn "Plugin does not have method $plugin_method";
65         }
66     } else {
67         warn "Plugin $plugin_class cannot be loaded";
68     }
69 }
70
71 =item delete
72
73 Deletes a plugin
74
75 =cut
76
77 sub delete {
78     my ( $class, $args ) = @_;
79     my $plugin_class = $args->{'class'};
80     my $plugin_dir   = C4::Context->config("pluginsdir");
81     my $plugin_path  = "$plugin_dir/" . join( '/', split( '::', $args->{'class'} ) );
82
83     Koha::Plugins::Handler->run( { class => $plugin_class, method => 'uninstall' } );
84
85     C4::Context->dbh->do( "DELETE FROM plugin_data WHERE plugin_class = ?", undef, ($plugin_class) );
86
87     unlink("$plugin_path.pm");
88     remove_tree($plugin_path);
89 }
90
91 1;
92 __END__
93
94 =back
95
96 =head1 AUTHOR
97
98 Kyle M Hall <kyle.m.hall@gmail.com>
99
100 =cut