Bug 27380: Move get_prepped_report to object and use for svc/reports
[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 use Carp;
21
22 use Koha::Database;
23 use JSON;
24 use Koha::Reports;
25
26 use base qw(Koha::Object);
27
28 =head1 NAME
29
30 Koha::Report - Koha Report Object class
31
32 =head1 API
33
34 =head2 Class Methods
35
36 =cut
37
38 =head3 get_search_info
39
40 Return search info
41
42 =cut
43
44 sub get_search_info {
45     my $self = shift;
46     my $sub_mana_info = { 'query' => shift };
47     return $sub_mana_info;
48 }
49
50 =head3 get_sharable_info
51
52 Return properties that can be shared.
53
54 =cut
55
56 sub get_sharable_info {
57     my $self             = shift;
58     my $shared_report_id = shift;
59     my $report           = Koha::Reports->find($shared_report_id);
60     my $sub_mana_info    = {
61         'savedsql'     => $report->savedsql,
62         'report_name'  => $report->report_name,
63         'notes'        => $report->notes,
64         'report_group' => $report->report_group,
65         'type'         => $report->type,
66     };
67     return $sub_mana_info;
68 }
69
70 =head3 new_from_mana
71
72 Clear a Mana report to be imported in Koha?
73
74 =cut
75
76 sub new_from_mana {
77     my $self = shift;
78     my $data = shift;
79
80     $data->{mana_id} = $data->{id};
81
82     delete $data->{exportemail};
83     delete $data->{kohaversion};
84     delete $data->{creationdate};
85     delete $data->{lastimport};
86     delete $data->{id};
87     delete $data->{nbofusers};
88     delete $data->{language};
89
90     Koha::Report->new($data)->store;
91 }
92
93 =head3 prep_report
94
95 Prep the report and return executable sql with parameters embedded and a list of header types
96 for building batch action links in the template
97
98 =cut
99
100 sub prep_report {
101     my ( $self, $param_names, $sql_params ) = @_;
102     my $sql = $self->savedsql;
103
104     # First we split out the placeholders
105     # This part of the code supports using [[ table.field | alias ]] in the
106     # query and replaces it by table.field AS alias. This is used to build
107     # the batch action links foir cardnumbers, itemnumbers, and biblionumbers in the template
108     # while allowing the library to alter the column names
109     my @split = split /\[\[|\]\]/, $sql;
110     my $headers;
111     for ( my $i = 0 ; $i < $#split / 2 ; $i++ )
112     {    #The placeholders are always the odd elements of the array
113         my ( $type, $name ) = split /\|/,
114           $split[ $i * 2 + 1 ];    # We split them on '|'
115         $headers->{$name} = $type; # Store as a lookup for the template
116         $headers->{$name} =~
117           s/^\w*\.//;    # strip the table name just as in $sth->{NAME} array
118         $split[ $i * 2 + 1 ] =~ s/(\||\?|\.|\*|\(|\)|\%)/\\$1/g
119           ;    #Quote any special characters so we can replace the placeholders
120         $name = C4::Context->dbh->quote($name);
121         $sql =~ s/\[\[$split[$i*2+1]\]\]/$type AS $name/
122           ;    # Remove placeholders from SQL
123     }
124
125     my %lookup;
126     @lookup{@$param_names} = @$sql_params;
127     @split = split /<<|>>/, $sql;
128     for ( my $i = 0 ; $i < $#split / 2 ; $i++ ) {
129         my $quoted =
130           @$param_names ? $lookup{ $split[ $i * 2 + 1 ] } : @$sql_params[$i];
131
132         # if there are special regexp chars, we must \ them
133         $split[ $i * 2 + 1 ] =~ s/(\||\?|\.|\*|\(|\)|\%)/\\$1/g;
134         if ( $split[ $i * 2 + 1 ] =~ /\|\s*date\s*$/ ) {
135             $quoted = output_pref(
136                 {
137                     dt         => dt_from_string($quoted),
138                     dateformat => 'iso',
139                     dateonly   => 1
140                 }
141             ) if $quoted;
142         }
143         unless ( $split[ $i * 2 + 1 ] =~ /\|\s*list\s*$/ && $quoted ) {
144             $quoted = C4::Context->dbh->quote($quoted);
145         }
146         else {
147             my @list = split /\n/, $quoted;
148             my @quoted_list;
149             foreach my $item (@list) {
150                 $item =~ s/\r//;
151                 push @quoted_list, C4::Context->dbh->quote($item);
152             }
153             $quoted = "(" . join( ",", @quoted_list ) . ")";
154         }
155         $sql =~ s/<<$split[$i*2+1]>>/$quoted/;
156     }
157     return $sql, $headers;
158 }
159
160 =head3 _type
161
162 Returns name of corresponding DBIC resultset
163
164 =cut
165
166 sub _type {
167     return 'SavedSql';
168 }
169
170 1;