3c536050692d869ba31704deea416c8a8ed7d4ff
[koha.git] / basket / sendbasket.pl
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 use Modern::Perl;
19
20 use CGI qw ( -utf8 );
21 use Encode;
22 use Carp qw( carp );
23 use Try::Tiny qw( catch try );
24
25 use C4::Biblio qw(
26     GetMarcBiblio
27     GetMarcSubjects
28 );
29 use C4::Items qw( GetItemsInfo );
30 use C4::Auth qw( get_template_and_user );
31 use C4::Output qw( output_and_exit output_html_with_http_headers );
32 use C4::Templates;
33 use Koha::Email;
34 use Koha::Token;
35
36 my $query = CGI->new;
37
38 my ( $template, $borrowernumber, $cookie ) = get_template_and_user (
39     {
40         template_name   => "basket/sendbasketform.tt",
41         query           => $query,
42         type            => "intranet",
43         flagsrequired   => { catalogue => 1 },
44     }
45 );
46
47 my $bib_list  = $query->param('bib_list') || '';
48 my $email_add = $query->param('email_add');
49
50 my $dbh = C4::Context->dbh;
51
52 if ( $email_add ) {
53     output_and_exit( $query, $cookie, $template, 'wrong_csrf_token' )
54         unless Koha::Token->new->check_csrf({
55             session_id => scalar $query->cookie('CGISESSID'),
56             token  => scalar $query->param('csrf_token'),
57         });
58     my $comment = $query->param('comment');
59
60     # Since we are already logged in, no need to check credentials again
61     # when loading a second template.
62     my $template2 = C4::Templates::gettemplate(
63         'basket/sendbasket.tt', 'intranet', $query,
64     );
65
66     my @bibs = split( /\//, $bib_list );
67     my @results;
68     my $iso2709;
69     my $marcflavour = C4::Context->preference('marcflavour');
70     foreach my $biblionumber (@bibs) {
71         $template2->param( biblionumber => $biblionumber );
72
73         my $biblio           = Koha::Biblios->find( $biblionumber ) or next;
74         my $dat              = $biblio->unblessed;
75         my $record           = GetMarcBiblio({
76             biblionumber => $biblionumber,
77             embed_items => 1 });
78         my $marcauthorsarray = $biblio->get_marc_authors;
79         my $marcsubjctsarray = GetMarcSubjects( $record, $marcflavour );
80
81         my @items = GetItemsInfo( $biblionumber );
82
83         my $hasauthors = 0;
84         if($dat->{'author'} || @$marcauthorsarray) {
85           $hasauthors = 1;
86         }
87         
88
89         $dat->{MARCSUBJCTS}    = $marcsubjctsarray;
90         $dat->{MARCAUTHORS}    = $marcauthorsarray;
91         $dat->{HASAUTHORS}     = $hasauthors;
92         $dat->{'biblionumber'} = $biblionumber;
93         $dat->{ITEM_RESULTS}   = \@items;
94
95         $iso2709 .= $record->as_usmarc();
96
97         push( @results, $dat );
98     }
99
100     my $resultsarray = \@results;
101     $template2->param(
102         BIBLIO_RESULTS => $resultsarray,
103         comment        => $comment
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
138         my $email = Koha::Email->create(
139             {
140                 to      => $email_add,
141                 subject => $subject,
142             }
143         );
144
145         $email->text_body( $THE_body );
146         $email->attach(
147             Encode::encode( "UTF-8", $iso2709 ),
148             content_type => 'application/octet-stream',
149             name         => 'basket.iso2709',
150             disposition  => 'attachment',
151         );
152
153         my $library = Koha::Patrons->find( $borrowernumber )->library;
154         $email->send_or_die({ transport => $library->smtp_server->transport });
155         $template->param( SENT => "1" );
156     }
157     catch {
158         carp "Error sending mail: $_";
159         $template->param( error => 1 );
160     };
161
162     $template->param( email_add => $email_add );
163     output_html_with_http_headers $query, $cookie, $template->output;
164 }
165 else {
166     $template->param(
167         bib_list       => $bib_list,
168         url            => "/cgi-bin/koha/basket/sendbasket.pl",
169         suggestion     => C4::Context->preference("suggestion"),
170         virtualshelves => C4::Context->preference("virtualshelves"),
171         csrf_token     => Koha::Token->new->generate_csrf({ session_id => scalar $query->cookie('CGISESSID'), }),
172     );
173     output_html_with_http_headers $query, $cookie, $template->output;
174 }