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