3 # This file is part of Koha.
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.
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.
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>.
23 #use Koha::DateUtils qw( dt_from_string output_pref );
25 use base qw(Koha::Object);
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
33 Koha::Report - Koha Report Object class
41 my ( $is_sql_valid, $errors ) = $report->is_sql_valid;
43 $errors is a arrayref of hashrefs, keys can be sqlerr or queryerr.
45 Validate SQL query string so it only contains a select,
46 not any of the harmful queries.
53 my $sql = $self->savedsql;
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' };
63 return ( @errors ? 0 : 1, \@errors );
66 =head3 get_search_info
74 my $sub_mana_info = { 'query' => shift };
75 return $sub_mana_info;
78 =head3 get_sharable_info
80 Return properties that can be shared.
84 sub get_sharable_info {
86 my $shared_report_id = shift;
87 my $report = Koha::Reports->find($shared_report_id);
89 'savedsql' => $report->savedsql,
90 'report_name' => $report->report_name,
91 'notes' => $report->notes,
92 'report_group' => $report->report_group,
93 'type' => $report->type,
95 return $sub_mana_info;
100 Clear a Mana report to be imported in Koha?
108 $data->{mana_id} = $data->{id};
110 delete $data->{exportemail};
111 delete $data->{kohaversion};
112 delete $data->{creationdate};
113 delete $data->{lastimport};
115 delete $data->{nbofusers};
116 delete $data->{language};
118 Koha::Report->new($data)->store;
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
129 my ( $self, $param_names, $sql_params ) = @_;
130 my $sql = $self->savedsql;
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;
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
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
154 @lookup{@$param_names} = @$sql_params;
155 @split = split /<<|>>/, $sql;
156 for ( my $i = 0 ; $i < $#split / 2 ; $i++ ) {
158 @$param_names ? $lookup{ $split[ $i * 2 + 1 ] } : @$sql_params[$i];
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(
165 # dt => dt_from_string($quoted),
166 # dateformat => 'iso',
171 unless ( $split[ $i * 2 + 1 ] =~ /\|\s*list\s*$/ && $quoted ) {
172 $quoted = C4::Context->dbh->quote($quoted);
175 my @list = split /\n/, $quoted;
177 foreach my $item (@list) {
179 push @quoted_list, C4::Context->dbh->quote($item);
181 $quoted = "(" . join( ",", @quoted_list ) . ")";
183 $sql =~ s/<<$split[$i*2+1]>>/$quoted/;
186 $sql = "$sql /* saved_sql.id: ${\( $self->id )} */";
187 return $sql, $headers;
192 Returns name of corresponding DBIC resultset