Bug 18050: DBRev 20.06.00.055
[koha.git] / virtualshelves / sendshelf.pl
blobf15e2c9f64d7c7a55a4175cea8eddefc4323780b
1 #!/usr/bin/perl
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>.
20 use Modern::Perl;
22 use CGI qw ( -utf8 );
23 use Encode qw( encode );
24 use Carp;
25 use Try::Tiny;
27 use C4::Auth;
28 use C4::Biblio;
29 use C4::Items;
30 use C4::Output;
31 use Koha::Email;
32 use Koha::Virtualshelves;
34 my $query = CGI->new;
36 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
38 template_name => "virtualshelves/sendshelfform.tt",
39 query => $query,
40 type => "intranet",
41 flagsrequired => { catalogue => 1 },
45 my $shelfid = $query->param('shelfid');
46 my $to_address = $query->param('email');
48 my $dbh = C4::Context->dbh;
50 if ($to_address) {
51 my $comment = $query->param('comment');
53 my ( $template2, $borrowernumber, $cookie ) = get_template_and_user(
55 template_name => "virtualshelves/sendshelf.tt",
56 query => $query,
57 type => "intranet",
58 flagsrequired => { catalogue => 1 },
62 my $shelf = Koha::Virtualshelves->find( $shelfid );
63 my $contents = $shelf->get_contents;
64 my $marcflavour = C4::Context->preference('marcflavour');
65 my $iso2709;
66 my @results;
68 while ( my $content = $contents->next ) {
69 my $biblionumber = $content->biblionumber;
70 my $dat = GetBiblioData($biblionumber);
71 my $record = GetMarcBiblio({
72 biblionumber => $biblionumber,
73 embed_items => 1 });
74 my $marcauthorsarray = GetMarcAuthors( $record, $marcflavour );
75 my $marcsubjctsarray = GetMarcSubjects( $record, $marcflavour );
77 my @items = GetItemsInfo($biblionumber);
79 $dat->{ISBN} = GetMarcISBN($record, $marcflavour);
80 $dat->{MARCSUBJCTS} = $marcsubjctsarray;
81 $dat->{MARCAUTHORS} = $marcauthorsarray;
82 $dat->{'biblionumber'} = $biblionumber;
83 $dat->{ITEM_RESULTS} = \@items;
84 $dat->{HASAUTHORS} = $dat->{'author'} || @$marcauthorsarray;
86 $iso2709 .= $record->as_usmarc();
88 push( @results, $dat );
91 $template2->param(
92 BIBLIO_RESULTS => \@results,
93 comment => $comment,
94 shelfname => $shelf->shelfname,
97 # Getting template result
98 my $template_res = $template2->output();
99 my $body;
101 my $subject;
102 # Analysing information and getting mail properties
103 if ( $template_res =~ /<SUBJECT>(?<subject>.*)<END_SUBJECT>/s ) {
104 $subject = $+{subject};
105 $subject =~ s|\n?(.*)\n?|$1|;
107 else {
108 $subject = "no subject";
111 my $email_header = "";
112 if ( $template_res =~ /<HEADER>(.*)<END_HEADER>/s ) {
113 $email_header = $1;
114 $email_header =~ s|\n?(.*)\n?|$1|;
117 if ( $template_res =~ /<MESSAGE>(.*)<END_MESSAGE>/s ) {
118 $body = $1;
119 $body =~ s|\n?(.*)\n?|$1|;
122 my $THE_body = <<END_OF_BODY;
123 $email_header
124 $body
125 END_OF_BODY
127 try {
128 my $email = Koha::Email->create(
130 to => $to_address,
131 subject => $subject,
134 $email->text_body( $THE_body );
135 $email->attach(
136 Encode::encode("UTF-8", $iso2709),
137 content_type => 'application/octet-stream',
138 name => 'shelf.iso2709',
139 disposition => 'attachment',
142 my $library = Koha::Patrons->find( $borrowernumber )->library;
143 $email->send_or_die({ transport => $library->smtp_server->transport });
144 $template->param( SENT => "1" );
146 catch {
147 carp "Error sending mail: $_";
148 $template->param( error => 1 );
151 $template->param( email => $to_address );
152 output_html_with_http_headers $query, $cookie, $template->output;
155 else {
156 $template->param(
157 shelfid => $shelfid,
158 url => "/cgi-bin/koha/virtualshelves/sendshelf.pl",
160 output_html_with_http_headers $query, $cookie, $template->output;