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