Bug 35279: Add fallback to print for 'Send welcome email'
[koha.git] / members / notices.pl
1 #!/usr/bin/perl
2
3 # Displays sent notices for a given borrower
4
5 # Copyright (c) 2009 BibLibre
6 #
7 # This file is part of Koha.
8 #
9 # Koha is free software; you can redistribute it and/or modify it
10 # under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 3 of the License, or
12 # (at your option) any later version.
13 #
14 # Koha is distributed in the hope that it will be useful, but
15 # WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License
20 # along with Koha; if not, see <http://www.gnu.org/licenses>.
21
22 use Modern::Perl;
23 use C4::Auth qw( get_template_and_user );
24 use C4::Output qw( output_and_exit_if_error output_and_exit output_html_with_http_headers );
25 use CGI qw ( -utf8 );
26 use C4::Members;
27 use C4::Letters qw( GetPreparedLetter EnqueueLetter );
28 use Koha::Patrons;
29 use Koha::Patron::Categories;
30 use Koha::Patron::Password::Recovery qw( SendPasswordRecoveryEmail ValidateBorrowernumber );
31
32 my $input=CGI->new;
33
34
35 my $borrowernumber = $input->param('borrowernumber');
36 my $patron = Koha::Patrons->find( $borrowernumber );
37 unless ( $patron ) {
38     print $input->redirect("/cgi-bin/koha/circ/circulation.pl?borrowernumber=$borrowernumber");
39     exit;
40 }
41 my $borrower = $patron->unblessed;
42
43 my ($template, $loggedinuser, $cookie)= get_template_and_user({template_name => "members/notices.tt",
44                                 query => $input,
45                                 type => "intranet",
46                 flagsrequired => {borrowers => 'edit_borrowers'},
47                                 });
48
49 my $logged_in_user = Koha::Patrons->find( $loggedinuser );
50 output_and_exit_if_error( $input, $cookie, $template, { module => 'members', logged_in_user => $logged_in_user, current_patron => $patron } );
51
52 # Allow resending of messages in Notices tab
53 my $op = $input->param('op') || q{};
54 if ( $op eq 'cud-resend_notice' ) {
55     my $message_id = $input->param('message_id');
56     my $message = C4::Letters::GetMessage( $message_id );
57     if ( $message->{borrowernumber} = $borrowernumber ) {
58         C4::Letters::ResendMessage( $message_id );
59         # redirect to self to avoid form submission on refresh
60         print $input->redirect("/cgi-bin/koha/members/notices.pl?borrowernumber=$borrowernumber");
61     }
62 }
63
64 if ( $op eq 'send_welcome' ) {
65     my $emailaddr = $patron->notice_email_address;
66
67     # if we manage to find a valid email address, send notice
68     if ($emailaddr) {
69         eval {
70             my $letter = GetPreparedLetter(
71                 module      => 'members',
72                 letter_code => 'WELCOME',
73                 branchcode  => $patron->branchcode,,
74                 lang        => $patron->lang || 'default',
75                 tables      => {
76                     'branches'  => $patron->branchcode,
77                     'borrowers' => $patron->borrowernumber,
78                 },
79                 want_librarian => 1,
80             ) or return;
81
82             my $message_id = EnqueueLetter(
83                 {
84                     letter                 => $letter,
85                     borrowernumber         => $patron->id,
86                     to_address             => $emailaddr,
87                     message_transport_type => 'email'
88                 }
89             );
90         };
91     } else {
92         eval {
93             my $print = GetPreparedLetter(
94                 module      => 'members',
95                 letter_code => 'WELCOME',
96                 branchcode  => $patron->branchcode,,
97                 lang        => $patron->lang || 'default',
98                 tables      => {
99                     'branches'  => $patron->branchcode,
100                     'borrowers' => $patron->borrowernumber,
101                 },
102                 want_librarian         => 1,
103                 message_transport_type => 'print'
104             ) or return;
105
106             my $message_id = EnqueueLetter(
107                 {
108                     letter                 => $print,
109                     borrowernumber         => $patron->id,
110                     message_transport_type => 'print'
111                 }
112             );
113         };
114     }
115
116     # redirect to self to avoid form submission on refresh
117     print $input->redirect("/cgi-bin/koha/members/notices.pl?borrowernumber=$borrowernumber");
118 }
119
120 if ( $op eq 'send_password_reset' ) {
121
122     my $emailaddr = $patron->notice_email_address;
123
124     if ($emailaddr) {
125
126         # send staff initiated password recovery
127         SendPasswordRecoveryEmail( $patron, $emailaddr, 1 );
128     }
129
130     # redirect to self to avoid form submission on refresh
131     print $input->redirect(
132         "/cgi-bin/koha/members/notices.pl?borrowernumber=$borrowernumber");
133 }
134
135 # Getting the messages
136 my $queued_messages = Koha::Notice::Messages->search({borrowernumber => $borrowernumber});
137
138 $template->param(
139     patron             => $patron,
140     QUEUED_MESSAGES    => $queued_messages,
141     borrowernumber     => $borrowernumber,
142     sentnotices        => 1,
143 );
144 output_html_with_http_headers $input, $cookie, $template->output;
145