3 # Copyright 2009 SARL 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 use C4::Output qw( output_html_with_http_headers );
37 use Koha::Virtualshelves;
41 # if virtualshelves is disabled, leave immediately
42 if ( ! C4::Context->preference('virtualshelves') ) {
43 print $query->redirect("/cgi-bin/koha/errors/404.pl");
47 my ( $template, $borrowernumber, $cookie ) = get_template_and_user (
49 template_name => "opac-sendshelfform.tt",
55 my $shelfid = $query->param('shelfid');
56 my $email = $query->param('email');
58 my $shelf = Koha::Virtualshelves->find( $shelfid );
59 if ( $shelf and $shelf->can_be_viewed( $borrowernumber ) ) {
61 my $comment = $query->param('comment');
63 my ( $template2, $borrowernumber, $cookie ) = get_template_and_user(
65 template_name => "opac-sendshelf.tt",
72 my $patron = Koha::Patrons->find( $borrowernumber );
73 my $user_email = $patron->first_valid_email_address;
74 my $shelf = Koha::Virtualshelves->find( $shelfid );
75 my $contents = $shelf->get_contents;
79 while ( my $content = $contents->next ) {
80 push @biblionumbers, $content->biblionumber;
81 my $biblio = Koha::Biblios->find($content->biblionumber);
82 $iso2709 .= $biblio->metadata->record->as_usmarc();
85 if ( !defined $iso2709 ) {
86 carp "Error sending mail: empty list";
87 $template->param( error => 1 );
88 } elsif ( !defined $user_email or $user_email eq '' ) {
89 carp "Error sending mail: sender's email address is invalid";
90 $template->param( error => 1 );
93 biblio => \@biblionumbers,
98 listname => $shelf->shelfname,
101 my $letter = C4::Letters::GetPreparedLetter(
102 module => 'catalogue',
103 letter_code => 'LIST',
104 lang => $patron->lang,
106 borrowers => $borrowernumber,
108 message_transport_type => 'email',
110 substitute => \%substitute,
114 filename => 'list.iso2709',
115 type => 'application/octet-stream',
116 content => Encode::encode("UTF-8", $iso2709),
119 C4::Letters::EnqueueLetter({
121 message_transport_type => 'email',
122 borrowernumber => $patron->borrowernumber,
123 to_address => $email,
124 reply_address => $user_email,
125 attachments => [$attachment],
128 $template->param( SENT => 1 );
135 output_html_with_http_headers $query, $cookie, $template->output, undef, { force_no_caching => 1 };
138 $template->param( shelfid => $shelfid,
139 url => "/cgi-bin/koha/opac-sendshelf.pl",
141 output_html_with_http_headers $query, $cookie, $template->output, undef, { force_no_caching => 1 };
144 $template->param( invalidlist => 1,
145 url => "/cgi-bin/koha/opac-sendshelf.pl",
147 output_html_with_http_headers $query, $cookie, $template->output, undef, { force_no_caching => 1 };