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;
43 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
45 template_name => "virtualshelves/sendshelfform.tt",
48 flagsrequired => { catalogue => 1 },
52 my $shelfid = $query->param('shelfid');
53 my $to_address = $query->param('email');
55 my $shelf = Koha::Virtualshelves->find( $shelfid );
57 output_and_exit( $query, $cookie, $template, 'insufficient_permission' )
58 if $shelf && !$shelf->can_be_viewed( $loggedinuser );
61 my $comment = $query->param('comment');
63 my ( $template2, $borrowernumber, $cookie ) = get_template_and_user(
65 template_name => "virtualshelves/sendshelf.tt",
68 flagsrequired => { catalogue => 1 },
72 my $contents = $shelf->get_contents;
73 my $marcflavour = C4::Context->preference('marcflavour');
77 while ( my $content = $contents->next ) {
78 my $biblionumber = $content->biblionumber;
79 my $biblio = Koha::Biblios->find( $biblionumber ) or next;
80 my $dat = $biblio->unblessed;
81 my $record = $biblio->metadata->record({ embed_items => 1 });
82 my $marcauthorsarray = $biblio->get_marc_contributors;
83 my $marcsubjctsarray = GetMarcSubjects( $record, $marcflavour );
85 my $items = $biblio->items->search_ordered;
87 $dat->{ISBN} = GetMarcISBN($record, $marcflavour);
88 $dat->{MARCSUBJCTS} = $marcsubjctsarray;
89 $dat->{MARCAUTHORS} = $marcauthorsarray;
90 $dat->{'biblionumber'} = $biblionumber;
91 $dat->{ITEM_RESULTS} = $items;
92 $dat->{HASAUTHORS} = $dat->{'author'} || @$marcauthorsarray;
94 $iso2709 .= $record->as_usmarc();
96 push( @results, $dat );
100 BIBLIO_RESULTS => \@results,
102 shelfname => $shelf->shelfname,
105 # Getting template result
106 my $template_res = $template2->output();
110 # Analysing information and getting mail properties
111 if ( $template_res =~ /<SUBJECT>(?<subject>.*)<END_SUBJECT>/s ) {
112 $subject = $+{subject};
113 $subject =~ s|\n?(.*)\n?|$1|;
116 $subject = "no subject";
119 my $email_header = "";
120 if ( $template_res =~ /<HEADER>(.*)<END_HEADER>/s ) {
122 $email_header =~ s|\n?(.*)\n?|$1|;
125 if ( $template_res =~ /<MESSAGE>(.*)<END_MESSAGE>/s ) {
127 $body =~ s|\n?(.*)\n?|$1|;
130 my $THE_body = <<END_OF_BODY;
136 my $email = Koha::Email->create(
142 $email->text_body( $THE_body );
144 Encode::encode("UTF-8", $iso2709),
145 content_type => 'application/octet-stream',
146 name => 'shelf.iso2709',
147 disposition => 'attachment',
150 my $library = Koha::Patrons->find( $borrowernumber )->library;
151 $email->send_or_die({ transport => $library->smtp_server->transport });
152 $template->param( SENT => "1" );
155 carp "Error sending mail: $_";
156 $template->param( error => 1 );
159 $template->param( email => $to_address );
160 output_html_with_http_headers $query, $cookie, $template->output;
166 url => "/cgi-bin/koha/virtualshelves/sendshelf.pl",
168 output_html_with_http_headers $query, $cookie, $template->output;