Bug 13789 - facets with accented utf-8 characters generate double encoded links
[koha.git] / opac / opac-sendbasket.pl
1 #!/usr/bin/perl
2
3 # Copyright Doxulting 2004
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 qw ( -utf8 );
24 use Encode qw(encode);
25 use Carp;
26
27 use Mail::Sendmail;
28 use MIME::QuotedPrint;
29 use MIME::Base64;
30 use C4::Biblio;
31 use C4::Items;
32 use C4::Auth;
33 use C4::Output;
34 use C4::Biblio;
35 use C4::Members;
36 use Koha::Email;
37
38 my $query = new CGI;
39
40 my ( $template, $borrowernumber, $cookie ) = get_template_and_user (
41     {
42         template_name   => "opac-sendbasketform.tt",
43         query           => $query,
44         type            => "opac",
45         authnotrequired => 0,
46         flagsrequired   => { borrow => 1 },
47     }
48 );
49
50 my $bib_list     = $query->param('bib_list');
51 my $email_add    = $query->param('email_add');
52 my $email_sender = $query->param('email_sender');
53
54 my $dbh          = C4::Context->dbh;
55
56 if ( $email_add ) {
57     my $email = Koha::Email->new();
58     my $user = GetMember(borrowernumber => $borrowernumber);
59     my $user_email = GetFirstValidEmailAddress($borrowernumber)
60     || C4::Context->preference('KohaAdminEmailAddress');
61
62     my $email_replyto = "$user->{firstname} $user->{surname} <$user_email>";
63     my $comment    = $query->param('comment');
64
65    # if you want to use the KohaAdmin address as from, that is the default no need to set it
66     my %mail = $email->create_message_headers({
67         to => $email_add,
68         replyto => $email_replyto,
69     });
70     $mail{'X-Abuse-Report'} = C4::Context->preference('KohaAdminEmailAddress');
71
72     my ( $template2, $borrowernumber, $cookie ) = get_template_and_user(
73         {
74             template_name   => "opac-sendbasket.tt",
75             query           => $query,
76             type            => "opac",
77             authnotrequired => 0,
78             flagsrequired   => { borrow => 1 },
79         }
80     );
81
82     my @bibs = split( /\//, $bib_list );
83     my @results;
84     my $iso2709;
85     my $marcflavour = C4::Context->preference('marcflavour');
86     foreach my $biblionumber (@bibs) {
87         $template2->param( biblionumber => $biblionumber );
88
89         my $dat              = GetBiblioData($biblionumber);
90         my $record           = GetMarcBiblio($biblionumber, 1);
91         my $marcnotesarray   = GetMarcNotes( $record, $marcflavour );
92         my $marcauthorsarray = GetMarcAuthors( $record, $marcflavour );
93         my $marcsubjctsarray = GetMarcSubjects( $record, $marcflavour );
94
95         my @items = GetItemsInfo( $biblionumber );
96
97         my $hasauthors = 0;
98         if($dat->{'author'} || @$marcauthorsarray) {
99           $hasauthors = 1;
100         }
101         
102
103         $dat->{MARCNOTES}      = $marcnotesarray;
104         $dat->{MARCSUBJCTS}    = $marcsubjctsarray;
105         $dat->{MARCAUTHORS}    = $marcauthorsarray;
106         $dat->{HASAUTHORS}     = $hasauthors;
107         $dat->{'biblionumber'} = $biblionumber;
108         $dat->{ITEM_RESULTS}   = \@items;
109
110         $iso2709 .= $record->as_usmarc();
111
112         push( @results, $dat );
113     }
114
115     my $resultsarray = \@results;
116     
117     $template2->param(
118         BIBLIO_RESULTS => $resultsarray,
119         email_sender   => $email_sender,
120         comment        => $comment,
121         firstname      => $user->{firstname},
122         surname        => $user->{surname},
123     );
124
125     # Getting template result
126     my $template_res = $template2->output();
127     my $body;
128
129     # Analysing information and getting mail properties
130
131     if ( $template_res =~ /<SUBJECT>(.*)<END_SUBJECT>/s ) {
132         $mail{subject} = $1;
133         $mail{subject} =~ s|\n?(.*)\n?|$1|;
134         $mail{subject} = Encode::encode("UTF-8", $mail{subject});
135     }
136     else { $mail{'subject'} = "no subject"; }
137
138     my $email_header = "";
139     if ( $template_res =~ /<HEADER>(.*)<END_HEADER>/s ) {
140         $email_header = $1;
141         $email_header =~ s|\n?(.*)\n?|$1|;
142         $email_header = encode_qp(Encode::encode("UTF-8", $email_header));
143     }
144
145     my $email_file = "basket.txt";
146     if ( $template_res =~ /<FILENAME>(.*)<END_FILENAME>/s ) {
147         $email_file = $1;
148         $email_file =~ s|\n?(.*)\n?|$1|;
149     }
150
151     if ( $template_res =~ /<MESSAGE>(.*)<END_MESSAGE>/s ) {
152         $body = $1;
153         $body =~ s|\n?(.*)\n?|$1|;
154         $body = encode_qp(Encode::encode("UTF-8", $body));
155     }
156
157     $mail{body} = $body;
158
159     my $boundary = "====" . time() . "====";
160
161     $mail{'content-type'} = "multipart/mixed; boundary=\"$boundary\"";
162     my $isofile = encode_base64(encode("UTF-8", $iso2709));
163     $boundary = '--' . $boundary;
164     $mail{body} = <<END_OF_BODY;
165 $boundary
166 MIME-Version: 1.0
167 Content-Type: text/plain; charset="UTF-8"
168 Content-Transfer-Encoding: quoted-printable
169
170 $email_header
171 $body
172 $boundary
173 Content-Type: application/octet-stream; name="basket.iso2709"
174 Content-Transfer-Encoding: base64
175 Content-Disposition: attachment; filename="basket.iso2709"
176
177 $isofile
178 $boundary--
179 END_OF_BODY
180
181     # Sending mail (if not empty basket)
182     if ( defined($iso2709) && sendmail %mail ) {
183     # do something if it works....
184         $template->param( SENT      => "1" );
185     }
186     else {
187         # do something if it doesnt work....
188     carp "Error sending mail: empty basket" if !defined($iso2709);
189         carp "Error sending mail: $Mail::Sendmail::error" if $Mail::Sendmail::error;
190         $template->param( error => 1 );
191     }
192     $template->param( email_add => $email_add );
193     output_html_with_http_headers $query, $cookie, $template->output;
194 }
195 else {
196     $template->param( bib_list => $bib_list );
197     $template->param(
198         url            => "/cgi-bin/koha/opac-sendbasket.pl",
199         suggestion     => C4::Context->preference("suggestion"),
200         virtualshelves => C4::Context->preference("virtualshelves"),
201     );
202     output_html_with_http_headers $query, $cookie, $template->output;
203 }