Bug 29544: Fix opac-issue-note.pl
[koha.git] / opac / opac-issue-note.pl
1 #!/usr/bin/perl
2
3 # Copyright 2016 Aleisha Amohia <aleisha@catalyst.net.nz>
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 use C4::Koha;
24 use C4::Context;
25 use C4::Scrubber;
26 use C4::Output qw( output_html_with_http_headers );
27 use C4::Auth qw( get_template_and_user );
28 use C4::Letters;
29 use Koha::Checkouts;
30 use Koha::DateUtils qw( dt_from_string );
31 use Koha::Patrons;
32
33 my $query = CGI->new;
34
35 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
36     {
37         template_name   => "opac-issue-note.tt",
38         query           => $query,
39         type            => "opac",
40     }
41 );
42
43 my $patron = Koha::Patrons->find( $borrowernumber );
44 $template->param(
45     firstname      => $patron->firstname,
46     surname        => $patron->surname,
47     borrowernumber => $borrowernumber,
48 );
49
50 my $issue_id = $query->param('issue_id');
51 my $issue = Koha::Checkouts->find( $issue_id );
52
53
54 if ( !$issue || $issue->borrowernumber != $borrowernumber ) {
55     # exit early
56     print $query->redirect("/cgi-bin/koha/opac-user.pl");
57     exit;
58 }
59
60 my $itemnumber = $issue->itemnumber;
61 my $biblio = $issue->item->biblio;
62 $template->param(
63     issue_id   => $issue_id,
64     title      => $biblio->title,
65     author     => $biblio->author,
66     note       => $issue->note,
67     itemnumber => $issue->itemnumber,
68 );
69
70 my $action = $query->param('action') || "";
71 if ( $action eq 'issuenote' && C4::Context->preference('AllowCheckoutNotes') && $issue ) {
72     my $note = $query->param('note');
73     my $scrubber = C4::Scrubber->new();
74     my $clean_note = $scrubber->scrub($note);
75
76     if ( $issue->set({ notedate => dt_from_string(), note => $clean_note, noteseen => 0 })->store ) {
77         if ($clean_note) { # only send email if note not empty
78             my $branch = Koha::Libraries->find( $issue->branchcode );
79             my $letter = C4::Letters::GetPreparedLetter (
80                 module => 'circulation',
81                 letter_code => 'CHECKOUT_NOTE',
82                 branchcode => $branch,
83                 lang => $patron->lang,
84                 tables => {
85                     'biblio' => $biblio->biblionumber,
86                     'borrowers' => $borrowernumber,
87                 },
88             );
89
90             my $to_address = $branch->inbound_email_address;
91             my $reply_address = $patron->email || $patron->emailpro || $patron->B_email;
92
93             C4::Letters::EnqueueLetter({
94                 letter => $letter,
95                 message_transport_type => 'email',
96                 borrowernumber => $patron->borrowernumber,
97                 to_address => $to_address,
98                 reply_address => $reply_address,
99             });
100         }
101     }
102     print $query->redirect("/cgi-bin/koha/opac-user.pl");
103 }
104
105 output_html_with_http_headers $query, $cookie, $template->output, undef, { force_no_caching => 1 };