|
Line 0
Link Here
|
| 0 |
- |
1 |
package Koha::Illrequest::Workflow::TypeDisclaimer; |
|
|
2 |
|
| 3 |
# Copyright 2023 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 POSIX qw( strftime ); |
| 23 |
|
| 24 |
use base qw(Koha::Illrequest::Workflow); |
| 25 |
|
| 26 |
=head1 NAME |
| 27 |
|
| 28 |
Koha::Illrequest::TypeDisclaimer - Koha ILL TypeDisclaimer |
| 29 |
|
| 30 |
=head1 SYNOPSIS |
| 31 |
|
| 32 |
Object-oriented class that provides the ILL request type disclaimer |
| 33 |
|
| 34 |
=head1 DESCRIPTION |
| 35 |
|
| 36 |
This class provides the ability to verify if it should render type disclaimer |
| 37 |
and handle the template params accordingly |
| 38 |
|
| 39 |
=head1 API |
| 40 |
|
| 41 |
=head2 Class Methods |
| 42 |
|
| 43 |
=head3 show_type_disclaimer |
| 44 |
|
| 45 |
my $show_type_disclaimer = |
| 46 |
Koha::Illrequest::TypeDisclaimer->show_type_disclaimer($params); |
| 47 |
|
| 48 |
Given $params, return true if type disclaimer should be shown |
| 49 |
|
| 50 |
=cut |
| 51 |
|
| 52 |
sub show_type_disclaimer { |
| 53 |
my ( $self, $request ) = @_; |
| 54 |
|
| 55 |
my $disc_sys_pref = $self->_get_type_disclaimer_sys_pref; |
| 56 |
|
| 57 |
my $disc_info = |
| 58 |
$self->_get_type_disclaimer_info( $self->_get_type_disclaimer_sys_pref, |
| 59 |
$self->{metadata}->{type} ); |
| 60 |
|
| 61 |
return |
| 62 |
|
| 63 |
# ILLModuleDisclaimerByType contains correct YAML |
| 64 |
%{$disc_sys_pref} |
| 65 |
|
| 66 |
# Check that we have info to display for this type |
| 67 |
&& $disc_info |
| 68 |
|
| 69 |
# ILLModuleDisclaimerByType contains at least 'all' |
| 70 |
&& $disc_sys_pref->{all} |
| 71 |
|
| 72 |
# Type disclaimer has not yet been submitted |
| 73 |
&& !$self->{metadata}->{type_disclaimer_submitted} |
| 74 |
|
| 75 |
# The form has been submitted and the backend is able to create the request |
| 76 |
&& $request->_backend_capability( 'can_create_request', |
| 77 |
$self->{metadata} ); |
| 78 |
} |
| 79 |
|
| 80 |
=head3 type_disclaimer_template_params |
| 81 |
|
| 82 |
my $type_disclaimer_template_params = |
| 83 |
Koha::Illrequest::TypeDisclaimer->type_disclaimer_template_params( |
| 84 |
$params); |
| 85 |
|
| 86 |
Given $params, return true if type disclaimer should be rendered |
| 87 |
|
| 88 |
=cut |
| 89 |
|
| 90 |
sub type_disclaimer_template_params { |
| 91 |
my ( $self, $params ) = @_; |
| 92 |
|
| 93 |
my $disc_info = |
| 94 |
$self->_get_type_disclaimer_info( $self->_get_type_disclaimer_sys_pref, |
| 95 |
$params->{type} ); |
| 96 |
|
| 97 |
$params->{method} = 'typedisclaimer' if $self->{ui_context} eq 'staff'; |
| 98 |
delete $params->{stage} if $self->{ui_context} eq 'staff'; |
| 99 |
|
| 100 |
return ( |
| 101 |
whole => $params, |
| 102 |
metadata => $self->prep_metadata($params), |
| 103 |
disclaimer => $disc_info, |
| 104 |
$self->{ui_context} eq 'opac' |
| 105 |
? ( |
| 106 |
illrequestsview => 1, |
| 107 |
message => $params->{message}, |
| 108 |
method => 'typedisclaimer', |
| 109 |
) |
| 110 |
: () |
| 111 |
); |
| 112 |
} |
| 113 |
|
| 114 |
=head3 after_request_created |
| 115 |
|
| 116 |
#example here |
| 117 |
|
| 118 |
#description here |
| 119 |
|
| 120 |
=cut |
| 121 |
|
| 122 |
sub after_request_created { |
| 123 |
my ( $self, $params, $request ) = @_; |
| 124 |
|
| 125 |
# Store type disclaimer date and value |
| 126 |
my $type_disclaimer_date = { |
| 127 |
illrequest_id => $request->illrequest_id, |
| 128 |
type => "type_disclaimer_date", |
| 129 |
value => strftime( "%Y-%m-%dT%H:%M:%S", localtime( time() ) ), |
| 130 |
readonly => 0 |
| 131 |
}; |
| 132 |
Koha::Illrequestattribute->new($type_disclaimer_date)->store; |
| 133 |
|
| 134 |
my $type_disclaimer_value = { |
| 135 |
illrequest_id => $request->illrequest_id, |
| 136 |
type => "type_disclaimer_value", |
| 137 |
value => $params->{type_disclaimer_value}, |
| 138 |
readonly => 0 |
| 139 |
}; |
| 140 |
Koha::Illrequestattribute->new($type_disclaimer_value)->store; |
| 141 |
} |
| 142 |
|
| 143 |
=head3 _get_type_disclaimer_info |
| 144 |
|
| 145 |
my $type_disclaimer_info = |
| 146 |
$self->_get_type_disclaimer_info( $type_disclaimer_sys_pref, $request_type ); |
| 147 |
|
| 148 |
Given ILLModuleDisclaimerByType sys pref and type, returns the respective |
| 149 |
type disclaimer info |
| 150 |
Returns undef if sys pref is empty or malformed |
| 151 |
=cut |
| 152 |
|
| 153 |
sub _get_type_disclaimer_info { |
| 154 |
my ( $self, $disc_sys_pref, $type ) = @_; |
| 155 |
|
| 156 |
my @matching_request_type = |
| 157 |
map ( $_ eq $type ? $_ : (), keys %$disc_sys_pref ); |
| 158 |
|
| 159 |
my $disc_info = undef; |
| 160 |
if ( scalar @matching_request_type ) { |
| 161 |
return if $disc_sys_pref->{$type}->{bypass}; |
| 162 |
|
| 163 |
$disc_info->{text} = $disc_sys_pref->{$type}->{text}; |
| 164 |
$disc_info->{av_cat} = $disc_sys_pref->{$type}->{av_category_code}; |
| 165 |
} |
| 166 |
elsif ( $disc_sys_pref->{all} ) { |
| 167 |
$disc_info->{text} = $disc_sys_pref->{all}->{text}; |
| 168 |
$disc_info->{av_cat} = $disc_sys_pref->{all}->{av_category_code}; |
| 169 |
} |
| 170 |
return $disc_info; |
| 171 |
} |
| 172 |
|
| 173 |
=head3 _get_type_disclaimer_sys_pref |
| 174 |
|
| 175 |
my $disc_sys_pref = $self->_get_type_disclaimer_sys_pref; |
| 176 |
|
| 177 |
Returns YAML from ILLModuleDisclaimerByType syspref |
| 178 |
Returns empty if empty or YAML error |
| 179 |
|
| 180 |
=cut |
| 181 |
|
| 182 |
sub _get_type_disclaimer_sys_pref { |
| 183 |
my ($self) = @_; |
| 184 |
|
| 185 |
return C4::Context->yaml_preference("ILLModuleDisclaimerByType") // {}; |
| 186 |
} |
| 187 |
|
| 188 |
=head1 AUTHOR |
| 189 |
|
| 190 |
Pedro Amorim <pedro.amorim@ptfs-europe.com> |
| 191 |
|
| 192 |
=cut |
| 193 |
|
| 194 |
1; |