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 C4::Dates qw( DHTMLcalendar );
30 Script to control the guided report creation
40 my $referer = $input->referer();
42 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
44 template_name => "reports/guided_reports_start.tmpl",
48 flagsrequired => { editcatalogue => 1 },
53 my $phase = $input->param('phase');
54 my $no_html = 0; # this will be set if we dont want to print out an html::template
57 $template->param( 'start' => 1 );
62 elsif ( $phase eq 'Build new' ) {
65 $template->param( 'build1' => 1 );
68 my $areas = C4::Reports::get_report_areas();
69 $template->param( 'areas' => $areas );
73 elsif ( $phase eq 'Used saved' ) {
76 # get list of reports and display them
77 $template->param( 'saved1' => 1 );
78 my $reports = get_saved_reports();
79 $template->param( 'savedreports' => $reports );
82 elsif ( $phase eq 'Delete Saved') {
84 # delete a report from the saved reports list
86 my $id = $input->param('reports');
88 print $input->redirect("/cgi-bin/koha/reports/guided_reports.pl?phase=Used%20saved");
92 elsif ( $phase eq 'Show SQL'){
94 my $id = $input->param('reports');
95 my $sql = get_sql($id);
102 elsif ($phase eq 'retrieve results') {
103 my $id = $input->param('id');
104 my ($results,$name,$notes) = format_results($id);
108 'results' => $results,
115 elsif ( $phase eq 'Report on this Area' ) {
117 # they have choosen a new report and the area to report on
119 my $area = $input->param('areas');
126 my $types = C4::Reports::get_report_types();
127 $template->param( 'types' => $types );
130 elsif ( $phase eq 'Choose this type' ) {
132 # they have chosen type and area
133 # get area and type and pass them to the template
134 my $area = $input->param('area');
135 my $type = $input->param('types');
143 my $columns = get_columns($area,$input);
144 $template->param( 'columns' => $columns );
147 elsif ( $phase eq 'Choose these columns' ) {
149 # we now know type, area, and columns
150 # next step is the constraints
151 my $area = $input->param('area');
152 my $type = $input->param('type');
153 my @columns = $input->param('columns');
154 my $column = join( ',', @columns );
155 my $definitions = get_from_dictionary($area);
162 my $criteria = get_criteria($area,$input);
163 $template->param( 'criteria' => $criteria,
164 'definitions' => $definitions);
167 elsif ( $phase eq 'Choose these criteria' ) {
168 my $area = $input->param('area');
169 my $type = $input->param('type');
170 my $column = $input->param('column');
171 my @definitions = $input->param('definition');
172 my $definition = join (',',@definitions);
173 my @criteria = $input->param('criteria_column');
175 foreach my $crit (@criteria) {
176 my $value = $input->param( $crit . "_value" );
178 if ($value =~ C4::Dates->regexp(C4::Context->preference('dateformat'))) {
179 my $date = C4::Dates->new($value);
180 $value = $date->output("iso");
182 $query_criteria .= " AND $crit='$value'";
191 'definition' => $definition,
192 'criteriastring' => $query_criteria,
196 my @columns = split( ',', $column );
199 # build structue for use by tmpl_loop to choose columns to order by
200 # need to do something about the order of the order :)
201 # we also want to use the %columns hash to get the plain english names
202 foreach my $col (@columns) {
204 $total{'name'} = $col;
207 $select1{'value'} = 'sum';
208 push @selects, \%select1;
210 $select2{'value'} = 'min';
211 push @selects, \%select2;
213 $select3{'value'} = 'max';
214 push @selects, \%select3;
216 $select4{'value'} = 'avg';
217 push @selects, \%select4;
219 $select5{'value'} = 'count';
220 push @selects, \%select5;
222 $total{'select'} = \@selects;
223 push @total_by, \%total;
226 $template->param( 'total_by' => \@total_by );
229 elsif ( $phase eq 'Choose These Operations' ) {
230 my $area = $input->param('area');
231 my $type = $input->param('type');
232 my $column = $input->param('column');
233 my $criteria = $input->param('criteria');
234 my $definition = $input->param('definition');
235 my @total_by = $input->param('total_by');
237 foreach my $total (@total_by) {
238 my $value = $input->param( $total . "_tvalue" );
239 $totals .= "$value($total),";
247 'criteriastring' => $criteria,
249 'definition' => $definition,
253 my @columns = split( ',', $column );
256 # build structue for use by tmpl_loop to choose columns to order by
257 # need to do something about the order of the order :)
258 foreach my $col (@columns) {
260 $order{'name'} = $col;
263 $select1{'value'} = 'asc';
264 push @selects, \%select1;
266 $select2{'value'} = 'desc';
267 push @selects, \%select2;
268 $order{'select'} = \@selects;
269 push @order_by, \%order;
272 $template->param( 'order_by' => \@order_by );
275 elsif ( $phase eq 'Build Report' ) {
277 # now we have all the info we need and can build the sql
278 my $area = $input->param('area');
279 my $type = $input->param('type');
280 my $column = $input->param('column');
281 my $crit = $input->param('criteria');
282 my $totals = $input->param('totals');
283 my $definition = $input->param('definition');
284 # my @criteria = split( ',', $crit );
285 my $query_criteria=$crit;
286 # split the columns up by ,
287 my @columns = split( ',', $column );
288 my @order_by = $input->param('order_by');
291 foreach my $order (@order_by) {
292 my $value = $input->param( $order . "_ovalue" );
293 if ($query_orderby) {
294 $query_orderby .= ",$order $value";
297 $query_orderby = " ORDER BY $order $value";
303 build_query( \@columns, $query_criteria, $query_orderby, $area, $totals, $definition );
311 elsif ( $phase eq 'Save' ) {
312 # Save the report that has just been built
313 my $sql = $input->param('sql');
314 my $type = $input->param('type');
322 elsif ( $phase eq 'Save Report' ) {
323 # save the sql pasted in by a user
324 my $sql = $input->param('sql');
325 my $name = $input->param('reportname');
326 my $type = $input->param('type');
327 my $notes = $input->param('notes');
328 save_report( $sql, $name, $type, $notes );
330 'save_successful' => 1,
334 # This condition is not used currently
335 #elsif ( $phase eq 'Execute' ) {
336 # # run the sql, and output results in a template
337 # my $sql = $input->param('sql');
338 # my $type = $input->param('type');
339 # my $results = execute_query($sql, $type);
341 # 'results' => $results,
347 elsif ($phase eq 'Run this report'){
348 # execute a saved report
349 # FIXME: The default limit should not be hardcoded...
352 my $report = $input->param('reports');
354 if ($input->param('page')) {
355 $offset = ($input->param('page') - 1) * 20;
359 my ($sql,$type,$name,$notes) = get_saved_report($report);
360 my ($results, $total) = execute_query($sql, $type, $offset, $limit);
361 my $totpages = int($total/$limit) + (($total % $limit) > 0 ? 1 : 0);
362 my $url = "/cgi-bin/koha/reports/guided_reports.pl?reports=$report&phase=Run%20this%20report";
364 'results' => $results,
369 'pagination_bar' => pagination_bar($url, $totpages, $input->param('page'), "page"),
373 elsif ($phase eq 'Export'){
374 # export results to tab separated text
375 my $sql = $input->param('sql');
377 print $input->header( -type => 'application/octet-stream',
378 -attachment=>'reportresults.csv');
379 my $format=$input->param('format');
380 my $results = execute_query($sql,1,0,0,$format);
385 elsif ($phase eq 'Create report from SQL'){
386 # alllow the user to paste in sql
387 $template->param('create' => 1);
388 my $types = C4::Reports::get_report_types();
389 $template->param( 'types' => $types );
392 elsif ($phase eq 'Create Compound Report'){
393 my $reports = get_saved_reports();
394 $template->param( 'savedreports' => $reports,
399 elsif ($phase eq 'Save Compound'){
400 my $master = $input->param('master');
401 my $subreport = $input->param('subreport');
402 # my $compound_report = create_compound($master,$subreport);
403 # my $results = run_compound($compound_report);
404 my ($mastertables,$subtables) = create_compound($master,$subreport);
405 $template->param( 'save_compound' => 1,
406 master=>$mastertables,
412 $template->param( 'referer' => $referer,
413 'DHTMLcalendar_dateformat' => C4::Dates->DHTMLcalendar(),
418 output_html_with_http_headers $input, $cookie, $template->output;