fix for bug 2371: Change fine block message behavior...
[koha.git] / reports / guided_reports.pl
blob140f48a3497344a7a5fc6269ae30ab2047bce6dd
1 #!/usr/bin/perl
3 # Copyright 2007 Liblime ltd
5 # This file is part of Koha.
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.
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.
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
20 use strict;
21 use CGI;
22 use C4::Reports;
23 use C4::Auth;
24 use C4::Output;
25 =head1 NAME
27 Script to control the guided report creation
29 =head1 DESCRIPTION
32 =over2
34 =cut
36 my $input = new CGI;
37 my $referer = $input->referer();
39 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
41 template_name => "reports/guided_reports_start.tmpl",
42 query => $input,
43 type => "intranet",
44 authnotrequired => 0,
45 flagsrequired => { editcatalogue => 1 },
46 debug => 1,
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
53 if ( !$phase ) {
54 $template->param( 'start' => 1 );
56 # show welcome page
59 elsif ( $phase eq 'Build new' ) {
61 # build a new report
62 $template->param( 'build1' => 1 );
64 # get report areas
65 my $areas = C4::Reports::get_report_areas();
66 $template->param( 'areas' => $areas );
70 elsif ( $phase eq 'Used saved' ) {
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 );
79 elsif ( $phase eq 'Delete Saved') {
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");
89 elsif ( $phase eq 'Show SQL'){
91 my $id = $input->param('reports');
92 my $sql = get_sql($id);
93 $template->param(
94 'sql' => $sql,
95 'showsql' => 1,
99 elsif ($phase eq 'retrieve results') {
100 my $id = $input->param('id');
101 my ($results,$name,$notes) = format_results($id);
102 # do something
103 $template->param(
104 'retresults' => 1,
105 'results' => $results,
106 'name' => $name,
107 'notes' => $notes,
112 elsif ( $phase eq 'Report on this Area' ) {
114 # they have choosen a new report and the area to report on
115 # get area
116 my $area = $input->param('areas');
117 $template->param(
118 'build2' => 1,
119 'area' => $area
122 # get report types
123 my $types = C4::Reports::get_report_types();
124 $template->param( 'types' => $types );
127 elsif ( $phase eq 'Choose this type' ) {
129 # they have chosen type and area
130 # get area and type and pass them to the template
131 my $area = $input->param('area');
132 my $type = $input->param('types');
133 $template->param(
134 'build3' => 1,
135 'area' => $area,
136 'type' => $type,
139 # get columns
140 my $columns = get_columns($area,$input);
141 $template->param( 'columns' => $columns );
144 elsif ( $phase eq 'Choose these columns' ) {
146 # we now know type, area, and columns
147 # next step is the constraints
148 my $area = $input->param('area');
149 my $type = $input->param('type');
150 my @columns = $input->param('columns');
151 my $column = join( ',', @columns );
152 my $definitions = get_from_dictionary($area);
153 $template->param(
154 'build4' => 1,
155 'area' => $area,
156 'type' => $type,
157 'column' => $column,
159 my $criteria = get_criteria($area,$input);
160 $template->param( 'criteria' => $criteria,
161 'definitions' => $definitions);
164 elsif ( $phase eq 'Choose these criteria' ) {
165 my $area = $input->param('area');
166 my $type = $input->param('type');
167 my $column = $input->param('column');
168 my @definitions = $input->param('definition');
169 my $definition = join (',',@definitions);
170 my @criteria = $input->param('criteria_column');
171 my $query_criteria;
172 foreach my $crit (@criteria) {
173 my $value = $input->param( $crit . "_value" );
174 if ($value) {
175 $query_criteria .= " AND $crit='$value'";
179 $template->param(
180 'build5' => 1,
181 'area' => $area,
182 'type' => $type,
183 'column' => $column,
184 'definition' => $definition,
185 'criteriastring' => $query_criteria,
188 # get columns
189 my @columns = split( ',', $column );
190 my @total_by;
192 # build structue for use by tmpl_loop to choose columns to order by
193 # need to do something about the order of the order :)
194 # we also want to use the %columns hash to get the plain english names
195 foreach my $col (@columns) {
196 my %total;
197 $total{'name'} = $col;
198 my @selects;
199 my %select1;
200 $select1{'value'} = 'sum';
201 push @selects, \%select1;
202 my %select2;
203 $select2{'value'} = 'min';
204 push @selects, \%select2;
205 my %select3;
206 $select3{'value'} = 'max';
207 push @selects, \%select3;
208 my %select4;
209 $select4{'value'} = 'avg';
210 push @selects, \%select4;
211 my %select5;
212 $select5{'value'} = 'count';
213 push @selects, \%select5;
215 $total{'select'} = \@selects;
216 push @total_by, \%total;
219 $template->param( 'total_by' => \@total_by );
222 elsif ( $phase eq 'Choose These Operations' ) {
223 my $area = $input->param('area');
224 my $type = $input->param('type');
225 my $column = $input->param('column');
226 my $criteria = $input->param('criteria');
227 my $definition = $input->param('definition');
228 my @total_by = $input->param('total_by');
229 my $totals;
230 foreach my $total (@total_by) {
231 my $value = $input->param( $total . "_tvalue" );
232 $totals .= "$value($total),";
235 $template->param(
236 'build6' => 1,
237 'area' => $area,
238 'type' => $type,
239 'column' => $column,
240 'criteriastring' => $criteria,
241 'totals' => $totals,
242 'definition' => $definition,
245 # get columns
246 my @columns = split( ',', $column );
247 my @order_by;
249 # build structue for use by tmpl_loop to choose columns to order by
250 # need to do something about the order of the order :)
251 foreach my $col (@columns) {
252 my %order;
253 $order{'name'} = $col;
254 my @selects;
255 my %select1;
256 $select1{'value'} = 'asc';
257 push @selects, \%select1;
258 my %select2;
259 $select2{'value'} = 'desc';
260 push @selects, \%select2;
261 $order{'select'} = \@selects;
262 push @order_by, \%order;
265 $template->param( 'order_by' => \@order_by );
268 elsif ( $phase eq 'Build Report' ) {
270 # now we have all the info we need and can build the sql
271 my $area = $input->param('area');
272 my $type = $input->param('type');
273 my $column = $input->param('column');
274 my $crit = $input->param('criteria');
275 my $totals = $input->param('totals');
276 my $definition = $input->param('definition');
277 # my @criteria = split( ',', $crit );
278 my $query_criteria=$crit;
279 # split the columns up by ,
280 my @columns = split( ',', $column );
281 my @order_by = $input->param('order_by');
283 my $query_orderby;
284 foreach my $order (@order_by) {
285 my $value = $input->param( $order . "_ovalue" );
286 if ($query_orderby) {
287 $query_orderby .= ",$order $value";
289 else {
290 $query_orderby = " ORDER BY $order $value";
294 # get the sql
295 my $sql =
296 build_query( \@columns, $query_criteria, $query_orderby, $area, $totals, $definition );
297 $template->param(
298 'showreport' => 1,
299 'sql' => $sql,
300 'type' => $type
304 elsif ( $phase eq 'Save' ) {
305 # Save the report that has just been built
306 my $sql = $input->param('sql');
307 my $type = $input->param('type');
308 $template->param(
309 'save' => 1,
310 'sql' => $sql,
311 'type' => $type
315 elsif ( $phase eq 'Save Report' ) {
316 # save the sql pasted in by a user
317 my $sql = $input->param('sql');
318 my $name = $input->param('reportname');
319 my $type = $input->param('type');
320 my $notes = $input->param('notes');
321 save_report( $sql, $name, $type, $notes );
322 $template->param(
323 'save_successful' => 1,
327 elsif ( $phase eq 'Execute' ) {
328 # run the sql, and output results in a template
329 my $sql = $input->param('sql');
330 my $type = $input->param('type');
331 my $results = execute_query($sql,$type);
332 $template->param(
333 'results' => $results,
334 'sql' => $sql,
335 'execute' => 1
339 elsif ($phase eq 'Run this report'){
340 # execute a saved report
341 my $report = $input->param('reports');
342 my ($sql,$type,$name,$notes) = get_saved_report($report);
343 my $results = execute_query($sql,$type);
344 $template->param(
345 'results' => $results,
346 'sql' => $sql,
347 'execute' => 1,
348 'name' => $name,
349 'notes' => $notes,
353 elsif ($phase eq 'Export'){
354 # export results to tab separated text
355 my $sql = $input->param('sql');
356 $no_html=1;
357 print $input->header( -type => 'application/octet-stream',
358 -attachment=>'reportresults.csv');
359 my $format=$input->param('format');
360 my $results = execute_query($sql,1,$format);
361 print $results;
365 elsif ($phase eq 'Create report from SQL'){
366 # alllow the user to paste in sql
367 $template->param('create' => 1);
368 my $types = C4::Reports::get_report_types();
369 $template->param( 'types' => $types );
372 elsif ($phase eq 'Create Compound Report'){
373 my $reports = get_saved_reports();
374 $template->param( 'savedreports' => $reports,
375 'compound' => 1,
379 elsif ($phase eq 'Save Compound'){
380 my $master = $input->param('master');
381 my $subreport = $input->param('subreport');
382 # my $compound_report = create_compound($master,$subreport);
383 # my $results = run_compound($compound_report);
384 my ($mastertables,$subtables) = create_compound($master,$subreport);
385 $template->param( 'save_compound' => 1,
386 master=>$mastertables,
387 subsql=>$subtables
392 $template->param( 'referer' => $referer );
395 if (!$no_html){
396 output_html_with_http_headers $input, $cookie, $template->output;