Bug 10694: (follow-up) fix various issues
[koha.git] / virtualshelves / sendshelf.pl
1 #!/usr/bin/perl
2
3 # Copyright 2009 BibLibre
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20 use strict;
21 use warnings;
22
23 use CGI;
24 use Encode qw(decode encode);
25 use Carp;
26
27 use Mail::Sendmail;
28 use MIME::QuotedPrint;
29 use MIME::Base64;
30 use C4::Auth;
31 use C4::Biblio;
32 use C4::Items;
33 use C4::Output;
34 use C4::VirtualShelves;
35
36 my $query = new CGI;
37
38 my ( $template, $borrowernumber, $cookie ) = get_template_and_user (
39     {
40         template_name   => "virtualshelves/sendshelfform.tmpl",
41         query           => $query,
42         type            => "intranet",
43         authnotrequired => 0,
44         flagsrequired   => { catalogue => 1 },
45     }
46 );
47
48 my $shelfid = $query->param('shelfid');
49 my $email   = $query->param('email');
50
51 my $dbh          = C4::Context->dbh;
52
53 if ( $email ) {
54     my $email_from = C4::Context->preference('KohaAdminEmailAddress');
55     my $comment    = $query->param('comment');
56
57     my %mail = (
58         To   => $email,
59         From => $email_from
60     );
61
62     my ( $template2, $borrowernumber, $cookie ) = get_template_and_user(
63         {
64             template_name   => "virtualshelves/sendshelf.tmpl",
65             query           => $query,
66             type            => "intranet",
67             authnotrequired => 0,
68             flagsrequired   => { catalogue => 1 },
69         }
70     );
71
72     my @shelf               = GetShelf($shelfid);
73     my ($items, $totitems)  = GetShelfContents($shelfid);
74     my $marcflavour         = C4::Context->preference('marcflavour');
75     my $iso2709;
76     my @results;
77
78     # retrieve biblios from shelf
79     foreach my $biblio (@$items) {
80         my $biblionumber     = $biblio->{biblionumber};
81         my $fw               = GetFrameworkCode($biblionumber);
82         my $dat              = GetBiblioData($biblionumber);
83         my $record           = GetMarcBiblio($biblionumber);
84         my $marcnotesarray   = GetMarcNotes( $record, $marcflavour );
85         my $marcauthorsarray = GetMarcAuthors( $record, $marcflavour );
86         my $marcsubjctsarray = GetMarcSubjects( $record, $marcflavour );
87         my $subtitle         = GetRecordValue('subtitle', $record, $fw);
88
89         my @items = GetItemsInfo( $biblionumber );
90
91         $dat->{MARCNOTES}      = $marcnotesarray;
92         $dat->{MARCSUBJCTS}    = $marcsubjctsarray;
93         $dat->{MARCAUTHORS}    = $marcauthorsarray;
94         $dat->{'biblionumber'} = $biblionumber;
95         $dat->{ITEM_RESULTS}   = \@items;
96         $dat->{subtitle}       = $subtitle;
97
98         $iso2709 .= $record->as_usmarc();
99
100         push( @results, $dat );
101     }
102
103   if (C4::Context->preference('OPACBaseURL')){
104         $template2->param( OPACBaseURL => C4::Context->preference('OPACBaseURL') );
105   }
106
107     $template2->param(
108         BIBLIO_RESULTS => \@results,
109         email_sender   => $email_from,
110         comment        => $comment,
111         shelfname      => $shelf[1],
112     );
113
114     # Getting template result
115     my $template_res = $template2->output();
116     my $body;
117
118     # Analysing information and getting mail properties
119     if ( $template_res =~ /<SUBJECT>(.*)<END_SUBJECT>/s ) {
120         $mail{subject} = $1;
121         $mail{subject} =~ s|\n?(.*)\n?|$1|;
122     }
123     else { $mail{'subject'} = "no subject"; }
124
125     my $email_header = "";
126     if ( $template_res =~ /<HEADER>(.*)<END_HEADER>/s ) {
127         $email_header = $1;
128         $email_header =~ s|\n?(.*)\n?|$1|;
129     }
130
131     my $email_file = "list.txt";
132     if ( $template_res =~ /<FILENAME>(.*)<END_FILENAME>/s ) {
133         $email_file = $1;
134         $email_file =~ s|\n?(.*)\n?|$1|;
135     }
136
137     if ( $template_res =~ /<MESSAGE>(.*)<END_MESSAGE>/s ) {
138         $body = $1;
139         $body =~ s|\n?(.*)\n?|$1|;
140         $body = encode("UTF-8", encode_qp($body));
141     }
142
143     my $boundary = "====" . time() . "====";
144
145     # We set and put the multipart content
146     $mail{'content-type'} = "multipart/mixed; boundary=\"$boundary\"";
147
148     my $isofile = encode_base64(encode("UTF-8", $iso2709));
149     $boundary = '--' . $boundary;
150
151     $mail{body} = <<END_OF_BODY;
152 $boundary
153 Content-Type: text/plain; charset="utf-8"
154 Content-Transfer-Encoding: quoted-printable
155
156 $email_header
157 $body
158 $boundary
159 Content-Type: application/octet-stream; name="shelf.iso2709"
160 Content-Transfer-Encoding: base64
161 Content-Disposition: attachment; filename="shelf.iso2709"
162
163 $isofile
164 $boundary--
165 END_OF_BODY
166
167     # Sending mail
168     if ( sendmail %mail ) {
169         # do something if it works....
170         $template->param( SENT      => "1" );
171     }
172     else {
173         # do something if it doesnt work....
174         carp "Error sending mail: $Mail::Sendmail::error \n";
175         $template->param( error => 1 );
176     }
177
178     $template->param( email => $email );
179     output_html_with_http_headers $query, $cookie, $template->output;
180
181
182 }else{
183     $template->param( shelfid => $shelfid,
184                       url     => "/cgi-bin/koha/virtualshelves/sendshelf.pl",
185                     );
186     output_html_with_http_headers $query, $cookie, $template->output;
187 }