Bug 11592: Applying filtering to opac interface.
[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 strict;
21 use warnings;
22
23 use CGI qw ( -utf8 );
24 use Encode qw(encode);
25 use Carp;
26
27 use Mail::Sendmail;
28 use MIME::QuotedPrint;
29 use MIME::Base64;
30 use C4::Biblio;
31 use C4::Items;
32 use C4::Auth;
33 use C4::Output;
34 use C4::Biblio;
35 use C4::Members;
36 use Koha::Email;
37
38 my $query = new CGI;
39
40 my ( $template, $borrowernumber, $cookie ) = get_template_and_user (
41     {
42         template_name   => "opac-sendbasketform.tt",
43         query           => $query,
44         type            => "opac",
45         authnotrequired => 0,
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     my $email = Koha::Email->new();
56     my $user = GetMember(borrowernumber => $borrowernumber);
57     my $user_email = GetFirstValidEmailAddress($borrowernumber)
58     || C4::Context->preference('KohaAdminEmailAddress');
59
60     my $email_replyto = "$user->{firstname} $user->{surname} <$user_email>";
61     my $comment    = $query->param('comment');
62
63    # if you want to use the KohaAdmin address as from, that is the default no need to set it
64     my %mail = $email->create_message_headers({
65         to => $email_add,
66         replyto => $email_replyto,
67     });
68     $mail{'X-Abuse-Report'} = C4::Context->preference('KohaAdminEmailAddress');
69
70     my ( $template2, $borrowernumber, $cookie ) = get_template_and_user(
71         {
72             template_name   => "opac-sendbasket.tt",
73             query           => $query,
74             type            => "opac",
75             authnotrequired => 0,
76         }
77     );
78
79     my @bibs = split( /\//, $bib_list );
80     my @results;
81     my $iso2709;
82     my $marcflavour = C4::Context->preference('marcflavour');
83     foreach my $biblionumber (@bibs) {
84         $template2->param( biblionumber => $biblionumber );
85
86         my $dat              = GetBiblioData($biblionumber);
87         next unless $dat;
88         my $record           = GetMarcBiblio($biblionumber, 1);
89         my $marcauthorsarray = GetMarcAuthors( $record, $marcflavour );
90         my $marcsubjctsarray = GetMarcSubjects( $record, $marcflavour );
91
92         my @items = GetItemsInfo( $biblionumber );
93
94         my $hasauthors = 0;
95         if($dat->{'author'} || @$marcauthorsarray) {
96           $hasauthors = 1;
97         }
98         
99
100         $dat->{MARCSUBJCTS}    = $marcsubjctsarray;
101         $dat->{MARCAUTHORS}    = $marcauthorsarray;
102         $dat->{HASAUTHORS}     = $hasauthors;
103         $dat->{'biblionumber'} = $biblionumber;
104         $dat->{ITEM_RESULTS}   = \@items;
105
106         $iso2709 .= $record->as_usmarc();
107
108         push( @results, $dat );
109     }
110
111     my $resultsarray = \@results;
112     
113     $template2->param(
114         BIBLIO_RESULTS => $resultsarray,
115         comment        => $comment,
116         firstname      => $user->{firstname},
117         surname        => $user->{surname},
118     );
119
120     # Getting template result
121     my $template_res = $template2->output();
122     my $body;
123
124     # Analysing information and getting mail properties
125
126     if ( $template_res =~ /<SUBJECT>(.*)<END_SUBJECT>/s ) {
127         $mail{subject} = $1;
128         $mail{subject} =~ s|\n?(.*)\n?|$1|;
129         $mail{subject} = Encode::encode("UTF-8", $mail{subject});
130     }
131     else { $mail{'subject'} = "no subject"; }
132
133     my $email_header = "";
134     if ( $template_res =~ /<HEADER>(.*)<END_HEADER>/s ) {
135         $email_header = $1;
136         $email_header =~ s|\n?(.*)\n?|$1|;
137         $email_header = encode_qp(Encode::encode("UTF-8", $email_header));
138     }
139
140     my $email_file = "basket.txt";
141     if ( $template_res =~ /<FILENAME>(.*)<END_FILENAME>/s ) {
142         $email_file = $1;
143         $email_file =~ s|\n?(.*)\n?|$1|;
144     }
145
146     if ( $template_res =~ /<MESSAGE>(.*)<END_MESSAGE>/s ) {
147         $body = $1;
148         $body =~ s|\n?(.*)\n?|$1|;
149         $body = encode_qp(Encode::encode("UTF-8", $body));
150     }
151
152     $mail{body} = $body;
153
154     my $boundary = "====" . time() . "====";
155
156     $mail{'content-type'} = "multipart/mixed; boundary=\"$boundary\"";
157     my $isofile = encode_base64(encode("UTF-8", $iso2709));
158     $boundary = '--' . $boundary;
159     $mail{body} = <<END_OF_BODY;
160 $boundary
161 MIME-Version: 1.0
162 Content-Type: text/plain; charset="UTF-8"
163 Content-Transfer-Encoding: quoted-printable
164
165 $email_header
166 $body
167 $boundary
168 Content-Type: application/octet-stream; name="basket.iso2709"
169 Content-Transfer-Encoding: base64
170 Content-Disposition: attachment; filename="basket.iso2709"
171
172 $isofile
173 $boundary--
174 END_OF_BODY
175
176     # Sending mail (if not empty basket)
177     if ( defined($iso2709) && sendmail %mail ) {
178     # do something if it works....
179         $template->param( SENT      => "1" );
180     }
181     else {
182         # do something if it doesnt work....
183     carp "Error sending mail: empty basket" if !defined($iso2709);
184         carp "Error sending mail: $Mail::Sendmail::error" if $Mail::Sendmail::error;
185         $template->param( error => 1 );
186     }
187     $template->param( email_add => $email_add );
188     output_html_with_http_headers $query, $cookie, $template->output;
189 }
190 else {
191     $template->param( bib_list => $bib_list );
192     $template->param(
193         url            => "/cgi-bin/koha/opac-sendbasket.pl",
194         suggestion     => C4::Context->preference("suggestion"),
195         virtualshelves => C4::Context->preference("virtualshelves"),
196     );
197     output_html_with_http_headers $query, $cookie, $template->output;
198 }