Can now add definitions to reports
authorChris Cormack <crc@liblime.com>
Mon, 29 Oct 2007 18:19:53 +0000 (13:19 -0500)
committerJoshua Ferraro <jmf@liblime.com>
Mon, 29 Oct 2007 21:11:27 +0000 (16:11 -0500)
Signed-off-by: Chris Cormack <crc@liblime.com>
Signed-off-by: Joshua Ferraro <jmf@liblime.com>
C4/Reports.pm
koha-tmpl/intranet-tmpl/prog/en/modules/reports/guided_reports_start.tmpl
reports/guided_reports.pl

index 509af85..336be04 100644 (file)
@@ -217,18 +217,18 @@ This is what get_columns returns.
 =cut
 
 sub build_query {
-    my ( $columns, $criteria, $orderby, $area, $totals ) = @_;
+    my ( $columns, $criteria, $orderby, $area, $totals, $definition ) = @_;
 ### $orderby
     my $keys   = $keys{$area};
     my $tables = $table_areas{$area};
 
     my $sql =
-      _build_query( $tables, $columns, $criteria, $keys, $orderby, $totals );
+      _build_query( $tables, $columns, $criteria, $keys, $orderby, $totals, $definition );
     return ($sql);
 }
 
 sub _build_query {
-    my ( $tables, $columns, $criteria, $keys, $orderby, $totals ) = @_;
+    my ( $tables, $columns, $criteria, $keys, $orderby, $totals, $definition) = @_;
 ### $orderby
     # $keys is an array of joining constraints
     my $dbh           = C4::Context->dbh();
@@ -245,6 +245,21 @@ sub _build_query {
                $criteria =~ s/AND/WHERE/;
         $query .= " $criteria";
     }
+       if ($definition){
+               my @definitions = split(',',$definition);
+               my $deftext;
+               foreach my $def (@definitions){
+                       my $defin=get_from_dictionary('',$def);
+                       $deftext .=" ".$defin->[0]->{'saved_sql'};
+               }
+               if ($query =~ /WHERE/i){
+                       $query .= $deftext;
+               }
+               else {
+                       $deftext  =~ s/AND/WHERE/;
+                       $query .= $deftext;                     
+               }
+       }
     if ($totals) {
         my $groupby;
         my @totcolumns = split( ',', $totals );
@@ -487,10 +502,25 @@ sub save_dictionary {
 }
 
 sub get_from_dictionary {
+       my ($area,$id) = @_;
        my $dbh = C4::Context->dbh();
        my $query = "SELECT * FROM reports_dictionary";
+       if ($area){
+               $query.= " WHERE area = ?";
+       }
+       elsif ($id){
+               $query.= " WHERE id = ?"
+       }
        my $sth = $dbh->prepare($query);
-       $sth->execute;
+       if ($id){
+               $sth->execute($id);
+       }
+       elsif ($area) {
+               $sth->execute($area);
+       }
+       else {
+               $sth->execute();
+       }
        my @loop;
        while (my $data = $sth->fetchrow_hashref()){
                push @loop,$data;
index 2db2d77..cd0c077 100644 (file)
@@ -140,6 +140,12 @@ align          : "Tl"
 <br />
 <!-- /TMPL_LOOP -->
 <br />
+<!-- TMPL_IF NAME="definitions" -->
+Dictionary Definitions <br />
+<!-- TMPL_LOOP NAME="definitions" -->
+<input type=checkbox name="definition" value="<!-- TMPL_VAR NAME="id" -->"><!-- TMPL_VAR NAME="name" --><br />
+<!-- /TMPL_LOOP -->
+<!-- /TMPL_IF -->
 <input type="submit" name="phase" value="Choose these criteria">
 </form>
 <!-- /TMPL_IF -->
@@ -151,6 +157,7 @@ align          : "Tl"
 <input type="hidden" name="area" value="<!-- TMPL_VAR NAME="area" -->">
 <input type="hidden" name="type" value="<!-- TMPL_VAR NAME="type" -->">
 <input type="hidden" name="column" value="<!-- TMPL_VAR NAME="column" -->">
+<input type="hidden" name="definition" value="<!-- TMPL_VAR NAME="definition" -->">
 <input type="hidden" name="criteria" value="<!-- TMPL_VAR NAME="criteriastring" -->">
 <!-- TMPL_LOOP NAME="total_by" -->
 <input type=checkbox name="total_by" value="<!-- TMPL_VAR NAME="name" -->"><!-- TMPL_VAR NAME="name"-->
@@ -176,6 +183,7 @@ align          : "Tl"
 <input type="hidden" name="type" value="<!-- TMPL_VAR NAME="type" -->">
 <input type="hidden" name="column" value="<!-- TMPL_VAR NAME="column" -->">
 <input type="hidden" name="criteria" value="<!-- TMPL_VAR NAME="criteriastring" -->">
+<input type="hidden" name="definition" value="<!-- TMPL_VAR NAME="definition" -->">
 <input type="hidden" name="totals" value="<!-- TMPL_VAR NAME="totals" -->">
 <!-- TMPL_LOOP NAME="order_by" -->
 <input type=checkbox name="order_by" value="<!-- TMPL_VAR NAME="name" -->"><!-- TMPL_VAR NAME="name"-->
index eb99290..2b50802 100755 (executable)
@@ -115,6 +115,7 @@ elsif ( $phase eq 'Choose these columns' ) {
     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,
@@ -122,13 +123,16 @@ elsif ( $phase eq 'Choose these columns' ) {
         'column' => $column,
     );
     my $criteria = get_criteria($area);
-    $template->param( 'criteria' => $criteria );
+    $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) {
@@ -143,6 +147,7 @@ elsif ( $phase eq 'Choose these criteria' ) {
         'area'           => $area,
         'type'           => $type,
         'column'         => $column,
+               'definition'     => $definition,
         'criteriastring' => $query_criteria,
     );
 
@@ -185,6 +190,7 @@ elsif ( $phase eq 'Choose Totals' ) {
     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) {
@@ -199,6 +205,7 @@ elsif ( $phase eq 'Choose Totals' ) {
         'column'         => $column,
         'criteriastring' => $criteria,
         'totals'         => $totals,
+               'definition'    => $definition,
     );
 
     # get columns
@@ -232,6 +239,7 @@ elsif ( $phase eq 'Build Report' ) {
     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 ,
@@ -251,7 +259,7 @@ elsif ( $phase eq 'Build Report' ) {
 
     # get the sql
     my $sql =
-      build_query( \@columns, $query_criteria, $query_orderby, $area, $totals );
+      build_query( \@columns, $query_criteria, $query_orderby, $area, $totals, $definition );
     $template->param(
         'showreport' => 1,
         'sql'        => $sql,