Bug 19532: (follow-up) aria-hidden attr on OPAC, and more
[koha.git] / 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     GetMarcBiblio
29     GetMarcSubjects
30 );
31 use C4::Items qw( GetItemsInfo );
32 use C4::Auth qw( get_template_and_user );
33 use C4::Output qw( output_html_with_http_headers );
34 use C4::Templates;
35 use Koha::Email;
36 use Koha::Patrons;
37 use Koha::Token;
38
39 my $query = CGI->new;
40
41 my ( $template, $borrowernumber, $cookie ) = get_template_and_user (
42     {
43         template_name   => "opac-sendbasketform.tt",
44         query           => $query,
45         type            => "opac",
46     }
47 );
48
49 my $bib_list     = $query->param('bib_list') || '';
50 my $email_add    = $query->param('email_add');
51
52 my $dbh          = C4::Context->dbh;
53
54 if ( $email_add ) {
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'),
58     });
59     my $patron = Koha::Patrons->find( $borrowernumber );
60     my $borcat = $patron ? $patron->categorycode : q{};
61     my $user_email = $patron->first_valid_email_address
62     || C4::Context->preference('KohaAdminEmailAddress');
63
64     my $email_replyto = $patron->firstname . " " . $patron->surname . " <$user_email>";
65     my $comment    = $query->param('comment');
66
67     # Since we are already logged in, no need to check credentials again
68     # when loading a second template.
69     my $template2 = C4::Templates::gettemplate(
70         'opac-sendbasket.tt', 'opac', $query,
71     );
72
73     my @bibs = split( /\//, $bib_list );
74     my @results;
75     my $iso2709;
76     my $marcflavour = C4::Context->preference('marcflavour');
77     foreach my $biblionumber (@bibs) {
78         $template2->param( biblionumber => $biblionumber );
79
80         my $biblio           = Koha::Biblios->find( $biblionumber ) or next;
81         my $dat              = $biblio->unblessed;
82         my $record           = GetMarcBiblio({
83             biblionumber => $biblionumber,
84             embed_items  => 1,
85             opac         => 1,
86             borcat       => $borcat });
87         my $marcauthorsarray = $biblio->get_marc_authors;
88         my $marcsubjctsarray = GetMarcSubjects( $record, $marcflavour );
89
90         my @items = GetItemsInfo( $biblionumber );
91
92         my $hasauthors = 0;
93         if($dat->{'author'} || @$marcauthorsarray) {
94           $hasauthors = 1;
95         }
96         
97
98         $dat->{MARCSUBJCTS}    = $marcsubjctsarray;
99         $dat->{MARCAUTHORS}    = $marcauthorsarray;
100         $dat->{HASAUTHORS}     = $hasauthors;
101         $dat->{'biblionumber'} = $biblionumber;
102         $dat->{ITEM_RESULTS}   = \@items;
103
104         $iso2709 .= $record->as_usmarc();
105
106         push( @results, $dat );
107     }
108
109     my $resultsarray = \@results;
110     
111     $template2->param(
112         BIBLIO_RESULTS => $resultsarray,
113         comment        => $comment,
114         firstname      => $patron->firstname,
115         surname        => $patron->surname,
116     );
117
118     # Getting template result
119     my $template_res = $template2->output();
120     my $body;
121
122     # Analysing information and getting mail properties
123     my $subject;
124     if ( $template_res =~ /\<SUBJECT\>(?<subject>.*)\<END_SUBJECT\>/s ) {
125         $subject = $+{subject};
126         $subject =~ s|\n?(.*)\n?|$1|;
127     }
128     else {
129         $subject = "no subject";
130     }
131
132     my $email_header = "";
133     if ( $template_res =~ /<HEADER>(.*)<END_HEADER>/s ) {
134         $email_header = $1;
135         $email_header =~ s|\n?(.*)\n?|$1|;
136     }
137
138     if ( $template_res =~ /<MESSAGE>(.*)<END_MESSAGE>/s ) {
139         $body = $1;
140         $body =~ s|\n?(.*)\n?|$1|;
141     }
142
143     my $THE_body = <<END_OF_BODY;
144 $email_header
145 $body
146 END_OF_BODY
147
148     if ( !defined $iso2709 ) {
149         carp "Error sending mail: empty basket";
150         $template->param( error => 1 );
151     }
152     else {
153         try {
154             # if you want to use the KohaAdmin address as from, that is the default no need to set it
155             my $email = Koha::Email->create({
156                 to       => $email_add,
157                 reply_to => $email_replyto,
158                 subject  => $subject,
159             });
160             $email->header( 'X-Abuse-Report' => C4::Context->preference('KohaAdminEmailAddress') );
161             $email->text_body( $THE_body );
162             $email->attach(
163                 Encode::encode( "UTF-8", $iso2709 ),
164                 content_type => 'application/octet-stream',
165                 name         => 'basket.iso2709',
166                 disposition  => 'attachment',
167             );
168             my $library = $patron->library;
169             $email->transport( $library->smtp_server->transport );
170             $email->send_or_die;
171             $template->param( SENT => "1" );
172         }
173         catch {
174             carp "Error sending mail: $_";
175             $template->param( error => 1 );
176         };
177     }
178
179     $template->param( email_add => $email_add );
180     output_html_with_http_headers $query, $cookie, $template->output, undef, { force_no_caching => 1 };
181 }
182 else {
183     my $new_session_id = $cookie->value;
184     $template->param(
185         bib_list       => $bib_list,
186         url            => "/cgi-bin/koha/opac-sendbasket.pl",
187         suggestion     => C4::Context->preference("suggestion"),
188         virtualshelves => C4::Context->preference("virtualshelves"),
189         csrf_token => Koha::Token->new->generate_csrf(
190             { session_id => $new_session_id, } ),
191     );
192     output_html_with_http_headers $query, $cookie, $template->output, undef, { force_no_caching => 1 };
193 }