Bug 4461: Simplify recipients code
[koha-ffzg.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
56 $template->param(
57     username => $username,
58     probpage => $problempage,
59     library => $library,
60 );
61
62 my $op = $input->param('op') || '';
63 if ( $op eq 'addreport' ) {
64
65     my $subject = $input->param('subject');
66     my $message = $input->param('message');
67     my $place = $input->param('place');
68     my $recipient = $input->param('recipient') || 'admin';
69     my $problem = Koha::ProblemReport->new(
70         {
71             title          => $subject,
72             content        => $message,
73             borrowernumber => $borrowernumber,
74             branchcode     => $branchcode,
75             username       => $username,
76             problempage    => $place,
77             recipient      => $recipient,
78         }
79     )->store;
80     $template->param(
81         recipient => $recipient,
82         successfuladd => 1,
83         probpage => $place,
84     );
85
86     # send notice to library
87     my $letter = C4::Letters::GetPreparedLetter(
88         module => 'members',
89         letter_code => 'PROBLEM_REPORT',
90         branchcode => $problem->branchcode,
91         tables => {
92             'problem_reports', $problem->reportid
93         }
94     );
95
96     my $from_address = C4::Context->preference('KohaAdminEmailAddress');
97     my $transport = 'email';
98
99     if ( $recipient eq 'admin' ) {
100         C4::Letters::EnqueueLetter({
101             letter                 => $letter,
102             borrowernumber         => $borrowernumber,
103             message_transport_type => $transport,
104             to_address             => C4::Context->preference('KohaAdminEmailAddress'),
105             from_address           => $from_address,
106         });
107     } else {
108         C4::Letters::EnqueueLetter({
109             letter                 => $letter,
110             borrowernumber         => $borrowernumber,
111             message_transport_type => $transport,
112             to_address             => $library->branchemail,
113             from_address           => $from_address,
114         });
115     }
116 }
117
118 output_html_with_http_headers $input, $cookie, $template->output;