Can now delete reports
[koha.git] / reports / guided_reports.pl
1 #!/usr/bin/perl
2
3 # Copyright 2007 Liblime ltd
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA  02111-1307 USA
19 use CGI::Carp qw(fatalsToBrowser warningsToBrowser);
20 use strict;
21 use C4::Auth;
22 use CGI;
23 use C4::Output;
24 use C4::Reports;
25
26 =head1 NAME
27
28 Script to control the guided report creation
29
30 =head1 DESCRIPTION
31
32
33 =over2
34
35 =cut
36
37 my $input = new CGI;
38 my $referer = $input->referer();
39 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
40     {
41         template_name   => "reports/guided_reports_start.tmpl",
42         query           => $input,
43         type            => "intranet",
44         authnotrequired => 0,
45         flagsrequired   => { editcatalogue => 1 },
46         debug           => 1,
47     }
48 );
49
50 my $phase = $input->param('phase');
51 my $no_html = 0; # this will be set if we dont want to print out an html::template
52
53 if ( !$phase ) {
54     $template->param( 'start' => 1 );
55
56     # show welcome page
57 }
58
59 elsif ( $phase eq 'Build new' ) {
60
61     # build a new report
62     $template->param( 'build1' => 1 );
63
64     # get report areas
65     my $areas = C4::Reports::get_report_areas();
66     $template->param( 'areas' => $areas );
67
68 }
69
70 elsif ( $phase eq 'Used saved' ) {
71
72     # use a saved report
73     # get list of reports and display them
74     $template->param( 'saved1' => 1 );
75     my $reports = get_saved_reports();
76     $template->param( 'savedreports' => $reports );
77 }
78
79 elsif ( $phase eq 'Delete Saved') {
80         
81         # delete a report from the saved reports list
82         $no_html = 1;
83         my $id = $input->param('reports');
84         delete_report($id);
85     print $input->redirect("/cgi-bin/koha/reports/guided_reports.pl?phase=Used%20saved");
86         
87 }               
88
89 elsif ( $phase eq 'Report on this Area' ) {
90
91     # they have choosen a new report and the area to report on
92     # get area
93     my $area = $input->param('areas');
94     $template->param(
95         'build2' => 1,
96         'area'   => $area
97     );
98
99     # get report types
100     my $types = C4::Reports::get_report_types();
101     $template->param( 'types' => $types );
102 }
103
104 elsif ( $phase eq 'Choose this type' ) {
105
106     # they have chosen type and area
107     # get area and type and pass them to the template
108     my $area = $input->param('area');
109     my $type = $input->param('types');
110     $template->param(
111         'build3' => 1,
112         'area'   => $area,
113         'type'   => $type,
114     );
115
116     # get columns
117     my $columns = get_columns($area);
118     $template->param( 'columns' => $columns );
119 }
120
121 elsif ( $phase eq 'Choose these columns' ) {
122
123     # we now know type, area, and columns
124     # next step is the constraints
125     my $area    = $input->param('area');
126     my $type    = $input->param('type');
127     my @columns = $input->param('columns');
128     my $column  = join( ',', @columns );
129         my $definitions = get_from_dictionary($area);
130     $template->param(
131         'build4' => 1,
132         'area'   => $area,
133         'type'   => $type,
134         'column' => $column,
135     );
136     my $criteria = get_criteria($area);
137     $template->param( 'criteria' => $criteria,
138         'definitions' => $definitions);
139 }
140
141 elsif ( $phase eq 'Choose these criteria' ) {
142     my $area     = $input->param('area');
143     my $type     = $input->param('type');
144     my $column   = $input->param('column');
145         my @definitions = $input->param('definition');
146         my $definition = join (',',@definitions);
147     my @criteria = $input->param('criteria_column');
148         my $query_criteria;
149     foreach my $crit (@criteria) {
150         my $value = $input->param( $crit . "_value" );
151         if ($value) {
152             $query_criteria .= " AND $crit='$value'";
153         }
154     }
155
156     $template->param(
157         'build5'         => 1,
158         'area'           => $area,
159         'type'           => $type,
160         'column'         => $column,
161                 'definition'     => $definition,
162         'criteriastring' => $query_criteria,
163     );
164
165     # get columns
166     my @columns = split( ',', $column );
167     my @total_by;
168
169     # build structue for use by tmpl_loop to choose columns to order by
170     # need to do something about the order of the order :)
171         # we also want to use the %columns hash to get the plain english names
172     foreach my $col (@columns) {
173         my %total;
174         $total{'name'} = $col;
175         my @selects;
176         my %select1;
177         $select1{'value'} = 'sum';
178         push @selects, \%select1;
179         my %select2;
180         $select2{'value'} = 'min';
181         push @selects, \%select2;
182         my %select3;
183         $select3{'value'} = 'max';
184         push @selects, \%select3;
185         my %select4;
186         $select4{'value'} = 'avg';
187         push @selects, \%select4;
188         my %select5;
189         $select5{'value'} = 'count';
190         push @selects, \%select5;
191
192         $total{'select'} = \@selects;
193         push @total_by, \%total;
194     }
195
196     $template->param( 'total_by' => \@total_by );
197 }
198
199 elsif ( $phase eq 'Choose These Operations' ) {
200     my $area     = $input->param('area');
201     my $type     = $input->param('type');
202     my $column   = $input->param('column');
203     my $criteria = $input->param('criteria');
204         my $definition = $input->param('definition');
205     my @total_by = $input->param('total_by');
206     my $totals;
207     foreach my $total (@total_by) {
208         my $value = $input->param( $total . "_tvalue" );
209         $totals .= "$value($total),";
210     }
211
212     $template->param(
213         'build6'         => 1,
214         'area'           => $area,
215         'type'           => $type,
216         'column'         => $column,
217         'criteriastring' => $criteria,
218         'totals'         => $totals,
219                 'definition'    => $definition,
220     );
221
222     # get columns
223     my @columns = split( ',', $column );
224     my @order_by;
225
226     # build structue for use by tmpl_loop to choose columns to order by
227     # need to do something about the order of the order :)
228     foreach my $col (@columns) {
229         my %order;
230         $order{'name'} = $col;
231         my @selects;
232         my %select1;
233         $select1{'value'} = 'asc';
234         push @selects, \%select1;
235         my %select2;
236         $select2{'value'} = 'desc';
237         push @selects, \%select2;
238         $order{'select'} = \@selects;
239         push @order_by, \%order;
240     }
241
242     $template->param( 'order_by' => \@order_by );
243 }
244
245 elsif ( $phase eq 'Build Report' ) {
246
247     # now we have all the info we need and can build the sql
248     my $area     = $input->param('area');
249     my $type     = $input->param('type');
250     my $column   = $input->param('column');
251     my $crit     = $input->param('criteria');
252     my $totals   = $input->param('totals');
253         my $definition = $input->param('definition');
254 #    my @criteria = split( ',', $crit );
255     my $query_criteria=$crit;
256     # split the columns up by ,
257     my @columns = split( ',', $column );
258     my @order_by = $input->param('order_by');
259
260     my $query_orderby;
261     foreach my $order (@order_by) {
262         my $value = $input->param( $order . "_ovalue" );
263         if ($query_orderby) {
264             $query_orderby .= ",$order $value";
265         }
266         else {
267             $query_orderby = " ORDER BY $order $value";
268         }
269     }
270
271     # get the sql
272     my $sql =
273       build_query( \@columns, $query_criteria, $query_orderby, $area, $totals, $definition );
274     $template->param(
275         'showreport' => 1,
276         'sql'        => $sql,
277         'type'       => $type
278     );
279 }
280
281 elsif ( $phase eq 'Save' ) {
282         # Save the report that has just been built
283     my $sql  = $input->param('sql');
284     my $type = $input->param('type');
285     $template->param(
286         'save' => 1,
287         'sql'  => $sql,
288         'type' => $type
289     );
290 }
291
292 elsif ( $phase eq 'Save Report' ) {
293     # save the sql pasted in by a user 
294     my $sql  = $input->param('sql');
295     my $name = $input->param('reportname');
296     my $type = $input->param('type');
297         my $notes = $input->param('notes');
298     save_report( $sql, $name, $type, $notes );
299 }
300
301 elsif ( $phase eq 'Execute' ) {
302         # run the sql, and output results in a template 
303     my $sql     = $input->param('sql');
304     my $type    = $input->param('type');
305     my $results = execute_query($sql,$type);
306     $template->param(
307         'results' => $results,
308                 'sql' => $sql,
309         'execute' => 1
310     );
311 }
312
313 elsif ($phase eq 'Run this report'){
314     # execute a saved report
315         my $report = $input->param('reports');
316         my ($sql,$type) = get_saved_report($report);
317         my $results = execute_query($sql,$type);
318     $template->param(
319         'results' => $results,
320                 'sql' => $sql,
321         'execute' => 1
322     );
323 }       
324
325 elsif ($phase eq 'Export'){
326         # export results to tab separated text
327         my $sql     = $input->param('sql');
328         $no_html=1;
329         print $input->header(   -type => 'application/octet-stream',
330                   -attachment=>'reportresults.csv');
331         my $format=$input->param('format');
332         my $results = execute_query($sql,1,$format);
333         print $results;
334         
335 }
336
337 elsif ($phase eq 'Create report from SQL'){
338         # alllow the user to paste in sql 
339         $template->param('create' => 1);
340          my $types = C4::Reports::get_report_types();
341         $template->param( 'types' => $types ); 
342 }
343
344 elsif ($phase eq 'Create Compound Report'){
345         my $reports = get_saved_reports();  
346         $template->param( 'savedreports' => $reports,
347                 'compound' => 1,
348         );
349 }
350
351 elsif ($phase eq 'Save Compound'){
352     my $master = $input->param('master');
353         my $subreport = $input->param('subreport');
354 #       my $compound_report = create_compound($master,$subreport);
355 #       my $results = run_compound($compound_report);
356         my ($mastertables,$subtables) = create_compound($master,$subreport);
357         $template->param( 'save_compound' => 1,
358                 master=>$mastertables,
359                 subsql=>$subtables
360         );
361 }
362
363
364 $template->param( 'referer' => $referer );
365
366
367 if (!$no_html){
368         output_html_with_http_headers $input, $cookie, $template->output;
369 }