3 # Copyright 2009 BibLibre
5 # This file is part of Koha.
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.
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.
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>.
25 use Try::Tiny qw( catch try );
27 use C4::Auth qw( get_template_and_user );
33 output_html_with_http_headers
39 use Koha::Virtualshelves;
42 my $op = $query->param('op') // q{};
44 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
46 template_name => "virtualshelves/sendshelfform.tt",
49 flagsrequired => { catalogue => 1 },
53 my $shelfid = $query->param('shelfid');
54 my $to_address = $query->param('email');
56 my $shelf = Koha::Virtualshelves->find($shelfid);
58 output_and_exit( $query, $cookie, $template, 'insufficient_permission' )
59 if $shelf && !$shelf->can_be_viewed($borrowernumber);
61 if ( $to_address && $op eq 'cud-send' ) {
62 my $comment = $query->param('comment');
64 my $patron = Koha::Patrons->find($borrowernumber);
65 my $user_email = $patron->notice_email_address;
66 my $contents = $shelf->get_contents;
70 while ( my $content = $contents->next ) {
71 push @biblionumbers, $content->biblionumber;
72 my $biblio = Koha::Biblios->find( $content->biblionumber );
73 $iso2709 .= $biblio->metadata->record->as_usmarc();
76 if ( !defined $iso2709 ) {
77 carp "Error sending mail: empty basket";
78 $template->param( error => 1 );
80 elsif ( !defined $user_email or $user_email eq '' ) {
81 carp "Error sending mail: sender's email address is invalid";
82 $template->param( error => 1 );
85 my %loops = ( biblio => \@biblionumbers, );
89 listname => $shelf->shelfname,
92 my $letter = C4::Letters::GetPreparedLetter(
93 module => 'catalogue',
94 letter_code => 'LIST',
95 lang => $patron->lang,
97 borrowers => $borrowernumber,
99 message_transport_type => 'email',
101 substitute => \%substitute,
105 filename => 'shelf.iso2709',
106 type => 'application/octet-stream',
107 content => Encode::encode( "UTF-8", $iso2709 ),
110 my $message_id = C4::Letters::EnqueueLetter(
113 message_transport_type => 'email',
114 borrowernumber => $patron->borrowernumber,
115 to_address => $to_address,
116 reply_address => $user_email,
117 attachments => [$attachment],
121 C4::Letters::SendQueuedMessages( { message_id => $message_id } ) if $message_id;
123 $template->param( SENT => 1 );
126 $template->param( email => $to_address );
127 output_html_with_http_headers $query, $cookie, $template->output;
133 url => "/cgi-bin/koha/virtualshelves/sendshelf.pl",
135 output_html_with_http_headers $query, $cookie, $template->output;