Bug 7663: Fix bad merge conflict with bug 15584
[koha.git] / basket / sendbasket.pl
blobab29824a64003293575530c92ee37489ad1b65b5
1 #!/usr/bin/perl
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>.
18 use Modern::Perl;
20 use CGI qw ( -utf8 );
21 use Encode qw(encode);
22 use Carp;
23 use Digest::MD5 qw(md5_base64);
24 use Mail::Sendmail;
25 use MIME::QuotedPrint;
26 use MIME::Base64;
28 use C4::Biblio;
29 use C4::Items;
30 use C4::Auth;
31 use C4::Output;
32 use C4::Templates ();
33 use Koha::Email;
34 use Koha::Token;
36 my $query = new CGI;
38 my ( $template, $borrowernumber, $cookie ) = get_template_and_user (
40 template_name => "basket/sendbasketform.tt",
41 query => $query,
42 type => "intranet",
43 authnotrequired => 0,
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;
53 if ( $email_add ) {
54 die "Wrong CSRF token" unless Koha::Token->new->check_csrf({
55 id => Encode::encode( 'UTF-8', C4::Context->userenv->{id} ),
56 secret => md5_base64( Encode::encode( 'UTF-8', C4::Context->config('pass') ) ),
57 token => scalar $query->param('csrf_token'),
58 });
59 my $email = Koha::Email->new();
60 my %mail = $email->create_message_headers({ to => $email_add });
61 my $comment = $query->param('comment');
63 # Since we are already logged in, no need to check credentials again
64 # when loading a second template.
65 my $template2 = C4::Templates::gettemplate(
66 'basket/sendbasket.tt', 'intranet', $query,
69 my @bibs = split( /\//, $bib_list );
70 my @results;
71 my $iso2709;
72 my $marcflavour = C4::Context->preference('marcflavour');
73 foreach my $biblionumber (@bibs) {
74 $template2->param( biblionumber => $biblionumber );
76 my $dat = GetBiblioData($biblionumber);
77 next unless $dat;
78 my $record = GetMarcBiblio($biblionumber, 1);
79 my $marcauthorsarray = GetMarcAuthors( $record, $marcflavour );
80 my $marcsubjctsarray = GetMarcSubjects( $record, $marcflavour );
82 my @items = GetItemsInfo( $biblionumber );
84 my $hasauthors = 0;
85 if($dat->{'author'} || @$marcauthorsarray) {
86 $hasauthors = 1;
90 $dat->{MARCSUBJCTS} = $marcsubjctsarray;
91 $dat->{MARCAUTHORS} = $marcauthorsarray;
92 $dat->{HASAUTHORS} = $hasauthors;
93 $dat->{'biblionumber'} = $biblionumber;
94 $dat->{ITEM_RESULTS} = \@items;
96 $iso2709 .= $record->as_usmarc();
98 push( @results, $dat );
101 my $resultsarray = \@results;
102 $template2->param(
103 BIBLIO_RESULTS => $resultsarray,
104 comment => $comment
107 # Getting template result
108 my $template_res = $template2->output();
109 my $body;
111 # Analysing information and getting mail properties
112 if ( $template_res =~ /<SUBJECT>(.*)<END_SUBJECT>/s ) {
113 $mail{subject} = $1;
114 $mail{subject} =~ s|\n?(.*)\n?|$1|;
115 $mail{subject} = Encode::encode("UTF-8", $mail{subject});
117 else { $mail{'subject'} = "no subject"; }
119 my $email_header = "";
120 if ( $template_res =~ /<HEADER>(.*)<END_HEADER>/s ) {
121 $email_header = $1;
122 $email_header =~ s|\n?(.*)\n?|$1|;
123 $email_header = encode_qp(Encode::encode("UTF-8", $email_header));
126 my $email_file = "basket.txt";
127 if ( $template_res =~ /<FILENAME>(.*)<END_FILENAME>/s ) {
128 $email_file = $1;
129 $email_file =~ s|\n?(.*)\n?|$1|;
132 if ( $template_res =~ /<MESSAGE>(.*)<END_MESSAGE>/s ) {
133 $body = $1;
134 $body =~ s|\n?(.*)\n?|$1|;
135 $body = encode_qp(Encode::encode("UTF-8", $body));
138 my $boundary = "====" . time() . "====";
140 # Writing mail
141 $mail{'content-type'} = "multipart/mixed; boundary=\"$boundary\"";
142 my $isofile = encode_base64(encode("UTF-8", $iso2709));
143 $boundary = '--' . $boundary;
144 $mail{body} = <<END_OF_BODY;
145 $boundary
146 Content-Type: text/plain; charset="utf-8"
147 Content-Transfer-Encoding: quoted-printable
149 $email_header
150 $body
151 $boundary
152 Content-Type: application/octet-stream; name="basket.iso2709"
153 Content-Transfer-Encoding: base64
154 Content-Disposition: attachment; filename="basket.iso2709"
156 $isofile
157 $boundary--
158 END_OF_BODY
160 # Sending mail
161 if ( sendmail %mail ) {
162 # do something if it works....
163 $template->param( SENT => "1" );
165 else {
166 # do something if it doesnt work....
167 carp "Error sending mail: $Mail::Sendmail::error \n";
168 $template->param( error => 1 );
170 $template->param( email_add => $email_add );
171 output_html_with_http_headers $query, $cookie, $template->output;
173 else {
174 $template->param(
175 bib_list => $bib_list,
176 url => "/cgi-bin/koha/basket/sendbasket.pl",
177 suggestion => C4::Context->preference("suggestion"),
178 virtualshelves => C4::Context->preference("virtualshelves"),
179 csrf_token => Koha::Token->new->generate_csrf(
180 { id => Encode::encode( 'UTF-8', C4::Context->userenv->{id} ),
181 secret => md5_base64( Encode::encode( 'UTF-8', C4::Context->config('pass') ) ),
185 output_html_with_http_headers $query, $cookie, $template->output;