Bug 29603: Fix responsive behavior of facets menu in OPAC search results
[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 qw(
31     output_html_with_http_headers
32     output_and_exit
33 );
34 use Koha::Email;
35 use Koha::Virtualshelves;
36
37 my $query = CGI->new;
38
39 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
40     {
41         template_name   => "virtualshelves/sendshelfform.tt",
42         query           => $query,
43         type            => "intranet",
44         flagsrequired   => { catalogue => 1 },
45     }
46 );
47
48 my $shelfid    = $query->param('shelfid');
49 my $to_address = $query->param('email');
50
51 my $shelf = Koha::Virtualshelves->find( $shelfid );
52
53 output_and_exit( $query, $cookie, $template, 'insufficient_permission' )
54     if $shelf && !$shelf->can_be_viewed( $loggedinuser );
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 $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 $dat              = GetBiblioData($biblionumber);
76         my $record           = GetMarcBiblio({
77             biblionumber => $biblionumber,
78             embed_items  => 1 });
79         my $marcauthorsarray = GetMarcAuthors( $record, $marcflavour );
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 }