Bug 17952 - Lost items not skipped by overdue_notices.pl
[koha.git] / basket / sendbasket.pl
blob47bd1c9592d595dc62dc41ca3f9c1a728d8769e5
1 #!/usr/bin/perl
3 # This file is part of Koha.
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.
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.
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>.
18 use Modern::Perl;
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;
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;
35 my $query = new CGI;
37 my ( $template, $borrowernumber, $cookie ) = get_template_and_user (
39 template_name => "basket/sendbasketform.tt",
40 query => $query,
41 type => "intranet",
42 authnotrequired => 0,
43 flagsrequired => { catalogue => 1 },
47 my $bib_list = $query->param('bib_list') || '';
48 my $email_add = $query->param('email_add');
50 my $dbh = C4::Context->dbh;
52 if ( $email_add ) {
53 die "Wrong CSRF token" unless Koha::Token->new->check_csrf({
54 session_id => scalar $query->cookie('CGISESSID'),
55 token => scalar $query->param('csrf_token'),
56 });
57 my $email = Koha::Email->new();
58 my %mail = $email->create_message_headers({ to => $email_add });
59 my $comment = $query->param('comment');
61 # Since we are already logged in, no need to check credentials again
62 # when loading a second template.
63 my $template2 = C4::Templates::gettemplate(
64 'basket/sendbasket.tt', 'intranet', $query,
67 my @bibs = split( /\//, $bib_list );
68 my @results;
69 my $iso2709;
70 my $marcflavour = C4::Context->preference('marcflavour');
71 foreach my $biblionumber (@bibs) {
72 $template2->param( biblionumber => $biblionumber );
74 my $dat = GetBiblioData($biblionumber);
75 next unless $dat;
76 my $record = GetMarcBiblio($biblionumber, 1);
77 my $marcauthorsarray = GetMarcAuthors( $record, $marcflavour );
78 my $marcsubjctsarray = GetMarcSubjects( $record, $marcflavour );
80 my @items = GetItemsInfo( $biblionumber );
82 my $hasauthors = 0;
83 if($dat->{'author'} || @$marcauthorsarray) {
84 $hasauthors = 1;
88 $dat->{MARCSUBJCTS} = $marcsubjctsarray;
89 $dat->{MARCAUTHORS} = $marcauthorsarray;
90 $dat->{HASAUTHORS} = $hasauthors;
91 $dat->{'biblionumber'} = $biblionumber;
92 $dat->{ITEM_RESULTS} = \@items;
94 $iso2709 .= $record->as_usmarc();
96 push( @results, $dat );
99 my $resultsarray = \@results;
100 $template2->param(
101 BIBLIO_RESULTS => $resultsarray,
102 comment => $comment
105 # Getting template result
106 my $template_res = $template2->output();
107 my $body;
109 # Analysing information and getting mail properties
110 if ( $template_res =~ /<SUBJECT>(.*)<END_SUBJECT>/s ) {
111 $mail{subject} = $1;
112 $mail{subject} =~ s|\n?(.*)\n?|$1|;
113 $mail{subject} = Encode::encode("UTF-8", $mail{subject});
115 else { $mail{'subject'} = "no subject"; }
117 my $email_header = "";
118 if ( $template_res =~ /<HEADER>(.*)<END_HEADER>/s ) {
119 $email_header = $1;
120 $email_header =~ s|\n?(.*)\n?|$1|;
121 $email_header = encode_qp(Encode::encode("UTF-8", $email_header));
124 my $email_file = "basket.txt";
125 if ( $template_res =~ /<FILENAME>(.*)<END_FILENAME>/s ) {
126 $email_file = $1;
127 $email_file =~ s|\n?(.*)\n?|$1|;
130 if ( $template_res =~ /<MESSAGE>(.*)<END_MESSAGE>/s ) {
131 $body = $1;
132 $body =~ s|\n?(.*)\n?|$1|;
133 $body = encode_qp(Encode::encode("UTF-8", $body));
136 my $boundary = "====" . time() . "====";
138 # Writing mail
139 $mail{'content-type'} = "multipart/mixed; boundary=\"$boundary\"";
140 my $isofile = encode_base64(encode("UTF-8", $iso2709));
141 $boundary = '--' . $boundary;
142 $mail{body} = <<END_OF_BODY;
143 $boundary
144 Content-Type: text/plain; charset="utf-8"
145 Content-Transfer-Encoding: quoted-printable
147 $email_header
148 $body
149 $boundary
150 Content-Type: application/octet-stream; name="basket.iso2709"
151 Content-Transfer-Encoding: base64
152 Content-Disposition: attachment; filename="basket.iso2709"
154 $isofile
155 $boundary--
156 END_OF_BODY
158 # Sending mail
159 if ( sendmail %mail ) {
160 # do something if it works....
161 $template->param( SENT => "1" );
163 else {
164 # do something if it doesnt work....
165 carp "Error sending mail: $Mail::Sendmail::error \n";
166 $template->param( error => 1 );
168 $template->param( email_add => $email_add );
169 output_html_with_http_headers $query, $cookie, $template->output;
171 else {
172 $template->param(
173 bib_list => $bib_list,
174 url => "/cgi-bin/koha/basket/sendbasket.pl",
175 suggestion => C4::Context->preference("suggestion"),
176 virtualshelves => C4::Context->preference("virtualshelves"),
177 csrf_token => Koha::Token->new->generate_csrf({ session_id => scalar $query->cookie('CGISESSID'), }),
179 output_html_with_http_headers $query, $cookie, $template->output;