3 # This file is part of Koha.
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.
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.
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>.
23 use Try::Tiny qw( catch try );
28 use C4::Auth qw( get_template_and_user );
29 use C4::Output qw( output_and_exit output_html_with_http_headers );
37 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
39 template_name => "basket/sendbasketform.tt",
42 flagsrequired => { catalogue => 1 },
46 my $bib_list = $query->param('bib_list') || '';
47 my $email_add = $query->param('email_add');
49 my $dbh = C4::Context->dbh;
52 output_and_exit( $query, $cookie, $template, 'wrong_csrf_token' )
53 unless Koha::Token->new->check_csrf(
55 session_id => scalar $query->cookie('CGISESSID'),
56 token => scalar $query->param('csrf_token'),
60 my $patron = Koha::Patrons->find($borrowernumber);
61 my $user_email = $patron->notice_email_address;
63 my $comment = $query->param('comment');
65 my @bibs = split( /\//, $bib_list );
68 foreach my $bib (@bibs) {
69 my $biblio = Koha::Biblios->find($bib) or next;
70 $iso2709 .= $biblio->metadata->record->as_usmarc();
73 if ( !defined $iso2709 ) {
74 carp "Error sending mail: empty basket";
75 $template->param( error => 1 );
77 elsif ( !defined $user_email or $user_email eq '' ) {
78 carp "Error sending mail: sender's email address is invalid";
79 $template->param( error => 1 );
82 my %loops = ( biblio => \@bibs, );
84 my %substitute = ( comment => $comment, );
86 my $letter = C4::Letters::GetPreparedLetter(
87 module => 'catalogue',
88 letter_code => 'CART',
89 lang => $patron->lang,
91 borrowers => $borrowernumber,
93 message_transport_type => 'email',
95 substitute => \%substitute,
99 filename => 'basket.iso2709',
100 type => 'application/octet-stream',
101 content => Encode::encode( "UTF-8", $iso2709 ),
104 my $message_id = C4::Letters::EnqueueLetter(
107 message_transport_type => 'email',
108 to_address => $email_add,
109 reply_address => $user_email,
110 attachments => [$attachment],
114 C4::Letters::SendQueuedMessages( { message_id => $message_id } ) if $message_id;
116 $template->param( SENT => 1 );
118 $template->param( email_add => $email_add );
119 output_html_with_http_headers $query, $cookie, $template->output;
123 bib_list => $bib_list,
124 url => "/cgi-bin/koha/basket/sendbasket.pl",
125 suggestion => C4::Context->preference("suggestion"),
126 virtualshelves => C4::Context->preference("virtualshelves"),
127 csrf_token => Koha::Token->new->generate_csrf(
128 { session_id => scalar $query->cookie('CGISESSID'), }
131 output_html_with_http_headers $query, $cookie, $template->output;