Bug 7941 : Fix version numbers in modules
[koha.git] / opac / opac-sendbasket.pl
1 #!/usr/bin/perl
2
3 # Copyright Doxulting 2004
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20 use strict;
21 use warnings;
22
23 use CGI;
24 use Encode qw(encode);
25 use Carp;
26
27 use Mail::Sendmail;
28 use MIME::QuotedPrint;
29 use MIME::Base64;
30 use HTML::FormatText;
31 use C4::Biblio;
32 use C4::Items;
33 use C4::Auth;
34 use C4::Output;
35 use C4::Biblio;
36 use C4::Members;
37
38 my $query = new CGI;
39
40 my ( $template, $borrowernumber, $cookie ) = get_template_and_user (
41     {
42         template_name   => "opac-sendbasketform.tmpl",
43         query           => $query,
44         type            => "opac",
45         authnotrequired => 0,
46         flagsrequired   => { borrow => 1 },
47     }
48 );
49
50 my $bib_list     = $query->param('bib_list');
51 my $email_add    = $query->param('email_add');
52 my $email_sender = $query->param('email_sender');
53
54 my $dbh          = C4::Context->dbh;
55
56 if ( $email_add ) {
57     my $email_from = C4::Context->preference('KohaAdminEmailAddress');
58     my $comment    = $query->param('comment');
59     my %mail = (
60         To   => $email_add,
61         From => $email_from
62     );
63
64     my ( $template2, $borrowernumber, $cookie ) = get_template_and_user(
65         {
66             template_name   => "opac-sendbasket.tmpl",
67             query           => $query,
68             type            => "opac",
69             authnotrequired => 1,
70             flagsrequired   => { borrow => 1 },
71         }
72     );
73
74     my @bibs = split( /\//, $bib_list );
75     my @results;
76     my $iso2709;
77     my $marcflavour = C4::Context->preference('marcflavour');
78     foreach my $biblionumber (@bibs) {
79         $template2->param( biblionumber => $biblionumber );
80
81         my $dat              = GetBiblioData($biblionumber);
82         my $record           = GetMarcBiblio($biblionumber);
83         my $marcnotesarray   = GetMarcNotes( $record, $marcflavour );
84         my $marcauthorsarray = GetMarcAuthors( $record, $marcflavour );
85         my $marcsubjctsarray = GetMarcSubjects( $record, $marcflavour );
86
87         my @items = GetItemsInfo( $biblionumber );
88
89         my $hasauthors = 0;
90         if($dat->{'author'} || @$marcauthorsarray) {
91           $hasauthors = 1;
92         }
93         
94
95         $dat->{MARCNOTES}      = $marcnotesarray;
96         $dat->{MARCSUBJCTS}    = $marcsubjctsarray;
97         $dat->{MARCAUTHORS}    = $marcauthorsarray;
98         $dat->{HASAUTHORS}     = $hasauthors;
99         $dat->{'biblionumber'} = $biblionumber;
100         $dat->{ITEM_RESULTS}   = \@items;
101
102         $iso2709 .= $record->as_usmarc();
103
104         push( @results, $dat );
105     }
106
107     my $resultsarray = \@results;
108     
109     my $user = GetMember(borrowernumber => $borrowernumber); 
110     
111     $template2->param(
112         BIBLIO_RESULTS => $resultsarray,
113         email_sender   => $email_sender,
114         comment        => $comment,
115         firstname      => $user->{firstname},
116         surname        => $user->{surname},
117     );
118
119     # Getting template result
120     my $template_res = $template2->output();
121     my $body;
122
123     # Analysing information and getting mail properties
124     if ( $template_res =~ /<SUBJECT>\n(.*)\n?<END_SUBJECT>/s ) {
125         $mail{'subject'} = $1;
126     }
127     else { $mail{'subject'} = "no subject"; }
128
129     my $email_header = "";
130     if ( $template_res =~ /<HEADER>\n(.*)\n?<END_HEADER>/s ) {
131         $email_header = $1;
132     }
133
134     my $email_file = "basket.txt";
135     if ( $template_res =~ /<FILENAME>\n(.*)\n?<END_FILENAME>/s ) {
136         $email_file = $1;
137     }
138
139     if ( $template_res =~ /<MESSAGE>\n(.*)\n?<END_MESSAGE>/s ) {
140         $body = $1;
141     }
142
143     my $boundary = "====" . time() . "====";
144
145     $mail{'content-type'} = "multipart/mixed; boundary=\"$boundary\"";
146     my $isofile = encode_base64(encode("UTF-8", $iso2709));
147     $boundary = '--' . $boundary;
148     $mail{body} = <<END_OF_BODY;
149 $boundary
150 MIME-Version: 1.0
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         carp "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 }