Bug 12412: Add ability for plugins to convert arbitrary files to MARC from record...
[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::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     my $params        = $args->{'params'};
60
61     if ( can_load( modules => { $plugin_class => undef } ) ) {
62         my $plugin = $plugin_class->new( { cgi => $cgi, enable_plugins => $args->{'enable_plugins'} } );
63         if ( $plugin->can($plugin_method) ) {
64             return $plugin->$plugin_method( $params );
65         } else {
66             warn "Plugin does not have method $plugin_method";
67         }
68     } else {
69         warn "Plugin $plugin_class cannot be loaded";
70     }
71 }
72
73 =item delete
74
75 Deletes a plugin
76
77 =cut
78
79 sub delete {
80     my ( $class, $args ) = @_;
81     my $plugin_class = $args->{'class'};
82     my $plugin_dir   = C4::Context->config("pluginsdir");
83     my $plugin_path  = "$plugin_dir/" . join( '/', split( '::', $args->{'class'} ) );
84
85     Koha::Plugins::Handler->run( { class => $plugin_class, method => 'uninstall' } );
86
87     C4::Context->dbh->do( "DELETE FROM plugin_data WHERE plugin_class = ?", undef, ($plugin_class) );
88
89     unlink("$plugin_path.pm");
90     remove_tree($plugin_path);
91 }
92
93 1;
94 __END__
95
96 =back
97
98 =head1 AUTHOR
99
100 Kyle M Hall <kyle.m.hall@gmail.com>
101
102 =cut