3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
21 use Encode qw(encode);
23 use Digest::MD5 qw(md5_base64);
25 use MIME::QuotedPrint;
38 my ( $template, $borrowernumber, $cookie ) = get_template_and_user (
40 template_name => "basket/sendbasketform.tt",
44 flagsrequired => { catalogue => 1 },
48 my $bib_list = $query->param('bib_list') || '';
49 my $email_add = $query->param('email_add');
51 my $dbh = C4::Context->dbh;
55 $csrf_err = 1 unless Koha::Token->new->check_csrf({
56 id => C4::Context->userenv->{id},
57 secret => md5_base64( C4::Context->config('pass') ),
58 token => scalar $query->param('csrf_token'),
63 $template->param( csrf_error => 1, email_add => 1 );
64 output_html_with_http_headers $query, $cookie, $template->output;
65 } elsif ( $email_add ) {
66 my $email = Koha::Email->new();
67 my %mail = $email->create_message_headers({ to => $email_add });
68 my $comment = $query->param('comment');
70 # Since we are already logged in, no need to check credentials again
71 # when loading a second template.
72 my $template2 = C4::Templates::gettemplate(
73 'basket/sendbasket.tt', 'intranet', $query,
76 my @bibs = split( /\//, $bib_list );
79 my $marcflavour = C4::Context->preference('marcflavour');
80 foreach my $biblionumber (@bibs) {
81 $template2->param( biblionumber => $biblionumber );
83 my $dat = GetBiblioData($biblionumber);
85 my $record = GetMarcBiblio($biblionumber, 1);
86 my $marcauthorsarray = GetMarcAuthors( $record, $marcflavour );
87 my $marcsubjctsarray = GetMarcSubjects( $record, $marcflavour );
89 my @items = GetItemsInfo( $biblionumber );
92 if($dat->{'author'} || @$marcauthorsarray) {
97 $dat->{MARCSUBJCTS} = $marcsubjctsarray;
98 $dat->{MARCAUTHORS} = $marcauthorsarray;
99 $dat->{HASAUTHORS} = $hasauthors;
100 $dat->{'biblionumber'} = $biblionumber;
101 $dat->{ITEM_RESULTS} = \@items;
103 $iso2709 .= $record->as_usmarc();
105 push( @results, $dat );
108 my $resultsarray = \@results;
110 BIBLIO_RESULTS => $resultsarray,
114 # Getting template result
115 my $template_res = $template2->output();
118 # Analysing information and getting mail properties
119 if ( $template_res =~ /<SUBJECT>(.*)<END_SUBJECT>/s ) {
121 $mail{subject} =~ s|\n?(.*)\n?|$1|;
122 $mail{subject} = Encode::encode("UTF-8", $mail{subject});
124 else { $mail{'subject'} = "no subject"; }
126 my $email_header = "";
127 if ( $template_res =~ /<HEADER>(.*)<END_HEADER>/s ) {
129 $email_header =~ s|\n?(.*)\n?|$1|;
130 $email_header = encode_qp(Encode::encode("UTF-8", $email_header));
133 my $email_file = "basket.txt";
134 if ( $template_res =~ /<FILENAME>(.*)<END_FILENAME>/s ) {
136 $email_file =~ s|\n?(.*)\n?|$1|;
139 if ( $template_res =~ /<MESSAGE>(.*)<END_MESSAGE>/s ) {
141 $body =~ s|\n?(.*)\n?|$1|;
142 $body = encode_qp(Encode::encode("UTF-8", $body));
145 my $boundary = "====" . time() . "====";
148 $mail{'content-type'} = "multipart/mixed; boundary=\"$boundary\"";
149 my $isofile = encode_base64(encode("UTF-8", $iso2709));
150 $boundary = '--' . $boundary;
151 $mail{body} = <<END_OF_BODY;
153 Content-Type: text/plain; charset="utf-8"
154 Content-Transfer-Encoding: quoted-printable
159 Content-Type: application/octet-stream; name="basket.iso2709"
160 Content-Transfer-Encoding: base64
161 Content-Disposition: attachment; filename="basket.iso2709"
168 if ( sendmail %mail ) {
169 # do something if it works....
170 $template->param( SENT => "1" );
173 # do something if it doesnt work....
174 carp "Error sending mail: $Mail::Sendmail::error \n";
175 $template->param( error => 1 );
177 $template->param( email_add => $email_add );
178 output_html_with_http_headers $query, $cookie, $template->output;
182 bib_list => $bib_list,
183 url => "/cgi-bin/koha/basket/sendbasket.pl",
184 suggestion => C4::Context->preference("suggestion"),
185 virtualshelves => C4::Context->preference("virtualshelves"),
186 csrf_token => Koha::Token->new->generate_csrf(
187 { id => C4::Context->userenv->{id},
188 secret => md5_base64( C4::Context->config('pass') ),
192 output_html_with_http_headers $query, $cookie, $template->output;