Bug 24412: (follow-up) prevent request.pl from failing
[koha.git] / basket / sendbasket.pl
blob9e273b1f45be255b281b14df6db749bff238df20
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 Try::Tiny;
25 use C4::Biblio;
26 use C4::Items;
27 use C4::Auth;
28 use C4::Output;
29 use C4::Templates ();
30 use Koha::Email;
31 use Koha::Token;
33 my $query = CGI->new;
35 my ( $template, $borrowernumber, $cookie ) = get_template_and_user (
37 template_name => "basket/sendbasketform.tt",
38 query => $query,
39 type => "intranet",
40 flagsrequired => { catalogue => 1 },
44 my $bib_list = $query->param('bib_list') || '';
45 my $email_add = $query->param('email_add');
47 my $dbh = C4::Context->dbh;
49 if ( $email_add ) {
50 output_and_exit( $query, $cookie, $template, 'wrong_csrf_token' )
51 unless Koha::Token->new->check_csrf({
52 session_id => scalar $query->cookie('CGISESSID'),
53 token => scalar $query->param('csrf_token'),
54 });
55 my $comment = $query->param('comment');
57 # Since we are already logged in, no need to check credentials again
58 # when loading a second template.
59 my $template2 = C4::Templates::gettemplate(
60 'basket/sendbasket.tt', 'intranet', $query,
63 my @bibs = split( /\//, $bib_list );
64 my @results;
65 my $iso2709;
66 my $marcflavour = C4::Context->preference('marcflavour');
67 foreach my $biblionumber (@bibs) {
68 $template2->param( biblionumber => $biblionumber );
70 my $dat = GetBiblioData($biblionumber);
71 next unless $dat;
72 my $record = GetMarcBiblio({
73 biblionumber => $biblionumber,
74 embed_items => 1 });
75 my $marcauthorsarray = GetMarcAuthors( $record, $marcflavour );
76 my $marcsubjctsarray = GetMarcSubjects( $record, $marcflavour );
78 my @items = GetItemsInfo( $biblionumber );
80 my $hasauthors = 0;
81 if($dat->{'author'} || @$marcauthorsarray) {
82 $hasauthors = 1;
86 $dat->{MARCSUBJCTS} = $marcsubjctsarray;
87 $dat->{MARCAUTHORS} = $marcauthorsarray;
88 $dat->{HASAUTHORS} = $hasauthors;
89 $dat->{'biblionumber'} = $biblionumber;
90 $dat->{ITEM_RESULTS} = \@items;
92 $iso2709 .= $record->as_usmarc();
94 push( @results, $dat );
97 my $resultsarray = \@results;
98 $template2->param(
99 BIBLIO_RESULTS => $resultsarray,
100 comment => $comment
103 # Getting template result
104 my $template_res = $template2->output();
105 my $body;
107 my $subject;
108 # Analysing information and getting mail properties
109 if ( $template_res =~ /<SUBJECT>(?<subject>.*)<END_SUBJECT>/s ) {
110 $subject = $+{subject};
111 $subject =~ s|\n?(.*)\n?|$1|;
113 else {
114 $subject = "no subject";
117 my $email_header = "";
118 if ( $template_res =~ /<HEADER>(.*)<END_HEADER>/s ) {
119 $email_header = $1;
120 $email_header =~ s|\n?(.*)\n?|$1|;
123 if ( $template_res =~ /<MESSAGE>(.*)<END_MESSAGE>/s ) {
124 $body = $1;
125 $body =~ s|\n?(.*)\n?|$1|;
128 my $THE_body = <<END_OF_BODY;
129 $email_header
130 $body
131 END_OF_BODY
133 try {
135 my $email = Koha::Email->create(
137 to => $email_add,
138 subject => $subject,
142 $email->text_body( $THE_body );
143 $email->attach(
144 Encode::encode( "UTF-8", $iso2709 ),
145 content_type => 'application/octet-stream',
146 name => 'basket.iso2709',
147 disposition => 'attachment',
150 my $library = Koha::Patrons->find( $borrowernumber )->library;
151 $email->send_or_die({ transport => $library->smtp_server->transport });
152 $template->param( SENT => "1" );
154 catch {
155 carp "Error sending mail: $_";
156 $template->param( error => 1 );
159 $template->param( email_add => $email_add );
160 output_html_with_http_headers $query, $cookie, $template->output;
162 else {
163 $template->param(
164 bib_list => $bib_list,
165 url => "/cgi-bin/koha/basket/sendbasket.pl",
166 suggestion => C4::Context->preference("suggestion"),
167 virtualshelves => C4::Context->preference("virtualshelves"),
168 csrf_token => Koha::Token->new->generate_csrf({ session_id => scalar $query->cookie('CGISESSID'), }),
170 output_html_with_http_headers $query, $cookie, $template->output;