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