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 );
36 use C4::Items qw( GetItemsInfo );
37 use C4::Output qw( output_html_with_http_headers );
40 use Koha::Virtualshelves;
44 # if virtualshelves is disabled, leave immediately
45 if ( ! C4::Context->preference('virtualshelves') ) {
46 print $query->redirect("/cgi-bin/koha/errors/404.pl");
50 my ( $template, $borrowernumber, $cookie ) = get_template_and_user (
52 template_name => "opac-sendshelfform.tt",
58 my $shelfid = $query->param('shelfid');
59 my $email = $query->param('email');
61 my $dbh = C4::Context->dbh;
63 my $shelf = Koha::Virtualshelves->find( $shelfid );
64 if ( $shelf and $shelf->can_be_viewed( $borrowernumber ) ) {
67 my $comment = $query->param('comment');
69 my ( $template2, $borrowernumber, $cookie ) = get_template_and_user(
71 template_name => "opac-sendshelf.tt",
78 my $patron = Koha::Patrons->find( $borrowernumber );
79 my $borcat = $patron ? $patron->categorycode : q{};
81 my $shelf = Koha::Virtualshelves->find( $shelfid );
82 my $contents = $shelf->get_contents;
83 my $marcflavour = C4::Context->preference('marcflavour');
87 while ( my $content = $contents->next ) {
88 my $biblionumber = $content->biblionumber;
89 my $record = GetMarcBiblio({
90 biblionumber => $biblionumber,
95 my $fw = GetFrameworkCode($biblionumber);
96 my $dat = GetBiblioData($biblionumber);
98 my $marcauthorsarray = GetMarcAuthors( $record, $marcflavour );
99 my $marcsubjctsarray = GetMarcSubjects( $record, $marcflavour );
101 my @items = GetItemsInfo( $biblionumber );
103 $dat->{ISBN} = GetMarcISBN($record, $marcflavour);
104 $dat->{MARCSUBJCTS} = $marcsubjctsarray;
105 $dat->{MARCAUTHORS} = $marcauthorsarray;
106 $dat->{'biblionumber'} = $biblionumber;
107 $dat->{ITEM_RESULTS} = \@items;
108 $dat->{HASAUTHORS} = $dat->{'author'} || @$marcauthorsarray;
110 $iso2709 .= $record->as_usmarc();
112 push( @results, $dat );
116 BIBLIO_RESULTS => \@results,
118 shelfname => $shelf->shelfname,
119 firstname => $patron->firstname,
120 surname => $patron->surname,
123 # Getting template result
124 my $template_res = $template2->output();
128 # Analysing information and getting mail properties
129 if ( $template_res =~ /<SUBJECT>(?<subject>.*)<END_SUBJECT>/s ) {
130 $subject = $+{subject};
131 $subject =~ s|\n?(.*)\n?|$1|;
134 $subject = "no subject";
137 my $email_header = "";
138 if ( $template_res =~ /<HEADER>(.*)<END_HEADER>/s ) {
140 $email_header =~ s|\n?(.*)\n?|$1|;
143 if ( $template_res =~ /<MESSAGE>(.*)<END_MESSAGE>/s ) {
145 $body =~ s|\n?(.*)\n?|$1|;
148 my $THE_body = <<END_OF_BODY;
154 my $email = Koha::Email->create(
160 $email->text_body( $THE_body );
162 Encode::encode( "UTF-8", $iso2709 ),
163 content_type => 'application/octet-stream',
164 name => 'list.iso2709',
165 disposition => 'attachment',
167 my $library = Koha::Patrons->find( $borrowernumber )->library;
168 $email->transport( $library->smtp_server->transport );
170 $template->param( SENT => "1" );
173 carp "Error sending mail: $_";
174 $template->param( error => 1 );
181 output_html_with_http_headers $query, $cookie, $template->output, undef, { force_no_caching => 1 };
185 $template->param( shelfid => $shelfid,
186 url => "/cgi-bin/koha/opac-sendshelf.pl",
188 output_html_with_http_headers $query, $cookie, $template->output, undef, { force_no_caching => 1 };
192 $template->param( invalidlist => 1,
193 url => "/cgi-bin/koha/opac-sendshelf.pl",
195 output_html_with_http_headers $query, $cookie, $template->output, undef, { force_no_caching => 1 };