]> git.koha-community.org Git - koha.git/blob - opac/opac-reportproblem.pl
Bug 24157: New permission - delete_baskets
[koha.git] / opac / opac-reportproblem.pl
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Copyright 2019 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 use CGI qw ( -utf8 );
22 use Try::Tiny;
23
24 use C4::Auth;    # get_template_and_user
25 use C4::Output;
26 use C4::Letters;
27 use Koha::ProblemReport;
28 use Koha::Libraries;
29 use Koha::Patrons;
30 use Koha::Util::Navigation;
31 use URI::Escape;
32 use Encode;
33
34 my $input = new CGI;
35
36 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
37     {
38         template_name   => "opac-reportproblem.tt",
39         type            => "opac",
40         query           => $input,
41         authnotrequired => 0,
42     }
43 );
44
45 if (   !C4::Context->preference('OPACReportProblem')
46     || !C4::Context->preference('KohaAdminEmailAddress') )
47 {
48     print $input->redirect("/cgi-bin/koha/errors/404.pl");
49 }
50
51 my $referer = Koha::Util::Navigation::local_referer($input );
52 $referer = Encode::decode_utf8 uri_unescape $referer,
53
54 my $patron = Koha::Patrons->find($borrowernumber);
55 my $library = $patron->library;
56 my $username = $patron->userid;
57 my @messages;
58
59 $template->param(
60     username    => $username,
61     problempage => $referer,
62     library     => $library,
63 );
64
65 my $op = $input->param('op') || '';
66 if ( $op eq 'addreport' ) {
67
68     my $subject = $input->param('subject');
69     my $message = $input->param('message');
70     my $problempage = $input->param('problempage');
71     $problempage = Encode::decode_utf8 uri_unescape $problempage;
72     my $recipient = $input->param('recipient') || 'admin';
73
74     try {
75         my $schema = Koha::Database->new->schema;
76         $schema->txn_do(
77             sub {
78                 my $problem = Koha::ProblemReport->new(
79                     {
80                         title          => $subject,
81                         content        => $message,
82                         borrowernumber => $borrowernumber,
83                         branchcode     => $patron->branchcode,
84                         username       => $username,
85                         problempage    => $problempage,
86                         recipient      => $recipient,
87                     }
88                 )->store;
89
90                 # send notice to library
91                 my $letter = C4::Letters::GetPreparedLetter(
92                     module => 'members',
93                     letter_code => 'PROBLEM_REPORT',
94                     branchcode => $problem->branchcode,
95                     tables => {
96                         'problem_reports', $problem->reportid
97                     }
98                 );
99
100                 my $transport = 'email';
101                 my $reply_address = $patron->email || $patron->emailpro || $patron->B_email;
102
103                 if ( $recipient eq 'library' and defined($library->inbound_email_address) and $library->inbound_email_address ne C4::Context->preference('KohaAdminEmailAddress') ) {
104                     # the problem report is intended for a librarian and will be received at a library email address
105                     C4::Letters::EnqueueLetter({
106                         letter                 => $letter,
107                         borrowernumber         => $borrowernumber,
108                         message_transport_type => $transport,
109                         to_address             => $library->inbound_email_address,
110                         reply_address          => $reply_address,
111                     });
112                 } else {
113                     C4::Letters::EnqueueLetter({
114                         letter                 => $letter,
115                         borrowernumber         => $borrowernumber,
116                         message_transport_type => $transport,
117                         to_address             => C4::Context->preference('KohaAdminEmailAddress'),
118                         reply_address          => $reply_address,
119                     });
120                 }
121
122                 push @messages, {
123                     type => 'info',
124                     code => 'success_on_send',
125                 };
126
127                 $template->param(
128                     recipient => $recipient,
129                 );
130             }
131         );
132     }
133     catch {
134         warn "Something wrong happened when sending the report problem: $_";
135         push @messages, {
136             type => 'error',
137             code => 'error_on_send',
138         };
139     }
140 }
141
142 $template->param( messages => \@messages );
143
144 output_html_with_http_headers $input, $cookie, $template->output;