Bug 17109: Use Koha.Preference in sendbasket template
[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 qw(encode);
22 use Carp;
23 use Digest::MD5 qw(md5_base64);
24 use Mail::Sendmail;
25 use MIME::QuotedPrint;
26 use MIME::Base64;
27
28 use C4::Biblio;
29 use C4::Items;
30 use C4::Auth;
31 use C4::Output;
32 use C4::Templates ();
33 use Koha::Email;
34 use Koha::Token;
35
36 my $query = new CGI;
37
38 my ( $template, $borrowernumber, $cookie ) = get_template_and_user (
39     {
40         template_name   => "basket/sendbasketform.tt",
41         query           => $query,
42         type            => "intranet",
43         authnotrequired => 0,
44         flagsrequired   => { catalogue => 1 },
45     }
46 );
47
48 my $bib_list     = $query->param('bib_list') || '';
49 my $email_add    = $query->param('email_add');
50
51 my $dbh          = C4::Context->dbh;
52
53 my $csrf_err;
54 if ( $email_add ) {
55     $csrf_err = 1 unless Koha::Token->new->check_csrf({
56         id     => C4::Context->userenv->{id},
57         secret => md5_base64( C4::Context->config('pass') ),
58         token  => scalar $query->param('csrf_token'),
59     });
60 }
61
62 if( $csrf_err ) {
63     $template->param( csrf_error => 1, email_add => 1 );
64     output_html_with_http_headers $query, $cookie, $template->output;
65 } elsif ( $email_add ) {
66     my $email = Koha::Email->new();
67     my %mail = $email->create_message_headers({ to => $email_add });
68     my $comment    = $query->param('comment');
69
70     # Since we are already logged in, no need to check credentials again
71     # when loading a second template.
72     my $template2 = C4::Templates::gettemplate(
73         'basket/sendbasket.tt', 'intranet', $query,
74     );
75
76     my @bibs = split( /\//, $bib_list );
77     my @results;
78     my $iso2709;
79     my $marcflavour = C4::Context->preference('marcflavour');
80     foreach my $biblionumber (@bibs) {
81         $template2->param( biblionumber => $biblionumber );
82
83         my $dat              = GetBiblioData($biblionumber);
84         next unless $dat;
85         my $record           = GetMarcBiblio($biblionumber, 1);
86         my $marcauthorsarray = GetMarcAuthors( $record, $marcflavour );
87         my $marcsubjctsarray = GetMarcSubjects( $record, $marcflavour );
88
89         my @items = GetItemsInfo( $biblionumber );
90
91         my $hasauthors = 0;
92         if($dat->{'author'} || @$marcauthorsarray) {
93           $hasauthors = 1;
94         }
95         
96
97         $dat->{MARCSUBJCTS}    = $marcsubjctsarray;
98         $dat->{MARCAUTHORS}    = $marcauthorsarray;
99         $dat->{HASAUTHORS}     = $hasauthors;
100         $dat->{'biblionumber'} = $biblionumber;
101         $dat->{ITEM_RESULTS}   = \@items;
102
103         $iso2709 .= $record->as_usmarc();
104
105         push( @results, $dat );
106     }
107
108     my $resultsarray = \@results;
109     $template2->param(
110         BIBLIO_RESULTS => $resultsarray,
111         comment        => $comment
112     );
113
114     # Getting template result
115     my $template_res = $template2->output();
116     my $body;
117
118     # Analysing information and getting mail properties
119     if ( $template_res =~ /<SUBJECT>(.*)<END_SUBJECT>/s ) {
120         $mail{subject} = $1;
121         $mail{subject} =~ s|\n?(.*)\n?|$1|;
122         $mail{subject} = Encode::encode("UTF-8", $mail{subject});
123     }
124     else { $mail{'subject'} = "no subject"; }
125
126     my $email_header = "";
127     if ( $template_res =~ /<HEADER>(.*)<END_HEADER>/s ) {
128         $email_header = $1;
129         $email_header =~ s|\n?(.*)\n?|$1|;
130         $email_header = encode_qp(Encode::encode("UTF-8", $email_header));
131     }
132
133     my $email_file = "basket.txt";
134     if ( $template_res =~ /<FILENAME>(.*)<END_FILENAME>/s ) {
135         $email_file = $1;
136         $email_file =~ s|\n?(.*)\n?|$1|;
137     }
138
139     if ( $template_res =~ /<MESSAGE>(.*)<END_MESSAGE>/s ) {
140         $body = $1;
141         $body =~ s|\n?(.*)\n?|$1|;
142         $body = encode_qp(Encode::encode("UTF-8", $body));
143     }
144
145     my $boundary = "====" . time() . "====";
146
147     # Writing mail
148     $mail{'content-type'} = "multipart/mixed; boundary=\"$boundary\"";
149     my $isofile = encode_base64(encode("UTF-8", $iso2709));
150     $boundary = '--' . $boundary;
151     $mail{body} = <<END_OF_BODY;
152 $boundary
153 Content-Type: text/plain; charset="utf-8"
154 Content-Transfer-Encoding: quoted-printable
155
156 $email_header
157 $body
158 $boundary
159 Content-Type: application/octet-stream; name="basket.iso2709"
160 Content-Transfer-Encoding: base64
161 Content-Disposition: attachment; filename="basket.iso2709"
162
163 $isofile
164 $boundary--
165 END_OF_BODY
166
167     # Sending mail
168     if ( sendmail %mail ) {
169         # do something if it works....
170         $template->param( SENT      => "1" );
171     }
172     else {
173         # do something if it doesnt work....
174         carp "Error sending mail: $Mail::Sendmail::error \n";
175         $template->param( error => 1 );
176     }
177     $template->param( email_add => $email_add );
178     output_html_with_http_headers $query, $cookie, $template->output;
179 }
180 else {
181     $template->param(
182         bib_list       => $bib_list,
183         url            => "/cgi-bin/koha/basket/sendbasket.pl",
184         suggestion     => C4::Context->preference("suggestion"),
185         virtualshelves => C4::Context->preference("virtualshelves"),
186         csrf_token     => Koha::Token->new->generate_csrf(
187             {   id     => C4::Context->userenv->{id},
188                 secret => md5_base64( C4::Context->config('pass') ),
189             }
190         ),
191     );
192     output_html_with_http_headers $query, $cookie, $template->output;
193 }