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 );
35 use C4::Items qw( GetItemsInfo );
37 output_html_with_http_headers
41 use Koha::Virtualshelves;
45 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
47 template_name => "virtualshelves/sendshelfform.tt",
50 flagsrequired => { catalogue => 1 },
54 my $shelfid = $query->param('shelfid');
55 my $to_address = $query->param('email');
57 my $shelf = Koha::Virtualshelves->find( $shelfid );
59 output_and_exit( $query, $cookie, $template, 'insufficient_permission' )
60 if $shelf && !$shelf->can_be_viewed( $loggedinuser );
63 my $comment = $query->param('comment');
65 my ( $template2, $borrowernumber, $cookie ) = get_template_and_user(
67 template_name => "virtualshelves/sendshelf.tt",
70 flagsrequired => { catalogue => 1 },
74 my $contents = $shelf->get_contents;
75 my $marcflavour = C4::Context->preference('marcflavour');
79 while ( my $content = $contents->next ) {
80 my $biblionumber = $content->biblionumber;
81 my $dat = GetBiblioData($biblionumber);
82 my $record = GetMarcBiblio({
83 biblionumber => $biblionumber,
85 my $marcauthorsarray = GetMarcAuthors( $record, $marcflavour );
86 my $marcsubjctsarray = GetMarcSubjects( $record, $marcflavour );
88 my @items = GetItemsInfo($biblionumber);
90 $dat->{ISBN} = GetMarcISBN($record, $marcflavour);
91 $dat->{MARCSUBJCTS} = $marcsubjctsarray;
92 $dat->{MARCAUTHORS} = $marcauthorsarray;
93 $dat->{'biblionumber'} = $biblionumber;
94 $dat->{ITEM_RESULTS} = \@items;
95 $dat->{HASAUTHORS} = $dat->{'author'} || @$marcauthorsarray;
97 $iso2709 .= $record->as_usmarc();
99 push( @results, $dat );
103 BIBLIO_RESULTS => \@results,
105 shelfname => $shelf->shelfname,
108 # Getting template result
109 my $template_res = $template2->output();
113 # Analysing information and getting mail properties
114 if ( $template_res =~ /<SUBJECT>(?<subject>.*)<END_SUBJECT>/s ) {
115 $subject = $+{subject};
116 $subject =~ s|\n?(.*)\n?|$1|;
119 $subject = "no subject";
122 my $email_header = "";
123 if ( $template_res =~ /<HEADER>(.*)<END_HEADER>/s ) {
125 $email_header =~ s|\n?(.*)\n?|$1|;
128 if ( $template_res =~ /<MESSAGE>(.*)<END_MESSAGE>/s ) {
130 $body =~ s|\n?(.*)\n?|$1|;
133 my $THE_body = <<END_OF_BODY;
139 my $email = Koha::Email->create(
145 $email->text_body( $THE_body );
147 Encode::encode("UTF-8", $iso2709),
148 content_type => 'application/octet-stream',
149 name => 'shelf.iso2709',
150 disposition => 'attachment',
153 my $library = Koha::Patrons->find( $borrowernumber )->library;
154 $email->send_or_die({ transport => $library->smtp_server->transport });
155 $template->param( SENT => "1" );
158 carp "Error sending mail: $_";
159 $template->param( error => 1 );
162 $template->param( email => $to_address );
163 output_html_with_http_headers $query, $cookie, $template->output;
169 url => "/cgi-bin/koha/virtualshelves/sendshelf.pl",
171 output_html_with_http_headers $query, $cookie, $template->output;