Bug 32336: (QA follow-up) Use $metadata->schema
[srvgit] / basket / 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
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 use Modern::Perl;
19
20 use CGI qw ( -utf8 );
21 use Encode;
22 use Carp qw( carp );
23 use Try::Tiny qw( catch try );
24
25 use C4::Biblio qw(
26   GetMarcSubjects
27 );
28 use C4::Auth qw( get_template_and_user );
29 use C4::Output qw( output_and_exit output_html_with_http_headers );
30 use C4::Templates;
31 use Koha::Biblios;
32 use Koha::Email;
33 use Koha::Token;
34
35 my $query = CGI->new;
36
37 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
38     {
39         template_name => "basket/sendbasketform.tt",
40         query         => $query,
41         type          => "intranet",
42         flagsrequired => { catalogue => 1 },
43     }
44 );
45
46 my $bib_list  = $query->param('bib_list') || '';
47 my $email_add = $query->param('email_add');
48
49 my $dbh = C4::Context->dbh;
50
51 if ($email_add) {
52     output_and_exit( $query, $cookie, $template, 'wrong_csrf_token' )
53       unless Koha::Token->new->check_csrf(
54         {
55             session_id => scalar $query->cookie('CGISESSID'),
56             token      => scalar $query->param('csrf_token'),
57         }
58       );
59
60     my $patron     = Koha::Patrons->find($borrowernumber);
61     my $user_email = $patron->first_valid_email_address;
62
63     my $comment = $query->param('comment');
64
65     my @bibs = split( /\//, $bib_list );
66     my $iso2709;
67
68     foreach my $bib (@bibs) {
69         my $biblio = Koha::Biblios->find($bib) or next;
70         $iso2709 .= $biblio->metadata->record->as_usmarc();
71     }
72
73     if ( !defined $iso2709 ) {
74         carp "Error sending mail: empty basket";
75         $template->param( error => 1 );
76     }
77     elsif ( !defined $user_email or $user_email eq '' ) {
78         carp "Error sending mail: sender's email address is invalid";
79         $template->param( error => 1 );
80     }
81     else {
82         my %loops = ( biblio => \@bibs, );
83
84         my %substitute = ( comment => $comment, );
85
86         my $letter = C4::Letters::GetPreparedLetter(
87             module      => 'catalogue',
88             letter_code => 'CART',
89             lang        => $patron->lang,
90             tables      => {
91                 borrowers => $borrowernumber,
92             },
93             message_transport_type => 'email',
94             loops                  => \%loops,
95             substitute             => \%substitute,
96         );
97
98         my $attachment = {
99             filename => 'basket.iso2709',
100             type     => 'application/octet-stream',
101             content  => Encode::encode( "UTF-8", $iso2709 ),
102         };
103
104         my $message_id = C4::Letters::EnqueueLetter(
105             {
106                 letter                 => $letter,
107                 message_transport_type => 'email',
108                 to_address             => $email_add,
109                 reply_address          => $user_email,
110                 attachments            => [$attachment],
111             }
112         );
113
114         C4::Letters::SendQueuedMessages( { message_id => $message_id } );
115
116         $template->param( SENT => 1 );
117     }
118     $template->param( email_add => $email_add );
119     output_html_with_http_headers $query, $cookie, $template->output;
120 }
121 else {
122     $template->param(
123         bib_list       => $bib_list,
124         url            => "/cgi-bin/koha/basket/sendbasket.pl",
125         suggestion     => C4::Context->preference("suggestion"),
126         virtualshelves => C4::Context->preference("virtualshelves"),
127         csrf_token     => Koha::Token->new->generate_csrf(
128             { session_id => scalar $query->cookie('CGISESSID'), }
129         ),
130     );
131     output_html_with_http_headers $query, $cookie, $template->output;
132 }