Bug 18704 - File types limit in tools/export.pl is causing issues with csv files...
[koha.git] / opac / opac-sendbasket.pl
blob063dd8bf06a016e41619a902f8c8eb9ff715e6a1
1 #!/usr/bin/perl
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>.
20 use Modern::Perl;
22 use CGI qw ( -utf8 );
23 use Encode qw(encode);
24 use Carp;
25 use Mail::Sendmail;
26 use MIME::QuotedPrint;
27 use MIME::Base64;
29 use C4::Biblio;
30 use C4::Items;
31 use C4::Auth;
32 use C4::Output;
33 use C4::Members;
34 use C4::Templates ();
35 use Koha::Email;
36 use Koha::Token;
38 my $query = new CGI;
40 my ( $template, $borrowernumber, $cookie ) = get_template_and_user (
42 template_name => "opac-sendbasketform.tt",
43 query => $query,
44 type => "opac",
45 authnotrequired => 0,
49 my $bib_list = $query->param('bib_list') || '';
50 my $email_add = $query->param('email_add');
52 my $dbh = C4::Context->dbh;
54 if ( $email_add ) {
55 die "Wrong CSRF token" unless Koha::Token->new->check_csrf({
56 session_id => scalar $query->cookie('CGISESSID'),
57 token => scalar $query->param('csrf_token'),
58 });
59 my $email = Koha::Email->new();
60 my $user = GetMember(borrowernumber => $borrowernumber);
61 my $user_email = GetFirstValidEmailAddress($borrowernumber)
62 || C4::Context->preference('KohaAdminEmailAddress');
64 my $email_replyto = "$user->{firstname} $user->{surname} <$user_email>";
65 my $comment = $query->param('comment');
67 # if you want to use the KohaAdmin address as from, that is the default no need to set it
68 my %mail = $email->create_message_headers({
69 to => $email_add,
70 replyto => $email_replyto,
71 });
72 $mail{'X-Abuse-Report'} = C4::Context->preference('KohaAdminEmailAddress');
74 # Since we are already logged in, no need to check credentials again
75 # when loading a second template.
76 my $template2 = C4::Templates::gettemplate(
77 'opac-sendbasket.tt', 'opac', $query,
80 my @bibs = split( /\//, $bib_list );
81 my @results;
82 my $iso2709;
83 my $marcflavour = C4::Context->preference('marcflavour');
84 foreach my $biblionumber (@bibs) {
85 $template2->param( biblionumber => $biblionumber );
87 my $dat = GetBiblioData($biblionumber);
88 next unless $dat;
89 my $record = GetMarcBiblio($biblionumber, 1);
90 my $marcauthorsarray = GetMarcAuthors( $record, $marcflavour );
91 my $marcsubjctsarray = GetMarcSubjects( $record, $marcflavour );
93 my @items = GetItemsInfo( $biblionumber );
95 my $hasauthors = 0;
96 if($dat->{'author'} || @$marcauthorsarray) {
97 $hasauthors = 1;
101 $dat->{MARCSUBJCTS} = $marcsubjctsarray;
102 $dat->{MARCAUTHORS} = $marcauthorsarray;
103 $dat->{HASAUTHORS} = $hasauthors;
104 $dat->{'biblionumber'} = $biblionumber;
105 $dat->{ITEM_RESULTS} = \@items;
107 $iso2709 .= $record->as_usmarc();
109 push( @results, $dat );
112 my $resultsarray = \@results;
114 $template2->param(
115 BIBLIO_RESULTS => $resultsarray,
116 comment => $comment,
117 firstname => $user->{firstname},
118 surname => $user->{surname},
121 # Getting template result
122 my $template_res = $template2->output();
123 my $body;
125 # Analysing information and getting mail properties
127 if ( $template_res =~ /<SUBJECT>(.*)<END_SUBJECT>/s ) {
128 $mail{subject} = $1;
129 $mail{subject} =~ s|\n?(.*)\n?|$1|;
130 $mail{subject} = Encode::encode("UTF-8", $mail{subject});
132 else { $mail{'subject'} = "no subject"; }
134 my $email_header = "";
135 if ( $template_res =~ /<HEADER>(.*)<END_HEADER>/s ) {
136 $email_header = $1;
137 $email_header =~ s|\n?(.*)\n?|$1|;
138 $email_header = encode_qp(Encode::encode("UTF-8", $email_header));
141 my $email_file = "basket.txt";
142 if ( $template_res =~ /<FILENAME>(.*)<END_FILENAME>/s ) {
143 $email_file = $1;
144 $email_file =~ s|\n?(.*)\n?|$1|;
147 if ( $template_res =~ /<MESSAGE>(.*)<END_MESSAGE>/s ) {
148 $body = $1;
149 $body =~ s|\n?(.*)\n?|$1|;
150 $body = encode_qp(Encode::encode("UTF-8", $body));
153 $mail{body} = $body;
155 my $boundary = "====" . time() . "====";
157 $mail{'content-type'} = "multipart/mixed; boundary=\"$boundary\"";
158 my $isofile = encode_base64(encode("UTF-8", $iso2709));
159 $boundary = '--' . $boundary;
160 $mail{body} = <<END_OF_BODY;
161 $boundary
162 MIME-Version: 1.0
163 Content-Type: text/plain; charset="UTF-8"
164 Content-Transfer-Encoding: quoted-printable
166 $email_header
167 $body
168 $boundary
169 Content-Type: application/octet-stream; name="basket.iso2709"
170 Content-Transfer-Encoding: base64
171 Content-Disposition: attachment; filename="basket.iso2709"
173 $isofile
174 $boundary--
175 END_OF_BODY
177 # Sending mail (if not empty basket)
178 if ( defined($iso2709) && sendmail %mail ) {
179 # do something if it works....
180 $template->param( SENT => "1" );
182 else {
183 # do something if it doesnt work....
184 carp "Error sending mail: empty basket" if !defined($iso2709);
185 carp "Error sending mail: $Mail::Sendmail::error" if $Mail::Sendmail::error;
186 $template->param( error => 1 );
188 $template->param( email_add => $email_add );
189 output_html_with_http_headers $query, $cookie, $template->output;
191 else {
192 $template->param(
193 bib_list => $bib_list,
194 url => "/cgi-bin/koha/opac-sendbasket.pl",
195 suggestion => C4::Context->preference("suggestion"),
196 virtualshelves => C4::Context->preference("virtualshelves"),
197 csrf_token => Koha::Token->new->generate_csrf(
198 { session_id => scalar $query->cookie('CGISESSID'), } ),
200 output_html_with_http_headers $query, $cookie, $template->output;