Bug 29646: Fix undefined category crash in opac-password-recovery
[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     GetMarcBiblio
31     GetMarcISBN
32     GetMarcSubjects
33 );
34 use C4::Items qw( GetItemsInfo );
35 use C4::Output qw( output_html_with_http_headers );
36 use Koha::Email;
37 use Koha::Patrons;
38 use Koha::Virtualshelves;
39
40 my $query = CGI->new;
41
42 # if virtualshelves is disabled, leave immediately
43 if ( ! C4::Context->preference('virtualshelves') ) {
44     print $query->redirect("/cgi-bin/koha/errors/404.pl");
45     exit;
46 }
47
48 my ( $template, $borrowernumber, $cookie ) = get_template_and_user (
49     {
50         template_name   => "opac-sendshelfform.tt",
51         query           => $query,
52         type            => "opac",
53     }
54 );
55
56 my $shelfid = $query->param('shelfid');
57 my $email   = $query->param('email');
58
59 my $dbh          = C4::Context->dbh;
60
61 my $shelf = Koha::Virtualshelves->find( $shelfid );
62 if ( $shelf and $shelf->can_be_viewed( $borrowernumber ) ) {
63   if ( $email ) {
64     my $comment    = $query->param('comment');
65
66     my ( $template2, $borrowernumber, $cookie ) = get_template_and_user(
67         {
68             template_name   => "opac-sendshelf.tt",
69             query           => $query,
70             type            => "opac",
71             authnotrequired => 1,
72         }
73     );
74
75     my $patron = Koha::Patrons->find( $borrowernumber );
76     my $borcat = $patron ? $patron->categorycode : q{};
77
78     my $shelf = Koha::Virtualshelves->find( $shelfid );
79     my $contents = $shelf->get_contents;
80     my $marcflavour         = C4::Context->preference('marcflavour');
81     my $iso2709;
82     my @results;
83
84     while ( my $content = $contents->next ) {
85         my $biblionumber = $content->biblionumber;
86         my $biblio       = Koha::Biblios->find( $biblionumber ) or next;
87         my $dat          = $biblio->unblessed;
88         my $record           = GetMarcBiblio({
89             biblionumber => $biblionumber,
90             embed_items  => 1,
91             opac         => 1,
92             borcat       => $borcat });
93         next unless $record;
94         my $fw               = GetFrameworkCode($biblionumber);
95
96         my $marcauthorsarray = $biblio->get_marc_authors;
97         my $marcsubjctsarray = GetMarcSubjects( $record, $marcflavour );
98
99         my @items = GetItemsInfo( $biblionumber );
100
101         $dat->{ISBN}           = GetMarcISBN($record, $marcflavour);
102         $dat->{MARCSUBJCTS}    = $marcsubjctsarray;
103         $dat->{MARCAUTHORS}    = $marcauthorsarray;
104         $dat->{'biblionumber'} = $biblionumber;
105         $dat->{ITEM_RESULTS}   = \@items;
106         $dat->{HASAUTHORS}     = $dat->{'author'} || @$marcauthorsarray;
107
108         $iso2709 .= $record->as_usmarc();
109
110         push( @results, $dat );
111     }
112
113     $template2->param(
114         BIBLIO_RESULTS => \@results,
115         comment        => $comment,
116         shelfname      => $shelf->shelfname,
117         firstname      => $patron->firstname,
118         surname        => $patron->surname,
119     );
120
121     # Getting template result
122     my $template_res = $template2->output();
123     my $body;
124
125     my $subject;
126     # Analysing information and getting mail properties
127     if ( $template_res =~ /<SUBJECT>(?<subject>.*)<END_SUBJECT>/s ) {
128         $subject = $+{subject};
129         $subject =~ s|\n?(.*)\n?|$1|;
130     }
131     else {
132         $subject = "no subject";
133     }
134
135     my $email_header = "";
136     if ( $template_res =~ /<HEADER>(.*)<END_HEADER>/s ) {
137         $email_header = $1;
138         $email_header =~ s|\n?(.*)\n?|$1|;
139     }
140
141     if ( $template_res =~ /<MESSAGE>(.*)<END_MESSAGE>/s ) {
142         $body = $1;
143         $body =~ s|\n?(.*)\n?|$1|;
144     }
145
146     my $THE_body = <<END_OF_BODY;
147 $email_header
148 $body
149 END_OF_BODY
150
151     try {
152         my $email = Koha::Email->create(
153             {
154                 to      => $email,
155                 subject => $subject,
156             }
157         );
158         $email->text_body( $THE_body );
159         $email->attach(
160             Encode::encode( "UTF-8", $iso2709 ),
161             content_type => 'application/octet-stream',
162             name         => 'list.iso2709',
163             disposition  => 'attachment',
164         );
165         my $library = Koha::Patrons->find( $borrowernumber )->library;
166         $email->transport( $library->smtp_server->transport );
167         $email->send_or_die;
168         $template->param( SENT => "1" );
169     }
170     catch {
171         carp "Error sending mail: $_";
172         $template->param( error => 1 );
173     };
174
175     $template->param(
176         shelfid => $shelfid,
177         email   => $email,
178     );
179     output_html_with_http_headers $query, $cookie, $template->output, undef, { force_no_caching => 1 };
180
181   } else {
182     $template->param( shelfid => $shelfid,
183                       url     => "/cgi-bin/koha/opac-sendshelf.pl",
184                     );
185     output_html_with_http_headers $query, $cookie, $template->output, undef, { force_no_caching => 1 };
186   }
187 } else {
188     $template->param( invalidlist => 1,
189                       url     => "/cgi-bin/koha/opac-sendshelf.pl",
190     );
191     output_html_with_http_headers $query, $cookie, $template->output, undef, { force_no_caching => 1 };
192 }