Koha/reports/guided_reports.pl
Chris Cormack 83433f61b6 Pretty close to finished the dictionary now
Signed-off-by: Chris Cormack <crc@liblime.com>
Signed-off-by: Joshua Ferraro <jmf@liblime.com>
2007-10-29 16:11:29 -05:00

560 lines
16 KiB
Perl
Executable file

#!/usr/bin/perl
# Copyright 2007 Liblime ltd
#
# This file is part of Koha.
#
# Koha is free software; you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation; either version 2 of the License, or (at your option) any later
# version.
#
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
# Suite 330, Boston, MA 02111-1307 USA
use CGI::Carp qw(fatalsToBrowser warningsToBrowser);
use strict;
use C4::Auth;
use CGI;
use C4::Output;
use C4::Reports;
=head1 NAME
Script to control the guided report creation
=head1 DESCRIPTION
=over2
=cut
my $input = new CGI;
my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
{
template_name => "reports/guided_reports_start.tmpl",
query => $input,
type => "intranet",
authnotrequired => 0,
flagsrequired => { editcatalogue => 1 },
debug => 1,
}
);
my $phase = $input->param('phase');
my $no_html = 0; # this will be set if we dont want to print out an html::template
if ( !$phase ) {
$template->param( 'start' => 1 );
# show welcome page
}
elsif ( $phase eq 'Build new' ) {
# build a new report
$template->param( 'build1' => 1 );
# get report areas
my $areas = C4::Reports::get_report_areas();
$template->param( 'areas' => $areas );
}
elsif ( $phase eq 'Used saved' ) {
# use a saved report
# get list of reports and display them
$template->param( 'saved1' => 1 );
my $reports = get_saved_reports();
$template->param( 'savedreports' => $reports );
}
elsif ( $phase eq 'Report on this Area' ) {
# they have choosen a new report and the area to report on
# get area
my $area = $input->param('areas');
$template->param(
'build2' => 1,
'area' => $area
);
# get report types
my $types = C4::Reports::get_report_types();
$template->param( 'types' => $types );
}
elsif ( $phase eq 'Choose this type' ) {
# they have chosen type and area
# get area and type and pass them to the template
my $area = $input->param('area');
my $type = $input->param('types');
$template->param(
'build3' => 1,
'area' => $area,
'type' => $type,
);
# get columns
my $columns = get_columns($area);
$template->param( 'columns' => $columns );
}
elsif ( $phase eq 'Choose these columns' ) {
# we now know type, area, and columns
# next step is the constraints
my $area = $input->param('area');
my $type = $input->param('type');
my @columns = $input->param('columns');
my $column = join( ',', @columns );
my $definitions = get_from_dictionary($area);
$template->param(
'build4' => 1,
'area' => $area,
'type' => $type,
'column' => $column,
);
my $criteria = get_criteria($area);
$template->param( 'criteria' => $criteria,
'definitions' => $definitions);
}
elsif ( $phase eq 'Choose these criteria' ) {
my $area = $input->param('area');
my $type = $input->param('type');
my $column = $input->param('column');
my @definitions = $input->param('definition');
my $definition = join (',',@definitions);
my @criteria = $input->param('criteria_column');
my $query_criteria;
foreach my $crit (@criteria) {
my $value = $input->param( $crit . "_value" );
if ($value) {
$query_criteria .= " AND $crit='$value'";
}
}
$template->param(
'build5' => 1,
'area' => $area,
'type' => $type,
'column' => $column,
'definition' => $definition,
'criteriastring' => $query_criteria,
);
# get columns
my @columns = split( ',', $column );
my @total_by;
# build structue for use by tmpl_loop to choose columns to order by
# need to do something about the order of the order :)
# we also want to use the %columns hash to get the plain english names
foreach my $col (@columns) {
my %total;
$total{'name'} = $col;
my @selects;
my %select1;
$select1{'value'} = 'sum';
push @selects, \%select1;
my %select2;
$select2{'value'} = 'min';
push @selects, \%select2;
my %select3;
$select3{'value'} = 'max';
push @selects, \%select3;
my %select4;
$select4{'value'} = 'avg';
push @selects, \%select4;
my %select5;
$select5{'value'} = 'count';
push @selects, \%select5;
$total{'select'} = \@selects;
push @total_by, \%total;
}
$template->param( 'total_by' => \@total_by );
}
elsif ( $phase eq 'Choose Totals' ) {
my $area = $input->param('area');
my $type = $input->param('type');
my $column = $input->param('column');
my $criteria = $input->param('criteria');
my $definition = $input->param('definition');
my @total_by = $input->param('total_by');
my $totals;
foreach my $total (@total_by) {
my $value = $input->param( $total . "_tvalue" );
$totals .= "$value($total),";
}
$template->param(
'build6' => 1,
'area' => $area,
'type' => $type,
'column' => $column,
'criteriastring' => $criteria,
'totals' => $totals,
'definition' => $definition,
);
# get columns
my @columns = split( ',', $column );
my @order_by;
# build structue for use by tmpl_loop to choose columns to order by
# need to do something about the order of the order :)
foreach my $col (@columns) {
my %order;
$order{'name'} = $col;
my @selects;
my %select1;
$select1{'value'} = 'asc';
push @selects, \%select1;
my %select2;
$select2{'value'} = 'desc';
push @selects, \%select2;
$order{'select'} = \@selects;
push @order_by, \%order;
}
$template->param( 'order_by' => \@order_by );
}
elsif ( $phase eq 'Build Report' ) {
# now we have all the info we need and can build the sql
my $area = $input->param('area');
my $type = $input->param('type');
my $column = $input->param('column');
my $crit = $input->param('criteria');
my $totals = $input->param('totals');
my $definition = $input->param('definition');
# my @criteria = split( ',', $crit );
my $query_criteria=$crit;
# split the columns up by ,
my @columns = split( ',', $column );
my @order_by = $input->param('order_by');
my $query_orderby;
foreach my $order (@order_by) {
my $value = $input->param( $order . "_ovalue" );
if ($query_orderby) {
$query_orderby .= ",$order $value";
}
else {
$query_orderby = " ORDER BY $order $value";
}
}
# get the sql
my $sql =
build_query( \@columns, $query_criteria, $query_orderby, $area, $totals, $definition );
$template->param(
'showreport' => 1,
'sql' => $sql,
'type' => $type
);
}
elsif ( $phase eq 'Save' ) {
# Save the report that has just been built
my $sql = $input->param('sql');
my $type = $input->param('type');
$template->param(
'save' => 1,
'sql' => $sql,
'type' => $type
);
}
elsif ( $phase eq 'Save Report' ) {
# 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 );
}
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
);
}
elsif ($phase eq 'Run this report'){
# execute a saved report
my $report = $input->param('reports');
my ($sql,$type) = get_saved_report($report);
my $results = execute_query($sql,$type);
$template->param(
'results' => $results,
'sql' => $sql,
'execute' => 1
);
}
elsif ($phase eq 'Export'){
# 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;
}
elsif ($phase eq 'Create report from SQL'){
# alllow the user to paste in sql
$template->param('create' => 1);
my $types = C4::Reports::get_report_types();
$template->param( 'types' => $types );
}
elsif ($phase eq 'Create Compound Report'){
my $reports = get_saved_reports();
$template->param( 'savedreports' => $reports,
'compound' => 1,
);
}
elsif ($phase eq 'Save Compound'){
my $master = $input->param('master');
my $subreport = $input->param('subreport');
# my $compound_report = create_compound($master,$subreport);
# my $results = run_compound($compound_report);
my ($mastertables,$subtables) = create_compound($master,$subreport);
$template->param( 'save_compound' => 1,
master=>$mastertables,
subsql=>$subtables
);
}
elsif ($phase eq 'View Dictionary'){
# view the dictionary we use to set up abstract variables such as all borrowers over fifty who live in a certain town
( $template, $borrowernumber, $cookie ) = get_template_and_user(
{
template_name => "reports/dictionary.tmpl",
query => $input,
type => "intranet",
authnotrequired => 0,
flagsrequired => { editcatalogue => 1 },
debug => 1,
}
);
my $areas = C4::Reports::get_report_areas();
my $definitions = get_from_dictionary();
$template->param( 'areas' => $areas ,
'start_dictionary' => 1,
'definitions' => $definitions,
);
}
elsif ($phase eq 'Add New Definition'){
# display form allowing them to add a new definition
( $template, $borrowernumber, $cookie ) = get_template_and_user(
{
template_name => "reports/dictionary.tmpl",
query => $input,
type => "intranet",
authnotrequired => 0,
flagsrequired => { editcatalogue => 1 },
debug => 1,
}
);
$template->param( 'new_dictionary' => 1,
);
}
elsif ($phase eq 'New Term step 2'){
# Choosing the area
( $template, $borrowernumber, $cookie ) = get_template_and_user(
{
template_name => "reports/dictionary.tmpl",
query => $input,
type => "intranet",
authnotrequired => 0,
flagsrequired => { editcatalogue => 1 },
debug => 1,
}
);
my $areas = C4::Reports::get_report_areas();
my $definition_name=$input->param('definition_name');
my $definition_description=$input->param('definition_description');
$template->param( 'step_2' => 1,
'areas' => $areas,
'definition_name' => $definition_name,
'definition_description' => $definition_description,
);
}
elsif ($phase eq 'New Term step 3'){
# Choosing the columns
( $template, $borrowernumber, $cookie ) = get_template_and_user(
{
template_name => "reports/dictionary.tmpl",
query => $input,
type => "intranet",
authnotrequired => 0,
flagsrequired => { editcatalogue => 1 },
debug => 1,
}
);
my $area = $input->param('areas');
my $columns = get_columns($area);
my $definition_name=$input->param('definition_name');
my $definition_description=$input->param('definition_description');
$template->param( 'step_3' => 1,
'area' => $area,
'columns' => $columns,
'definition_name' => $definition_name,
'definition_description' => $definition_description,
);
}
elsif ($phase eq 'New Term step 4'){
# Choosing the values
( $template, $borrowernumber, $cookie ) = get_template_and_user(
{
template_name => "reports/dictionary.tmpl",
query => $input,
type => "intranet",
authnotrequired => 0,
flagsrequired => { editcatalogue => 1 },
debug => 1,
}
);
my $area=$input->param('area');
my $definition_name=$input->param('definition_name');
my $definition_description=$input->param('definition_description');
my @columns = $input->param('columns');
my $columnstring = join (',',@columns);
my @column_loop;
foreach my $column (@columns){
my %tmp_hash;
$tmp_hash{'name'}=$column;
my $type =get_column_type($column);
if ($type eq 'distinct'){
my $values = get_distinct_values($column);
$tmp_hash{'values'} = $values;
$tmp_hash{'distinct'} = 1;
}
if ($type eq 'DATE'){
$tmp_hash{'date'}=1;
}
if ($type eq 'TEXT'){
$tmp_hash{'text'}=1;
}
# else {
# warn $type;#
# }
push @column_loop,\%tmp_hash;
}
$template->param( 'step_4' => 1,
'area' => $area,
'definition_name' => $definition_name,
'definition_description' => $definition_description,
'columns' => \@column_loop,
'columnstring' => $columnstring,
);
}
elsif ($phase eq 'New Term step 5'){
# Confirmation screen
( $template, $borrowernumber, $cookie ) = get_template_and_user(
{
template_name => "reports/dictionary.tmpl",
query => $input,
type => "intranet",
authnotrequired => 0,
flagsrequired => { editcatalogue => 1 },
debug => 1,
}
);
my $area = $input->param('area');
my $columnstring = $input->param('columnstring');
my $definition_name=$input->param('definition_name');
my $definition_description=$input->param('definition_description');
my @criteria = $input->param('criteria_column');
my $query_criteria;
my @criteria_loop;
foreach my $crit (@criteria) {
my $value = $input->param( $crit . "_value" );
if ($value) {
$query_criteria .= " AND $crit='$value'";
my %tmp_hash;
$tmp_hash{'name'}=$crit;
$tmp_hash{'value'} = $value;
push @criteria_loop,\%tmp_hash;
}
$value = $input->param( $crit . "_start_value" );
if ($value) {
$query_criteria .= " AND $crit > '$value'";
my %tmp_hash;
$tmp_hash{'name'}="$crit Start";
$tmp_hash{'value'} = $value;
push @criteria_loop,\%tmp_hash;
}
$value = $input->param( $crit . "_end_value" );
if ($value) {
$query_criteria .= " AND $crit <= '$value'";
my %tmp_hash;
$tmp_hash{'name'}="$crit End";
$tmp_hash{'value'} = $value;
push @criteria_loop,\%tmp_hash;
}
}
$template->param( 'step_5' => 1,
'area' => $area,
'definition_name' => $definition_name,
'definition_description' => $definition_description,
'query' => $query_criteria,
'columnstring' => $columnstring,
'criteria_loop' => \@criteria_loop,
);
}
elsif ($phase eq 'New Term step 6'){
# Saving
my $area = $input->param('area');
my $definition_name=$input->param('definition_name');
my $definition_description=$input->param('definition_description');
my $sql=$input->param('sql');
save_dictionary($definition_name,$definition_description,$sql,$area);
$no_html=1;
print $input->redirect("/cgi-bin/koha/reports/guided_reports.pl?phase=View%20Dictionary");
}
elsif ($phase eq 'Delete Definition'){
$no_html=1;
my $id = $input->param('id');
delete_definition($id);
print $input->redirect("/cgi-bin/koha/reports/guided_reports.pl?phase=View%20Dictionary");
}
if (!$no_html){
output_html_with_http_headers $input, $cookie, $template->output;
}