Bug 34478: op =~ ^cud- in pl/pm
[koha.git] / opac / opac-sendbasket.pl
1 #!/usr/bin/perl
2
3 # Copyright Doxulting 2004
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::Biblio qw(
28   GetMarcSubjects
29 );
30 use C4::Auth qw( get_template_and_user );
31 use C4::Output qw( output_html_with_http_headers );
32 use C4::Templates;
33 use Koha::Biblios;
34 use Koha::Email;
35 use Koha::Patrons;
36
37 my $query = CGI->new;
38
39 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
40     {
41         template_name => "opac-sendbasketform.tt",
42         query         => $query,
43         type          => "opac",
44     }
45 );
46
47 my $bib_list  = $query->param('bib_list') || '';
48 my $email_add = $query->param('email_add');
49
50 if ( $email_add ) {
51
52     my $patron     = Koha::Patrons->find($borrowernumber);
53     my $user_email = $patron->notice_email_address;
54
55     my $comment = $query->param('comment');
56
57     my @bibs = split( /\//, $bib_list );
58     my $iso2709;
59     foreach my $bib (@bibs) {
60         my $biblio = Koha::Biblios->find($bib) or next;
61         $iso2709 .= $biblio->metadata->record->as_usmarc();
62     }
63
64     if ( !defined $iso2709 ) {
65         carp "Error sending mail: empty basket";
66         $template->param( error => 1 );
67     }
68     elsif ( !defined $user_email or $user_email eq '' ) {
69         carp "Error sending mail: sender's email address is invalid";
70         $template->param( error => 1 );
71     }
72     else {
73         my %loops = ( biblio => \@bibs, );
74
75         my %substitute = ( comment => $comment, );
76
77         my $letter = C4::Letters::GetPreparedLetter(
78             module      => 'catalogue',
79             letter_code => 'CART',
80             lang        => $patron->lang,
81             tables      => {
82                 borrowers => $borrowernumber,
83             },
84             message_transport_type => 'cud-email',
85             loops                  => \%loops,
86             substitute             => \%substitute,
87         );
88
89         my $attachment = {
90             filename => 'basket.iso2709',
91             type     => 'application/octet-stream',
92             content  => Encode::encode( "UTF-8", $iso2709 ),
93         };
94
95         my $message_id = C4::Letters::EnqueueLetter(
96             {
97                 letter                 => $letter,
98                 message_transport_type => 'cud-email',
99                 to_address             => $email_add,
100                 reply_address          => $user_email,
101                 attachments            => [$attachment],
102             }
103         );
104
105         C4::Letters::SendQueuedMessages( { message_id => $message_id } ) if $message_id;
106
107         $template->param( SENT => 1 );
108     }
109
110     $template->param( email_add => $email_add );
111     output_html_with_http_headers $query, $cookie, $template->output, undef,
112       { force_no_caching => 1 };
113 }
114 else {
115     my $new_session_id = $query->cookie('CGISESSID');
116     $template->param(
117         bib_list       => $bib_list,
118         url            => "/cgi-bin/koha/opac-sendbasket.pl",
119         suggestion     => C4::Context->preference("suggestion"),
120         virtualshelves => C4::Context->preference("virtualshelves"),
121     );
122     output_html_with_http_headers $query, $cookie, $template->output, undef,
123       { force_no_caching => 1 };
124 }