|
Lines 25-31
use C4::Context;
Link Here
|
| 25 |
|
25 |
|
| 26 |
use Getopt::Long qw(:config auto_help auto_version); |
26 |
use Getopt::Long qw(:config auto_help auto_version); |
| 27 |
use Pod::Usage; |
27 |
use Pod::Usage; |
| 28 |
use Mail::Sendmail; |
28 |
use MIME::Lite; |
| 29 |
use Text::CSV_XS; |
29 |
use Text::CSV_XS; |
| 30 |
use CGI; |
30 |
use CGI; |
| 31 |
use Carp; |
31 |
use Carp; |
|
Lines 148-153
my $from = "";
Link Here
|
| 148 |
my $subject = 'Koha Saved Report'; |
148 |
my $subject = 'Koha Saved Report'; |
| 149 |
my $separator = ','; |
149 |
my $separator = ','; |
| 150 |
my $quote = '"'; |
150 |
my $quote = '"'; |
|
|
151 |
my $message = ""; |
| 152 |
my $mail; |
| 153 |
my $outfile; |
| 151 |
|
154 |
|
| 152 |
GetOptions( |
155 |
GetOptions( |
| 153 |
'help|?' => \$help, |
156 |
'help|?' => \$help, |
|
Lines 168-178
unless ($format) {
Link Here
|
| 168 |
$format = 'text'; |
171 |
$format = 'text'; |
| 169 |
} |
172 |
} |
| 170 |
|
173 |
|
| 171 |
if ($format eq 'tsv' || $format eq 'text') { |
|
|
| 172 |
$format = 'csv'; |
| 173 |
$separator = "\t"; |
| 174 |
} |
| 175 |
|
| 176 |
if ($to or $from or $email) { |
174 |
if ($to or $from or $email) { |
| 177 |
$email = 1; |
175 |
$email = 1; |
| 178 |
$from or $from = C4::Context->preference('KohaAdminEmailAddress'); |
176 |
$from or $from = C4::Context->preference('KohaAdminEmailAddress'); |
|
Lines 187-198
unless (scalar(@ARGV)) {
Link Here
|
| 187 |
|
185 |
|
| 188 |
|
186 |
|
| 189 |
foreach my $report (@ARGV) { |
187 |
foreach my $report (@ARGV) { |
| 190 |
my ($sql, $type) = get_saved_report($report); |
188 |
my ($sql, $type, $report_name) = get_saved_report($report); |
| 191 |
unless ($sql) { |
189 |
unless ($sql) { |
| 192 |
carp "ERROR: No saved report $report found"; |
190 |
carp "ERROR: No saved report $report found"; |
| 193 |
next; |
191 |
next; |
| 194 |
} |
192 |
} |
| 195 |
$verbose and print "SQL: $sql\n\n"; |
193 |
$verbose and print "SQL: $sql\n\n"; |
|
|
194 |
if (defined($report_name) and $report_name ne "") |
| 195 |
{ |
| 196 |
$subject = $report_name ; |
| 197 |
} |
| 198 |
else |
| 199 |
{ |
| 200 |
$subject = 'Koha Saved Report'; |
| 201 |
} |
| 196 |
# my $results = execute_query($sql, undef, 0, 99999, $format, $report); |
202 |
# my $results = execute_query($sql, undef, 0, 99999, $format, $report); |
| 197 |
my ($sth) = execute_query($sql); |
203 |
my ($sth) = execute_query($sql); |
| 198 |
# execute_query(sql, , 0, 20, , ) |
204 |
# execute_query(sql, , 0, 20, , ) |
|
Lines 203-210
foreach my $report (@ARGV) {
Link Here
|
| 203 |
} |
209 |
} |
| 204 |
$verbose and print "$count results from execute_query\n"; |
210 |
$verbose and print "$count results from execute_query\n"; |
| 205 |
|
211 |
|
| 206 |
my $message; |
212 |
if ($format eq 'text') { |
| 207 |
if ($format eq 'html') { |
|
|
| 208 |
my $cgi = CGI->new(); |
213 |
my $cgi = CGI->new(); |
| 209 |
my @rows = (); |
214 |
my @rows = (); |
| 210 |
while (my $line = $sth->fetchrow_arrayref) { |
215 |
while (my $line = $sth->fetchrow_arrayref) { |
|
Lines 212-217
foreach my $report (@ARGV) {
Link Here
|
| 212 |
push @rows, $cgi->TR( join('', $cgi->td($line)) ) . "\n"; |
217 |
push @rows, $cgi->TR( join('', $cgi->td($line)) ) . "\n"; |
| 213 |
} |
218 |
} |
| 214 |
$message = $cgi->table(join "", @rows); |
219 |
$message = $cgi->table(join "", @rows); |
|
|
220 |
if ( $email ) { |
| 221 |
$mail = MIME::Lite->new ( |
| 222 |
To => $to, |
| 223 |
From => $from, |
| 224 |
Subject => $subject, |
| 225 |
Data => $message, |
| 226 |
Type => 'text/html' |
| 227 |
); |
| 228 |
} |
| 215 |
} elsif ($format eq 'csv') { |
229 |
} elsif ($format eq 'csv') { |
| 216 |
my $csv = Text::CSV_XS->new({ |
230 |
my $csv = Text::CSV_XS->new({ |
| 217 |
quote_char => $quote, |
231 |
quote_char => $quote, |
|
Lines 227-245
foreach my $report (@ARGV) {
Link Here
|
| 227 |
# $message .= join ($separator, @$line) . "\n"; |
241 |
# $message .= join ($separator, @$line) . "\n"; |
| 228 |
$message .= $csv->string() . "\n"; |
242 |
$message .= $csv->string() . "\n"; |
| 229 |
} |
243 |
} |
|
|
244 |
if ($email) { |
| 245 |
$outfile = "/tmp/report$report.csv"; |
| 246 |
open( my $fh, ">:encoding(UTF-8)", $outfile ) or die "unable to open $outfile: $!"; |
| 247 |
print $fh $message; |
| 248 |
close $fh; |
| 249 |
|
| 250 |
$mail = MIME::Lite->new ( |
| 251 |
From => $from, |
| 252 |
To => $to, |
| 253 |
Subject => $subject, |
| 254 |
Type => 'multipart/mixed', |
| 255 |
); |
| 256 |
$mail->attach ( |
| 257 |
Type => 'application/csv', |
| 258 |
Path => $outfile, |
| 259 |
Filename => "report$report.csv", |
| 260 |
); |
| 261 |
} |
| 230 |
} |
262 |
} |
| 231 |
|
263 |
|
| 232 |
if ($email){ |
264 |
if ($email){ |
| 233 |
my %mail = ( |
265 |
$mail->send or carp 'mail not sent:'; |
| 234 |
To => $to, |
266 |
unlink $outfile if ($outfile); |
| 235 |
From => $from, |
|
|
| 236 |
Subject => $subject, |
| 237 |
Message => $message |
| 238 |
); |
| 239 |
sendmail(%mail) or carp 'mail not sent:' . $Mail::Sendmail::error; |
| 240 |
} else { |
267 |
} else { |
| 241 |
print $message; |
268 |
print $message; |
| 242 |
} |
269 |
} |
|
|
270 |
|
| 243 |
# my @xmlarray = ... ; |
271 |
# my @xmlarray = ... ; |
| 244 |
# my $url = "/cgi-bin/koha/reports/guided_reports.pl?phase=retrieve%20results&id=$id"; |
272 |
# my $url = "/cgi-bin/koha/reports/guided_reports.pl?phase=retrieve%20results&id=$id"; |
| 245 |
# my $xml = XML::Dumper->new()->pl2xml( \@xmlarray ); |
273 |
# my $xml = XML::Dumper->new()->pl2xml( \@xmlarray ); |