=cut
-my $input = new CGI;
+my $input = CGI->new;
my $usecache = Koha::Caches->get_instance->memcached_cache;
my $phase = $input->param('phase') // '';
my $flagsrequired;
-if ( ( $phase eq 'Build new' ) || ( $phase eq 'Create report from SQL' ) || ( $phase eq 'Edit SQL' ) ){
+if ( ( $phase eq 'Build new' ) || ( $phase eq 'Create report from SQL' ) || ( $phase eq 'Edit SQL' )
+ || ( $phase eq 'Build new from existing' ) ) {
$flagsrequired = 'create_reports';
}
elsif ( $phase eq 'Use saved' ) {
template_name => "reports/guided_reports_start.tt",
query => $input,
type => "intranet",
- authnotrequired => 0,
flagsrequired => { reports => $flagsrequired },
debug => 1,
}
my $cache_expiry_units = $input->param('cache_expiry_units');
my $public = $input->param('public');
my $save_anyway = $input->param('save_anyway');
-
my @errors;
# if we have the units, then we came from creating a report from SQL and thus need to handle converting units
} elsif ( $authorised_value eq "date" ) {
# require a date, provide a date picker
$input = 'date';
+ } elsif ( $authorised_value eq "list" ) {
+ # require a list, provide a textarea
+ $input = 'textarea';
} else {
# defined $authorised_value, and not 'date'
my $dbh=C4::Context->dbh;
'reports' => $report_id,
);
} else {
- my ($sql,$header_types) = get_prepped_report( $sql, \@param_names, \@sql_params);
+ my ($sql,$header_types) = $report->prep_report( \@param_names, \@sql_params );
$template->param(header_types => $header_types);
my ( $sth, $errors ) = execute_query( $sql, $offset, $limit, undef, $report_id );
my $total = nb_rows($sql) || 0;
my $reportname = $input->param('reportname');
my $reportfilename = $reportname ? "$reportname-reportresults.$format" : "reportresults.$format" ;
- ($sql, undef) = get_prepped_report( $sql, \@param_names, \@sql_params );
+ ($sql, undef) = $report->prep_report( \@param_names, \@sql_params );
my ($sth, $q_errors) = execute_query($sql);
unless ($q_errors and @$q_errors) {
my ( $type, $content );
$content .= join("\t", header_cell_values($sth)) . "\n";
$content = Encode::decode('UTF-8', $content);
while (my $row = $sth->fetchrow_arrayref()) {
- $content .= join("\t", @$row) . "\n";
+ $content .= join("\t", map { $_ // '' } @$row) . "\n";
}
} else {
- my $delimiter = C4::Context->preference('delimiter') || ',';
+ my $delimiter = C4::Context->preference('CSVDelimiter') || ',';
if ( $format eq 'csv' ) {
$delimiter = "\t" if $delimiter eq 'tabulation';
$type = 'application/csv';
);
}
-elsif ( $phase eq 'Create report from SQL' ) {
+elsif ( $phase eq 'Create report from SQL' || $phase eq 'Create report from existing' ) {
- my ($group, $subgroup);
- # allow the user to paste in sql
+ my ($group, $subgroup, $sql, $reportname, $notes);
if ( $input->param('sql') ) {
- $group = $input->param('report_group');
- $subgroup = $input->param('report_subgroup');
- $template->param(
- 'sql' => scalar $input->param('sql') // '',
- 'reportname' => scalar $input->param('reportname') // '',
- 'notes' => scalar $input->param('notes') // '',
- );
+ $group = $input->param('report_group');
+ $subgroup = $input->param('report_subgroup');
+ $sql = $input->param('sql') // '';
+ $reportname = $input->param('reportname') // '';
+ $notes = $input->param('notes') // '';
}
+ elsif ( my $report_id = $input->param('report_id') ) {
+ my $report = Koha::Reports->find($report_id);
+ $group = $report->report_group;
+ $subgroup = $report->report_subgroup;
+ $sql = $report->savedsql // '';
+ $reportname = $report->report_name // '';
+ $notes = $report->notes // '';
+ }
+
$template->param(
+ sql => $sql,
+ reportname => $reportname,
+ notes => $notes,
'create' => 1,
'groups_with_subgroups' => groups_with_subgroups($group, $subgroup),
'public' => '0',
'cache_expiry' => 300,
'usecache' => $usecache,
+
);
}
}
}
-# pass $sth and sql_params, get back an executable query
-sub get_prepped_report {
- my ($sql, $param_names, $sql_params ) = @_;
-
- # First we split out the placeholders
- # This part of the code supports using [[ table.field | alias ]] in the
- # query and replaces it by table.field AS alias. Not sure why we would
- # need it if we can type the latter (which is simpler)?
- my @split = split /\[\[|\]\]/,$sql;
- my $headers;
- for(my $i=0;$i<$#split/2;$i++){ #The placeholders are always the odd elements of the array
- my ($type,$name) = split /\|/,$split[$i*2+1]; # We split them on '|'
- $headers->{$name} = $type; # Store as a lookup for the template
- $split[$i*2+1] =~ s/(\||\?|\.|\*|\(|\)|\%)/\\$1/g; #Quote any special characters so we can replace the placeholders
- $name = C4::Context->dbh->quote($name);
- $sql =~ s/\[\[$split[$i*2+1]\]\]/$type AS $name/; # Remove placeholders from SQL
- }
-
- my %lookup;
- @lookup{@$param_names} = @$sql_params;
- @split = split /<<|>>/,$sql;
- my @tmpl_parameters;
- for(my $i=0;$i<$#split/2;$i++) {
- my $quoted = @$param_names ? $lookup{ $split[$i*2+1] } : @$sql_params[$i];
- # if there are special regexp chars, we must \ them
- $split[$i*2+1] =~ s/(\||\?|\.|\*|\(|\)|\%)/\\$1/g;
- if ($split[$i*2+1] =~ /\|\s*date\s*$/) {
- $quoted = output_pref({ dt => dt_from_string($quoted), dateformat => 'iso', dateonly => 1 }) if $quoted;
- }
- $quoted = C4::Context->dbh->quote($quoted);
- $sql =~ s/<<$split[$i*2+1]>>/$quoted/;
- }
- return $sql,$headers;
-}