item rework: moved various accessor functions
[koha.git] / opac / opac-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 require Exporter;
20 use CGI;
21 use Mail::Sendmail;
22 use MIME::QuotedPrint;
23 use MIME::Base64;
24 use C4::Biblio;
25 use C4::Items;
26 use C4::Auth;
27 use C4::Output;
28 use C4::Biblio;
29
30 my $query = new CGI;
31
32 my ( $template, $borrowernumber, $cookie ) = get_template_and_user (
33     {
34         template_name   => "opac-sendbasketform.tmpl",
35         query           => $query,
36         type            => "opac",
37         authnotrequired => 1,
38         flagsrequired   => { borrow => 1 },
39     }
40 );
41
42 my $bib_list     = $query->param('bib_list');
43 my $email_add    = $query->param('email_add');
44 my $email_sender = $query->param('email_sender');
45
46 my $dbh          = C4::Context->dbh;
47
48 if ( $email_add ) {
49     my $email_from = C4::Context->preference('KohaAdminEmailAddress');
50     my $comment    = $query->param('comment');
51     my %mail = (
52         To   => $email_add,
53         From => $email_from
54     );
55
56     my ( $template2, $borrowernumber, $cookie ) = get_template_and_user(
57         {
58             template_name   => "opac-sendbasket.tmpl",
59             query           => $query,
60             type            => "opac",
61             authnotrequired => 1,
62             flagsrequired   => { borrow => 1 },
63         }
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 $dat              = GetBiblioData($biblionumber);
74         my $record           = GetMarcBiblio($biblionumber);
75         my $marcnotesarray   = GetMarcNotes( $record, $marcflavour );
76         my $marcauthorsarray = GetMarcAuthors( $record, $marcflavour );
77         my $marcsubjctsarray = GetMarcSubjects( $record, $marcflavour );
78
79         my @items = &GetItemsInfo( $biblionumber, 'opac' );
80
81         $dat->{MARCNOTES}      = $marcnotesarray;
82         $dat->{MARCSUBJCTS}    = $marcsubjctsarray;
83         $dat->{MARCAUTHORS}    = $marcauthorsarray;
84         $dat->{'biblionumber'} = $biblionumber;
85         $dat->{ITEM_RESULTS}   = \@items;
86
87         $iso2709 .= $record->as_usmarc();
88
89         push( @results, $dat );
90     }
91
92     my $resultsarray = \@results;
93     $template2->param(
94         BIBLIO_RESULTS => $resultsarray,
95         email_sender   => $email_sender,
96         comment        => $comment
97     );
98
99     # Getting template result
100     my $template_res = $template2->output();
101     my $body;
102
103     # Analysing information and getting mail properties
104     if ( $template_res =~ /<SUBJECT>\n(.*)\n<END_SUBJECT>/s ) {
105         $mail{'subject'} = $1;
106     }
107     else { $mail{'subject'} = "no subject"; }
108
109     my $email_header = "";
110     if ( $template_res =~ /<HEADER>\n(.*)\n<END_HEADER>/s ) {
111         $email_header = $1;
112     }
113
114     my $email_file = "basket.txt";
115     if ( $template_res =~ /<FILENAME>\n(.*)\n<END_FILENAME>/s ) {
116         $email_file = $1;
117     }
118
119     if ( $template_res =~ /<MESSAGE>\n(.*)\n<END_MESSAGE>/s ) { $body = $1; }
120
121     my $boundary = "====" . time() . "====";
122
123     #     $mail{'content-type'} = "multipart/mixed; boundary=\"$boundary\"";
124     #
125     #     $email_header = encode_qp($email_header);
126     #
127     #     $boundary = "--".$boundary;
128     #
129     #     # Writing mail
130     #     $mail{body} =
131     $mail{'content-type'} = "multipart/mixed; boundary=\"$boundary\"";
132     my $isofile = encode_base64($iso2709);
133     $boundary = '--' . $boundary;
134     $mail{body} = <<END_OF_BODY;
135 $boundary
136 Content-Type: text/plain; charset="iso-8859-1"
137 Content-Transfer-Encoding: quoted-printable
138
139 $body
140 $boundary
141 Content-Type: application/octet-stream; name="basket.iso2709"
142 Content-Transfer-Encoding: base64
143 Content-Disposition: attachment; filename="basket.iso2709"
144
145 $isofile
146 $boundary--
147 END_OF_BODY
148
149     # Sending mail
150     if ( sendmail %mail ) {
151         # do something if it works....
152         $template->param( SENT      => "1" );
153     }
154     else {
155         # do something if it doesnt work....
156         warn "Error sending mail: $Mail::Sendmail::error \n";
157         $template->param( error => 1 );
158     }
159     $template->param( email_add => $email_add );
160     output_html_with_http_headers $query, $cookie, $template->output;
161 }
162 else {
163     $template->param( bib_list => $bib_list );
164     $template->param(
165         url            => "/cgi-bin/koha/opac-sendbasket.pl",
166         suggestion     => C4::Context->preference("suggestion"),
167         virtualshelves => C4::Context->preference("virtualshelves"),
168     );
169     output_html_with_http_headers $query, $cookie, $template->output;
170 }