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