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