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