2076c425fa6f57c5ee046c2cebe7d7e3228491ef
[koha.git] / basket / 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
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 use Modern::Perl;
19
20 use CGI qw ( -utf8 );
21 use Encode qw(encode);
22 use Carp;
23 use Mail::Sendmail;
24 use MIME::QuotedPrint;
25 use MIME::Base64;
26
27 use C4::Biblio;
28 use C4::Items;
29 use C4::Auth;
30 use C4::Output;
31 use C4::Templates ();
32 use Koha::Email;
33 use Koha::Token;
34
35 my $query = new CGI;
36
37 my ( $template, $borrowernumber, $cookie ) = get_template_and_user (
38     {
39         template_name   => "basket/sendbasketform.tt",
40         query           => $query,
41         type            => "intranet",
42         authnotrequired => 0,
43         flagsrequired   => { catalogue => 1 },
44     }
45 );
46
47 my $bib_list     = $query->param('bib_list') || '';
48 my $email_add    = $query->param('email_add');
49
50 my $dbh          = C4::Context->dbh;
51
52 if ( $email_add ) {
53     output_and_exit( $query, $cookie, $template, 'wrong_csrf_token' )
54         unless Koha::Token->new->check_csrf({
55             session_id => scalar $query->cookie('CGISESSID'),
56             token  => scalar $query->param('csrf_token'),
57         });
58     my $email = Koha::Email->new();
59     my %mail = $email->create_message_headers({ to => $email_add });
60     my $comment    = $query->param('comment');
61
62     # Since we are already logged in, no need to check credentials again
63     # when loading a second template.
64     my $template2 = C4::Templates::gettemplate(
65         'basket/sendbasket.tt', 'intranet', $query,
66     );
67
68     my @bibs = split( /\//, $bib_list );
69     my @results;
70     my $iso2709;
71     my $marcflavour = C4::Context->preference('marcflavour');
72     foreach my $biblionumber (@bibs) {
73         $template2->param( biblionumber => $biblionumber );
74
75         my $dat              = GetBiblioData($biblionumber);
76         next unless $dat;
77         my $record           = GetMarcBiblio({
78             biblionumber => $biblionumber,
79             embed_items => 1 });
80         my $marcauthorsarray = GetMarcAuthors( $record, $marcflavour );
81         my $marcsubjctsarray = GetMarcSubjects( $record, $marcflavour );
82
83         my @items = GetItemsInfo( $biblionumber );
84
85         my $hasauthors = 0;
86         if($dat->{'author'} || @$marcauthorsarray) {
87           $hasauthors = 1;
88         }
89         
90
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     $template2->param(
104         BIBLIO_RESULTS => $resultsarray,
105         comment        => $comment
106     );
107
108     # Getting template result
109     my $template_res = $template2->output();
110     my $body;
111
112     # Analysing information and getting mail properties
113     if ( $template_res =~ /<SUBJECT>(.*)<END_SUBJECT>/s ) {
114         $mail{subject} = $1;
115         $mail{subject} =~ s|\n?(.*)\n?|$1|;
116         $mail{subject} = encode('MIME-Header',$mail{subject});
117     }
118     else { $mail{'subject'} = "no subject"; }
119
120     my $email_header = "";
121     if ( $template_res =~ /<HEADER>(.*)<END_HEADER>/s ) {
122         $email_header = $1;
123         $email_header =~ s|\n?(.*)\n?|$1|;
124         $email_header = encode_qp(Encode::encode("UTF-8", $email_header));
125     }
126
127     my $email_file = "basket.txt";
128     if ( $template_res =~ /<FILENAME>(.*)<END_FILENAME>/s ) {
129         $email_file = $1;
130         $email_file =~ s|\n?(.*)\n?|$1|;
131     }
132
133     if ( $template_res =~ /<MESSAGE>(.*)<END_MESSAGE>/s ) {
134         $body = $1;
135         $body =~ s|\n?(.*)\n?|$1|;
136         $body = encode_qp(Encode::encode("UTF-8", $body));
137     }
138
139     my $boundary = "====" . time() . "====";
140
141     # Writing mail
142     $mail{'content-type'} = "multipart/mixed; boundary=\"$boundary\"";
143     my $isofile = encode_base64(encode("UTF-8", $iso2709));
144     $boundary = '--' . $boundary;
145     $mail{body} = <<END_OF_BODY;
146 $boundary
147 Content-Type: text/plain; charset="utf-8"
148 Content-Transfer-Encoding: quoted-printable
149
150 $email_header
151 $body
152 $boundary
153 Content-Type: application/octet-stream; name="basket.iso2709"
154 Content-Transfer-Encoding: base64
155 Content-Disposition: attachment; filename="basket.iso2709"
156
157 $isofile
158 $boundary--
159 END_OF_BODY
160
161     # Sending mail
162     if ( sendmail %mail ) {
163         # do something if it works....
164         $template->param( SENT      => "1" );
165     }
166     else {
167         # do something if it doesn't work....
168         carp "Error sending mail: $Mail::Sendmail::error \n";
169         $template->param( error => 1 );
170     }
171     $template->param( email_add => $email_add );
172     output_html_with_http_headers $query, $cookie, $template->output;
173 }
174 else {
175     $template->param(
176         bib_list       => $bib_list,
177         url            => "/cgi-bin/koha/basket/sendbasket.pl",
178         suggestion     => C4::Context->preference("suggestion"),
179         virtualshelves => C4::Context->preference("virtualshelves"),
180         csrf_token     => Koha::Token->new->generate_csrf({ session_id => scalar $query->cookie('CGISESSID'), }),
181     );
182     output_html_with_http_headers $query, $cookie, $template->output;
183 }