bug 10549: make the ILS-DI services advertise that they return UTF-8
[koha.git] / C4 / Reports / Guided.pm
index 377a55a..f0db7a3 100644 (file)
@@ -45,10 +45,28 @@ BEGIN {
       get_column_type get_distinct_values save_dictionary get_from_dictionary
       delete_definition delete_report format_results get_sql
       nb_rows update_sql build_authorised_value_list
+      GetReservedAuthorisedValues
+      GetParametersFromSQL
+      IsAuthorisedValueValid
+      ValidateSQLParameters
     );
 }
 
-=item get_report_areas()
+=head1 NAME
+
+C4::Reports::Guided - Module for generating guided reports 
+
+=head1 SYNOPSIS
+
+  use C4::Reports::Guided;
+
+=head1 DESCRIPTION
+
+=cut
+
+=head1 METHODS
+
+=head2 get_report_areas
 
 This will return a list of all the available report areas
 
@@ -113,25 +131,7 @@ if ( C4::Context->preference('item-level_itypes') ) {
     unshift @{ $criteria{'CAT'} }, 'biblioitems.itemtype';
 }
 
-=head1 NAME
-
-C4::Reports::Guided - Module for generating guided reports 
-
-=head1 SYNOPSIS
-
-  use C4::Reports::Guided;
-
-=head1 DESCRIPTION
-
-=cut
-
-=head1 METHODS
-
-=over 2
-
-=cut
-
-=item get_report_types()
+=head2 get_report_types
 
 This will return a list of all the available report types
 
@@ -153,7 +153,7 @@ sub get_report_types {
 
 }
 
-=item get_report_groups()
+=head2 get_report_groups
 
 This will return a list of all the available report areas with groups
 
@@ -182,7 +182,7 @@ sub get_report_groups {
     return \%groups_with_subgroups
 }
 
-=item get_all_tables()
+=head2 get_all_tables
 
 This will return a list of all tables in the database 
 
@@ -202,7 +202,7 @@ sub get_all_tables {
 
 }
 
-=item get_columns($area)
+=head2 get_columns($area)
 
 This will return a list of all columns for a report area
 
@@ -246,7 +246,7 @@ sub _get_columns {
     return (@columns);
 }
 
-=item build_query($columns,$criteria,$orderby,$area)
+=head2 build_query($columns,$criteria,$orderby,$area)
 
 This will build the sql needed to return the results asked for, 
 $columns is expected to be of the format tablename.columnname.
@@ -318,7 +318,7 @@ sub _build_query {
     return ($query);
 }
 
-=item get_criteria($area,$cgi);
+=head2 get_criteria($area,$cgi);
 
 Returns an arraref to hashrefs suitable for using in a tmpl_loop. With the criteria and available values.
 
@@ -390,7 +390,7 @@ sub get_criteria {
     return ( \@criteria_array );
 }
 
-sub nb_rows($) {
+sub nb_rows {
     my $sql = shift or return;
     my $sth = C4::Context->dbh->prepare($sql);
     $sth->execute();
@@ -398,7 +398,7 @@ sub nb_rows($) {
     return scalar (@$rows);
 }
 
-=item execute_query
+=head2 execute_query
 
   ($results, $error) = execute_query($sql, $offset, $limit)
 
@@ -424,7 +424,7 @@ the user in a user-supplied SQL query WILL apply in any case.
 #  ~ remove any LIMIT clause
 #  ~ repace SELECT clause w/ SELECT count(*)
 
-sub select_2_select_count ($) {
+sub select_2_select_count {
     # Modify the query passed in to create a count query... (I think this covers all cases -crn)
     my ($sql) = strip_limit(shift) or return;
     $sql =~ s/\bSELECT\W+(?:\w+\W+){1,}?FROM\b|\bSELECT\W\*\WFROM\b/SELECT count(*) FROM /ig;
@@ -514,7 +514,7 @@ sub execute_query ($;$$$) {
     # store_results($id,$xml);
 }
 
-=item save_report($sql,$name,$type,$notes)
+=head2 save_report($sql,$name,$type,$notes)
 
 Given some sql and a name this will saved it so that it can reused
 Returns id of the newly created report
@@ -702,7 +702,7 @@ sub get_saved_report {
     return $dbh->selectrow_hashref($query, undef, $report_arg);
 }
 
-=item create_compound($masterID,$subreportID)
+=head2 create_compound($masterID,$subreportID)
 
 This will take 2 reports and create a compound report using both of them
 
@@ -732,7 +732,7 @@ sub create_compound {
     return ( $mastertables, $subtables );
 }
 
-=item get_column_type($column)
+=head2 get_column_type($column)
 
 This takes a column name of the format table.column and will return what type it is
 (free text, set values, date)
@@ -760,7 +760,7 @@ sub get_column_type {
        }
 }
 
-=item get_distinct_values($column)
+=head2 get_distinct_values($column)
 
 Given a column name, return an arrary ref of hashrefs suitable for use as a tmpl_loop 
 with the distinct values of the column
@@ -854,7 +854,7 @@ sub _get_column_defs {
        return \%columns;
 }
 
-=item build_authorised_value_list($authorised_value)
+=head2 build_authorised_value_list($authorised_value)
 
 Returns an arrayref - hashref pair. The hashref consists of
 various code => name lists depending on the $authorised_value.
@@ -919,11 +919,98 @@ sub build_authorised_value_list {
     return (\@authorised_values, \%authorised_lib);
 }
 
+=head2 GetReservedAuthorisedValues
+
+    my %reserved_authorised_values = GetReservedAuthorisedValues();
+
+Returns a hash containig all reserved words
+
+=cut
+
+sub GetReservedAuthorisedValues {
+    my %reserved_authorised_values =
+            map { $_ => 1 } ( 'date',
+                              'branches',
+                              'itemtypes',
+                              'cn_source',
+                              'categorycode' );
+
+   return \%reserved_authorised_values;
+}
+
+
+=head2 IsAuthorisedValueValid
+
+    my $is_valid_ath_value = IsAuthorisedValueValid($authorised_value)
+
+Returns 1 if $authorised_value is on the reserved authorised values list or
+in the authorised value categories defined in
+
+=cut
+
+sub IsAuthorisedValueValid {
+
+    my $authorised_value = shift;
+    my $reserved_authorised_values = GetReservedAuthorisedValues();
+
+    if ( exists $reserved_authorised_values->{$authorised_value} ||
+         IsAuthorisedValueCategory($authorised_value)   ) {
+        return 1;
+    }
+
+    return 0;
+}
+
+=head2 GetParametersFromSQL
+
+    my @sql_parameters = GetParametersFromSQL($sql)
+
+Returns an arrayref of hashes containing the keys name and authval
+
+=cut
+
+sub GetParametersFromSQL {
+
+    my $sql = shift ;
+    my @split = split(/<<|>>/,$sql);
+    my @sql_parameters = ();
+
+    for ( my $i = 0; $i < ($#split/2) ; $i++ ) {
+        my ($name,$authval) = split(/\|/,$split[$i*2+1]);
+        push @sql_parameters, { 'name' => $name, 'authval' => $authval };
+    }
+
+    return \@sql_parameters;
+}
+
+=head2 ValidateSQLParameters
+
+    my @problematic_parameters = ValidateSQLParameters($sql)
+
+Returns an arrayref of hashes containing the keys name and authval of
+those SQL parameters that do not correspond to valid authorised names
+
+=cut
+
+sub ValidateSQLParameters {
+
+    my $sql = shift;
+    my @problematic_parameters = ();
+    my $sql_parameters = GetParametersFromSQL($sql);
+
+    foreach my $sql_parameter (@$sql_parameters) {
+        if ( defined $sql_parameter->{'authval'} ) {
+            push @problematic_parameters, $sql_parameter unless
+                IsAuthorisedValueValid($sql_parameter->{'authval'});
+        }
+    }
+
+    return \@problematic_parameters;
+}
+
 1;
 __END__
 
-=back
-
 =head1 AUTHOR
 
 Chris Cormack <crc@liblime.com>