Bug 3150: Move emails for sending cart and list contents to notices
[srvgit] / basket / sendbasket.pl
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 use Modern::Perl;
19
20 use CGI qw ( -utf8 );
21 use Encode;
22 use Carp qw( carp );
23 use Try::Tiny qw( catch try );
24
25 use C4::Biblio qw(
26     GetMarcSubjects
27 );
28 use C4::Auth qw( get_template_and_user );
29 use C4::Output qw( output_and_exit output_html_with_http_headers );
30 use C4::Templates;
31 use Koha::Biblios;
32 use Koha::Email;
33 use Koha::Token;
34
35 my $query = CGI->new;
36
37 my ( $template, $borrowernumber, $cookie ) = get_template_and_user (
38     {
39         template_name   => "basket/sendbasketform.tt",
40         query           => $query,
41         type            => "intranet",
42         flagsrequired   => { catalogue => 1 },
43     }
44 );
45
46 my $bib_list  = $query->param('bib_list') || '';
47 my $email_add = $query->param('email_add');
48
49 my $dbh = C4::Context->dbh;
50
51 if ( $email_add ) {
52     output_and_exit( $query, $cookie, $template, 'wrong_csrf_token' )
53         unless Koha::Token->new->check_csrf({
54             session_id => scalar $query->cookie('CGISESSID'),
55             token  => scalar $query->param('csrf_token'),
56         });
57
58     my $patron = Koha::Patrons->find( $borrowernumber );
59     my $user_email = $patron->first_valid_email_address;
60
61     my $comment = $query->param('comment');
62
63     my @bibs = split( /\//, $bib_list );
64     my $iso2709;
65
66     foreach my $bib ( @bibs ) {
67         my $biblio = Koha::Biblios->find( $bib ) or next;
68         $iso2709 .= $biblio->metadata->record->as_usmarc();
69     }
70
71     if ( !defined $iso2709 ) {
72         carp "Error sending mail: empty basket";
73         $template->param( error => 1 );
74     } elsif ( !defined $user_email or $user_email eq '' ) {
75         carp "Error sending mail: sender's email address is invalid";
76         $template->param( error => 1 );
77     } else {
78         my %loops = (
79             biblio => \@bibs,
80         );
81
82         my %substitute = (
83             comment => $comment,
84         );
85
86         my $letter = C4::Letters::GetPreparedLetter(
87             module => 'catalogue',
88             letter_code => 'CART',
89             lang => $patron->lang,
90             tables => {
91                 borrowers => $borrowernumber,
92             },
93             message_transport_type => 'email',
94             loops => \%loops,
95             substitute => \%substitute,
96         );
97
98         my $attachment = {
99             filename => 'basket.iso2709',
100             type => 'application/octet-stream',
101             content => Encode::encode("UTF-8", $iso2709),
102         };
103
104         C4::Letters::EnqueueLetter({
105             letter => $letter,
106             message_transport_type => 'email',
107             borrowernumber => $patron->borrowernumber,
108             to_address => $email_add,
109             reply_address => $user_email,
110             attachments => [$attachment],
111         });
112
113         $template->param( SENT => 1 );
114     }
115     $template->param( email_add => $email_add );
116     output_html_with_http_headers $query, $cookie, $template->output;
117 }
118 else {
119     $template->param(
120         bib_list       => $bib_list,
121         url            => "/cgi-bin/koha/basket/sendbasket.pl",
122         suggestion     => C4::Context->preference("suggestion"),
123         virtualshelves => C4::Context->preference("virtualshelves"),
124         csrf_token     => Koha::Token->new->generate_csrf({ session_id => scalar $query->cookie('CGISESSID'), }),
125     );
126     output_html_with_http_headers $query, $cookie, $template->output;
127 }