Bug 27266: (follow-up) Remove instances of GetMarcAuthors
[srvgit] / virtualshelves / sendshelf.pl
1 #!/usr/bin/perl
2
3 # Copyright 2009 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     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::Virtualshelves;
38
39 my $query = CGI->new;
40
41 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
42     {
43         template_name   => "virtualshelves/sendshelfform.tt",
44         query           => $query,
45         type            => "intranet",
46         flagsrequired   => { catalogue => 1 },
47     }
48 );
49
50 my $shelfid    = $query->param('shelfid');
51 my $to_address = $query->param('email');
52
53 my $dbh = C4::Context->dbh;
54
55 if ($to_address) {
56     my $comment = $query->param('comment');
57
58     my ( $template2, $borrowernumber, $cookie ) = get_template_and_user(
59         {
60         template_name   => "virtualshelves/sendshelf.tt",
61         query           => $query,
62         type            => "intranet",
63         flagsrequired   => { catalogue => 1 },
64         }
65     );
66
67     my $shelf = Koha::Virtualshelves->find( $shelfid );
68     my $contents = $shelf->get_contents;
69     my $marcflavour = C4::Context->preference('marcflavour');
70     my $iso2709;
71     my @results;
72
73     while ( my $content = $contents->next ) {
74         my $biblionumber     = $content->biblionumber;
75         my $biblio           = Koha::Biblios->find( $biblionumber );
76         my $dat              = GetBiblioData($biblionumber);
77         my $record           = GetMarcBiblio({
78             biblionumber => $biblionumber,
79             embed_items  => 1 });
80         my $marcauthorsarray = $biblio->get_marc_authors;
81         my $marcsubjctsarray = GetMarcSubjects( $record, $marcflavour );
82
83         my @items = GetItemsInfo($biblionumber);
84
85         $dat->{ISBN}           = GetMarcISBN($record, $marcflavour);
86         $dat->{MARCSUBJCTS}    = $marcsubjctsarray;
87         $dat->{MARCAUTHORS}    = $marcauthorsarray;
88         $dat->{'biblionumber'} = $biblionumber;
89         $dat->{ITEM_RESULTS}   = \@items;
90         $dat->{HASAUTHORS}     = $dat->{'author'} || @$marcauthorsarray;
91
92         $iso2709 .= $record->as_usmarc();
93
94         push( @results, $dat );
95     }
96
97     $template2->param(
98         BIBLIO_RESULTS => \@results,
99         comment        => $comment,
100         shelfname      => $shelf->shelfname,
101     );
102
103     # Getting template result
104     my $template_res = $template2->output();
105     my $body;
106
107     my $subject;
108     # Analysing information and getting mail properties
109     if ( $template_res =~ /<SUBJECT>(?<subject>.*)<END_SUBJECT>/s ) {
110         $subject = $+{subject};
111         $subject =~ s|\n?(.*)\n?|$1|;
112     }
113     else {
114         $subject = "no subject";
115     }
116
117     my $email_header = "";
118     if ( $template_res =~ /<HEADER>(.*)<END_HEADER>/s ) {
119         $email_header = $1;
120         $email_header =~ s|\n?(.*)\n?|$1|;
121     }
122
123     if ( $template_res =~ /<MESSAGE>(.*)<END_MESSAGE>/s ) {
124         $body = $1;
125         $body =~ s|\n?(.*)\n?|$1|;
126     }
127
128     my $THE_body = <<END_OF_BODY;
129 $email_header
130 $body
131 END_OF_BODY
132
133     try {
134         my $email = Koha::Email->create(
135             {
136                 to      => $to_address,
137                 subject => $subject,
138             }
139         );
140         $email->text_body( $THE_body );
141         $email->attach(
142             Encode::encode("UTF-8", $iso2709),
143             content_type => 'application/octet-stream',
144             name         => 'shelf.iso2709',
145             disposition  => 'attachment',
146         );
147
148         my $library = Koha::Patrons->find( $borrowernumber )->library;
149         $email->send_or_die({ transport => $library->smtp_server->transport });
150         $template->param( SENT => "1" );
151     }
152     catch {
153         carp "Error sending mail: $_";
154         $template->param( error => 1 );
155     };
156
157     $template->param( email => $to_address );
158     output_html_with_http_headers $query, $cookie, $template->output;
159
160 }
161 else {
162     $template->param(
163         shelfid => $shelfid,
164         url     => "/cgi-bin/koha/virtualshelves/sendshelf.pl",
165     );
166     output_html_with_http_headers $query, $cookie, $template->output;
167 }