synch'ing 2.2 and head
[koha_ffzg] / opac / opac-sendbasket.pl
1 #!/usr/bin/perl
2 use strict;
3 require Exporter;
4 use CGI;
5 use Mail::Sendmail;
6 use MIME::QuotedPrint;
7 use MIME::Base64;
8 use C4::Context;
9 use C4::Search;
10 use C4::Biblio;
11 use C4::Auth;
12 use C4::Interface::CGI::Output;
13 use C4::Biblio;
14 use HTML::Template;
15
16 my $query = new CGI;
17
18 my ($template, $borrowernumber, $cookie) 
19     = get_template_and_user({template_name => "opac-sendbasketform.tmpl",
20                              query => $query,
21                              type => "opac",
22                              authnotrequired => 1,
23                              flagsrequired => {borrow => 1},
24                          });
25
26 my $bib_list=$query->param('bib_list');
27 my $email_add=$query->param('email_add');
28 my $email_sender=$query->param('email_sender');
29 my $dbh=C4::Context->dbh;
30 my $sth;
31 $sth=$dbh->prepare("select bibid from marc_biblio where biblionumber=? order by bibid");
32
33
34 if ($email_add) {
35         my $email_from = C4::Context->preference('KohaAdminEmailAddress');
36
37         my %mail = (     To      => $email_add,
38                                                  From    => $email_from);
39
40         my ($template2, $borrowernumber, $cookie) 
41     = get_template_and_user({template_name => "opac-sendbasket.tmpl",
42                              query => $query,
43                              type => "opac",
44                              authnotrequired => 1,
45                              flagsrequired => {borrow => 1},
46                          });
47
48         my @bibs = split(/\//, $bib_list);
49         my @results;
50         my $iso2709;
51         foreach my $biblionumber (@bibs) {
52                 $template2->param(biblionumber => $biblionumber);
53
54                 my $dat = &bibdata($biblionumber);
55                 my ($authorcount, $addauthor) = &addauthor($biblionumber);
56                 my @items                     = &ItemInfo(undef, $biblionumber, 'opac');
57
58                 $dat->{'additional'}=$addauthor->[0]->{'author'};
59                 for (my $i = 1; $i < $authorcount; $i++) {
60                         $dat->{'additional'} .= "|" . $addauthor->[$i]->{'author'};
61                 }
62
63                 $dat->{'biblionumber'} = $biblionumber;
64                 $dat->{ITEM_RESULTS} = \@items;
65                 $sth->execute($biblionumber);
66                 my ($bibid) = $sth->fetchrow;
67                 my $record = MARCgetbiblio($dbh,$bibid);
68                 $iso2709 .= $record->as_usmarc();
69
70                 push (@results, $dat);
71         }
72
73     $email_header = encode_qp($email_header);
74
75     $boundary = "--" . $boundary;
76
77         # Analysing information and getting mail properties
78         if ($template_res =~ /<SUBJECT>\n(.*)\n<END_SUBJECT>/s) { $mail{'subject'} = $1; }
79         else { $mail{'subject'} = "no subject"; }
80
81         my $email_header = "";
82         if ($template_res =~ /<HEADER>\n(.*)\n<END_HEADER>/s) { $email_header = $1; }
83
84         my $email_file = "basket.txt";
85         if ($template_res =~ /<FILENAME>\n(.*)\n<END_FILENAME>/s) { $email_file = $1; }
86
87         if ($template_res =~ /<MESSAGE>\n(.*)\n<END_MESSAGE>/s) { $mail{'body'} = $1; }
88
89         my $boundary = "====" . time() . "====";
90 #       $mail{'content-type'} = "multipart/mixed; boundary=\"$boundary\"";
91
92 #       $email_header = encode_qp($email_header);
93
94 #       $boundary = "--".$boundary;
95
96 #       # Writing mail
97 #       $mail{body} =
98         $mail{'content-type'} = "multipart/mixed; boundary=\"$boundary\"";
99
100         my $message = encode_qp( "" );
101
102 # $file = $^X; # This is the perl executable
103
104 # open (F, $file) or die "Cannot read $file: $!";
105 # binmode F; undef $/;
106 $mail{body} = encode_base64($iso2709);
107 # close F;
108
109 $boundary = '--'.$boundary;
110 $mail{body} = <<END_OF_BODY;
111 $boundary
112 Content-Type: text/plain; charset="iso-8859-1"
113 Content-Transfer-Encoding: quoted-printable
114
115 $message
116 $boundary
117 Content-Type: application/octet-stream; name="basket"
118 Content-Transfer-Encoding: base64
119 Content-Disposition: attachment; filename="basket"
120
121 $mail{body}
122 $boundary--
123 END_OF_BODY
124
125
126
127          <<END_OF_BODY;
128 $boundary
129 Content-Type: text/plain; charset="iso-8859-1"
130 Content-Transfer-Encoding: quoted-printable
131
132 $email_header
133
134 $mail{'body'}
135
136 $boundary--
137 END_OF_BODY
138
139 $mail{attachment} = $iso2709;
140 #       $mail{body} = <<END_OF_BODY;
141 #$boundary
142 #Content-Type: text/plain; charset="iso-8859-1"
143 #Content-Transfer-Encoding: quoted-printable
144 #
145 #$email_header
146 #
147 #$boundary
148 #Content-Type: text/plain; name="$email_file"
149 #Content-Transfer-Encoding: quoted-printable
150 #Content-Disposition: attachment; filename="$email_file"
151 #
152 #$mail{'body'}
153 #
154 #$boundary--
155 #END_OF_BODY
156
157         # Sending mail
158         if (sendmail %mail) {
159         # do something if it works....
160 #               warn " ".$mail{body};
161 #               warn " ".$mail{PJ};
162                 $template->param(SENT => "1");
163                 $template->param(email_add => $email_add);
164         } else {
165                 # do something if it doesnt work....
166                 warn "Error sending mail: $Mail::Sendmail::error \n";
167         }
168
169     output_html_with_http_headers $query, $cookie, $template->output;
170 }
171 else {
172         $template->param(bib_list => $bib_list);
173         $template->param(url => "/cgi-bin/koha/opac-sendbasket.pl",
174         suggestion => C4::Context->preference("suggestion"),
175         virtualshelves => C4::Context->preference("virtualshelves"),
176         );
177         output_html_with_http_headers $query, $cookie, $template->output;
178 }