8062 Followup for HTML::FormatText
[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 under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20 use strict;
21 use warnings;
22
23 use CGI;
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
37 my $query = new CGI;
38
39 my ( $template, $borrowernumber, $cookie ) = get_template_and_user (
40     {
41         template_name   => "opac-sendbasketform.tmpl",
42         query           => $query,
43         type            => "opac",
44         authnotrequired => 0,
45         flagsrequired   => { borrow => 1 },
46     }
47 );
48
49 my $bib_list     = $query->param('bib_list');
50 my $email_add    = $query->param('email_add');
51 my $email_sender = $query->param('email_sender');
52
53 my $dbh          = C4::Context->dbh;
54
55 if ( $email_add ) {
56     my $email_from = C4::Context->preference('KohaAdminEmailAddress');
57     my $comment    = $query->param('comment');
58     my %mail = (
59         To   => $email_add,
60         From => $email_from
61     );
62
63     my ( $template2, $borrowernumber, $cookie ) = get_template_and_user(
64         {
65             template_name   => "opac-sendbasket.tmpl",
66             query           => $query,
67             type            => "opac",
68             authnotrequired => 1,
69             flagsrequired   => { borrow => 1 },
70         }
71     );
72
73     my @bibs = split( /\//, $bib_list );
74     my @results;
75     my $iso2709;
76     my $marcflavour = C4::Context->preference('marcflavour');
77     foreach my $biblionumber (@bibs) {
78         $template2->param( biblionumber => $biblionumber );
79
80         my $dat              = GetBiblioData($biblionumber);
81         my $record           = GetMarcBiblio($biblionumber);
82         my $marcnotesarray   = GetMarcNotes( $record, $marcflavour );
83         my $marcauthorsarray = GetMarcAuthors( $record, $marcflavour );
84         my $marcsubjctsarray = GetMarcSubjects( $record, $marcflavour );
85
86         my @items = GetItemsInfo( $biblionumber );
87
88         my $hasauthors = 0;
89         if($dat->{'author'} || @$marcauthorsarray) {
90           $hasauthors = 1;
91         }
92         
93
94         $dat->{MARCNOTES}      = $marcnotesarray;
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     my $user = GetMember(borrowernumber => $borrowernumber); 
109     
110     $template2->param(
111         BIBLIO_RESULTS => $resultsarray,
112         email_sender   => $email_sender,
113         comment        => $comment,
114         firstname      => $user->{firstname},
115         surname        => $user->{surname},
116     );
117
118     # Getting template result
119     my $template_res = $template2->output();
120     my $body;
121
122     # Analysing information and getting mail properties
123     if ( $template_res =~ /<SUBJECT>\n(.*)\n?<END_SUBJECT>/s ) {
124         $mail{'subject'} = $1;
125     }
126     else { $mail{'subject'} = "no subject"; }
127
128     my $email_header = "";
129     if ( $template_res =~ /<HEADER>\n(.*)\n?<END_HEADER>/s ) {
130         $email_header = $1;
131     }
132
133     my $email_file = "basket.txt";
134     if ( $template_res =~ /<FILENAME>\n(.*)\n?<END_FILENAME>/s ) {
135         $email_file = $1;
136     }
137
138     if ( $template_res =~ /<MESSAGE>\n(.*)\n?<END_MESSAGE>/s ) {
139         $body = $1;
140     }
141
142     my $boundary = "====" . time() . "====";
143
144     $mail{'content-type'} = "multipart/mixed; boundary=\"$boundary\"";
145     my $isofile = encode_base64(encode("UTF-8", $iso2709));
146     $boundary = '--' . $boundary;
147     $mail{body} = <<END_OF_BODY;
148 $boundary
149 MIME-Version: 1.0
150 Content-Type: text/plain; charset="UTF-8"
151 Content-Transfer-Encoding: quoted-printable
152
153 $email_header
154 $body
155 $boundary
156 Content-Type: application/octet-stream; name="basket.iso2709"
157 Content-Transfer-Encoding: base64
158 Content-Disposition: attachment; filename="basket.iso2709"
159
160 $isofile
161 $boundary--
162 END_OF_BODY
163
164     # Sending mail
165     if ( sendmail %mail ) {
166         # do something if it works....
167         $template->param( SENT      => "1" );
168     }
169     else {
170         # do something if it doesnt work....
171         carp "Error sending mail: $Mail::Sendmail::error \n";
172         $template->param( error => 1 );
173     }
174     $template->param( email_add => $email_add );
175     output_html_with_http_headers $query, $cookie, $template->output;
176 }
177 else {
178     $template->param( bib_list => $bib_list );
179     $template->param(
180         url            => "/cgi-bin/koha/opac-sendbasket.pl",
181         suggestion     => C4::Context->preference("suggestion"),
182         virtualshelves => C4::Context->preference("virtualshelves"),
183     );
184     output_html_with_http_headers $query, $cookie, $template->output;
185 }