Bug 14570: Make it possible to add multiple guarantors to a record
[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 Mail::Sendmail;
26 use MIME::QuotedPrint;
27 use MIME::Base64;
28
29 use C4::Biblio;
30 use C4::Items;
31 use C4::Auth;
32 use C4::Output;
33 use C4::Members;
34 use C4::Templates ();
35 use Koha::Email;
36 use Koha::Patrons;
37 use Koha::Token;
38
39 my $query = new CGI;
40
41 my ( $template, $borrowernumber, $cookie ) = get_template_and_user (
42     {
43         template_name   => "opac-sendbasketform.tt",
44         query           => $query,
45         type            => "opac",
46         authnotrequired => 0,
47     }
48 );
49
50 my $bib_list     = $query->param('bib_list') || '';
51 my $email_add    = $query->param('email_add');
52
53 my $dbh          = C4::Context->dbh;
54
55 if ( $email_add ) {
56     die "Wrong CSRF token" unless Koha::Token->new->check_csrf({
57         session_id => scalar $query->cookie('CGISESSID'),
58         token  => scalar $query->param('csrf_token'),
59     });
60     my $email = Koha::Email->new();
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    # if you want to use the KohaAdmin address as from, that is the default no need to set it
70     my %mail = $email->create_message_headers({
71         to => $email_add,
72         replyto => $email_replyto,
73     });
74     $mail{'X-Abuse-Report'} = C4::Context->preference('KohaAdminEmailAddress');
75
76     # Since we are already logged in, no need to check credentials again
77     # when loading a second template.
78     my $template2 = C4::Templates::gettemplate(
79         'opac-sendbasket.tt', 'opac', $query,
80     );
81
82     my @bibs = split( /\//, $bib_list );
83     my @results;
84     my $iso2709;
85     my $marcflavour = C4::Context->preference('marcflavour');
86     foreach my $biblionumber (@bibs) {
87         $template2->param( biblionumber => $biblionumber );
88
89         my $dat              = GetBiblioData($biblionumber);
90         next unless $dat;
91         my $record           = GetMarcBiblio({
92             biblionumber => $biblionumber,
93             embed_items  => 1,
94             opac         => 1,
95             borcat       => $borcat });
96         my $marcauthorsarray = GetMarcAuthors( $record, $marcflavour );
97         my $marcsubjctsarray = GetMarcSubjects( $record, $marcflavour );
98
99         my @items = GetItemsInfo( $biblionumber );
100
101         my $hasauthors = 0;
102         if($dat->{'author'} || @$marcauthorsarray) {
103           $hasauthors = 1;
104         }
105         
106
107         $dat->{MARCSUBJCTS}    = $marcsubjctsarray;
108         $dat->{MARCAUTHORS}    = $marcauthorsarray;
109         $dat->{HASAUTHORS}     = $hasauthors;
110         $dat->{'biblionumber'} = $biblionumber;
111         $dat->{ITEM_RESULTS}   = \@items;
112
113         $iso2709 .= $record->as_usmarc();
114
115         push( @results, $dat );
116     }
117
118     my $resultsarray = \@results;
119     
120     $template2->param(
121         BIBLIO_RESULTS => $resultsarray,
122         comment        => $comment,
123         firstname      => $patron->firstname,
124         surname        => $patron->surname,
125     );
126
127     # Getting template result
128     my $template_res = $template2->output();
129     my $body;
130
131     # Analysing information and getting mail properties
132
133     if ( $template_res =~ /<SUBJECT>(.*)<END_SUBJECT>/s ) {
134         $mail{subject} = $1;
135         $mail{subject} =~ s|\n?(.*)\n?|$1|;
136         $mail{subject} = encode('MIME-Header',$mail{subject});
137     }
138     else { $mail{'subject'} = "no subject"; }
139
140     my $email_header = "";
141     if ( $template_res =~ /<HEADER>(.*)<END_HEADER>/s ) {
142         $email_header = $1;
143         $email_header =~ s|\n?(.*)\n?|$1|;
144         $email_header = encode_qp(Encode::encode("UTF-8", $email_header));
145     }
146
147     my $email_file = "basket.txt";
148     if ( $template_res =~ /<FILENAME>(.*)<END_FILENAME>/s ) {
149         $email_file = $1;
150         $email_file =~ s|\n?(.*)\n?|$1|;
151     }
152
153     if ( $template_res =~ /<MESSAGE>(.*)<END_MESSAGE>/s ) {
154         $body = $1;
155         $body =~ s|\n?(.*)\n?|$1|;
156         $body = encode_qp(Encode::encode("UTF-8", $body));
157     }
158
159     $mail{body} = $body;
160
161     my $boundary = "====" . time() . "====";
162
163     $mail{'content-type'} = "multipart/mixed; boundary=\"$boundary\"";
164     my $isofile = encode_base64(encode("UTF-8", $iso2709));
165     $boundary = '--' . $boundary;
166     $mail{body} = <<END_OF_BODY;
167 $boundary
168 MIME-Version: 1.0
169 Content-Type: text/plain; charset="UTF-8"
170 Content-Transfer-Encoding: quoted-printable
171
172 $email_header
173 $body
174 $boundary
175 Content-Type: application/octet-stream; name="basket.iso2709"
176 Content-Transfer-Encoding: base64
177 Content-Disposition: attachment; filename="basket.iso2709"
178
179 $isofile
180 $boundary--
181 END_OF_BODY
182
183     # Sending mail (if not empty basket)
184     if ( defined($iso2709) && sendmail %mail ) {
185     # do something if it works....
186         $template->param( SENT      => "1" );
187     }
188     else {
189         # do something if it doesn't work....
190     carp "Error sending mail: empty basket" if !defined($iso2709);
191         carp "Error sending mail: $Mail::Sendmail::error" if $Mail::Sendmail::error;
192         $template->param( error => 1 );
193     }
194     $template->param( email_add => $email_add );
195     output_html_with_http_headers $query, $cookie, $template->output, undef, { force_no_caching => 1 };
196 }
197 else {
198     my $new_session_id = $cookie->value;
199     $template->param(
200         bib_list       => $bib_list,
201         url            => "/cgi-bin/koha/opac-sendbasket.pl",
202         suggestion     => C4::Context->preference("suggestion"),
203         virtualshelves => C4::Context->preference("virtualshelves"),
204         csrf_token => Koha::Token->new->generate_csrf(
205             { session_id => $new_session_id, } ),
206     );
207     output_html_with_http_headers $query, $cookie, $template->output, undef, { force_no_caching => 1 };
208 }