Bug 4461: Use KohaAdminEmailAddress pref as 'from'
[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
31 my $input = new CGI;
32
33 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
34     {
35         template_name   => "opac-reportproblem.tt",
36         type            => "opac",
37         query           => $input,
38         authnotrequired => 0,
39     }
40 );
41
42 if ( !C4::Context->preference('OPACReportProblem') ){
43     print $input->redirect("/cgi-bin/koha/errors/404.pl");
44 }
45
46 my $problempage = $ENV{HTTP_REFERER};
47 my $member = Koha::Patrons->find($borrowernumber);
48 my $username = $member->userid;
49 my $branchcode = $member->branchcode;
50 my $library = Koha::Libraries->find($branchcode);
51 my $recipients = 2;
52
53 if (
54     ( !defined($library->branchreplyto) || $library->branchreplyto eq '' ) &&
55     ( C4::Context->preference('ReplytoDefault') eq '' ) &&
56     ( !defined($library->branchemail) || $library->branchemail eq '' )
57     ) {
58     $template->param( nolibemail => 1 );
59     $recipients--;
60 }
61
62 my $koha_admin = C4::Context->preference('KohaAdminEmailAddress');
63 if ( $koha_admin eq '' ) {
64     $template->param( noadminemail => 1 );
65     $recipients--;
66 }
67
68 $template->param(
69     username => $username,
70     probpage => $problempage,
71 );
72
73 my $op = $input->param('op') || '';
74 if ( $op eq 'addreport' ) {
75
76     if ( $recipients == 0 ){
77         print $input->redirect("/cgi-bin/koha/opac-reportproblem?norecipients=1.pl");
78         exit;
79     }
80
81     my $subject = $input->param('subject');
82     my $message = $input->param('message');
83     my $place = $input->param('place');
84     my $recipient = $input->param('recipient') || 'library';
85     my $problem = Koha::ProblemReport->new(
86         {
87             title          => $subject,
88             content        => $message,
89             borrowernumber => $borrowernumber,
90             branchcode     => $branchcode,
91             username       => $username,
92             problempage    => $place,
93             recipient      => $recipient,
94         }
95     )->store;
96     $template->param(
97         recipient => $recipient,
98         successfuladd => 1,
99         probpage => $place,
100     );
101
102     # send notice to library
103     my $letter = C4::Letters::GetPreparedLetter(
104         module => 'members',
105         letter_code => 'PROBLEM_REPORT',
106         branchcode => $problem->branchcode,
107         tables => {
108             'problem_reports', $problem->reportid
109         }
110     );
111
112     my $from_address = C4::Context->preference('KohaAdminEmailAddress');
113     my $transport = 'email';
114
115     if ( $recipient eq 'admin' ) {
116         C4::Letters::EnqueueLetter({
117             letter                 => $letter,
118             borrowernumber         => $borrowernumber,
119             message_transport_type => $transport,
120             to_address             => $koha_admin,
121             from_address           => $from_address,
122         });
123     } else {
124         my  $to_address = $library->branchreplyto ||
125             C4::Context->preference('ReplytoDefault') ||
126             $library->branchemail;
127         C4::Letters::EnqueueLetter({
128             letter                 => $letter,
129             borrowernumber         => $borrowernumber,
130             message_transport_type => $transport,
131             to_address             => $to_address,
132             from_address           => $from_address,
133         });
134     }
135 }
136
137 output_html_with_http_headers $input, $cookie, $template->output;