(bug #4491) fix weird code in search scripts
[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
20 use CGI;
21 use Encode qw(encode);
22
23 use Mail::Sendmail;
24 use MIME::QuotedPrint;
25 use MIME::Base64;
26 use C4::Biblio;
27 use C4::Items;
28 use C4::Auth;
29 use C4::Output;
30 use C4::Biblio;
31 use C4::Members;
32
33 my $query = new CGI;
34
35 my ( $template, $borrowernumber, $cookie ) = get_template_and_user (
36     {
37         template_name   => "opac-sendbasketform.tmpl",
38         query           => $query,
39         type            => "opac",
40         authnotrequired => 0,
41         flagsrequired   => { borrow => 1 },
42     }
43 );
44
45 my $bib_list     = $query->param('bib_list');
46 my $email_add    = $query->param('email_add');
47 my $email_sender = $query->param('email_sender');
48
49 my $dbh          = C4::Context->dbh;
50
51 if ( $email_add ) {
52     my $email_from = C4::Context->preference('KohaAdminEmailAddress');
53     my $comment    = $query->param('comment');
54     my %mail = (
55         To   => $email_add,
56         From => $email_from
57     );
58
59     my ( $template2, $borrowernumber, $cookie ) = get_template_and_user(
60         {
61             template_name   => "opac-sendbasket.tmpl",
62             query           => $query,
63             type            => "opac",
64             authnotrequired => 1,
65             flagsrequired   => { borrow => 1 },
66         }
67     );
68
69     my @bibs = split( /\//, $bib_list );
70     my @results;
71     my $iso2709;
72     my $marcflavour = C4::Context->preference('marcflavour');
73     foreach my $biblionumber (@bibs) {
74         $template2->param( biblionumber => $biblionumber );
75
76         my $dat              = GetBiblioData($biblionumber);
77         my $record           = GetMarcBiblio($biblionumber);
78         my $marcnotesarray   = GetMarcNotes( $record, $marcflavour );
79         my $marcauthorsarray = GetMarcAuthors( $record, $marcflavour );
80         my $marcsubjctsarray = GetMarcSubjects( $record, $marcflavour );
81
82         my @items = &GetItemsInfo( $biblionumber, 'opac' );
83
84         my $hasauthors = 0;
85         if($dat->{'author'} || @$marcauthorsarray) {
86           $hasauthors = 1;
87         }
88         
89
90         $dat->{MARCNOTES}      = $marcnotesarray;
91         $dat->{MARCSUBJCTS}    = $marcsubjctsarray;
92         $dat->{MARCAUTHORS}    = $marcauthorsarray;
93         $dat->{HASAUTHORS}     = $hasauthors;
94         $dat->{'biblionumber'} = $biblionumber;
95         $dat->{ITEM_RESULTS}   = \@items;
96
97         $iso2709 .= $record->as_usmarc();
98
99         push( @results, $dat );
100     }
101
102     my $resultsarray = \@results;
103     
104     my $user = GetMember($borrowernumber); 
105     
106     $template2->param(
107         BIBLIO_RESULTS => $resultsarray,
108         email_sender   => $email_sender,
109         comment        => $comment,
110         firstname      => $user->{firstname},
111         surname        => $user->{surname},
112     );
113
114     # Getting template result
115     my $template_res = $template2->output();
116     my $body;
117
118     # Analysing information and getting mail properties
119     if ( $template_res =~ /<SUBJECT>\n(.*)\n<END_SUBJECT>/s ) {
120         $mail{'subject'} = $1;
121     }
122     else { $mail{'subject'} = "no subject"; }
123
124     my $email_header = "";
125     if ( $template_res =~ /<HEADER>\n(.*)\n<END_HEADER>/s ) {
126         $email_header = $1;
127     }
128
129     my $email_file = "basket.txt";
130     if ( $template_res =~ /<FILENAME>\n(.*)\n<END_FILENAME>/s ) {
131         $email_file = $1;
132     }
133
134     if ( $template_res =~ /<MESSAGE>\n(.*)\n<END_MESSAGE>/s ) { $body = encode_qp($1); }
135
136     my $boundary = "====" . time() . "====";
137
138     #     $mail{'content-type'} = "multipart/mixed; boundary=\"$boundary\"";
139     #
140     #     $email_header = encode_qp($email_header);
141     #
142     #     $boundary = "--".$boundary;
143     #
144     #     # Writing mail
145     #     $mail{body} =
146     $mail{'content-type'} = "multipart/mixed; boundary=\"$boundary\"";
147     my $isofile = encode_base64(encode("UTF-8", $iso2709));
148     $boundary = '--' . $boundary;
149     $mail{body} = <<END_OF_BODY;
150 $boundary
151 Content-Type: text/plain; charset="utf-8"
152 Content-Transfer-Encoding: quoted-printable
153
154 $email_header
155 $body
156 $boundary
157 Content-Type: application/octet-stream; name="basket.iso2709"
158 Content-Transfer-Encoding: base64
159 Content-Disposition: attachment; filename="basket.iso2709"
160
161 $isofile
162 $boundary--
163 END_OF_BODY
164
165     # Sending mail
166     if ( sendmail %mail ) {
167         # do something if it works....
168         $template->param( SENT      => "1" );
169     }
170     else {
171         # do something if it doesnt work....
172         warn "Error sending mail: $Mail::Sendmail::error \n";
173         $template->param( error => 1 );
174     }
175     $template->param( email_add => $email_add );
176     output_html_with_http_headers $query, $cookie, $template->output;
177 }
178 else {
179     $template->param( bib_list => $bib_list );
180     $template->param(
181         url            => "/cgi-bin/koha/opac-sendbasket.pl",
182         suggestion     => C4::Context->preference("suggestion"),
183         virtualshelves => C4::Context->preference("virtualshelves"),
184     );
185     output_html_with_http_headers $query, $cookie, $template->output;
186 }