Bug 4289: 'OpacPublic' feature
[koha.git] / opac / opac-sendbasket.pl
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 2 of the License, or (at your option) any later
8 # version.
9 #
10 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License along with
15 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
16 # Suite 330, Boston, MA  02111-1307 USA
17
18 use strict;
19 use warnings;
20
21 use CGI;
22 use Encode qw(encode);
23
24 use Mail::Sendmail;
25 use MIME::QuotedPrint;
26 use MIME::Base64;
27 use C4::Biblio;
28 use C4::Items;
29 use C4::Auth;
30 use C4::Output;
31 use C4::Biblio;
32 use C4::Members;
33
34 my $query = new CGI;
35
36 my ( $template, $borrowernumber, $cookie ) = get_template_and_user (
37     {
38         template_name   => "opac-sendbasketform.tmpl",
39         query           => $query,
40         type            => "opac",
41         authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ),
42         flagsrequired   => { borrow => 1 },
43     }
44 );
45
46 my $bib_list     = $query->param('bib_list');
47 my $email_add    = $query->param('email_add');
48 my $email_sender = $query->param('email_sender');
49
50 my $dbh          = C4::Context->dbh;
51
52 if ( $email_add ) {
53     my $email_from = C4::Context->preference('KohaAdminEmailAddress');
54     my $comment    = $query->param('comment');
55     my %mail = (
56         To   => $email_add,
57         From => $email_from
58     );
59
60     my ( $template2, $borrowernumber, $cookie ) = get_template_and_user(
61         {
62             template_name   => "opac-sendbasket.tmpl",
63             query           => $query,
64             type            => "opac",
65             authnotrequired => 1,
66             flagsrequired   => { borrow => 1 },
67         }
68     );
69
70     my @bibs = split( /\//, $bib_list );
71     my @results;
72     my $iso2709;
73     my $marcflavour = C4::Context->preference('marcflavour');
74     foreach my $biblionumber (@bibs) {
75         $template2->param( biblionumber => $biblionumber );
76
77         my $dat              = GetBiblioData($biblionumber);
78         my $record           = GetMarcBiblio($biblionumber);
79         my $marcnotesarray   = GetMarcNotes( $record, $marcflavour );
80         my $marcauthorsarray = GetMarcAuthors( $record, $marcflavour );
81         my $marcsubjctsarray = GetMarcSubjects( $record, $marcflavour );
82
83         my @items = &GetItemsInfo( $biblionumber, 'opac' );
84
85         my $hasauthors = 0;
86         if($dat->{'author'} || @$marcauthorsarray) {
87           $hasauthors = 1;
88         }
89         
90
91         $dat->{MARCNOTES}      = $marcnotesarray;
92         $dat->{MARCSUBJCTS}    = $marcsubjctsarray;
93         $dat->{MARCAUTHORS}    = $marcauthorsarray;
94         $dat->{HASAUTHORS}     = $hasauthors;
95         $dat->{'biblionumber'} = $biblionumber;
96         $dat->{ITEM_RESULTS}   = \@items;
97
98         $iso2709 .= $record->as_usmarc();
99
100         push( @results, $dat );
101     }
102
103     my $resultsarray = \@results;
104     
105     my $user = GetMember(borrowernumber => $borrowernumber); 
106     
107     $template2->param(
108         BIBLIO_RESULTS => $resultsarray,
109         email_sender   => $email_sender,
110         comment        => $comment,
111         firstname      => $user->{firstname},
112         surname        => $user->{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     if ( $template_res =~ /<SUBJECT>\n(.*)\n<END_SUBJECT>/s ) {
121         $mail{'subject'} = $1;
122     }
123     else { $mail{'subject'} = "no subject"; }
124
125     my $email_header = "";
126     if ( $template_res =~ /<HEADER>\n(.*)\n<END_HEADER>/s ) {
127         $email_header = $1;
128     }
129
130     my $email_file = "basket.txt";
131     if ( $template_res =~ /<FILENAME>\n(.*)\n<END_FILENAME>/s ) {
132         $email_file = $1;
133     }
134
135     if ( $template_res =~ /<MESSAGE>\n(.*)\n<END_MESSAGE>/s ) { $body = encode_qp($1); }
136
137     my $boundary = "====" . time() . "====";
138
139     #     $mail{'content-type'} = "multipart/mixed; boundary=\"$boundary\"";
140     #
141     #     $email_header = encode_qp($email_header);
142     #
143     #     $boundary = "--".$boundary;
144     #
145     #     # Writing mail
146     #     $mail{body} =
147     $mail{'content-type'} = "multipart/mixed; boundary=\"$boundary\"";
148     my $isofile = encode_base64(encode("UTF-8", $iso2709));
149     $boundary = '--' . $boundary;
150     $mail{body} = <<END_OF_BODY;
151 $boundary
152 Content-Type: text/plain; charset="utf-8"
153 Content-Transfer-Encoding: quoted-printable
154
155 $email_header
156 $body
157 $boundary
158 Content-Type: application/octet-stream; name="basket.iso2709"
159 Content-Transfer-Encoding: base64
160 Content-Disposition: attachment; filename="basket.iso2709"
161
162 $isofile
163 $boundary--
164 END_OF_BODY
165
166     # Sending mail
167     if ( sendmail %mail ) {
168         # do something if it works....
169         $template->param( SENT      => "1" );
170     }
171     else {
172         # do something if it doesnt work....
173         warn "Error sending mail: $Mail::Sendmail::error \n";
174         $template->param( error => 1 );
175     }
176     $template->param( email_add => $email_add );
177     output_html_with_http_headers $query, $cookie, $template->output;
178 }
179 else {
180     $template->param( bib_list => $bib_list );
181     $template->param(
182         url            => "/cgi-bin/koha/opac-sendbasket.pl",
183         suggestion     => C4::Context->preference("suggestion"),
184         virtualshelves => C4::Context->preference("virtualshelves"),
185     );
186     output_html_with_http_headers $query, $cookie, $template->output;
187 }