Getting stored results working
[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 'retrieve results') {
90         my $id = $input->param('id');
91         my $results = format_results($id);
92         # do something
93         $template->param(
94                 'retresults' => 1,
95                 'results' => $results,
96                 );
97         
98 }
99
100 elsif ( $phase eq 'Report on this Area' ) {
101
102     # they have choosen a new report and the area to report on
103     # get area
104     my $area = $input->param('areas');
105     $template->param(
106         'build2' => 1,
107         'area'   => $area
108     );
109
110     # get report types
111     my $types = C4::Reports::get_report_types();
112     $template->param( 'types' => $types );
113 }
114
115 elsif ( $phase eq 'Choose this type' ) {
116
117     # they have chosen type and area
118     # get area and type and pass them to the template
119     my $area = $input->param('area');
120     my $type = $input->param('types');
121     $template->param(
122         'build3' => 1,
123         'area'   => $area,
124         'type'   => $type,
125     );
126
127     # get columns
128     my $columns = get_columns($area);
129     $template->param( 'columns' => $columns );
130 }
131
132 elsif ( $phase eq 'Choose these columns' ) {
133
134     # we now know type, area, and columns
135     # next step is the constraints
136     my $area    = $input->param('area');
137     my $type    = $input->param('type');
138     my @columns = $input->param('columns');
139     my $column  = join( ',', @columns );
140         my $definitions = get_from_dictionary($area);
141     $template->param(
142         'build4' => 1,
143         'area'   => $area,
144         'type'   => $type,
145         'column' => $column,
146     );
147     my $criteria = get_criteria($area);
148     $template->param( 'criteria' => $criteria,
149         'definitions' => $definitions);
150 }
151
152 elsif ( $phase eq 'Choose these criteria' ) {
153     my $area     = $input->param('area');
154     my $type     = $input->param('type');
155     my $column   = $input->param('column');
156         my @definitions = $input->param('definition');
157         my $definition = join (',',@definitions);
158     my @criteria = $input->param('criteria_column');
159         my $query_criteria;
160     foreach my $crit (@criteria) {
161         my $value = $input->param( $crit . "_value" );
162         if ($value) {
163             $query_criteria .= " AND $crit='$value'";
164         }
165     }
166
167     $template->param(
168         'build5'         => 1,
169         'area'           => $area,
170         'type'           => $type,
171         'column'         => $column,
172                 'definition'     => $definition,
173         'criteriastring' => $query_criteria,
174     );
175
176     # get columns
177     my @columns = split( ',', $column );
178     my @total_by;
179
180     # build structue for use by tmpl_loop to choose columns to order by
181     # need to do something about the order of the order :)
182         # we also want to use the %columns hash to get the plain english names
183     foreach my $col (@columns) {
184         my %total;
185         $total{'name'} = $col;
186         my @selects;
187         my %select1;
188         $select1{'value'} = 'sum';
189         push @selects, \%select1;
190         my %select2;
191         $select2{'value'} = 'min';
192         push @selects, \%select2;
193         my %select3;
194         $select3{'value'} = 'max';
195         push @selects, \%select3;
196         my %select4;
197         $select4{'value'} = 'avg';
198         push @selects, \%select4;
199         my %select5;
200         $select5{'value'} = 'count';
201         push @selects, \%select5;
202
203         $total{'select'} = \@selects;
204         push @total_by, \%total;
205     }
206
207     $template->param( 'total_by' => \@total_by );
208 }
209
210 elsif ( $phase eq 'Choose These Operations' ) {
211     my $area     = $input->param('area');
212     my $type     = $input->param('type');
213     my $column   = $input->param('column');
214     my $criteria = $input->param('criteria');
215         my $definition = $input->param('definition');
216     my @total_by = $input->param('total_by');
217     my $totals;
218     foreach my $total (@total_by) {
219         my $value = $input->param( $total . "_tvalue" );
220         $totals .= "$value($total),";
221     }
222
223     $template->param(
224         'build6'         => 1,
225         'area'           => $area,
226         'type'           => $type,
227         'column'         => $column,
228         'criteriastring' => $criteria,
229         'totals'         => $totals,
230                 'definition'    => $definition,
231     );
232
233     # get columns
234     my @columns = split( ',', $column );
235     my @order_by;
236
237     # build structue for use by tmpl_loop to choose columns to order by
238     # need to do something about the order of the order :)
239     foreach my $col (@columns) {
240         my %order;
241         $order{'name'} = $col;
242         my @selects;
243         my %select1;
244         $select1{'value'} = 'asc';
245         push @selects, \%select1;
246         my %select2;
247         $select2{'value'} = 'desc';
248         push @selects, \%select2;
249         $order{'select'} = \@selects;
250         push @order_by, \%order;
251     }
252
253     $template->param( 'order_by' => \@order_by );
254 }
255
256 elsif ( $phase eq 'Build Report' ) {
257
258     # now we have all the info we need and can build the sql
259     my $area     = $input->param('area');
260     my $type     = $input->param('type');
261     my $column   = $input->param('column');
262     my $crit     = $input->param('criteria');
263     my $totals   = $input->param('totals');
264         my $definition = $input->param('definition');
265 #    my @criteria = split( ',', $crit );
266     my $query_criteria=$crit;
267     # split the columns up by ,
268     my @columns = split( ',', $column );
269     my @order_by = $input->param('order_by');
270
271     my $query_orderby;
272     foreach my $order (@order_by) {
273         my $value = $input->param( $order . "_ovalue" );
274         if ($query_orderby) {
275             $query_orderby .= ",$order $value";
276         }
277         else {
278             $query_orderby = " ORDER BY $order $value";
279         }
280     }
281
282     # get the sql
283     my $sql =
284       build_query( \@columns, $query_criteria, $query_orderby, $area, $totals, $definition );
285     $template->param(
286         'showreport' => 1,
287         'sql'        => $sql,
288         'type'       => $type
289     );
290 }
291
292 elsif ( $phase eq 'Save' ) {
293         # Save the report that has just been built
294     my $sql  = $input->param('sql');
295     my $type = $input->param('type');
296     $template->param(
297         'save' => 1,
298         'sql'  => $sql,
299         'type' => $type
300     );
301 }
302
303 elsif ( $phase eq 'Save Report' ) {
304     # save the sql pasted in by a user 
305     my $sql  = $input->param('sql');
306     my $name = $input->param('reportname');
307     my $type = $input->param('type');
308         my $notes = $input->param('notes');
309     save_report( $sql, $name, $type, $notes );
310 }
311
312 elsif ( $phase eq 'Execute' ) {
313         # run the sql, and output results in a template 
314     my $sql     = $input->param('sql');
315     my $type    = $input->param('type');
316     my $results = execute_query($sql,$type);
317     $template->param(
318         'results' => $results,
319                 'sql' => $sql,
320         'execute' => 1
321     );
322 }
323
324 elsif ($phase eq 'Run this report'){
325     # execute a saved report
326         my $report = $input->param('reports');
327         my ($sql,$type) = get_saved_report($report);
328         my $results = execute_query($sql,$type);
329     $template->param(
330         'results' => $results,
331                 'sql' => $sql,
332         'execute' => 1
333     );
334 }       
335
336 elsif ($phase eq 'Export'){
337         # export results to tab separated text
338         my $sql     = $input->param('sql');
339         $no_html=1;
340         print $input->header(   -type => 'application/octet-stream',
341                   -attachment=>'reportresults.csv');
342         my $format=$input->param('format');
343         my $results = execute_query($sql,1,$format);
344         print $results;
345         
346 }
347
348 elsif ($phase eq 'Create report from SQL'){
349         # alllow the user to paste in sql 
350         $template->param('create' => 1);
351          my $types = C4::Reports::get_report_types();
352         $template->param( 'types' => $types ); 
353 }
354
355 elsif ($phase eq 'Create Compound Report'){
356         my $reports = get_saved_reports();  
357         $template->param( 'savedreports' => $reports,
358                 'compound' => 1,
359         );
360 }
361
362 elsif ($phase eq 'Save Compound'){
363     my $master = $input->param('master');
364         my $subreport = $input->param('subreport');
365 #       my $compound_report = create_compound($master,$subreport);
366 #       my $results = run_compound($compound_report);
367         my ($mastertables,$subtables) = create_compound($master,$subreport);
368         $template->param( 'save_compound' => 1,
369                 master=>$mastertables,
370                 subsql=>$subtables
371         );
372 }
373
374
375 $template->param( 'referer' => $referer );
376
377
378 if (!$no_html){
379         output_html_with_http_headers $input, $cookie, $template->output;
380 }