Bug 11349: Change .tmpl -> .tt in scripts using templates
[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 under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 2 of the License, or (at your option) any later
8 # version.
9 #
10 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License along with
15 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
16 # Suite 330, Boston, MA  02111-1307 USA
17
18 use strict;
19 use warnings;
20
21 use CGI;
22 use Encode qw(encode);
23 use Carp;
24
25 use Mail::Sendmail;
26 use MIME::QuotedPrint;
27 use MIME::Base64;
28 use C4::Biblio;
29 use C4::Items;
30 use C4::Auth;
31 use C4::Output;
32 use C4::Biblio;
33
34 my $query = new CGI;
35
36 my ( $template, $borrowernumber, $cookie ) = get_template_and_user (
37     {
38         template_name   => "basket/sendbasketform.tt",
39         query           => $query,
40         type            => "intranet",
41         authnotrequired => 0,
42         flagsrequired   => { borrow => 1 },
43     }
44 );
45
46 my $bib_list     = $query->param('bib_list');
47 my $email_add    = $query->param('email_add');
48 my $email_sender = $query->param('email_sender');
49
50 my $dbh          = C4::Context->dbh;
51
52 if ( $email_add ) {
53     my $email_from = C4::Context->preference('KohaAdminEmailAddress');
54     my $comment    = $query->param('comment');
55     my %mail = (
56         To   => $email_add,
57         From => $email_from
58     );
59
60     my ( $template2, $borrowernumber, $cookie ) = get_template_and_user(
61         {
62             template_name   => "basket/sendbasket.tt",
63             query           => $query,
64             type            => "intranet",
65             authnotrequired => 0,
66             flagsrequired   => { borrow => 1 },
67         }
68     );
69
70     my @bibs = split( /\//, $bib_list );
71     my @results;
72     my $iso2709;
73     my $marcflavour = C4::Context->preference('marcflavour');
74     foreach my $biblionumber (@bibs) {
75         $template2->param( biblionumber => $biblionumber );
76
77         my $dat              = GetBiblioData($biblionumber);
78         my $record           = GetMarcBiblio($biblionumber);
79         my $marcnotesarray   = GetMarcNotes( $record, $marcflavour );
80         my $marcauthorsarray = GetMarcAuthors( $record, $marcflavour );
81         my $marcsubjctsarray = GetMarcSubjects( $record, $marcflavour );
82
83         my @items = GetItemsInfo( $biblionumber );
84
85         my $hasauthors = 0;
86         if($dat->{'author'} || @$marcauthorsarray) {
87           $hasauthors = 1;
88         }
89         
90
91         $dat->{MARCNOTES}      = $marcnotesarray;
92         $dat->{MARCSUBJCTS}    = $marcsubjctsarray;
93         $dat->{MARCAUTHORS}    = $marcauthorsarray;
94         $dat->{HASAUTHORS}     = $hasauthors;
95         $dat->{'biblionumber'} = $biblionumber;
96         $dat->{ITEM_RESULTS}   = \@items;
97
98         $iso2709 .= $record->as_usmarc();
99
100         push( @results, $dat );
101     }
102
103     my $resultsarray = \@results;
104     $template2->param(
105         BIBLIO_RESULTS => $resultsarray,
106         email_sender   => $email_sender,
107         comment        => $comment
108     );
109
110     # Getting template result
111     my $template_res = $template2->output();
112     my $body;
113
114     # Analysing information and getting mail properties
115     if ( $template_res =~ /<SUBJECT>(.*)<END_SUBJECT>/s ) {
116         $mail{subject} = $1;
117         $mail{subject} =~ s|\n?(.*)\n?|$1|;
118     }
119     else { $mail{'subject'} = "no subject"; }
120
121     my $email_header = "";
122     if ( $template_res =~ /<HEADER>(.*)<END_HEADER>/s ) {
123         $email_header = $1;
124         $email_header =~ s|\n?(.*)\n?|$1|;
125         $email_header = encode_qp($email_header);
126     }
127
128     my $email_file = "basket.txt";
129     if ( $template_res =~ /<FILENAME>(.*)<END_FILENAME>/s ) {
130         $email_file = $1;
131         $email_file =~ s|\n?(.*)\n?|$1|;
132     }
133
134     if ( $template_res =~ /<MESSAGE>(.*)<END_MESSAGE>/s ) {
135         $body = $1;
136         $body =~ s|\n?(.*)\n?|$1|;
137         $body = encode_qp($body);
138     }
139
140     my $boundary = "====" . time() . "====";
141
142     # Writing mail
143     $mail{'content-type'} = "multipart/mixed; boundary=\"$boundary\"";
144     my $isofile = encode_base64(encode("UTF-8", $iso2709));
145     $boundary = '--' . $boundary;
146     $mail{body} = <<END_OF_BODY;
147 $boundary
148 Content-Type: text/plain; charset="utf-8"
149 Content-Transfer-Encoding: quoted-printable
150
151 $email_header
152 $body
153 $boundary
154 Content-Type: application/octet-stream; name="basket.iso2709"
155 Content-Transfer-Encoding: base64
156 Content-Disposition: attachment; filename="basket.iso2709"
157
158 $isofile
159 $boundary--
160 END_OF_BODY
161
162     # Sending mail
163     if ( sendmail %mail ) {
164         # do something if it works....
165         $template->param( SENT      => "1" );
166     }
167     else {
168         # do something if it doesnt work....
169         carp "Error sending mail: $Mail::Sendmail::error \n";
170         $template->param( error => 1 );
171     }
172     $template->param( email_add => $email_add );
173     output_html_with_http_headers $query, $cookie, $template->output;
174 }
175 else {
176     $template->param( bib_list => $bib_list );
177     $template->param(
178         url            => "/cgi-bin/koha/basket/sendbasket.pl",
179         suggestion     => C4::Context->preference("suggestion"),
180         virtualshelves => C4::Context->preference("virtualshelves"),
181     );
182     output_html_with_http_headers $query, $cookie, $template->output;
183 }