|
Line 0
Link Here
|
| 0 |
- |
1 |
#!/usr/bin/perl |
|
|
2 |
# Copyright 2021 AutoParallel Technologies Inc. |
| 3 |
# Copyright 2009 PTFS Inc. |
| 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 CGI qw ( -utf8 ); |
| 23 |
|
| 24 |
use C4::Auth qw( get_template_and_user ); |
| 25 |
use C4::Output; |
| 26 |
use Koha::Patron::Messages; |
| 27 |
|
| 28 |
use Try::Tiny; |
| 29 |
use JSON qw//; |
| 30 |
|
| 31 |
use constant { |
| 32 |
TRUE => 1, |
| 33 |
FALSE => 0, |
| 34 |
json => JSON->new->pretty, |
| 35 |
REDIRECT_URL => '/cgi-bin/koha/opac-user.pl', |
| 36 |
}; |
| 37 |
|
| 38 |
sub dismiss_patron_message { |
| 39 |
# accept a CGI object as input |
| 40 |
my ($cgi) = @_; |
| 41 |
|
| 42 |
# get input parameters |
| 43 |
my $message_id = $cgi->param('message_id') || undef; |
| 44 |
my $borrowernumber = $cgi->param('borrowernumber') || undef; |
| 45 |
my $inputs = { |
| 46 |
message_id => $message_id, |
| 47 |
borrowernumber => $borrowernumber, |
| 48 |
}; |
| 49 |
|
| 50 |
# set default result values |
| 51 |
my $result = { |
| 52 |
success => FALSE, |
| 53 |
comment => 'default failure', |
| 54 |
inputs => $inputs, |
| 55 |
}; |
| 56 |
|
| 57 |
|
| 58 |
my $rs = Koha::Patron::Messages->search($inputs); |
| 59 |
my $message = $rs->next(); # Koha::Patron::Messages->find($message_id); |
| 60 |
#- exit early if no message found matching message_id |
| 61 |
if (! defined $message) { |
| 62 |
no warnings qw/uninitialized/; |
| 63 |
return { |
| 64 |
%$result, |
| 65 |
comment => "Couldn't find message for parameters", |
| 66 |
inputs => $inputs, |
| 67 |
}; |
| 68 |
} |
| 69 |
|
| 70 |
#- found message, so success is TRUE |
| 71 |
$result->{success} = TRUE; |
| 72 |
|
| 73 |
#- exit early if patron_read_date is already set |
| 74 |
if (defined $message->patron_read_date) { |
| 75 |
return { |
| 76 |
%$result, |
| 77 |
comment => "Patron read date already set", |
| 78 |
inputs => $inputs, |
| 79 |
}; |
| 80 |
} |
| 81 |
|
| 82 |
#- update the patron_read_date to database's NOW() |
| 83 |
$message->update({ |
| 84 |
patron_read_date => \'NOW()', |
| 85 |
}); |
| 86 |
|
| 87 |
my $patron_read_date = $message->patron_read_date || undef; |
| 88 |
return { |
| 89 |
%$result, |
| 90 |
inputs => $inputs, |
| 91 |
comment => "Set patron_read_date to ($patron_read_date)", |
| 92 |
}; |
| 93 |
} |
| 94 |
|
| 95 |
my CGI $cgi; |
| 96 |
my $result; |
| 97 |
try { |
| 98 |
$cgi = CGI->new; |
| 99 |
$result = dismiss_patron_message($cgi); |
| 100 |
|
| 101 |
# uncomment assignment below if you want to view environment |
| 102 |
# $result->{env} = \%ENV; |
| 103 |
|
| 104 |
|
| 105 |
} |
| 106 |
catch { |
| 107 |
my ($exception) = $_; |
| 108 |
|
| 109 |
# no warnings qw/once/; |
| 110 |
# my $ig = 'Examine variable $exception'; |
| 111 |
# push @DB::typeahead, 'x $exception'; |
| 112 |
# $DB::single++; |
| 113 |
|
| 114 |
$result->{exception} = $exception; |
| 115 |
$result->{success} = FALSE; |
| 116 |
} |
| 117 |
finally { |
| 118 |
# redirect to referrer |
| 119 |
my $redir_url = $ENV{HTTP_REFERRER} || REDIRECT_URL; |
| 120 |
print $cgi->redirect($redir_url); |
| 121 |
}; |