Bug 7563 follow-up DBRev
[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;
24 use Encode qw(encode);
25 use Carp;
26
27 use Mail::Sendmail;
28 use MIME::QuotedPrint;
29 use MIME::Base64;
30 use HTML::FormatText;
31 use C4::Biblio;
32 use C4::Items;
33 use C4::Auth;
34 use C4::Output;
35 use C4::Biblio;
36 use C4::Members;
37
38 my $query = new CGI;
39
40 my ( $template, $borrowernumber, $cookie ) = get_template_and_user (
41     {
42         template_name   => "opac-sendbasketform.tmpl",
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 $user = GetMember(borrowernumber => $borrowernumber);
58     my $user_email = GetFirstValidEmailAddress($borrowernumber)
59     || C4::Context->preference('KohaAdminEmailAddress');
60
61     my $email_from = C4::Context->preference('KohaAdminEmailAddress');
62     my $email_replyto = "$user->{firstname} $user->{surname} <$user_email>";
63     my $comment    = $query->param('comment');
64     my %mail = (
65         To   => $email_add,
66         From => $email_from,
67     'Reply-To' => $email_replyto,
68 #    'X-Orig-IP' => $ENV{'REMOTE_ADDR'},
69 #    FIXME Commented out for now: discussion on privacy issue
70     'X-Abuse-Report' => C4::Context->preference('KohaAdminEmailAddress'),
71     );
72
73     my ( $template2, $borrowernumber, $cookie ) = get_template_and_user(
74         {
75             template_name   => "opac-sendbasket.tmpl",
76             query           => $query,
77             type            => "opac",
78             authnotrequired => 0,
79             flagsrequired   => { borrow => 1 },
80         }
81     );
82
83     my @bibs = split( /\//, $bib_list );
84     my @results;
85     my $iso2709;
86     my $marcflavour = C4::Context->preference('marcflavour');
87     foreach my $biblionumber (@bibs) {
88         $template2->param( biblionumber => $biblionumber );
89
90         my $dat              = GetBiblioData($biblionumber);
91         my $record           = GetMarcBiblio($biblionumber);
92         my $marcnotesarray   = GetMarcNotes( $record, $marcflavour );
93         my $marcauthorsarray = GetMarcAuthors( $record, $marcflavour );
94         my $marcsubjctsarray = GetMarcSubjects( $record, $marcflavour );
95
96         my @items = GetItemsInfo( $biblionumber );
97
98         my $hasauthors = 0;
99         if($dat->{'author'} || @$marcauthorsarray) {
100           $hasauthors = 1;
101         }
102         
103
104         $dat->{MARCNOTES}      = $marcnotesarray;
105         $dat->{MARCSUBJCTS}    = $marcsubjctsarray;
106         $dat->{MARCAUTHORS}    = $marcauthorsarray;
107         $dat->{HASAUTHORS}     = $hasauthors;
108         $dat->{'biblionumber'} = $biblionumber;
109         $dat->{ITEM_RESULTS}   = \@items;
110
111         $iso2709 .= $record->as_usmarc();
112
113         push( @results, $dat );
114     }
115
116     my $resultsarray = \@results;
117     
118     $template2->param(
119         BIBLIO_RESULTS => $resultsarray,
120         email_sender   => $email_sender,
121         comment        => $comment,
122         firstname      => $user->{firstname},
123         surname        => $user->{surname},
124     );
125
126     # Getting template result
127     my $template_res = $template2->output();
128     my $body;
129
130     # Analysing information and getting mail properties
131     if ( $template_res =~ /<SUBJECT>\n(.*)\n?<END_SUBJECT>/s ) {
132         $mail{'subject'} = $1;
133     }
134     else { $mail{'subject'} = "no subject"; }
135
136     my $email_header = "";
137     if ( $template_res =~ /<HEADER>\n(.*)\n?<END_HEADER>/s ) {
138         $email_header = $1;
139     }
140
141     my $email_file = "basket.txt";
142     if ( $template_res =~ /<FILENAME>\n(.*)\n?<END_FILENAME>/s ) {
143         $email_file = $1;
144     }
145
146     if ( $template_res =~ /<MESSAGE>\n(.*)\n?<END_MESSAGE>/s ) {
147         $body = $1;
148     }
149
150     my $boundary = "====" . time() . "====";
151
152     $mail{'content-type'} = "multipart/mixed; boundary=\"$boundary\"";
153     my $isofile = encode_base64(encode("UTF-8", $iso2709));
154     $boundary = '--' . $boundary;
155     $mail{body} = <<END_OF_BODY;
156 $boundary
157 MIME-Version: 1.0
158 Content-Type: text/plain; charset="UTF-8"
159 Content-Transfer-Encoding: quoted-printable
160
161 $email_header
162 $body
163 $boundary
164 Content-Type: application/octet-stream; name="basket.iso2709"
165 Content-Transfer-Encoding: base64
166 Content-Disposition: attachment; filename="basket.iso2709"
167
168 $isofile
169 $boundary--
170 END_OF_BODY
171
172     # Sending mail (if not empty basket)
173     if ( defined($iso2709) && sendmail %mail ) {
174     # do something if it works....
175         $template->param( SENT      => "1" );
176     }
177     else {
178         # do something if it doesnt work....
179     carp "Error sending mail: empty basket" if !defined($iso2709);
180         carp "Error sending mail: $Mail::Sendmail::error" if $Mail::Sendmail::error;
181         $template->param( error => 1 );
182     }
183     $template->param( email_add => $email_add );
184     output_html_with_http_headers $query, $cookie, $template->output;
185 }
186 else {
187     $template->param( bib_list => $bib_list );
188     $template->param(
189         url            => "/cgi-bin/koha/opac-sendbasket.pl",
190         suggestion     => C4::Context->preference("suggestion"),
191         virtualshelves => C4::Context->preference("virtualshelves"),
192     );
193     output_html_with_http_headers $query, $cookie, $template->output;
194 }