Bug 10860: In-House Use
[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 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 Koha::Email;
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   => { 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 = Koha::Email->new();
55     my %mail = $email->create_message_headers({ to => $email_add });
56     my $comment    = $query->param('comment');
57     my ( $template2, $borrowernumber, $cookie ) = get_template_and_user(
58         {
59             template_name   => "basket/sendbasket.tt",
60             query           => $query,
61             type            => "intranet",
62             authnotrequired => 0,
63             flagsrequired   => { borrow => 1 },
64         }
65     );
66
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 );
73
74         my $dat              = GetBiblioData($biblionumber);
75         my $record           = GetMarcBiblio($biblionumber);
76         my $marcnotesarray   = GetMarcNotes( $record, $marcflavour );
77         my $marcauthorsarray = GetMarcAuthors( $record, $marcflavour );
78         my $marcsubjctsarray = GetMarcSubjects( $record, $marcflavour );
79
80         my @items = GetItemsInfo( $biblionumber );
81
82         my $hasauthors = 0;
83         if($dat->{'author'} || @$marcauthorsarray) {
84           $hasauthors = 1;
85         }
86         
87
88         $dat->{MARCNOTES}      = $marcnotesarray;
89         $dat->{MARCSUBJCTS}    = $marcsubjctsarray;
90         $dat->{MARCAUTHORS}    = $marcauthorsarray;
91         $dat->{HASAUTHORS}     = $hasauthors;
92         $dat->{'biblionumber'} = $biblionumber;
93         $dat->{ITEM_RESULTS}   = \@items;
94
95         $iso2709 .= $record->as_usmarc();
96
97         push( @results, $dat );
98     }
99
100     my $resultsarray = \@results;
101     $template2->param(
102         BIBLIO_RESULTS => $resultsarray,
103         email_sender   => $email_sender,
104         comment        => $comment
105     );
106
107     # Getting template result
108     my $template_res = $template2->output();
109     my $body;
110
111     # Analysing information and getting mail properties
112     if ( $template_res =~ /<SUBJECT>(.*)<END_SUBJECT>/s ) {
113         $mail{subject} = $1;
114         $mail{subject} =~ s|\n?(.*)\n?|$1|;
115     }
116     else { $mail{'subject'} = "no subject"; }
117
118     my $email_header = "";
119     if ( $template_res =~ /<HEADER>(.*)<END_HEADER>/s ) {
120         $email_header = $1;
121         $email_header =~ s|\n?(.*)\n?|$1|;
122         $email_header = encode_qp($email_header);
123     }
124
125     my $email_file = "basket.txt";
126     if ( $template_res =~ /<FILENAME>(.*)<END_FILENAME>/s ) {
127         $email_file = $1;
128         $email_file =~ s|\n?(.*)\n?|$1|;
129     }
130
131     if ( $template_res =~ /<MESSAGE>(.*)<END_MESSAGE>/s ) {
132         $body = $1;
133         $body =~ s|\n?(.*)\n?|$1|;
134         $body = encode_qp($body);
135     }
136
137     my $boundary = "====" . time() . "====";
138
139     # Writing mail
140     $mail{'content-type'} = "multipart/mixed; boundary=\"$boundary\"";
141     my $isofile = encode_base64(encode("UTF-8", $iso2709));
142     $boundary = '--' . $boundary;
143     $mail{body} = <<END_OF_BODY;
144 $boundary
145 Content-Type: text/plain; charset="utf-8"
146 Content-Transfer-Encoding: quoted-printable
147
148 $email_header
149 $body
150 $boundary
151 Content-Type: application/octet-stream; name="basket.iso2709"
152 Content-Transfer-Encoding: base64
153 Content-Disposition: attachment; filename="basket.iso2709"
154
155 $isofile
156 $boundary--
157 END_OF_BODY
158
159     # Sending mail
160     if ( sendmail %mail ) {
161         # do something if it works....
162         $template->param( SENT      => "1" );
163     }
164     else {
165         # do something if it doesnt work....
166         carp "Error sending mail: $Mail::Sendmail::error \n";
167         $template->param( error => 1 );
168     }
169     $template->param( email_add => $email_add );
170     output_html_with_http_headers $query, $cookie, $template->output;
171 }
172 else {
173     $template->param( bib_list => $bib_list );
174     $template->param(
175         url            => "/cgi-bin/koha/basket/sendbasket.pl",
176         suggestion     => C4::Context->preference("suggestion"),
177         virtualshelves => C4::Context->preference("virtualshelves"),
178     );
179     output_html_with_http_headers $query, $cookie, $template->output;
180 }