use strict;
use CGI;
-use C4::Reports;
+use C4::Reports::Guided;
use C4::Auth;
use C4::Output;
+use C4::Dates;
+use C4::Debug;
+
=head1 NAME
Script to control the guided report creation
query => $input,
type => "intranet",
authnotrequired => 0,
- flagsrequired => { editcatalogue => 1 },
+ flagsrequired => { reports => 1 },
debug => 1,
}
);
$template->param( 'build1' => 1 );
# get report areas
- my $areas = C4::Reports::get_report_areas();
+ my $areas = get_report_areas();
$template->param( 'areas' => $areas );
}
);
# get report types
- my $types = C4::Reports::get_report_types();
+ my $types = get_report_types();
$template->param( 'types' => $types );
}
foreach my $crit (@criteria) {
my $value = $input->param( $crit . "_value" );
if ($value) {
+ if ($value =~ C4::Dates->regexp(C4::Context->preference('dateformat'))) {
+ my $date = C4::Dates->new($value);
+ $value = $date->output("iso");
+ }
$query_criteria .= " AND $crit='$value'";
}
}
'area' => $area,
'type' => $type,
'column' => $column,
- 'definition' => $definition,
+ 'definition' => $definition,
'criteriastring' => $query_criteria,
);
'column' => $column,
'criteriastring' => $criteria,
'totals' => $totals,
- 'definition' => $definition,
+ 'definition' => $definition,
);
# get columns
# save the sql pasted in by a user
my $sql = $input->param('sql');
my $name = $input->param('reportname');
- my $type = $input->param('type');
- my $notes = $input->param('notes');
- save_report( $sql, $name, $type, $notes );
- $template->param(
- 'save_successful' => 1,
- );
+ my $type = $input->param('types');
+ my $notes = $input->param('notes');
+ my @errors = ();
+ my $error = {};
+ if ($sql =~ /;?\W?(UPDATE|DELETE|DROP|INSERT|SHOW|CREATE)\W/i) {
+ $error->{'sqlerr'} = $1;
+ push @errors, $error;
+ }
+ elsif ($sql !~ /^(SELECT)/i) {
+ $error->{'queryerr'} = 1;
+ push @errors, $error;
+ }
+ if (@errors) {
+ $template->param(
+ 'save_successful' => 1,
+ 'errors' => \@errors,
+ 'sql' => $sql,
+ 'reportname'=> $name,
+ 'type' => $type,
+ 'notes' => $notes,
+ );
+ }
+ else {
+ save_report( $sql, $name, $type, $notes );
+ $template->param(
+ 'save_successful' => 1,
+ );
+ }
}
-elsif ( $phase eq 'Execute' ) {
- # run the sql, and output results in a template
- my $sql = $input->param('sql');
- my $type = $input->param('type');
- my $results = execute_query($sql,$type);
- $template->param(
- 'results' => $results,
- 'sql' => $sql,
- 'execute' => 1
- );
-}
+# This condition is not used currently
+#elsif ( $phase eq 'Execute' ) {
+# # run the sql, and output results in a template
+# my $sql = $input->param('sql');
+# my $type = $input->param('type');
+# my ($results, $total, $errors) = execute_query($sql, $type);
+# $template->param(
+# 'results' => $results,
+# 'sql' => $sql,
+# 'execute' => 1,
+# );
+#}
elsif ($phase eq 'Run this report'){
# execute a saved report
- my $report = $input->param('reports');
- my ($sql,$type,$name,$notes) = get_saved_report($report);
- my $results = execute_query($sql,$type);
+ # FIXME The default limit should not be hardcoded...
+ my $limit = 20;
+ my $offset;
+ my $report = $input->param('reports');
+ # offset algorithm
+ if ($input->param('page')) {
+ $offset = ($input->param('page') - 1) * 20;
+ }
+ else {
+ $offset = 0;
+ }
+ my ($sql,$type,$name,$notes) = get_saved_report($report);
+ my ($results, $total, $errors) = execute_query($sql, $type, $offset, $limit);
+ my $totpages = int($total/$limit) + (($total % $limit) > 0 ? 1 : 0);
+ my $url = "/cgi-bin/koha/reports/guided_reports.pl?reports=$report&phase=Run%20this%20report";
$template->param(
- 'results' => $results,
- 'sql' => $sql,
- 'execute' => 1,
- 'name' => $name,
- 'notes' => $notes,
+ 'results' => $results,
+ 'sql' => $sql,
+ 'execute' => 1,
+ 'name' => $name,
+ 'notes' => $notes,
+ 'pagination_bar' => pagination_bar($url, $totpages, $input->param('page'), "page"),
+ 'errors' => $errors,
);
}
elsif ($phase eq 'Export'){
+ binmode STDOUT, ':utf8';
+
# export results to tab separated text
- my $sql = $input->param('sql');
- $no_html=1;
- print $input->header( -type => 'application/octet-stream',
- -attachment=>'reportresults.csv');
- my $format=$input->param('format');
- my $results = execute_query($sql,1,$format);
- print $results;
-
+ my $sql = $input->param('sql');
+ my $format = $input->param('format');
+ my ($results, $total, $errors) = execute_query($sql,1,0,0,$format);
+ if ($#$errors == -1) {
+ $no_html=1;
+ print $input->header( -type => 'application/octet-stream',
+ -attachment=>'reportresults.csv'
+ );
+ print $results;
+ } else {
+ $template->param(
+ 'results' => $results,
+ 'sql' => $sql,
+ 'execute' => 1,
+ 'name' => 'Error exporting report!',
+ 'notes' => '',
+ 'pagination_bar' => '',
+ 'errors' => $errors,
+ );
+ }
}
-elsif ($phase eq 'Create report from SQL'){
- # alllow the user to paste in sql
+elsif ($phase eq 'Create report from SQL') {
+ # allow the user to paste in sql
+ if ($input->param('sql')) {
+ $template->param(
+ 'sql' => $input->param('sql'),
+ 'reportname' => $input->param('reportname'),
+ 'notes' => $input->param('notes'),
+ );
+ }
$template->param('create' => 1);
- my $types = C4::Reports::get_report_types();
+ my $types = get_report_types();
+ if (my $type = $input->param('type')) {
+ for my $i ( 0 .. $#{@$types}) {
+ @$types[$i]->{'selected'} = 1 if @$types[$i]->{'id'} eq $type;
+ }
+ }
$template->param( 'types' => $types );
}
}
-$template->param( 'referer' => $referer );
+$template->param( 'referer' => $referer,
+ 'DHTMLcalendar_dateformat' => C4::Dates->DHTMLcalendar(),
+ );
if (!$no_html){