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
25 use CGI::Carp qw(fatalsToBrowser);
28 Script to control the guided report creation
38 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
40 template_name => "reports/guided_reports_start.tmpl",
44 flagsrequired => { editcatalogue => 1 },
49 my $phase = $input->param('phase');
50 my $no_html = 0; # this will be set if we dont want to print out an html::template
53 $template->param( 'start' => 1 );
58 elsif ( $phase eq 'Build new' ) {
61 $template->param( 'build1' => 1 );
64 my $areas = C4::Reports::get_report_areas();
65 $template->param( 'areas' => $areas );
69 elsif ( $phase eq 'Used saved' ) {
72 # get list of reports and display them
73 $template->param( 'saved1' => 1 );
74 my $reports = get_saved_reports();
75 $template->param( 'savedreports' => $reports );
78 elsif ( $phase eq 'Report on this Area' ) {
80 # they have choosen a new report and the area to report on
82 my $area = $input->param('areas');
89 my $types = C4::Reports::get_report_types();
90 $template->param( 'types' => $types );
93 elsif ( $phase eq 'Choose this type' ) {
95 # they have chosen type and area
96 # get area and type and pass them to the template
97 my $area = $input->param('area');
98 my $type = $input->param('types');
106 my $columns = get_columns($area);
107 $template->param( 'columns' => $columns );
110 elsif ( $phase eq 'Choose these columns' ) {
112 # we now know type, area, and columns
113 # next step is the constraints
114 my $area = $input->param('area');
115 my $type = $input->param('type');
116 my @columns = $input->param('columns');
117 my $column = join( ',', @columns );
124 my $criteria = get_criteria($area);
125 $template->param( 'criteria' => $criteria );
128 elsif ( $phase eq 'Choose these criteria' ) {
129 my $area = $input->param('area');
130 my $type = $input->param('type');
131 my $column = $input->param('column');
132 my @criteria = $input->param('criteria_column');
134 foreach my $crit (@criteria) {
135 my $value = $input->param( $crit . "_value" );
137 $query_criteria .= " AND $crit='$value'";
146 'criteriastring' => $query_criteria,
150 my @columns = split( ',', $column );
153 # build structue for use by tmpl_loop to choose columns to order by
154 # need to do something about the order of the order :)
155 # we also want to use the %columns hash to get the plain english names
156 foreach my $col (@columns) {
158 $total{'name'} = $col;
161 $select1{'value'} = 'sum';
162 push @selects, \%select1;
164 $select2{'value'} = 'min';
165 push @selects, \%select2;
167 $select3{'value'} = 'max';
168 push @selects, \%select3;
170 $select4{'value'} = 'avg';
171 push @selects, \%select4;
173 $select5{'value'} = 'count';
174 push @selects, \%select5;
176 $total{'select'} = \@selects;
177 push @total_by, \%total;
180 $template->param( 'total_by' => \@total_by );
183 elsif ( $phase eq 'Choose Totals' ) {
184 my $area = $input->param('area');
185 my $type = $input->param('type');
186 my $column = $input->param('column');
187 my $criteria = $input->param('criteria');
188 my @total_by = $input->param('total_by');
190 foreach my $total (@total_by) {
191 my $value = $input->param( $total . "_tvalue" );
192 $totals .= "$value($total),";
200 'criteriastring' => $criteria,
205 my @columns = split( ',', $column );
208 # build structue for use by tmpl_loop to choose columns to order by
209 # need to do something about the order of the order :)
210 foreach my $col (@columns) {
212 $order{'name'} = $col;
215 $select1{'value'} = 'asc';
216 push @selects, \%select1;
218 $select2{'value'} = 'desc';
219 push @selects, \%select2;
220 $order{'select'} = \@selects;
221 push @order_by, \%order;
224 $template->param( 'order_by' => \@order_by );
227 elsif ( $phase eq 'Build Report' ) {
229 # now we have all the info we need and can build the sql
230 my $area = $input->param('area');
231 my $type = $input->param('type');
232 my $column = $input->param('column');
233 my $crit = $input->param('criteria');
234 my $totals = $input->param('totals');
235 # my @criteria = split( ',', $crit );
236 my $query_criteria=$crit;
237 # split the columns up by ,
238 my @columns = split( ',', $column );
239 my @order_by = $input->param('order_by');
242 foreach my $order (@order_by) {
243 my $value = $input->param( $order . "_ovalue" );
244 if ($query_orderby) {
245 $query_orderby .= ",$order $value";
248 $query_orderby = " ORDER BY $order $value";
254 build_query( \@columns, $query_criteria, $query_orderby, $area, $totals );
262 elsif ( $phase eq 'Save' ) {
263 # Save the report that has just been built
264 my $sql = $input->param('sql');
265 my $type = $input->param('type');
273 elsif ( $phase eq 'Save Report' ) {
274 # save the sql pasted in by a user
275 my $sql = $input->param('sql');
276 my $name = $input->param('reportname');
277 my $type = $input->param('type');
278 my $notes = $input->param('notes');
279 save_report( $sql, $name, $type, $notes );
282 elsif ( $phase eq 'Execute' ) {
283 # run the sql, and output results in a template
284 my $sql = $input->param('sql');
285 my $type = $input->param('type');
286 my $results = execute_query($sql,$type);
288 'results' => $results,
294 elsif ($phase eq 'Run this report'){
295 # execute a saved report
296 my $report = $input->param('reports');
297 my ($sql,$type) = get_saved_report($report);
298 my $results = execute_query($sql,$type);
300 'results' => $results,
306 elsif ($phase eq 'Export'){
307 # export results to tab separated text
308 my $sql = $input->param('sql');
310 print $input->header( -type => 'application/octet-stream',
311 -attachment=>'reportresults.csv');
312 my $format=$input->param('format');
313 my $results = execute_query($sql,1,$format);
318 elsif ($phase eq 'Create report from SQL'){
319 # alllow the user to paste in sql
320 $template->param('create' => 1);
321 my $types = C4::Reports::get_report_types();
322 $template->param( 'types' => $types );
325 elsif ($phase eq 'Create Compound Report'){
326 my $reports = get_saved_reports();
327 $template->param( 'savedreports' => $reports,
332 elsif ($phase eq 'Save Compound'){
333 my $master = $input->param('master');
334 my $subreport = $input->param('subreport');
335 # my $compound_report = create_compound($master,$subreport);
336 # my $results = run_compound($compound_report);
337 my ($mastertables,$subtables) = create_compound($master,$subreport);
338 $template->param( 'save_compound' => 1,
339 master=>$mastertables,
344 elsif ($phase eq 'View Dictionary'){
345 # view the dictionary we use to set up abstract variables such as all borrowers over fifty who live in a certain town
346 ( $template, $borrowernumber, $cookie ) = get_template_and_user(
348 template_name => "reports/dictionary.tmpl",
351 authnotrequired => 0,
352 flagsrequired => { editcatalogue => 1 },
356 my $areas = C4::Reports::get_report_areas();
357 $template->param( 'areas' => $areas ,
358 'start_dictionary' => 1,
361 elsif ($phase eq 'Add New Definition'){
362 # display form allowing them to add a new definition
363 ( $template, $borrowernumber, $cookie ) = get_template_and_user(
365 template_name => "reports/dictionary.tmpl",
368 authnotrequired => 0,
369 flagsrequired => { editcatalogue => 1 },
373 $template->param( 'new_dictionary' => 1,
377 elsif ($phase eq 'New Term step 2'){
379 ( $template, $borrowernumber, $cookie ) = get_template_and_user(
381 template_name => "reports/dictionary.tmpl",
384 authnotrequired => 0,
385 flagsrequired => { editcatalogue => 1 },
389 my $areas = C4::Reports::get_report_areas();
390 $template->param( 'step_2' => 1,
393 my $definition_name=$input->param('definition_name');
394 my $definition_description=$input->param('definition_description');
397 elsif ($phase eq 'New Term step 3'){
398 # Choosing the columns
399 ( $template, $borrowernumber, $cookie ) = get_template_and_user(
401 template_name => "reports/dictionary.tmpl",
404 authnotrequired => 0,
405 flagsrequired => { editcatalogue => 1 },
409 my $area = $input->param('areas');
410 my $columns = get_columns($area);
411 $template->param( 'step_3' => 1,
413 'columns' => $columns,
417 elsif ($phase eq 'New Term step 4'){
418 # Choosing the columns
419 ( $template, $borrowernumber, $cookie ) = get_template_and_user(
421 template_name => "reports/dictionary.tmpl",
424 authnotrequired => 0,
425 flagsrequired => { editcatalogue => 1 },
429 $template->param( 'step_4' => 1,
435 output_html_with_http_headers $input, $cookie, $template->output;