Bug 28959: Add virtualshelves.public as a boolean
[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         $headers->{$name} = $type; # Store as a lookup for the template
144         $headers->{$name} =~
145           s/^\w*\.//;    # strip the table name just as in $sth->{NAME} array
146         $split[ $i * 2 + 1 ] =~ s/(\||\?|\.|\*|\(|\)|\%)/\\$1/g
147           ;    #Quote any special characters so we can replace the placeholders
148         $name = C4::Context->dbh->quote($name);
149         $sql =~ s/\[\[$split[$i*2+1]\]\]/$type AS $name/
150           ;    # Remove placeholders from SQL
151     }
152
153     my %lookup;
154     @lookup{@$param_names} = @$sql_params;
155     @split = split /<<|>>/, $sql;
156     for ( my $i = 0 ; $i < $#split / 2 ; $i++ ) {
157         my $quoted =
158           @$param_names ? $lookup{ $split[ $i * 2 + 1 ] } : @$sql_params[$i];
159
160         # if there are special regexp chars, we must \ them
161         $split[ $i * 2 + 1 ] =~ s/(\||\?|\.|\*|\(|\)|\%)/\\$1/g;
162         if ( $split[ $i * 2 + 1 ] =~ /\|\s*date\s*$/ ) {
163             $quoted = output_pref(
164                 {
165                     dt         => dt_from_string($quoted),
166                     dateformat => 'iso',
167                     dateonly   => 1
168                 }
169             ) if $quoted;
170         }
171         unless ( $split[ $i * 2 + 1 ] =~ /\|\s*list\s*$/ && $quoted ) {
172             $quoted = C4::Context->dbh->quote($quoted);
173         }
174         else {
175             my @list = split /\n/, $quoted;
176             my @quoted_list;
177             foreach my $item (@list) {
178                 $item =~ s/\r//;
179                 push @quoted_list, C4::Context->dbh->quote($item);
180             }
181             $quoted = "(" . join( ",", @quoted_list ) . ")";
182         }
183         $sql =~ s/<<$split[$i*2+1]>>/$quoted/;
184     }
185     return $sql, $headers;
186 }
187
188 =head3 _type
189
190 Returns name of corresponding DBIC resultset
191
192 =cut
193
194 sub _type {
195     return 'SavedSql';
196 }
197
198 1;