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 );
30 use C4::Items qw( GetItemsInfo );
31 use C4::Auth qw( get_template_and_user );
32 use C4::Output qw( output_html_with_http_headers );
41 my ( $template, $borrowernumber, $cookie ) = get_template_and_user (
43 template_name => "opac-sendbasketform.tt",
49 my $bib_list = $query->param('bib_list') || '';
50 my $email_add = $query->param('email_add');
52 my $dbh = C4::Context->dbh;
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'),
59 my $patron = Koha::Patrons->find( $borrowernumber );
60 my $user_email = $patron->first_valid_email_address
61 || C4::Context->preference('KohaAdminEmailAddress');
63 my $email_replyto = $patron->firstname . " " . $patron->surname . " <$user_email>";
64 my $comment = $query->param('comment');
66 # Since we are already logged in, no need to check credentials again
67 # when loading a second template.
68 my $template2 = C4::Templates::gettemplate(
69 'opac-sendbasket.tt', 'opac', $query,
72 my @bibs = split( /\//, $bib_list );
75 my $marcflavour = C4::Context->preference('marcflavour');
76 foreach my $biblionumber (@bibs) {
77 $template2->param( biblionumber => $biblionumber );
79 my $biblio = Koha::Biblios->find( $biblionumber ) or next;
80 my $dat = $biblio->unblessed;
81 my $record = $biblio->metadata->record(
88 my $marcauthorsarray = $biblio->get_marc_contributors;
89 my $marcsubjctsarray = GetMarcSubjects( $record, $marcflavour );
91 my @items = GetItemsInfo( $biblionumber );
94 if($dat->{'author'} || @$marcauthorsarray) {
99 $dat->{MARCSUBJCTS} = $marcsubjctsarray;
100 $dat->{MARCAUTHORS} = $marcauthorsarray;
101 $dat->{HASAUTHORS} = $hasauthors;
102 $dat->{'biblionumber'} = $biblionumber;
103 $dat->{ITEM_RESULTS} = \@items;
105 $iso2709 .= $record->as_usmarc();
107 push( @results, $dat );
110 my $resultsarray = \@results;
113 BIBLIO_RESULTS => $resultsarray,
115 firstname => $patron->firstname,
116 surname => $patron->surname,
119 # Getting template result
120 my $template_res = $template2->output();
123 # Analysing information and getting mail properties
125 if ( $template_res =~ /\<SUBJECT\>(?<subject>.*)\<END_SUBJECT\>/s ) {
126 $subject = $+{subject};
127 $subject =~ s|\n?(.*)\n?|$1|;
130 $subject = "no subject";
133 my $email_header = "";
134 if ( $template_res =~ /<HEADER>(.*)<END_HEADER>/s ) {
136 $email_header =~ s|\n?(.*)\n?|$1|;
139 if ( $template_res =~ /<MESSAGE>(.*)<END_MESSAGE>/s ) {
141 $body =~ s|\n?(.*)\n?|$1|;
144 my $THE_body = <<END_OF_BODY;
149 if ( !defined $iso2709 ) {
150 carp "Error sending mail: empty basket";
151 $template->param( error => 1 );
155 # if you want to use the KohaAdmin address as from, that is the default no need to set it
156 my $email = Koha::Email->create({
158 reply_to => $email_replyto,
161 $email->header( 'X-Abuse-Report' => C4::Context->preference('KohaAdminEmailAddress') );
162 $email->text_body( $THE_body );
164 Encode::encode( "UTF-8", $iso2709 ),
165 content_type => 'application/octet-stream',
166 name => 'basket.iso2709',
167 disposition => 'attachment',
169 my $library = $patron->library;
170 $email->transport( $library->smtp_server->transport );
172 $template->param( SENT => "1" );
175 carp "Error sending mail: $_";
176 $template->param( error => 1 );
180 $template->param( email_add => $email_add );
181 output_html_with_http_headers $query, $cookie, $template->output, undef, { force_no_caching => 1 };
184 my $new_session_id = $query->cookie('CGISESSID');
186 bib_list => $bib_list,
187 url => "/cgi-bin/koha/opac-sendbasket.pl",
188 suggestion => C4::Context->preference("suggestion"),
189 virtualshelves => C4::Context->preference("virtualshelves"),
190 csrf_token => Koha::Token->new->generate_csrf(
191 { session_id => $new_session_id, } ),
193 output_html_with_http_headers $query, $cookie, $template->output, undef, { force_no_caching => 1 };