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