1110 : Balance carts and lists, follow-up : Added Opac lists features to admin lists
[koha.git] / reports / dictionary.pl
index 5818905..2c87a49 100755 (executable)
 # Suite 330, Boston, MA  02111-1307 USA
 use CGI::Carp qw(fatalsToBrowser warningsToBrowser);
 use strict;
+use warnings;
 use C4::Auth;
 use CGI;
 use C4::Output;
-use C4::Reports;
+use C4::Reports::Guided;
+use C4::Dates;
 
 =head1 NAME
 
@@ -37,7 +39,8 @@ Script to control the guided report creation
 my $input = new CGI;
 my $referer = $input->referer();
 
-my $phase = $input->param('phase');
+my $phase = $input->param('phase') || 'View Dictionary';
+my $area = $input->param('areas') || '';
 my $no_html = 0; # this will be set if we dont want to print out an html::template
 my     ( $template, $borrowernumber, $cookie ) = get_template_and_user(
     {
@@ -45,15 +48,15 @@ my  ( $template, $borrowernumber, $cookie ) = get_template_and_user(
         query           => $input,
         type            => "intranet",
         authnotrequired => 0,
-        flagsrequired   => { editcatalogue => 1 },
+        flagsrequired   => { reports => 1 },
         debug           => 1,
     }
        );
 
 if ($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
-       my $areas = C4::Reports::get_report_areas();
-       my $definitions = get_from_dictionary();
+       my $areas = get_report_areas();
+       my $definitions = get_from_dictionary($area);
        $template->param( 'areas' => $areas ,
                'start_dictionary' => 1,
                'definitions' => $definitions,
@@ -67,7 +70,7 @@ elsif ($phase eq 'Add New Definition'){
 
 elsif ($phase eq 'New Term step 2'){
        # Choosing the area
-       my $areas = C4::Reports::get_report_areas();
+       my $areas = C4::Reports::Guided::get_report_areas();
        my $definition_name=$input->param('definition_name');
        my $definition_description=$input->param('definition_description');             
        $template->param( 'step_2' => 1,
@@ -127,13 +130,15 @@ elsif ($phase eq 'New Term step 4'){
                'definition_description' => $definition_description,
                'columns' => \@column_loop,
                'columnstring' => $columnstring,
-
+                'DHTMLcalendar_dateformat' => C4::Dates->DHTMLcalendar(),
        );
 }
 
 elsif ($phase eq 'New Term step 5'){
        # Confirmation screen
+       my $areas = C4::Reports::Guided::get_report_areas();
        my $area = $input->param('area');
+    my $areaname = $areas->[$area - 1]->{'name'};
        my $columnstring = $input->param('columnstring');
        my $definition_name=$input->param('definition_name');
        my $definition_description=$input->param('definition_description');     
@@ -143,32 +148,44 @@ elsif ($phase eq 'New Term step 5'){
        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;
+                    my %tmp_hash;
+                    $tmp_hash{'name'}=$crit;
+                    $tmp_hash{'value'} = $value;
+                    push @criteria_loop,\%tmp_hash;
+                    if ($value =~ C4::Dates->regexp(C4::Context->preference('dateformat'))) {    
+                        my $date = C4::Dates->new($value);
+                        $value = $date->output("iso");
+                    }
+                    $query_criteria .= " AND $crit='$value'";
                }
-               
                $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;
+                    my %tmp_hash;
+                    $tmp_hash{'name'}="$crit Start";
+                    $tmp_hash{'value'} = $value;
+                    push @criteria_loop,\%tmp_hash;
+                    if ($value =~ C4::Dates->regexp(C4::Context->preference('dateformat'))) {    
+                        my $date = C4::Dates->new($value);
+                        $value = $date->output("iso");
+                    }
+                    $query_criteria .= " AND $crit >= '$value'";
                }
                $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;
+                    my %tmp_hash;
+                    $tmp_hash{'name'}="$crit End";
+                    $tmp_hash{'value'} = $value;
+                    push @criteria_loop,\%tmp_hash;
+                    if ($value =~ C4::Dates->regexp(C4::Context->preference('dateformat'))) {    
+                        my $date = C4::Dates->new($value);
+                        $value = $date->output("iso");
+                    }
+                    $query_criteria .= " AND $crit <= '$value'";
                }                 
        }
        $template->param( 'step_5' => 1,
                'area' => $area,
+               'areaname' => $areaname,
                'definition_name' => $definition_name,
                'definition_description' => $definition_description,
                'query' => $query_criteria,
@@ -185,14 +202,14 @@ elsif ($phase eq 'New Term step 6'){
        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");      
+       print $input->redirect("/cgi-bin/koha/reports/dictionary.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");
+       print $input->redirect("/cgi-bin/koha/reports/dictionary.pl?phase=View%20Dictionary");
        }
 
 $template->param( 'referer' => $referer );