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>.
23 use Try::Tiny qw( catch try );
31 use C4::Items qw( GetItemsInfo );
32 use C4::Auth qw( get_template_and_user );
33 use C4::Output qw( output_and_exit output_html_with_http_headers );
40 my ( $template, $borrowernumber, $cookie ) = get_template_and_user (
42 template_name => "basket/sendbasketform.tt",
45 flagsrequired => { catalogue => 1 },
49 my $bib_list = $query->param('bib_list') || '';
50 my $email_add = $query->param('email_add');
52 my $dbh = C4::Context->dbh;
55 output_and_exit( $query, $cookie, $template, 'wrong_csrf_token' )
56 unless Koha::Token->new->check_csrf({
57 session_id => scalar $query->cookie('CGISESSID'),
58 token => scalar $query->param('csrf_token'),
60 my $comment = $query->param('comment');
62 # Since we are already logged in, no need to check credentials again
63 # when loading a second template.
64 my $template2 = C4::Templates::gettemplate(
65 'basket/sendbasket.tt', 'intranet', $query,
68 my @bibs = split( /\//, $bib_list );
71 my $marcflavour = C4::Context->preference('marcflavour');
72 foreach my $biblionumber (@bibs) {
73 $template2->param( biblionumber => $biblionumber );
75 my $dat = GetBiblioData($biblionumber);
77 my $record = GetMarcBiblio({
78 biblionumber => $biblionumber,
80 my $marcauthorsarray = GetMarcAuthors( $record, $marcflavour );
81 my $marcsubjctsarray = GetMarcSubjects( $record, $marcflavour );
83 my @items = GetItemsInfo( $biblionumber );
86 if($dat->{'author'} || @$marcauthorsarray) {
91 $dat->{MARCSUBJCTS} = $marcsubjctsarray;
92 $dat->{MARCAUTHORS} = $marcauthorsarray;
93 $dat->{HASAUTHORS} = $hasauthors;
94 $dat->{'biblionumber'} = $biblionumber;
95 $dat->{ITEM_RESULTS} = \@items;
97 $iso2709 .= $record->as_usmarc();
99 push( @results, $dat );
102 my $resultsarray = \@results;
104 BIBLIO_RESULTS => $resultsarray,
108 # Getting template result
109 my $template_res = $template2->output();
113 # Analysing information and getting mail properties
114 if ( $template_res =~ /<SUBJECT>(?<subject>.*)<END_SUBJECT>/s ) {
115 $subject = $+{subject};
116 $subject =~ s|\n?(.*)\n?|$1|;
119 $subject = "no subject";
122 my $email_header = "";
123 if ( $template_res =~ /<HEADER>(.*)<END_HEADER>/s ) {
125 $email_header =~ s|\n?(.*)\n?|$1|;
128 if ( $template_res =~ /<MESSAGE>(.*)<END_MESSAGE>/s ) {
130 $body =~ s|\n?(.*)\n?|$1|;
133 my $THE_body = <<END_OF_BODY;
140 my $email = Koha::Email->create(
147 $email->text_body( $THE_body );
149 Encode::encode( "UTF-8", $iso2709 ),
150 content_type => 'application/octet-stream',
151 name => 'basket.iso2709',
152 disposition => 'attachment',
155 my $library = Koha::Patrons->find( $borrowernumber )->library;
156 $email->send_or_die({ transport => $library->smtp_server->transport });
157 $template->param( SENT => "1" );
160 carp "Error sending mail: $_";
161 $template->param( error => 1 );
164 $template->param( email_add => $email_add );
165 output_html_with_http_headers $query, $cookie, $template->output;
169 bib_list => $bib_list,
170 url => "/cgi-bin/koha/basket/sendbasket.pl",
171 suggestion => C4::Context->preference("suggestion"),
172 virtualshelves => C4::Context->preference("virtualshelves"),
173 csrf_token => Koha::Token->new->generate_csrf({ session_id => scalar $query->cookie('CGISESSID'), }),
175 output_html_with_http_headers $query, $cookie, $template->output;