(bug #4319) allow reserves on waiting items
[koha.git] / opac / opac-sendshelf.pl
1 #!/usr/bin/perl
2
3 # Copyright 2009 SARL Biblibre
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
26 use Mail::Sendmail;
27 use MIME::QuotedPrint;
28 use MIME::Base64;
29 use C4::Auth;
30 use C4::Biblio;
31 use C4::Items;
32 use C4::Output;
33 use C4::VirtualShelves;
34 use C4::Members;
35
36 my $query = new CGI;
37
38 my ( $template, $borrowernumber, $cookie ) = get_template_and_user (
39     {
40         template_name   => "opac-sendshelfform.tmpl",
41         query           => $query,
42         type            => "opac",
43         authnotrequired => 1,
44         flagsrequired   => { borrow => 1 },
45     }
46 );
47
48 my $shelfid = $query->param('shelfid');
49 my $email   = $query->param('email');
50
51 my $dbh          = C4::Context->dbh;
52
53 if ( $email ) {
54     my $email_from = C4::Context->preference('KohaAdminEmailAddress');
55     my $comment    = $query->param('comment');
56
57     my %mail = (
58         To   => $email,
59         From => $email_from
60     );
61
62     my ( $template2, $borrowernumber, $cookie ) = get_template_and_user(
63         {
64             template_name   => "opac-sendshelf.tmpl",
65             query           => $query,
66             type            => "opac",
67             authnotrequired => 1,
68             flagsrequired   => { borrow => 1 },
69         }
70     );
71
72     my @shelf               = GetShelf($shelfid);
73     my ($items, $totitems)  = GetShelfContents($shelfid);
74     my $marcflavour         = C4::Context->preference('marcflavour');
75     my $iso2709;
76     my @results;
77
78     # retrieve biblios from shelf
79     foreach my $biblio (@$items) {
80         my $biblionumber = $biblio->{biblionumber};
81
82         my $dat              = GetBiblioData($biblionumber);
83         my $record           = GetMarcBiblio($biblionumber);
84         my $marcnotesarray   = GetMarcNotes( $record, $marcflavour );
85         my $marcauthorsarray = GetMarcAuthors( $record, $marcflavour );
86         my $marcsubjctsarray = GetMarcSubjects( $record, $marcflavour );
87
88         my @items = &GetItemsInfo( $biblionumber, 'opac' );
89
90         $dat->{MARCNOTES}      = $marcnotesarray;
91         $dat->{MARCSUBJCTS}    = $marcsubjctsarray;
92         $dat->{MARCAUTHORS}    = $marcauthorsarray;
93         $dat->{'biblionumber'} = $biblionumber;
94         $dat->{ITEM_RESULTS}   = \@items;
95
96         $iso2709 .= $record->as_usmarc();
97
98         push( @results, $dat );
99     }
100
101     my $user = GetMember(borrowernumber => $borrowernumber); 
102
103     $template2->param(
104         BIBLIO_RESULTS => \@results,
105         email_sender   => $email_from,
106         comment        => $comment,
107         shelfname      => $shelf[1],
108         firstname      => $user->{firstname},
109         surname        => $user->{surname},
110     );
111
112     # Getting template result
113     my $template_res = $template2->output();
114     my $body;
115
116     # Analysing information and getting mail properties
117     if ( $template_res =~ /<SUBJECT>\n(.*)\n<END_SUBJECT>/s ) {
118         $mail{'subject'} = $1;
119     }
120     else { $mail{'subject'} = "no subject"; }
121
122     my $email_header = "";
123     if ( $template_res =~ /<HEADER>\n(.*)\n<END_HEADER>/s ) {
124         $email_header = $1;
125     }
126
127     my $email_file = "basket.txt";
128     if ( $template_res =~ /<FILENAME>\n(.*)\n<END_FILENAME>/s ) {
129         $email_file = $1;
130     }
131
132     if ( $template_res =~ /<MESSAGE>\n(.*)\n<END_MESSAGE>/s ) { $body = encode_qp($1); }
133
134     my $boundary = "====" . time() . "====";
135
136     # We set and put the multipart content
137     $mail{'content-type'} = "multipart/mixed; boundary=\"$boundary\"";
138
139     my $isofile = encode_base64(encode("UTF-8", $iso2709));
140     $boundary = '--' . $boundary;
141
142     $mail{body} = <<END_OF_BODY;
143 $boundary
144 Content-Type: text/plain; charset="utf-8"
145 Content-Transfer-Encoding: quoted-printable
146
147 $email_header
148 $body
149 $boundary
150 Content-Type: application/octet-stream; name="shelf.iso2709"
151 Content-Transfer-Encoding: base64
152 Content-Disposition: attachment; filename="shelf.iso2709"
153
154 $isofile
155 $boundary--
156 END_OF_BODY
157
158     # Sending mail
159     if ( sendmail %mail ) {
160         # do something if it works....
161         $template->param( SENT      => "1" );
162     }
163     else {
164         # do something if it doesnt work....
165         warn "Error sending mail: $Mail::Sendmail::error \n";
166         $template->param( error => 1 );
167     }
168
169     $template->param( email => $email );
170     output_html_with_http_headers $query, $cookie, $template->output;
171
172
173 }else{
174     $template->param( shelfid => $shelfid,
175                       url     => "/cgi-bin/koha/opac-sendshelf.pl",
176                     );
177     output_html_with_http_headers $query, $cookie, $template->output;
178 }