Bug 27668: Improve validation of patron entry form in the OPAC
[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 = CGI->new;
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_header = "";
112     if ( $template_res =~ /<HEADER>(.*)<END_HEADER>/s ) {
113         $email_header = $1;
114         $email_header =~ s|\n?(.*)\n?|$1|;
115     }
116
117     if ( $template_res =~ /<MESSAGE>(.*)<END_MESSAGE>/s ) {
118         $body = $1;
119         $body =~ s|\n?(.*)\n?|$1|;
120     }
121
122     my $THE_body = <<END_OF_BODY;
123 $email_header
124 $body
125 END_OF_BODY
126
127     try {
128         my $email = Koha::Email->create(
129             {
130                 to      => $to_address,
131                 subject => $subject,
132             }
133         );
134         $email->text_body( $THE_body );
135         $email->attach(
136             Encode::encode("UTF-8", $iso2709),
137             content_type => 'application/octet-stream',
138             name         => 'shelf.iso2709',
139             disposition  => 'attachment',
140         );
141
142         my $library = Koha::Patrons->find( $borrowernumber )->library;
143         $email->send_or_die({ transport => $library->smtp_server->transport });
144         $template->param( SENT => "1" );
145     }
146     catch {
147         carp "Error sending mail: $_";
148         $template->param( error => 1 );
149     };
150
151     $template->param( email => $to_address );
152     output_html_with_http_headers $query, $cookie, $template->output;
153
154 }
155 else {
156     $template->param(
157         shelfid => $shelfid,
158         url     => "/cgi-bin/koha/virtualshelves/sendshelf.pl",
159     );
160     output_html_with_http_headers $query, $cookie, $template->output;
161 }