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