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 = format_results($id);
105 'results' => $results,
110 elsif ( $phase eq 'Report on this Area' ) {
112 # they have choosen a new report and the area to report on
114 my $area = $input->param('areas');
121 my $types = C4::Reports::get_report_types();
122 $template->param( 'types' => $types );
125 elsif ( $phase eq 'Choose this type' ) {
127 # they have chosen type and area
128 # get area and type and pass them to the template
129 my $area = $input->param('area');
130 my $type = $input->param('types');
138 my $columns = get_columns($area);
139 $template->param( 'columns' => $columns );
142 elsif ( $phase eq 'Choose these columns' ) {
144 # we now know type, area, and columns
145 # next step is the constraints
146 my $area = $input->param('area');
147 my $type = $input->param('type');
148 my @columns = $input->param('columns');
149 my $column = join( ',', @columns );
150 my $definitions = get_from_dictionary($area);
157 my $criteria = get_criteria($area);
158 $template->param( 'criteria' => $criteria,
159 'definitions' => $definitions);
162 elsif ( $phase eq 'Choose these criteria' ) {
163 my $area = $input->param('area');
164 my $type = $input->param('type');
165 my $column = $input->param('column');
166 my @definitions = $input->param('definition');
167 my $definition = join (',',@definitions);
168 my @criteria = $input->param('criteria_column');
170 foreach my $crit (@criteria) {
171 my $value = $input->param( $crit . "_value" );
173 $query_criteria .= " AND $crit='$value'";
182 'definition' => $definition,
183 'criteriastring' => $query_criteria,
187 my @columns = split( ',', $column );
190 # build structue for use by tmpl_loop to choose columns to order by
191 # need to do something about the order of the order :)
192 # we also want to use the %columns hash to get the plain english names
193 foreach my $col (@columns) {
195 $total{'name'} = $col;
198 $select1{'value'} = 'sum';
199 push @selects, \%select1;
201 $select2{'value'} = 'min';
202 push @selects, \%select2;
204 $select3{'value'} = 'max';
205 push @selects, \%select3;
207 $select4{'value'} = 'avg';
208 push @selects, \%select4;
210 $select5{'value'} = 'count';
211 push @selects, \%select5;
213 $total{'select'} = \@selects;
214 push @total_by, \%total;
217 $template->param( 'total_by' => \@total_by );
220 elsif ( $phase eq 'Choose These Operations' ) {
221 my $area = $input->param('area');
222 my $type = $input->param('type');
223 my $column = $input->param('column');
224 my $criteria = $input->param('criteria');
225 my $definition = $input->param('definition');
226 my @total_by = $input->param('total_by');
228 foreach my $total (@total_by) {
229 my $value = $input->param( $total . "_tvalue" );
230 $totals .= "$value($total),";
238 'criteriastring' => $criteria,
240 'definition' => $definition,
244 my @columns = split( ',', $column );
247 # build structue for use by tmpl_loop to choose columns to order by
248 # need to do something about the order of the order :)
249 foreach my $col (@columns) {
251 $order{'name'} = $col;
254 $select1{'value'} = 'asc';
255 push @selects, \%select1;
257 $select2{'value'} = 'desc';
258 push @selects, \%select2;
259 $order{'select'} = \@selects;
260 push @order_by, \%order;
263 $template->param( 'order_by' => \@order_by );
266 elsif ( $phase eq 'Build Report' ) {
268 # now we have all the info we need and can build the sql
269 my $area = $input->param('area');
270 my $type = $input->param('type');
271 my $column = $input->param('column');
272 my $crit = $input->param('criteria');
273 my $totals = $input->param('totals');
274 my $definition = $input->param('definition');
275 # my @criteria = split( ',', $crit );
276 my $query_criteria=$crit;
277 # split the columns up by ,
278 my @columns = split( ',', $column );
279 my @order_by = $input->param('order_by');
282 foreach my $order (@order_by) {
283 my $value = $input->param( $order . "_ovalue" );
284 if ($query_orderby) {
285 $query_orderby .= ",$order $value";
288 $query_orderby = " ORDER BY $order $value";
294 build_query( \@columns, $query_criteria, $query_orderby, $area, $totals, $definition );
302 elsif ( $phase eq 'Save' ) {
303 # Save the report that has just been built
304 my $sql = $input->param('sql');
305 my $type = $input->param('type');
307 'save_successful' => 1,
313 elsif ( $phase eq 'Save Report' ) {
314 # save the sql pasted in by a user
315 my $sql = $input->param('sql');
316 my $name = $input->param('reportname');
317 my $type = $input->param('type');
318 my $notes = $input->param('notes');
319 save_report( $sql, $name, $type, $notes );
321 'save_successful' => 1,
325 elsif ( $phase eq 'Execute' ) {
326 # run the sql, and output results in a template
327 my $sql = $input->param('sql');
328 my $type = $input->param('type');
329 my $results = execute_query($sql,$type);
331 'results' => $results,
337 elsif ($phase eq 'Run this report'){
338 # execute a saved report
339 my $report = $input->param('reports');
340 my ($sql,$type,$name,$notes) = get_saved_report($report);
341 my $results = execute_query($sql,$type);
343 'results' => $results,
351 elsif ($phase eq 'Export'){
352 # export results to tab separated text
353 my $sql = $input->param('sql');
355 print $input->header( -type => 'application/octet-stream',
356 -attachment=>'reportresults.csv');
357 my $format=$input->param('format');
358 my $results = execute_query($sql,1,$format);
363 elsif ($phase eq 'Create report from SQL'){
364 # alllow the user to paste in sql
365 $template->param('create' => 1);
366 my $types = C4::Reports::get_report_types();
367 $template->param( 'types' => $types );
370 elsif ($phase eq 'Create Compound Report'){
371 my $reports = get_saved_reports();
372 $template->param( 'savedreports' => $reports,
377 elsif ($phase eq 'Save Compound'){
378 my $master = $input->param('master');
379 my $subreport = $input->param('subreport');
380 # my $compound_report = create_compound($master,$subreport);
381 # my $results = run_compound($compound_report);
382 my ($mastertables,$subtables) = create_compound($master,$subreport);
383 $template->param( 'save_compound' => 1,
384 master=>$mastertables,
390 $template->param( 'referer' => $referer );
394 output_html_with_http_headers $input, $cookie, $template->output;