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