No more used since a while.
[koha_fer] / reports / guided_reports.pl
index f114216..c833466 100755 (executable)
 # 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::Reports::Guided;
+use C4::Auth;
 use C4::Output;
-use C4::Reports;
+use C4::Dates;
+use C4::Debug;
 
 =head1 NAME
 
@@ -35,13 +37,15 @@ Script to control the guided report creation
 =cut
 
 my $input = new CGI;
+my $referer = $input->referer();
+
 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
     {
         template_name   => "reports/guided_reports_start.tmpl",
         query           => $input,
         type            => "intranet",
         authnotrequired => 0,
-        flagsrequired   => { editcatalogue => 1 },
+        flagsrequired   => { reports => 1 },
         debug           => 1,
     }
 );
@@ -61,7 +65,7 @@ elsif ( $phase eq 'Build new' ) {
     $template->param( 'build1' => 1 );
 
     # get report areas
-    my $areas = C4::Reports::get_report_areas();
+    my $areas = get_report_areas();
     $template->param( 'areas' => $areas );
 
 }
@@ -75,6 +79,39 @@ elsif ( $phase eq 'Used saved' ) {
     $template->param( 'savedreports' => $reports );
 }
 
+elsif ( $phase eq 'Delete Saved') {
+       
+       # delete a report from the saved reports list
+       $no_html = 1;
+       my $id = $input->param('reports');
+       delete_report($id);
+    print $input->redirect("/cgi-bin/koha/reports/guided_reports.pl?phase=Used%20saved");
+       
+}              
+
+elsif ( $phase eq 'Show SQL'){
+       
+       my $id = $input->param('reports');
+       my $sql = get_sql($id);
+       $template->param(
+               'sql' => $sql,
+               'showsql' => 1,
+               );
+}
+
+elsif ($phase eq 'retrieve results') {
+       my $id = $input->param('id');
+       my ($results,$name,$notes) = format_results($id);
+       # do something
+       $template->param(
+               'retresults' => 1,
+               'results' => $results,
+               'name' => $name,
+               'notes' => $notes,
+               );
+       
+}
+
 elsif ( $phase eq 'Report on this Area' ) {
 
     # they have choosen a new report and the area to report on
@@ -86,7 +123,7 @@ elsif ( $phase eq 'Report on this Area' ) {
     );
 
     # get report types
-    my $types = C4::Reports::get_report_types();
+    my $types = get_report_types();
     $template->param( 'types' => $types );
 }
 
@@ -103,7 +140,7 @@ elsif ( $phase eq 'Choose this type' ) {
     );
 
     # get columns
-    my $columns = get_columns($area);
+    my $columns = get_columns($area,$input);
     $template->param( 'columns' => $columns );
 }
 
@@ -122,7 +159,7 @@ elsif ( $phase eq 'Choose these columns' ) {
         'type'   => $type,
         'column' => $column,
     );
-    my $criteria = get_criteria($area);
+    my $criteria = get_criteria($area,$input);
     $template->param( 'criteria' => $criteria,
        'definitions' => $definitions);
 }
@@ -138,6 +175,10 @@ elsif ( $phase eq 'Choose these criteria' ) {
     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'";
         }
     }
@@ -147,7 +188,7 @@ elsif ( $phase eq 'Choose these criteria' ) {
         'area'           => $area,
         'type'           => $type,
         'column'         => $column,
-               'definition'     => $definition,
+        'definition'     => $definition,
         'criteriastring' => $query_criteria,
     );
 
@@ -185,7 +226,7 @@ elsif ( $phase eq 'Choose these criteria' ) {
     $template->param( 'total_by' => \@total_by );
 }
 
-elsif ( $phase eq 'Choose Totals' ) {
+elsif ( $phase eq 'Choose These Operations' ) {
     my $area     = $input->param('area');
     my $type     = $input->param('type');
     my $column   = $input->param('column');
@@ -205,7 +246,7 @@ elsif ( $phase eq 'Choose Totals' ) {
         'column'         => $column,
         'criteriastring' => $criteria,
         'totals'         => $totals,
-               'definition'    => $definition,
+        'definition'    => $definition,
     );
 
     # get columns
@@ -282,51 +323,119 @@ 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 );
+    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) = 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
+        '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 ); 
 }
 
@@ -349,211 +458,11 @@ elsif ($phase eq 'Save Compound'){
        );
 }
 
-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,
 
-       );
-}
+$template->param(   'referer' => $referer,
+                    'DHTMLcalendar_dateformat' => C4::Dates->DHTMLcalendar(),
+                );
 
-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;