Bug 35930: Add guards for plugins_enabled
[koha.git] / Koha / Illrequest / Workflow / Availability.pm
1 package Koha::Illrequest::Workflow::Availability;
2
3 # Copyright 2019 PTFS Europe Ltd
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 JSON;
23
24 use base qw(Koha::Illrequest::Workflow);
25
26 use Koha::Plugins;
27
28 =head1 NAME
29
30 Koha::Illrequest::Workflow::Availability - Koha ILL Availability Searching
31
32 =head1 SYNOPSIS
33
34 Object-oriented class that provides availability searching via
35 availability plugins
36
37 =head1 DESCRIPTION
38
39 This class provides the ability to identify and fetch API services
40 that can be used to search for item availability
41
42 =head1 API
43
44 =head2 Class Methods
45
46 =head3 get_services
47
48     my $services =
49       Koha::Illrequest::Workflow::Availability->get_services($params);
50
51 Given our metadata, iterate plugins with the right method and
52 check if they can service our request and, if so, return an arrayref
53 of services. Optionally accept a hashref specifying additional filter
54 parameters
55
56 =cut
57
58 sub get_services {
59     my ( $self, $params ) = @_;
60
61     my $plugin_filter = { method => 'ill_availability_services' };
62
63     if ( $params->{metadata} ) {
64         $plugin_filter->{metadata} = $params->{metadata};
65     }
66
67     return [] unless C4::Context->config("enable_plugins");
68
69     my @candidates = Koha::Plugins->new()->GetPlugins($plugin_filter);
70     my @services   = ();
71     foreach my $plugin (@candidates) {
72         my $valid_service = $plugin->ill_availability_services(
73             {
74                 metadata   => $self->{metadata},
75                 ui_context => $self->{ui_context},
76             }
77         );
78         push @services, $valid_service if $valid_service;
79     }
80
81     return \@services;
82 }
83
84 =head3 show_availability
85
86     my $show_availability =
87     Koha::Illrequest::Workflow::Availability->show_availability($params);
88
89 Given $params, return true if availability should be shown
90
91 =cut
92
93 sub show_availability {
94     my ( $self, $request ) = @_;
95
96     my $services = $self->get_services;
97
98     return
99
100       # ILLCheckAvailability is enabled
101       C4::Context->preference("ILLCheckAvailability")
102
103       # At least 1 availability service exists
104       && scalar @{$services}
105
106       # Availability has not yet been checked
107       && !$self->{metadata}->{checked_availability}
108
109      # The form has been submitted and the backend is able to create the request
110       && $request->_backend_capability( 'can_create_request',
111         $self->{metadata} );
112 }
113
114 =head3 availability_template_params
115
116     my $availability_template_params =
117     Koha::Illrequest::Workflow::Availability->availability_template_params(
118         $params);
119
120 Given $params, return true if availability should be shown
121
122 =cut
123
124 sub availability_template_params {
125     my ( $self, $params ) = @_;
126
127     $params->{method} = 'availability' if $self->{ui_context} eq 'staff';
128     delete $params->{stage}            if $self->{ui_context} eq 'staff';
129     my $services = $self->get_services;
130
131     return (
132         whole         => $params,
133         metadata      => $self->prep_metadata($params),
134         services_json => scalar encode_json($services),
135         services      => $services,
136         $self->{ui_context} eq 'opac'
137         ? (
138             illrequestsview => 1,
139             message         => $params->{message},
140             method          => 'availability',
141           )
142         : ()
143     );
144 }
145
146 =head1 AUTHOR
147
148 Andrew Isherwood <andrew.isherwood@ptfs-europe.com>
149
150 =cut
151
152 1;