Bug 14224: Update Copyright
[koha.git] / opac / svc / patron_notes
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Copyright 2016 Aleisha Amohia <aleisha@catalyst.net.nz>
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 C4::Service;
23 use C4::Auth qw /check_cookie_auth/;
24 use C4::Letters;
25 use CGI;
26 use C4::Output qw(:DEFAULT :ajax);
27 use C4::Scrubber;
28 use C4::Circulation;
29 use C4::Members;
30 use C4::Biblio;
31 use Koha::Checkouts;
32 use Koha::DateUtils;
33
34 =head1 NAME
35
36 svc/patron_notes - Web service for setting patron notes on items
37
38 =head1 DESCRIPTION
39
40 =cut
41
42 # AJAX requests
43 my $is_ajax = is_ajax();
44 my $query = new CGI;
45 my ( $auth_status, $sessionID ) = check_cookie_auth( $query->cookie('CGISESSID'), {} );
46 if ( $auth_status ne "ok" ) {
47     exit 0;
48 }
49 if ($is_ajax) {
50     my $action = $query->param('action');
51
52     # Issue Note
53     if ( $action eq 'issuenote' && C4::Context->preference('AllowCheckoutNotes') ) {
54         my $scrubber = C4::Scrubber->new();
55         my $note = $query->param('note');
56         my $issue_id = $query->param('issue_id');
57         my $clean_note = $scrubber->scrub($note);
58         my $status = "saved";
59         my ($member, $issue);
60
61         my ( $template, $borrowernumber, $cookie ) = C4::Auth::get_template_and_user({
62             template_name   => "opac-user.tt",
63             query           => $query,
64             type            => "opac",
65             authnotrequired => 1,
66         });
67
68         # verify issue_id
69         if ( $issue_id =~ /\d+/ ) {
70             $member = GetMember(borrowernumber => $borrowernumber);
71             $issue = Koha::Checkouts->find($issue_id);
72             if ( $issue->borrowernumber != $borrowernumber ) {
73                 $status = "fail";
74             }
75         }
76
77         if ( $issue ) {
78             $issue->set({ notedate => dt_from_string(), note => $clean_note })->store;
79             if($clean_note) { # only send email if note not empty
80                 my $branch = Koha::Libraries->find( $issue->branchcode );
81                 my $biblio = GetBiblioFromItemNumber($issue->itemnumber);
82                 my $letter = C4::Letters::GetPreparedLetter (
83                     module => 'circulation',
84                     letter_code => 'PATRON_NOTE',
85                     branchcode => $branch,
86                     tables => {
87                         'biblio' => $biblio->{biblionumber},
88                         'borrowers' => $member->{borrowernumber},
89                     },
90                 );
91                 C4::Message->enqueue($letter, $member, 'email');
92             } else { # note empty, i.e removed
93                 $status = "removed";
94             }
95         } else {
96             $status = "fail";
97         }
98
99         my $response = "{\"status\": \"$status\", \"note\": \"$clean_note\", \"issue_id\": \"$issue_id\"}";
100         output_with_http_headers($query, undef, $response, 'js');
101         exit;
102     } # END Issue Note
103 }