Bug 4461: Redirect to 404 if KohaAdminEmailAddress is not defined
[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 C4::Auth;    # get_template_and_user
23 use C4::Output;
24 use C4::Members;
25 use C4::Letters;
26 use Koha::ProblemReport;
27 use Koha::DateUtils;
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 $member = Koha::Patrons->find($borrowernumber);
52 my $username = $member->userid;
53 my $branchcode = $member->branchcode;
54 my $library = Koha::Libraries->find($branchcode);
55 my $recipients = 2;
56
57 if (
58     ( !defined($library->branchreplyto) || $library->branchreplyto eq '' ) &&
59     ( C4::Context->preference('ReplytoDefault') eq '' ) &&
60     ( !defined($library->branchemail) || $library->branchemail eq '' )
61     ) {
62     $template->param( nolibemail => 1 );
63     $recipients--;
64 }
65
66 my $koha_admin = C4::Context->preference('KohaAdminEmailAddress');
67 if ( $koha_admin eq '' ) {
68     $template->param( noadminemail => 1 );
69     $recipients--;
70 }
71
72 $template->param(
73     username => $username,
74     probpage => $problempage,
75 );
76
77 my $op = $input->param('op') || '';
78 if ( $op eq 'addreport' ) {
79
80     if ( $recipients == 0 ){
81         print $input->redirect("/cgi-bin/koha/opac-reportproblem?norecipients=1.pl");
82         exit;
83     }
84
85     my $subject = $input->param('subject');
86     my $message = $input->param('message');
87     my $place = $input->param('place');
88     my $recipient = $input->param('recipient') || 'library';
89     my $problem = Koha::ProblemReport->new(
90         {
91             title          => $subject,
92             content        => $message,
93             borrowernumber => $borrowernumber,
94             branchcode     => $branchcode,
95             username       => $username,
96             problempage    => $place,
97             recipient      => $recipient,
98         }
99     )->store;
100     $template->param(
101         recipient => $recipient,
102         successfuladd => 1,
103         probpage => $place,
104     );
105
106     # send notice to library
107     my $letter = C4::Letters::GetPreparedLetter(
108         module => 'members',
109         letter_code => 'PROBLEM_REPORT',
110         branchcode => $problem->branchcode,
111         tables => {
112             'problem_reports', $problem->reportid
113         }
114     );
115
116     my $from_address = C4::Context->preference('KohaAdminEmailAddress');
117     my $transport = 'email';
118
119     if ( $recipient eq 'admin' ) {
120         C4::Letters::EnqueueLetter({
121             letter                 => $letter,
122             borrowernumber         => $borrowernumber,
123             message_transport_type => $transport,
124             to_address             => $koha_admin,
125             from_address           => $from_address,
126         });
127     } else {
128         my  $to_address = $library->branchreplyto ||
129             C4::Context->preference('ReplytoDefault') ||
130             $library->branchemail;
131         C4::Letters::EnqueueLetter({
132             letter                 => $letter,
133             borrowernumber         => $borrowernumber,
134             message_transport_type => $transport,
135             to_address             => $to_address,
136             from_address           => $from_address,
137         });
138     }
139 }
140
141 output_html_with_http_headers $input, $cookie, $template->output;