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