Bug 23129: (RM follow-up) Fix failing test
[koha.git] / opac / opac-sendshelf.pl
1 #!/usr/bin/perl
2
3 # Copyright 2009 SARL Biblibre
4 #
5 # This file is part of Koha.
6 #
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.
11 #
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.
16 #
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>.
19
20 use Modern::Perl;
21
22 use CGI qw ( -utf8 );
23 use Encode qw( encode );
24 use Carp;
25
26 use Mail::Sendmail;
27 use MIME::QuotedPrint;
28 use MIME::Base64;
29 use C4::Auth;
30 use C4::Biblio;
31 use C4::Items;
32 use C4::Output;
33 use C4::Members;
34 use Koha::Email;
35 use Koha::Patrons;
36 use Koha::Virtualshelves;
37
38 my $query = new CGI;
39
40 # if virtualshelves is disabled, leave immediately
41 if ( ! C4::Context->preference('virtualshelves') ) {
42     print $query->redirect("/cgi-bin/koha/errors/404.pl");
43     exit;
44 }
45
46 my ( $template, $borrowernumber, $cookie ) = get_template_and_user (
47     {
48         template_name   => "opac-sendshelfform.tt",
49         query           => $query,
50         type            => "opac",
51     }
52 );
53
54 my $shelfid = $query->param('shelfid');
55 my $email   = $query->param('email');
56
57 my $dbh          = C4::Context->dbh;
58
59 my $shelf = Koha::Virtualshelves->find( $shelfid );
60 if ( $shelf and $shelf->can_be_viewed( $borrowernumber ) ) {
61
62 if ( $email ) {
63     my $message = Koha::Email->new();
64     my $comment    = $query->param('comment');
65
66     my %mail = $message->create_message_headers(
67         {
68             to => $email,
69         }
70     );
71
72     my ( $template2, $borrowernumber, $cookie ) = get_template_and_user(
73         {
74             template_name   => "opac-sendshelf.tt",
75             query           => $query,
76             type            => "opac",
77             authnotrequired => 1,
78         }
79     );
80
81     my $patron = Koha::Patrons->find( $borrowernumber );
82     my $borcat = $patron ? $patron->categorycode : q{};
83
84     my $shelf = Koha::Virtualshelves->find( $shelfid );
85     my $contents = $shelf->get_contents;
86     my $marcflavour         = C4::Context->preference('marcflavour');
87     my $iso2709;
88     my @results;
89
90     while ( my $content = $contents->next ) {
91         my $biblionumber = $content->biblionumber;
92         my $record           = GetMarcBiblio({
93             biblionumber => $biblionumber,
94             embed_items  => 1,
95             opac         => 1,
96             borcat       => $borcat });
97         next unless $record;
98         my $fw               = GetFrameworkCode($biblionumber);
99         my $dat              = GetBiblioData($biblionumber);
100
101         my $marcauthorsarray = GetMarcAuthors( $record, $marcflavour );
102         my $marcsubjctsarray = GetMarcSubjects( $record, $marcflavour );
103         my $subtitle         = GetRecordValue('subtitle', $record, $fw);
104
105         my @items = GetItemsInfo( $biblionumber );
106
107         $dat->{ISBN}           = GetMarcISBN($record, $marcflavour);
108         $dat->{MARCSUBJCTS}    = $marcsubjctsarray;
109         $dat->{MARCAUTHORS}    = $marcauthorsarray;
110         $dat->{'biblionumber'} = $biblionumber;
111         $dat->{ITEM_RESULTS}   = \@items;
112         $dat->{subtitle}       = $subtitle;
113         $dat->{HASAUTHORS}     = $dat->{'author'} || @$marcauthorsarray;
114
115         $iso2709 .= $record->as_usmarc();
116
117         push( @results, $dat );
118     }
119
120     $template2->param(
121         BIBLIO_RESULTS => \@results,
122         comment        => $comment,
123         shelfname      => $shelf->shelfname,
124         firstname      => $patron->firstname,
125         surname        => $patron->surname,
126     );
127
128     # Getting template result
129     my $template_res = $template2->output();
130     my $body;
131
132     # Analysing information and getting mail properties
133     if ( $template_res =~ /<SUBJECT>(.*)<END_SUBJECT>/s ) {
134         $mail{subject} = $1;
135         $mail{subject} =~ s|\n?(.*)\n?|$1|;
136     }
137     else { $mail{'subject'} = "no subject"; }
138     $mail{subject} = encode('MIME-Header', $mail{subject});
139
140     my $email_header = "";
141     if ( $template_res =~ /<HEADER>(.*)<END_HEADER>/s ) {
142         $email_header = $1;
143         $email_header =~ s|\n?(.*)\n?|$1|;
144         $email_header = encode_qp(Encode::encode("UTF-8", $email_header));
145     }
146
147     my $email_file = "list.txt";
148     if ( $template_res =~ /<FILENAME>(.*)<END_FILENAME>/s ) {
149         $email_file = $1;
150         $email_file =~ s|\n?(.*)\n?|$1|;
151     }
152
153     if ( $template_res =~ /<MESSAGE>(.*)<END_MESSAGE>/s ) {
154         $body = $1;
155         $body =~ s|\n?(.*)\n?|$1|;
156         $body = encode_qp(Encode::encode("UTF-8", $body));
157     }
158
159     my $boundary = "====" . time() . "====";
160
161     # We set and put the multipart content
162     $mail{'content-type'} = "multipart/mixed; boundary=\"$boundary\"";
163
164     my $isofile = encode_base64(encode("UTF-8", $iso2709));
165     $boundary = '--' . $boundary;
166
167     $mail{body} = <<END_OF_BODY;
168 $boundary
169 MIME-Version: 1.0
170 Content-Type: text/plain; charset="utf-8"
171 Content-Transfer-Encoding: quoted-printable
172
173 $email_header
174 $body
175 $boundary
176 Content-Type: application/octet-stream; name="list.iso2709"
177 Content-Transfer-Encoding: base64
178 Content-Disposition: attachment; filename="list.iso2709"
179
180 $isofile
181 $boundary--
182 END_OF_BODY
183
184     # Sending mail
185     if ( sendmail %mail ) {
186         # do something if it works....
187         $template->param( SENT      => "1" );
188     }
189     else {
190         # do something if it doesn't work....
191         carp "Error sending mail: $Mail::Sendmail::error \n";
192         $template->param( error => 1 );
193     }
194
195     $template->param(
196         shelfid => $shelfid,
197         email => $email,
198     );
199     output_html_with_http_headers $query, $cookie, $template->output, undef, { force_no_caching => 1 };
200
201
202 }else{
203     $template->param( shelfid => $shelfid,
204                       url     => "/cgi-bin/koha/opac-sendshelf.pl",
205                     );
206     output_html_with_http_headers $query, $cookie, $template->output, undef, { force_no_caching => 1 };
207 }
208
209 } else {
210     $template->param( invalidlist => 1,
211                       url     => "/cgi-bin/koha/opac-sendshelf.pl",
212     );
213     output_html_with_http_headers $query, $cookie, $template->output, undef, { force_no_caching => 1 };
214 }