Bug 14570: (follow-up) fix qa FAIL tests
[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
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.
11 #
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.
16 #
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>.
19
20 use Modern::Perl;
21
22 use File::Path qw(remove_tree);
23
24 use Module::Load qw(load);
25
26 use C4::Context;
27 use Koha::Plugins::Methods;
28
29 BEGIN {
30     my $pluginsdir = C4::Context->config("pluginsdir");
31     my @pluginsdir = ref($pluginsdir) eq 'ARRAY' ? @$pluginsdir : $pluginsdir;
32     push( @INC, @pluginsdir );
33     pop @INC if $INC[-1] eq '.' ;
34 }
35
36 =head1 NAME
37
38 Koha::Plugins::Handler - Handler Module for running plugins
39
40 =head1 SYNOPSIS
41
42   Koha::Plugins::Handler->run({ class => $class, method => $method, cgi => $cgi });
43   $p->run();
44
45 =over 2
46
47 =cut
48
49 =item run
50
51 Runs a plugin
52
53 =cut
54
55 sub run {
56     my ( $class, $args ) = @_;
57
58     return unless ( C4::Context->config("enable_plugins") || $args->{'enable_plugins'} );
59
60     my $plugin_class  = $args->{'class'};
61     my $plugin_method = $args->{'method'};
62     my $cgi           = $args->{'cgi'};
63     my $params        = $args->{'params'};
64
65     my $has_method = Koha::Plugins::Methods->search({ plugin_class => $plugin_class, plugin_method => $plugin_method })->count();
66     if ( $has_method ) {
67         load $plugin_class;
68         my $plugin = $plugin_class->new( { cgi => $cgi, enable_plugins => $args->{'enable_plugins'} } );
69         return $plugin->$plugin_method( $params );
70     } else {
71         warn "Plugin does not have method $plugin_method";
72         return;
73     }
74 }
75
76 =item delete
77
78 Deletes a plugin
79
80 =cut
81
82 sub delete {
83     my ( $class, $args ) = @_;
84
85     return unless ( C4::Context->config("enable_plugins") || $args->{'enable_plugins'} );
86
87     my $plugin_class = $args->{'class'};
88
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
93     $plugin_path =
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
96
97     Koha::Plugins::Handler->run({
98         class          => $plugin_class,
99         method         => 'uninstall',
100         enable_plugins => $args->{enable_plugins},
101     });
102
103     C4::Context->dbh->do( "DELETE FROM plugin_data WHERE plugin_class = ?", undef, ($plugin_class) );
104     Koha::Plugins::Methods->search({ plugin_class => $plugin_class })->delete();
105
106     unlink "$plugin_path.pm" or warn "Could not unlink $plugin_path.pm: $!";
107     remove_tree($plugin_path);
108 }
109
110 1;
111 __END__
112
113 =back
114
115 =head1 AUTHOR
116
117 Kyle M Hall <kyle.m.hall@gmail.com>
118
119 =cut