Bug 34478: ILL OPAC: create => cud-create
[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
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use Modern::Perl;
21
22 use CGI qw ( -utf8 );
23 use Encode;
24 use Carp qw( carp );
25 use Try::Tiny qw( catch try );
26
27 use C4::Auth qw( get_template_and_user );
28 use C4::Biblio qw(
29   GetFrameworkCode
30   GetMarcISBN
31   GetMarcSubjects
32 );
33 use C4::Output qw( output_html_with_http_headers );
34 use Koha::Biblios;
35 use Koha::Email;
36 use Koha::Patrons;
37 use Koha::Virtualshelves;
38
39 my $query = CGI->new;
40
41 # if virtualshelves is disabled, leave immediately
42 if ( !C4::Context->preference('virtualshelves') ) {
43     print $query->redirect("/cgi-bin/koha/errors/404.pl");
44     exit;
45 }
46
47 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
48     {
49         template_name => "opac-sendshelfform.tt",
50         query         => $query,
51         type          => "opac",
52     }
53 );
54
55 my $shelfid = $query->param('shelfid');
56 my $email   = $query->param('email');
57 my $op      = $query->param('op') // q{};
58
59 my $shelf = Koha::Virtualshelves->find($shelfid);
60 if ( $shelf and $shelf->can_be_viewed($borrowernumber) ) {
61     if ( $email && $op eq 'cud-send' ) {
62         my $comment = $query->param('comment');
63
64         my $patron     = Koha::Patrons->find($borrowernumber);
65         my $user_email = $patron->notice_email_address;
66         my $shelf      = Koha::Virtualshelves->find($shelfid);
67         my $contents   = $shelf->get_contents;
68         my $iso2709;
69
70         my @biblionumbers;
71         while ( my $content = $contents->next ) {
72             push @biblionumbers, $content->biblionumber;
73             my $biblio = Koha::Biblios->find( $content->biblionumber );
74             $iso2709 .= $biblio->metadata->record->as_usmarc();
75         }
76
77         if ( !defined $iso2709 ) {
78             carp "Error sending mail: empty list";
79             $template->param( error => 1 );
80         }
81         elsif ( !defined $user_email or $user_email eq '' ) {
82             carp "Error sending mail: sender's email address is invalid";
83             $template->param( error => 1 );
84         }
85         else {
86             my %loops = ( biblio => \@biblionumbers, );
87
88             my %substitute = (
89                 comment  => $comment,
90                 listname => $shelf->shelfname,
91             );
92
93             my $letter = C4::Letters::GetPreparedLetter(
94                 module      => 'catalogue',
95                 letter_code => 'LIST',
96                 lang        => $patron->lang,
97                 tables      => {
98                     borrowers => $borrowernumber,
99                 },
100                 message_transport_type => 'email',
101                 loops                  => \%loops,
102                 substitute             => \%substitute,
103             );
104
105             my $attachment = {
106                 filename => 'list.iso2709',
107                 type     => 'application/octet-stream',
108                 content  => Encode::encode( "UTF-8", $iso2709 ),
109             };
110
111             my $message_id = C4::Letters::EnqueueLetter(
112                 {
113                     letter                 => $letter,
114                     message_transport_type => 'email',
115                     borrowernumber         => $patron->borrowernumber,
116                     to_address             => $email,
117                     reply_address          => $user_email,
118                     attachments            => [$attachment],
119                 }
120             );
121
122             C4::Letters::SendQueuedMessages( { message_id => $message_id } ) if $message_id;
123
124             $template->param( SENT => 1 );
125         }
126
127         $template->param(
128             shelfid => $shelfid,
129             email   => $email,
130         );
131         output_html_with_http_headers $query, $cookie, $template->output,
132           undef, { force_no_caching => 1 };
133
134     }
135     else {
136         $template->param(
137             shelfid => $shelfid,
138             url     => "/cgi-bin/koha/opac-sendshelf.pl",
139         );
140         output_html_with_http_headers $query, $cookie, $template->output,
141           undef, { force_no_caching => 1 };
142     }
143 }
144 else {
145     $template->param(
146         invalidlist => 1,
147         url         => "/cgi-bin/koha/opac-sendshelf.pl",
148     );
149     output_html_with_http_headers $query, $cookie, $template->output, undef,
150       { force_no_caching => 1 };
151 }