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>.
25 use Try::Tiny qw( catch try );
33 use C4::Items qw( GetItemsInfo );
34 use C4::Auth qw( get_template_and_user );
35 use C4::Output qw( output_html_with_http_headers );
43 my ( $template, $borrowernumber, $cookie ) = get_template_and_user (
45 template_name => "opac-sendbasketform.tt",
51 my $bib_list = $query->param('bib_list') || '';
52 my $email_add = $query->param('email_add');
54 my $dbh = C4::Context->dbh;
57 die "Wrong CSRF token" unless Koha::Token->new->check_csrf({
58 session_id => scalar $query->cookie('CGISESSID'),
59 token => scalar $query->param('csrf_token'),
61 my $patron = Koha::Patrons->find( $borrowernumber );
62 my $borcat = $patron ? $patron->categorycode : q{};
63 my $user_email = $patron->first_valid_email_address
64 || C4::Context->preference('KohaAdminEmailAddress');
66 my $email_replyto = $patron->firstname . " " . $patron->surname . " <$user_email>";
67 my $comment = $query->param('comment');
69 # Since we are already logged in, no need to check credentials again
70 # when loading a second template.
71 my $template2 = C4::Templates::gettemplate(
72 'opac-sendbasket.tt', 'opac', $query,
75 my @bibs = split( /\//, $bib_list );
78 my $marcflavour = C4::Context->preference('marcflavour');
79 foreach my $biblionumber (@bibs) {
80 $template2->param( biblionumber => $biblionumber );
82 my $dat = GetBiblioData($biblionumber);
84 my $record = GetMarcBiblio({
85 biblionumber => $biblionumber,
89 my $marcauthorsarray = GetMarcAuthors( $record, $marcflavour );
90 my $marcsubjctsarray = GetMarcSubjects( $record, $marcflavour );
92 my @items = GetItemsInfo( $biblionumber );
95 if($dat->{'author'} || @$marcauthorsarray) {
100 $dat->{MARCSUBJCTS} = $marcsubjctsarray;
101 $dat->{MARCAUTHORS} = $marcauthorsarray;
102 $dat->{HASAUTHORS} = $hasauthors;
103 $dat->{'biblionumber'} = $biblionumber;
104 $dat->{ITEM_RESULTS} = \@items;
106 $iso2709 .= $record->as_usmarc();
108 push( @results, $dat );
111 my $resultsarray = \@results;
114 BIBLIO_RESULTS => $resultsarray,
116 firstname => $patron->firstname,
117 surname => $patron->surname,
120 # Getting template result
121 my $template_res = $template2->output();
124 # Analysing information and getting mail properties
126 if ( $template_res =~ /\<SUBJECT\>(?<subject>.*)\<END_SUBJECT\>/s ) {
127 $subject = $+{subject};
128 $subject =~ s|\n?(.*)\n?|$1|;
131 $subject = "no subject";
134 my $email_header = "";
135 if ( $template_res =~ /<HEADER>(.*)<END_HEADER>/s ) {
137 $email_header =~ s|\n?(.*)\n?|$1|;
140 if ( $template_res =~ /<MESSAGE>(.*)<END_MESSAGE>/s ) {
142 $body =~ s|\n?(.*)\n?|$1|;
145 my $THE_body = <<END_OF_BODY;
150 if ( !defined $iso2709 ) {
151 carp "Error sending mail: empty basket";
152 $template->param( error => 1 );
156 # if you want to use the KohaAdmin address as from, that is the default no need to set it
157 my $email = Koha::Email->create({
159 reply_to => $email_replyto,
162 $email->header( 'X-Abuse-Report' => C4::Context->preference('KohaAdminEmailAddress') );
163 $email->text_body( $THE_body );
165 Encode::encode( "UTF-8", $iso2709 ),
166 content_type => 'application/octet-stream',
167 name => 'basket.iso2709',
168 disposition => 'attachment',
170 my $library = $patron->library;
171 $email->transport( $library->smtp_server->transport );
173 $template->param( SENT => "1" );
176 carp "Error sending mail: $_";
177 $template->param( error => 1 );
181 $template->param( email_add => $email_add );
182 output_html_with_http_headers $query, $cookie, $template->output, undef, { force_no_caching => 1 };
185 my $new_session_id = $cookie->value;
187 bib_list => $bib_list,
188 url => "/cgi-bin/koha/opac-sendbasket.pl",
189 suggestion => C4::Context->preference("suggestion"),
190 virtualshelves => C4::Context->preference("virtualshelves"),
191 csrf_token => Koha::Token->new->generate_csrf(
192 { session_id => $new_session_id, } ),
194 output_html_with_http_headers $query, $cookie, $template->output, undef, { force_no_caching => 1 };