synch'ing 2.2 and head
[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
9 use C4::Search;
10 use C4::Auth;
11 use C4::Interface::CGI::Output;
12 use HTML::Template;
13
14 my $query = new CGI;
15
16 my ($template, $borrowernumber, $cookie) 
17     = get_template_and_user({template_name => "opac-sendbasketform.tmpl",
18                              query => $query,
19                              type => "opac",
20                              authnotrequired => 1,
21                              flagsrequired => {borrow => 1},
22                          });
23
24 my $bib_list=$query->param('bib_list');
25 my $email_add=$query->param('email_add');
26 my $email_sender=$query->param('email_sender');
27
28 if ($email_add) {
29         my $email_from = C4::Context->preference('KohaAdminEmailAddress');
30
31         my %mail = (     To      => $email_add,
32                                                  From    => $email_from);
33
34         my ($template2, $borrowernumber, $cookie) 
35     = get_template_and_user({template_name => "opac-sendbasket.tmpl",
36                              query => $query,
37                              type => "opac",
38                              authnotrequired => 1,
39                              flagsrequired => {borrow => 1},
40                          });
41
42         my @bibs = split(/\//, $bib_list);
43         my @results;
44         my $iso2709;
45         foreach my $biblionumber (@bibs) {
46                 $template2->param(biblionumber => $biblionumber);
47
48                 my $dat = &bibdata($biblionumber);
49                 my ($authorcount, $addauthor) = &addauthor($biblionumber);
50                 my @items                     = &ItemInfo(undef, $biblionumber, 'opac');
51
52                 $dat->{'additional'}=$addauthor->[0]->{'author'};
53                 for (my $i = 1; $i < $authorcount; $i++) {
54                         $dat->{'additional'} .= "|" . $addauthor->[$i]->{'author'};
55                 }
56
57                 $dat->{'biblionumber'} = $biblionumber;
58                 $dat->{ITEM_RESULTS} = \@items;
59                 my $record = MARCgetbiblio($dbh,$bibid);
60                 $iso2709 .= $record->as_usmarc();
61
62                 push (@results, $dat);
63         }
64
65         my $resultsarray=\@results;
66         $template2->param(BIBLIO_RESULTS => $resultsarray,
67                                         email_sender => $email_sender);
68
69         # Getting template result
70         my $template_res = $template2->output();
71
72         # Analysing information and getting mail properties
73         if ($template_res =~ /<SUBJECT>\n(.*)\n<END_SUBJECT>/s) { $mail{'subject'} = $1; }
74         else { $mail{'subject'} = "no subject"; }
75
76         my $email_header = "";
77         if ($template_res =~ /<HEADER>\n(.*)\n<END_HEADER>/s) { $email_header = $1; }
78
79         my $email_file = "basket.txt";
80         if ($template_res =~ /<FILENAME>\n(.*)\n<END_FILENAME>/s) { $email_file = $1; }
81
82         if ($template_res =~ /<MESSAGE>\n(.*)\n<END_MESSAGE>/s) { $mail{'body'} = $1; }
83
84         my $boundary = "====" . time() . "====";
85         $mail{'content-type'} = "multipart/mixed; boundary=\"$boundary\"";
86
87         $email_header = encode_qp($email_header);
88
89         $boundary = "--".$boundary;
90
91         # Writing mail
92         $mail{body} = <<END_OF_BODY;
93 $boundary
94 Content-Type: text/plain; charset="iso-8859-1"
95 Content-Transfer-Encoding: quoted-printable
96
97 $email_header
98
99 $mail{'body'}
100
101 $boundary--
102 END_OF_BODY
103
104 $mail{PJ} = $iso2709;
105 #       $mail{body} = <<END_OF_BODY;
106 #$boundary
107 #Content-Type: text/plain; charset="iso-8859-1"
108 #Content-Transfer-Encoding: quoted-printable
109 #
110 #$email_header
111 #
112 #$boundary
113 #Content-Type: text/plain; name="$email_file"
114 #Content-Transfer-Encoding: quoted-printable
115 #Content-Disposition: attachment; filename="$email_file"
116 #
117 #$mail{'body'}
118 #
119 #$boundary--
120 #END_OF_BODY
121
122         # Sending mail
123         if (sendmail %mail) {
124         # do something if it works....
125                 warn "Mail sent ok\n";
126                 $template->param(SENT => "1");
127                 $template->param(email_add => $email_add);
128         } else {
129                 # do something if it doesnt work....
130                 warn "Error sending mail: $Mail::Sendmail::error \n";
131         }
132
133         output_html_with_http_headers $query, $cookie, $template->output;
134 }
135 else {
136         $template->param(bib_list => $bib_list);
137         $template->param(url => "/cgi-bin/koha/opac-sendbasket.pl",
138         suggestion => C4::Context->preference("suggestion"),
139         virtualshelves => C4::Context->preference("virtualshelves"),
140         );
141         output_html_with_http_headers $query, $cookie, $template->output;
142 }