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