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
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
19 use CGI
::Carp
qw(fatalsToBrowser warningsToBrowser);
28 Script to control the guided report creation
38 my $referer = $input->referer();
39 my ( $template, $borrowernumber, $cookie ) = get_template_and_user
(
41 template_name
=> "reports/guided_reports_start.tmpl",
45 flagsrequired
=> { editcatalogue
=> 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
54 $template->param( 'start' => 1 );
59 elsif ( $phase eq 'Build new' ) {
62 $template->param( 'build1' => 1 );
65 my $areas = C4
::Reports
::get_report_areas
();
66 $template->param( 'areas' => $areas );
70 elsif ( $phase eq 'Used saved' ) {
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
83 my $id = $input->param('reports');
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);
99 elsif ($phase eq 'retrieve results') {
100 my $id = $input->param('id');
101 my ($results,$name,$notes) = format_results
($id);
105 'results' => $results,
112 elsif ( $phase eq 'Report on this Area' ) {
114 # they have choosen a new report and the area to report on
116 my $area = $input->param('areas');
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');
140 my $columns = get_columns
($area);
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);
159 my $criteria = get_criteria
($area);
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');
172 foreach my $crit (@criteria) {
173 my $value = $input->param( $crit . "_value" );
175 $query_criteria .= " AND $crit='$value'";
184 'definition' => $definition,
185 'criteriastring' => $query_criteria,
189 my @columns = split( ',', $column );
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) {
197 $total{'name'} = $col;
200 $select1{'value'} = 'sum';
201 push @selects, \
%select1;
203 $select2{'value'} = 'min';
204 push @selects, \
%select2;
206 $select3{'value'} = 'max';
207 push @selects, \
%select3;
209 $select4{'value'} = 'avg';
210 push @selects, \
%select4;
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');
230 foreach my $total (@total_by) {
231 my $value = $input->param( $total . "_tvalue" );
232 $totals .= "$value($total),";
240 'criteriastring' => $criteria,
242 'definition' => $definition,
246 my @columns = split( ',', $column );
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) {
253 $order{'name'} = $col;
256 $select1{'value'} = 'asc';
257 push @selects, \
%select1;
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');
284 foreach my $order (@order_by) {
285 my $value = $input->param( $order . "_ovalue" );
286 if ($query_orderby) {
287 $query_orderby .= ",$order $value";
290 $query_orderby = " ORDER BY $order $value";
296 build_query
( \
@columns, $query_criteria, $query_orderby, $area, $totals, $definition );
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');
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 );
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);
333 'results' => $results,
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);
345 'results' => $results,
353 elsif ($phase eq 'Export'){
354 # export results to tab separated text
355 my $sql = $input->param('sql');
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);
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,
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,
392 $template->param( 'referer' => $referer );
396 output_html_with_http_headers
$input, $cookie, $template->output;