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 );
28 use C4::Auth qw( get_template_and_user );
29 use C4::Output qw( output_and_exit output_html_with_http_headers );
37 my ( $template, $borrowernumber, $cookie ) = get_template_and_user (
39 template_name => "basket/sendbasketform.tt",
42 flagsrequired => { catalogue => 1 },
46 my $bib_list = $query->param('bib_list') || '';
47 my $email_add = $query->param('email_add');
49 my $dbh = C4::Context->dbh;
52 output_and_exit( $query, $cookie, $template, 'wrong_csrf_token' )
53 unless Koha::Token->new->check_csrf({
54 session_id => scalar $query->cookie('CGISESSID'),
55 token => scalar $query->param('csrf_token'),
57 my $comment = $query->param('comment');
59 # Since we are already logged in, no need to check credentials again
60 # when loading a second template.
61 my $template2 = C4::Templates::gettemplate(
62 'basket/sendbasket.tt', 'intranet', $query,
65 my @bibs = split( /\//, $bib_list );
68 my $marcflavour = C4::Context->preference('marcflavour');
69 foreach my $biblionumber (@bibs) {
70 $template2->param( biblionumber => $biblionumber );
72 my $biblio = Koha::Biblios->find( $biblionumber ) or next;
73 my $dat = $biblio->unblessed;
74 my $record = $biblio->metadata->record({ embed_items => 1 });
75 my $marcauthorsarray = $biblio->get_marc_contributors;
76 my $marcsubjctsarray = GetMarcSubjects( $record, $marcflavour );
79 if($dat->{'author'} || @$marcauthorsarray) {
84 $dat->{MARCSUBJCTS} = $marcsubjctsarray;
85 $dat->{MARCAUTHORS} = $marcauthorsarray;
86 $dat->{HASAUTHORS} = $hasauthors;
87 $dat->{'biblionumber'} = $biblionumber;
88 $dat->{ITEM_RESULTS} = $biblio->items->search_ordered;
90 $iso2709 .= $record->as_usmarc();
92 push( @results, $dat );
95 my $resultsarray = \@results;
97 BIBLIO_RESULTS => $resultsarray,
101 # Getting template result
102 my $template_res = $template2->output();
106 # Analysing information and getting mail properties
107 if ( $template_res =~ /<SUBJECT>(?<subject>.*)<END_SUBJECT>/s ) {
108 $subject = $+{subject};
109 $subject =~ s|\n?(.*)\n?|$1|;
112 $subject = "no subject";
115 my $email_header = "";
116 if ( $template_res =~ /<HEADER>(.*)<END_HEADER>/s ) {
118 $email_header =~ s|\n?(.*)\n?|$1|;
121 if ( $template_res =~ /<MESSAGE>(.*)<END_MESSAGE>/s ) {
123 $body =~ s|\n?(.*)\n?|$1|;
126 my $THE_body = <<END_OF_BODY;
133 my $email = Koha::Email->create(
140 $email->text_body( $THE_body );
142 Encode::encode( "UTF-8", $iso2709 ),
143 content_type => 'application/octet-stream',
144 name => 'basket.iso2709',
145 disposition => 'attachment',
148 my $library = Koha::Patrons->find( $borrowernumber )->library;
149 $email->send_or_die({ transport => $library->smtp_server->transport });
150 $template->param( SENT => "1" );
153 carp "Error sending mail: $_";
154 $template->param( error => 1 );
157 $template->param( email_add => $email_add );
158 output_html_with_http_headers $query, $cookie, $template->output;
162 bib_list => $bib_list,
163 url => "/cgi-bin/koha/basket/sendbasket.pl",
164 suggestion => C4::Context->preference("suggestion"),
165 virtualshelves => C4::Context->preference("virtualshelves"),
166 csrf_token => Koha::Token->new->generate_csrf({ session_id => scalar $query->cookie('CGISESSID'), }),
168 output_html_with_http_headers $query, $cookie, $template->output;