Bug 24031: Add plugin hook after_hold_create
[koha.git] / Koha / Plugins.pm
1 package Koha::Plugins;
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 Class::Inspector;
23 use List::MoreUtils qw(any);
24 use Module::Load::Conditional qw(can_load);
25 use Module::Load qw(load);
26 use Module::Pluggable search_path => ['Koha::Plugin'], except => qr/::Edifact(|::Line|::Message|::Order|::Segment|::Transport)$/;
27
28 use C4::Context;
29 use C4::Output;
30 use Koha::Plugins::Methods;
31
32 BEGIN {
33     my $pluginsdir = C4::Context->config("pluginsdir");
34     my @pluginsdir = ref($pluginsdir) eq 'ARRAY' ? @$pluginsdir : $pluginsdir;
35     push( @INC, @pluginsdir );
36     pop @INC if $INC[-1] eq '.';
37 }
38
39 =head1 NAME
40
41 Koha::Plugins - Module for loading and managing plugins.
42
43 =cut
44
45 sub new {
46     my ( $class, $args ) = @_;
47
48     return unless ( C4::Context->config("enable_plugins") || $args->{'enable_plugins'} );
49
50     $args->{'pluginsdir'} = C4::Context->config("pluginsdir");
51
52     return bless( $args, $class );
53 }
54
55 =head2 call
56
57 Calls a plugin method for all enabled plugins
58
59     @responses = Koha::Plugins->call($method, @args)
60
61 =cut
62
63 sub call {
64     my ($class, $method, @args) = @_;
65
66     if (C4::Context->preference('UseKohaPlugins') && C4::Context->config('enable_plugins')) {
67         my @plugins = $class->new({ enable_plugins => 1 })->GetPlugins({ method => $method });
68         my @responses;
69         foreach my $plugin (@plugins) {
70             my $response = $plugin->$method(@args);
71             push @responses, $response;
72         }
73
74         return @responses;
75     }
76 }
77
78 =head2 GetPlugins
79
80 This will return a list of all available plugins, optionally limited by
81 method or metadata value.
82
83     my @plugins = Koha::Plugins::GetPlugins({
84         method => 'some_method',
85         metadata => { some_key => 'some_value' },
86     });
87
88 The method and metadata parameters are optional.
89 Available methods currently are: 'report', 'tool', 'to_marc', 'edifact'.
90 If you pass multiple keys in the metadata hash, all keys must match.
91
92 =cut
93
94 sub GetPlugins {
95     my ( $self, $params ) = @_;
96
97     my $method       = $params->{method};
98     my $req_metadata = $params->{metadata} // {};
99
100     my $filter = ( $method ) ? { plugin_method => $method } : undef;
101
102     my $plugin_classes = Koha::Plugins::Methods->search(
103         $filter,
104         {   columns  => 'plugin_class',
105             distinct => 1
106         }
107     )->_resultset->get_column('plugin_class');
108
109     my @plugins;
110
111     # Loop through all plugins that implement at least a method
112     while ( my $plugin_class = $plugin_classes->next ) {
113
114         load $plugin_class;
115         my $plugin = $plugin_class->new({
116             enable_plugins => $self->{'enable_plugins'}
117                 # loads even if plugins are disabled
118                 # FIXME: is this for testing without bothering to mock config?
119         });
120
121         next unless $plugin->is_enabled or
122                     defined($params->{all}) && $params->{all};
123
124         # filter the plugin out by metadata
125         my $plugin_metadata = $plugin->get_metadata;
126         next
127             if $plugin_metadata
128             and %$req_metadata
129             and any { !$plugin_metadata->{$_} || $plugin_metadata->{$_} ne $req_metadata->{$_} } keys %$req_metadata;
130
131         push @plugins, $plugin;
132
133     }
134
135     return @plugins;
136 }
137
138 =head2 InstallPlugins
139
140 Koha::Plugins::InstallPlugins()
141
142 This method iterates through all plugins physically present on a system.
143 For each plugin module found, it will test that the plugin can be loaded,
144 and if it can, will store its available methods in the plugin_methods table.
145
146 NOTE: We re-load all plugins here as a protective measure in case someone
147 has removed a plugin directly from the system without using the UI
148
149 =cut
150
151 sub InstallPlugins {
152     my ( $self, $params ) = @_;
153
154     my @plugin_classes = $self->plugins();
155     my @plugins;
156
157     foreach my $plugin_class (@plugin_classes) {
158         if ( can_load( modules => { $plugin_class => undef }, nocache => 1 ) ) {
159             next unless $plugin_class->isa('Koha::Plugins::Base');
160
161             my $plugin = $plugin_class->new({ enable_plugins => $self->{'enable_plugins'} });
162
163             Koha::Plugins::Methods->search({ plugin_class => $plugin_class })->delete();
164
165             foreach my $method ( @{ Class::Inspector->methods( $plugin_class, 'public' ) } ) {
166                 Koha::Plugins::Method->new(
167                     {
168                         plugin_class  => $plugin_class,
169                         plugin_method => $method,
170                     }
171                 )->store();
172             }
173
174             push @plugins, $plugin;
175         } else {
176             my $error = $Module::Load::Conditional::ERROR;
177             # Do not warn the error if the plugin has been uninstalled
178             warn $error unless $error =~ m|^Could not find or check module '$plugin_class'|;
179         }
180     }
181     return @plugins;
182 }
183
184 1;
185 __END__
186
187 =head1 AVAILABLE HOOKS
188
189 =head2 after_hold_create
190
191 =head3 Parameters
192
193 =over
194
195 =item * C<$hold> - A Koha::Hold object that has just been inserted in database
196
197 =back
198
199 =head3 Return value
200
201 None
202
203 =head3 Example
204
205     sub after_hold_create {
206         my ($self, $hold) = @_;
207
208         warn "New hold for borrower " . $hold->borrower->borrowernumber;
209     }
210
211 =head1 AUTHOR
212
213 Kyle M Hall <kyle.m.hall@gmail.com>
214
215 =cut