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