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