Bug 3150: (QA follow-up) Tidy scripts
[srvgit] / virtualshelves / sendshelf.pl
1 #!/usr/bin/perl
2
3 # Copyright 2009 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   GetMarcISBN
30   GetMarcSubjects
31 );
32 use C4::Output qw(
33   output_html_with_http_headers
34   output_and_exit
35 );
36
37 use Koha::Biblios;
38 use Koha::Email;
39 use Koha::Virtualshelves;
40
41 my $query = CGI->new;
42
43 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
44     {
45         template_name => "virtualshelves/sendshelfform.tt",
46         query         => $query,
47         type          => "intranet",
48         flagsrequired => { catalogue => 1 },
49     }
50 );
51
52 my $shelfid    = $query->param('shelfid');
53 my $to_address = $query->param('email');
54
55 my $shelf = Koha::Virtualshelves->find($shelfid);
56
57 output_and_exit( $query, $cookie, $template, 'insufficient_permission' )
58   if $shelf && !$shelf->can_be_viewed($borrowernumber);
59
60 if ($to_address) {
61     my $comment = $query->param('comment');
62
63     my $patron     = Koha::Patrons->find($borrowernumber);
64     my $user_email = $patron->first_valid_email_address;
65     my $contents   = $shelf->get_contents;
66     my @biblionumbers;
67     my $iso2709;
68
69     while ( my $content = $contents->next ) {
70         push @biblionumbers, $content->biblionumber;
71         my $biblio = Koha::Biblios->find( $content->biblionumber );
72         $iso2709 .= $biblio->metadata->record->as_usmarc();
73     }
74
75     if ( !defined $iso2709 ) {
76         carp "Error sending mail: empty basket";
77         $template->param( error => 1 );
78     }
79     elsif ( !defined $user_email or $user_email eq '' ) {
80         carp "Error sending mail: sender's email address is invalid";
81         $template->param( error => 1 );
82     }
83     else {
84         my %loops = ( biblio => \@biblionumbers, );
85
86         my %substitute = (
87             comment  => $comment,
88             listname => $shelf->shelfname,
89         );
90
91         my $letter = C4::Letters::GetPreparedLetter(
92             module      => 'catalogue',
93             letter_code => 'LIST',
94             lang        => $patron->lang,
95             tables      => {
96                 borrowers => $borrowernumber,
97             },
98             message_transport_type => 'email',
99             loops                  => \%loops,
100             substitute             => \%substitute,
101         );
102
103         my $attachment = {
104             filename => 'shelf.iso2709',
105             type     => 'application/octet-stream',
106             content  => Encode::encode( "UTF-8", $iso2709 ),
107         };
108
109         my $message_id = C4::Letters::EnqueueLetter(
110             {
111                 letter                 => $letter,
112                 message_transport_type => 'email',
113                 borrowernumber         => $patron->borrowernumber,
114                 to_address             => $to_address,
115                 reply_address          => $user_email,
116                 attachments            => [$attachment],
117             }
118         );
119
120         C4::Letters::SendQueuedMessages( { message_id => $message_id } );
121
122         $template->param( SENT => 1 );
123     }
124
125     $template->param( email => $to_address );
126     output_html_with_http_headers $query, $cookie, $template->output;
127
128 }
129 else {
130     $template->param(
131         shelfid => $shelfid,
132         url     => "/cgi-bin/koha/virtualshelves/sendshelf.pl",
133     );
134     output_html_with_http_headers $query, $cookie, $template->output;
135 }