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