Bug 27266: (QA follow-up) Rename to get_marc_authors
[srvgit] / opac / opac-sendbasket.pl
1 #!/usr/bin/perl
2
3 # Copyright Doxulting 2004
4 #
5 # This file is part of Koha.
6 #
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.
11 #
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.
16 #
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>.
19
20 use Modern::Perl;
21
22 use CGI qw ( -utf8 );
23 use Encode;
24 use Carp qw( carp );
25 use Try::Tiny qw( catch try );
26
27 use C4::Biblio qw(
28     GetBiblioData
29     GetMarcAuthors
30     GetMarcBiblio
31     GetMarcSubjects
32 );
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 );
36 use C4::Templates;
37 use Koha::Email;
38 use Koha::Patrons;
39 use Koha::Token;
40
41 my $query = CGI->new;
42
43 my ( $template, $borrowernumber, $cookie ) = get_template_and_user (
44     {
45         template_name   => "opac-sendbasketform.tt",
46         query           => $query,
47         type            => "opac",
48     }
49 );
50
51 my $bib_list     = $query->param('bib_list') || '';
52 my $email_add    = $query->param('email_add');
53
54 my $dbh          = C4::Context->dbh;
55
56 if ( $email_add ) {
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'),
60     });
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');
65
66     my $email_replyto = $patron->firstname . " " . $patron->surname . " <$user_email>";
67     my $comment    = $query->param('comment');
68
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,
73     );
74
75     my @bibs = split( /\//, $bib_list );
76     my @results;
77     my $iso2709;
78     my $marcflavour = C4::Context->preference('marcflavour');
79     foreach my $biblionumber (@bibs) {
80         $template2->param( biblionumber => $biblionumber );
81
82         my $dat              = GetBiblioData($biblionumber);
83         next unless $dat;
84         my $biblio           = Koha::Biblios->find( $biblionumber );
85         my $record           = GetMarcBiblio({
86             biblionumber => $biblionumber,
87             embed_items  => 1,
88             opac         => 1,
89             borcat       => $borcat });
90         my $marcauthorsarray = $biblio->get_marc_authors;
91         my $marcsubjctsarray = GetMarcSubjects( $record, $marcflavour );
92
93         my @items = GetItemsInfo( $biblionumber );
94
95         my $hasauthors = 0;
96         if($dat->{'author'} || @$marcauthorsarray) {
97           $hasauthors = 1;
98         }
99         
100
101         $dat->{MARCSUBJCTS}    = $marcsubjctsarray;
102         $dat->{MARCAUTHORS}    = $marcauthorsarray;
103         $dat->{HASAUTHORS}     = $hasauthors;
104         $dat->{'biblionumber'} = $biblionumber;
105         $dat->{ITEM_RESULTS}   = \@items;
106
107         $iso2709 .= $record->as_usmarc();
108
109         push( @results, $dat );
110     }
111
112     my $resultsarray = \@results;
113     
114     $template2->param(
115         BIBLIO_RESULTS => $resultsarray,
116         comment        => $comment,
117         firstname      => $patron->firstname,
118         surname        => $patron->surname,
119     );
120
121     # Getting template result
122     my $template_res = $template2->output();
123     my $body;
124
125     # Analysing information and getting mail properties
126     my $subject;
127     if ( $template_res =~ /\<SUBJECT\>(?<subject>.*)\<END_SUBJECT\>/s ) {
128         $subject = $+{subject};
129         $subject =~ s|\n?(.*)\n?|$1|;
130     }
131     else {
132         $subject = "no subject";
133     }
134
135     my $email_header = "";
136     if ( $template_res =~ /<HEADER>(.*)<END_HEADER>/s ) {
137         $email_header = $1;
138         $email_header =~ s|\n?(.*)\n?|$1|;
139     }
140
141     if ( $template_res =~ /<MESSAGE>(.*)<END_MESSAGE>/s ) {
142         $body = $1;
143         $body =~ s|\n?(.*)\n?|$1|;
144     }
145
146     my $THE_body = <<END_OF_BODY;
147 $email_header
148 $body
149 END_OF_BODY
150
151     if ( !defined $iso2709 ) {
152         carp "Error sending mail: empty basket";
153         $template->param( error => 1 );
154     }
155     else {
156         try {
157             # if you want to use the KohaAdmin address as from, that is the default no need to set it
158             my $email = Koha::Email->create({
159                 to       => $email_add,
160                 reply_to => $email_replyto,
161                 subject  => $subject,
162             });
163             $email->header( 'X-Abuse-Report' => C4::Context->preference('KohaAdminEmailAddress') );
164             $email->text_body( $THE_body );
165             $email->attach(
166                 Encode::encode( "UTF-8", $iso2709 ),
167                 content_type => 'application/octet-stream',
168                 name         => 'basket.iso2709',
169                 disposition  => 'attachment',
170             );
171             my $library = $patron->library;
172             $email->transport( $library->smtp_server->transport );
173             $email->send_or_die;
174             $template->param( SENT => "1" );
175         }
176         catch {
177             carp "Error sending mail: $_";
178             $template->param( error => 1 );
179         };
180     }
181
182     $template->param( email_add => $email_add );
183     output_html_with_http_headers $query, $cookie, $template->output, undef, { force_no_caching => 1 };
184 }
185 else {
186     my $new_session_id = $cookie->value;
187     $template->param(
188         bib_list       => $bib_list,
189         url            => "/cgi-bin/koha/opac-sendbasket.pl",
190         suggestion     => C4::Context->preference("suggestion"),
191         virtualshelves => C4::Context->preference("virtualshelves"),
192         csrf_token => Koha::Token->new->generate_csrf(
193             { session_id => $new_session_id, } ),
194     );
195     output_html_with_http_headers $query, $cookie, $template->output, undef, { force_no_caching => 1 };
196 }