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