Bug 27824: Trim column headers
[koha.git] / Koha / Report.pm
1 package Koha::Report;
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 use Modern::Perl;
19
20
21 use Koha::Database;
22 use Koha::Reports;
23 #use Koha::DateUtils qw( dt_from_string output_pref );
24
25 use base qw(Koha::Object);
26 #
27 # FIXME We could only return an error code instead of the arrayref
28 # Only 1 error is returned
29 # TODO Koha::Report->store should check this before saving
30
31 =head1 NAME
32
33 Koha::Report - Koha Report Object class
34
35 =head1 API
36
37 =head2 Class Methods
38
39 =head3 is_sql_valid
40
41 my ( $is_sql_valid, $errors ) = $report->is_sql_valid;
42
43 $errors is a arrayref of hashrefs, keys can be sqlerr or queryerr.
44
45 Validate SQL query string so it only contains a select,
46 not any of the harmful queries.
47
48 =cut
49
50 sub is_sql_valid {
51     my ($self) = @_;
52
53     my $sql = $self->savedsql;
54     $sql //= '';
55     my @errors = ();
56
57     if ($sql =~ /;?\W?(UPDATE|DELETE|DROP|INSERT|SHOW|CREATE)\W/i) {
58         push @errors, { sqlerr => $1 };
59     } elsif ($sql !~ /^\s*SELECT\b\s*/i) {
60         push @errors, { queryerr => 'Missing SELECT' };
61     }
62
63     return ( @errors ? 0 : 1, \@errors );
64 }
65
66 =head3 get_search_info
67
68 Return search info
69
70 =cut
71
72 sub get_search_info {
73     my $self = shift;
74     my $sub_mana_info = { 'query' => shift };
75     return $sub_mana_info;
76 }
77
78 =head3 get_sharable_info
79
80 Return properties that can be shared.
81
82 =cut
83
84 sub get_sharable_info {
85     my $self             = shift;
86     my $shared_report_id = shift;
87     my $report           = Koha::Reports->find($shared_report_id);
88     my $sub_mana_info    = {
89         'savedsql'     => $report->savedsql,
90         'report_name'  => $report->report_name,
91         'notes'        => $report->notes,
92         'report_group' => $report->report_group,
93         'type'         => $report->type,
94     };
95     return $sub_mana_info;
96 }
97
98 =head3 new_from_mana
99
100 Clear a Mana report to be imported in Koha?
101
102 =cut
103
104 sub new_from_mana {
105     my $self = shift;
106     my $data = shift;
107
108     $data->{mana_id} = $data->{id};
109
110     delete $data->{exportemail};
111     delete $data->{kohaversion};
112     delete $data->{creationdate};
113     delete $data->{lastimport};
114     delete $data->{id};
115     delete $data->{nbofusers};
116     delete $data->{language};
117
118     Koha::Report->new($data)->store;
119 }
120
121 =head3 prep_report
122
123 Prep the report and return executable sql with parameters embedded and a list of header types
124 for building batch action links in the template
125
126 =cut
127
128 sub prep_report {
129     my ( $self, $param_names, $sql_params ) = @_;
130     my $sql = $self->savedsql;
131
132     # First we split out the placeholders
133     # This part of the code supports using [[ table.field | alias ]] in the
134     # query and replaces it by table.field AS alias. This is used to build
135     # the batch action links foir cardnumbers, itemnumbers, and biblionumbers in the template
136     # while allowing the library to alter the column names
137     my @split = split /\[\[|\]\]/, $sql;
138     my $headers;
139     for ( my $i = 0 ; $i < $#split / 2 ; $i++ )
140     {    #The placeholders are always the odd elements of the array
141         my ( $type, $name ) = split /\|/,
142           $split[ $i * 2 + 1 ];    # We split them on '|'
143         $name =~ s/^\s+|\s+$//;    # Trim
144         $headers->{$name} = $type; # Store as a lookup for the template
145         $headers->{$name} =~
146           s/^\w*\.//;    # strip the table name just as in $sth->{NAME} array
147         $split[ $i * 2 + 1 ] =~ s/(\||\?|\.|\*|\(|\)|\%)/\\$1/g
148           ;    #Quote any special characters so we can replace the placeholders
149         $name = C4::Context->dbh->quote($name);
150         $sql =~ s/\[\[$split[$i*2+1]\]\]/$type AS $name/
151           ;    # Remove placeholders from SQL
152     }
153
154     my %lookup;
155     @lookup{@$param_names} = @$sql_params;
156     @split = split /<<|>>/, $sql;
157     for ( my $i = 0 ; $i < $#split / 2 ; $i++ ) {
158         my $quoted =
159           @$param_names ? $lookup{ $split[ $i * 2 + 1 ] } : @$sql_params[$i];
160
161         # if there are special regexp chars, we must \ them
162         $split[ $i * 2 + 1 ] =~ s/(\||\?|\.|\*|\(|\)|\%)/\\$1/g;
163         #if ( $split[ $i * 2 + 1 ] =~ /\|\s*date\s*$/ ) {
164         #    $quoted = output_pref(
165         #        {
166         #            dt         => dt_from_string($quoted),
167         #            dateformat => 'iso',
168         #            dateonly   => 1
169         #        }
170         #    ) if $quoted;
171         #}
172         unless ( $split[ $i * 2 + 1 ] =~ /\|\s*list\s*$/ && $quoted ) {
173             $quoted = C4::Context->dbh->quote($quoted);
174         }
175         else {
176             my @list = split /\n/, $quoted;
177             my @quoted_list;
178             foreach my $item (@list) {
179                 $item =~ s/\r//;
180                 push @quoted_list, C4::Context->dbh->quote($item);
181             }
182             $quoted = "(" . join( ",", @quoted_list ) . ")";
183         }
184         $sql =~ s/<<$split[$i*2+1]>>/$quoted/;
185     }
186
187     $sql = "$sql /* saved_sql.id: ${\( $self->id )} */";
188     return $sql, $headers;
189 }
190
191 =head3 _type
192
193 Returns name of corresponding DBIC resultset
194
195 =cut
196
197 sub _type {
198     return 'SavedSql';
199 }
200
201 1;