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 'Report on this Area' ) {
81 # they have choosen a new report and the area to report on
83 my $area = $input->param('areas');
90 my $types = C4::Reports::get_report_types();
91 $template->param( 'types' => $types );
94 elsif ( $phase eq 'Choose this type' ) {
96 # they have chosen type and area
97 # get area and type and pass them to the template
98 my $area = $input->param('area');
99 my $type = $input->param('types');
107 my $columns = get_columns($area);
108 $template->param( 'columns' => $columns );
111 elsif ( $phase eq 'Choose these columns' ) {
113 # we now know type, area, and columns
114 # next step is the constraints
115 my $area = $input->param('area');
116 my $type = $input->param('type');
117 my @columns = $input->param('columns');
118 my $column = join( ',', @columns );
119 my $definitions = get_from_dictionary($area);
126 my $criteria = get_criteria($area);
127 $template->param( 'criteria' => $criteria,
128 'definitions' => $definitions);
131 elsif ( $phase eq 'Choose these criteria' ) {
132 my $area = $input->param('area');
133 my $type = $input->param('type');
134 my $column = $input->param('column');
135 my @definitions = $input->param('definition');
136 my $definition = join (',',@definitions);
137 my @criteria = $input->param('criteria_column');
139 foreach my $crit (@criteria) {
140 my $value = $input->param( $crit . "_value" );
142 $query_criteria .= " AND $crit='$value'";
151 'definition' => $definition,
152 'criteriastring' => $query_criteria,
156 my @columns = split( ',', $column );
159 # build structue for use by tmpl_loop to choose columns to order by
160 # need to do something about the order of the order :)
161 # we also want to use the %columns hash to get the plain english names
162 foreach my $col (@columns) {
164 $total{'name'} = $col;
167 $select1{'value'} = 'sum';
168 push @selects, \%select1;
170 $select2{'value'} = 'min';
171 push @selects, \%select2;
173 $select3{'value'} = 'max';
174 push @selects, \%select3;
176 $select4{'value'} = 'avg';
177 push @selects, \%select4;
179 $select5{'value'} = 'count';
180 push @selects, \%select5;
182 $total{'select'} = \@selects;
183 push @total_by, \%total;
186 $template->param( 'total_by' => \@total_by );
189 elsif ( $phase eq 'Choose These Operations' ) {
190 my $area = $input->param('area');
191 my $type = $input->param('type');
192 my $column = $input->param('column');
193 my $criteria = $input->param('criteria');
194 my $definition = $input->param('definition');
195 my @total_by = $input->param('total_by');
197 foreach my $total (@total_by) {
198 my $value = $input->param( $total . "_tvalue" );
199 $totals .= "$value($total),";
207 'criteriastring' => $criteria,
209 'definition' => $definition,
213 my @columns = split( ',', $column );
216 # build structue for use by tmpl_loop to choose columns to order by
217 # need to do something about the order of the order :)
218 foreach my $col (@columns) {
220 $order{'name'} = $col;
223 $select1{'value'} = 'asc';
224 push @selects, \%select1;
226 $select2{'value'} = 'desc';
227 push @selects, \%select2;
228 $order{'select'} = \@selects;
229 push @order_by, \%order;
232 $template->param( 'order_by' => \@order_by );
235 elsif ( $phase eq 'Build Report' ) {
237 # now we have all the info we need and can build the sql
238 my $area = $input->param('area');
239 my $type = $input->param('type');
240 my $column = $input->param('column');
241 my $crit = $input->param('criteria');
242 my $totals = $input->param('totals');
243 my $definition = $input->param('definition');
244 # my @criteria = split( ',', $crit );
245 my $query_criteria=$crit;
246 # split the columns up by ,
247 my @columns = split( ',', $column );
248 my @order_by = $input->param('order_by');
251 foreach my $order (@order_by) {
252 my $value = $input->param( $order . "_ovalue" );
253 if ($query_orderby) {
254 $query_orderby .= ",$order $value";
257 $query_orderby = " ORDER BY $order $value";
263 build_query( \@columns, $query_criteria, $query_orderby, $area, $totals, $definition );
271 elsif ( $phase eq 'Save' ) {
272 # Save the report that has just been built
273 my $sql = $input->param('sql');
274 my $type = $input->param('type');
282 elsif ( $phase eq 'Save Report' ) {
283 # save the sql pasted in by a user
284 my $sql = $input->param('sql');
285 my $name = $input->param('reportname');
286 my $type = $input->param('type');
287 my $notes = $input->param('notes');
288 save_report( $sql, $name, $type, $notes );
291 elsif ( $phase eq 'Execute' ) {
292 # run the sql, and output results in a template
293 my $sql = $input->param('sql');
294 my $type = $input->param('type');
295 my $results = execute_query($sql,$type);
297 'results' => $results,
303 elsif ($phase eq 'Run this report'){
304 # execute a saved report
305 my $report = $input->param('reports');
306 my ($sql,$type) = get_saved_report($report);
307 my $results = execute_query($sql,$type);
309 'results' => $results,
315 elsif ($phase eq 'Export'){
316 # export results to tab separated text
317 my $sql = $input->param('sql');
319 print $input->header( -type => 'application/octet-stream',
320 -attachment=>'reportresults.csv');
321 my $format=$input->param('format');
322 my $results = execute_query($sql,1,$format);
327 elsif ($phase eq 'Create report from SQL'){
328 # alllow the user to paste in sql
329 $template->param('create' => 1);
330 my $types = C4::Reports::get_report_types();
331 $template->param( 'types' => $types );
334 elsif ($phase eq 'Create Compound Report'){
335 my $reports = get_saved_reports();
336 $template->param( 'savedreports' => $reports,
341 elsif ($phase eq 'Save Compound'){
342 my $master = $input->param('master');
343 my $subreport = $input->param('subreport');
344 # my $compound_report = create_compound($master,$subreport);
345 # my $results = run_compound($compound_report);
346 my ($mastertables,$subtables) = create_compound($master,$subreport);
347 $template->param( 'save_compound' => 1,
348 master=>$mastertables,
354 $template->param( 'referer' => $referer );
358 output_html_with_http_headers $input, $cookie, $template->output;