3 # Copyright Doxulting 2004
5 # This file is part of Koha.
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.
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.
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>.
23 use Encode qw(encode);
25 use Digest::MD5 qw(md5_base64);
27 use MIME::QuotedPrint;
41 my ( $template, $borrowernumber, $cookie ) = get_template_and_user (
43 template_name => "opac-sendbasketform.tt",
50 my $bib_list = $query->param('bib_list') || '';
51 my $email_add = $query->param('email_add');
53 my $dbh = C4::Context->dbh;
57 $csrf_err = 1 unless Koha::Token->new->check_csrf({
58 id => C4::Context->userenv->{id},
59 secret => md5_base64( C4::Context->config('pass') ),
60 token => scalar $query->param('csrf_token'),
65 $template->param( csrf_error => 1, email_add => 1 );
66 output_html_with_http_headers $query, $cookie, $template->output;
67 } elsif ( $email_add ) {
68 my $email = Koha::Email->new();
69 my $user = GetMember(borrowernumber => $borrowernumber);
70 my $user_email = GetFirstValidEmailAddress($borrowernumber)
71 || C4::Context->preference('KohaAdminEmailAddress');
73 my $email_replyto = "$user->{firstname} $user->{surname} <$user_email>";
74 my $comment = $query->param('comment');
76 # if you want to use the KohaAdmin address as from, that is the default no need to set it
77 my %mail = $email->create_message_headers({
79 replyto => $email_replyto,
81 $mail{'X-Abuse-Report'} = C4::Context->preference('KohaAdminEmailAddress');
83 # Since we are already logged in, no need to check credentials again
84 # when loading a second template.
85 my $template2 = C4::Templates::gettemplate(
86 'opac-sendbasket.tt', 'opac', $query,
89 my @bibs = split( /\//, $bib_list );
92 my $marcflavour = C4::Context->preference('marcflavour');
93 foreach my $biblionumber (@bibs) {
94 $template2->param( biblionumber => $biblionumber );
96 my $dat = GetBiblioData($biblionumber);
98 my $record = GetMarcBiblio($biblionumber, 1);
99 my $marcauthorsarray = GetMarcAuthors( $record, $marcflavour );
100 my $marcsubjctsarray = GetMarcSubjects( $record, $marcflavour );
102 my @items = GetItemsInfo( $biblionumber );
105 if($dat->{'author'} || @$marcauthorsarray) {
110 $dat->{MARCSUBJCTS} = $marcsubjctsarray;
111 $dat->{MARCAUTHORS} = $marcauthorsarray;
112 $dat->{HASAUTHORS} = $hasauthors;
113 $dat->{'biblionumber'} = $biblionumber;
114 $dat->{ITEM_RESULTS} = \@items;
116 $iso2709 .= $record->as_usmarc();
118 push( @results, $dat );
121 my $resultsarray = \@results;
124 BIBLIO_RESULTS => $resultsarray,
126 firstname => $user->{firstname},
127 surname => $user->{surname},
130 # Getting template result
131 my $template_res = $template2->output();
134 # Analysing information and getting mail properties
136 if ( $template_res =~ /<SUBJECT>(.*)<END_SUBJECT>/s ) {
138 $mail{subject} =~ s|\n?(.*)\n?|$1|;
139 $mail{subject} = Encode::encode("UTF-8", $mail{subject});
141 else { $mail{'subject'} = "no subject"; }
143 my $email_header = "";
144 if ( $template_res =~ /<HEADER>(.*)<END_HEADER>/s ) {
146 $email_header =~ s|\n?(.*)\n?|$1|;
147 $email_header = encode_qp(Encode::encode("UTF-8", $email_header));
150 my $email_file = "basket.txt";
151 if ( $template_res =~ /<FILENAME>(.*)<END_FILENAME>/s ) {
153 $email_file =~ s|\n?(.*)\n?|$1|;
156 if ( $template_res =~ /<MESSAGE>(.*)<END_MESSAGE>/s ) {
158 $body =~ s|\n?(.*)\n?|$1|;
159 $body = encode_qp(Encode::encode("UTF-8", $body));
164 my $boundary = "====" . time() . "====";
166 $mail{'content-type'} = "multipart/mixed; boundary=\"$boundary\"";
167 my $isofile = encode_base64(encode("UTF-8", $iso2709));
168 $boundary = '--' . $boundary;
169 $mail{body} = <<END_OF_BODY;
172 Content-Type: text/plain; charset="UTF-8"
173 Content-Transfer-Encoding: quoted-printable
178 Content-Type: application/octet-stream; name="basket.iso2709"
179 Content-Transfer-Encoding: base64
180 Content-Disposition: attachment; filename="basket.iso2709"
186 # Sending mail (if not empty basket)
187 if ( defined($iso2709) && sendmail %mail ) {
188 # do something if it works....
189 $template->param( SENT => "1" );
192 # do something if it doesnt work....
193 carp "Error sending mail: empty basket" if !defined($iso2709);
194 carp "Error sending mail: $Mail::Sendmail::error" if $Mail::Sendmail::error;
195 $template->param( error => 1 );
197 $template->param( email_add => $email_add );
198 output_html_with_http_headers $query, $cookie, $template->output;
202 bib_list => $bib_list,
203 url => "/cgi-bin/koha/opac-sendbasket.pl",
204 suggestion => C4::Context->preference("suggestion"),
205 virtualshelves => C4::Context->preference("virtualshelves"),
206 csrf_token => Koha::Token->new->generate_csrf(
207 { id => C4::Context->userenv->{id},
208 secret => md5_base64( C4::Context->config('pass') ),
212 output_html_with_http_headers $query, $cookie, $template->output;