Lines 18-31
Link Here
|
18 |
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
18 |
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
19 |
|
19 |
|
20 |
use strict; |
20 |
use strict; |
21 |
#use warnings; FIXME - Bug 2505 |
21 |
use warnings; |
|
|
22 |
|
22 |
use CGI; |
23 |
use CGI; |
23 |
use Text::CSV; |
24 |
use Text::CSV; |
24 |
use URI::Escape; |
25 |
use URI::Escape; |
25 |
use C4::Reports::Guided; |
26 |
use C4::Reports::Guided; |
26 |
use C4::Auth qw/:DEFAULT get_session/; |
27 |
use C4::Auth qw/:DEFAULT get_session/; |
27 |
use C4::Output; |
28 |
use C4::Output; |
28 |
use C4::Dates; |
29 |
use C4::Dates qw/format_date/; |
29 |
use C4::Debug; |
30 |
use C4::Debug; |
30 |
use C4::Branch; # XXX subfield_is_koha_internal_p |
31 |
use C4::Branch; # XXX subfield_is_koha_internal_p |
31 |
|
32 |
|
Lines 68-74
my $session = $cookie ? get_session($cookie->value) : undef;
Link Here
|
68 |
my $filter; |
69 |
my $filter; |
69 |
if ( $input->param("filter_set") ) { |
70 |
if ( $input->param("filter_set") ) { |
70 |
$filter = {}; |
71 |
$filter = {}; |
71 |
$filter->{$_} = $input->param("filter_$_") foreach qw/date author keyword/; |
72 |
$filter->{$_} = $input->param("filter_$_") foreach qw/date author keyword group subgroup/; |
72 |
$session->param('report_filter', $filter) if $session; |
73 |
$session->param('report_filter', $filter) if $session; |
73 |
$template->param( 'filter_set' => 1 ); |
74 |
$template->param( 'filter_set' => 1 ); |
74 |
} |
75 |
} |
Lines 85-105
if ( !$phase ) {
Link Here
|
85 |
elsif ( $phase eq 'Build new' ) { |
86 |
elsif ( $phase eq 'Build new' ) { |
86 |
# build a new report |
87 |
# build a new report |
87 |
$template->param( 'build1' => 1 ); |
88 |
$template->param( 'build1' => 1 ); |
88 |
$template->param( 'areas' => get_report_areas(), 'usecache' => $usecache, 'cache_expiry' => 300, 'public' => '0' ); |
89 |
my $areas = get_report_areas(); |
89 |
} |
90 |
$template->param( |
90 |
elsif ( $phase eq 'Use saved' ) { |
91 |
'areas' => [map { id => $_->[0], name => $_->[1] }, @$areas], |
|
|
92 |
'usecache' => $usecache, |
93 |
'cache_expiry' => 300, |
94 |
'public' => '0', |
95 |
); |
96 |
} elsif ( $phase eq 'Use saved' ) { |
97 |
|
91 |
# use a saved report |
98 |
# use a saved report |
92 |
# get list of reports and display them |
99 |
# get list of reports and display them |
|
|
100 |
my $group = $input->param('group'); |
101 |
my $subgroup = $input->param('subgroup'); |
102 |
$filter->{group} = $group; |
103 |
$filter->{subgroup} = $subgroup; |
93 |
$template->param( |
104 |
$template->param( |
94 |
'saved1' => 1, |
105 |
'saved1' => 1, |
95 |
'savedreports' => get_saved_reports($filter), |
106 |
'savedreports' => get_saved_reports($filter), |
96 |
'usecache' => $usecache, |
107 |
'usecache' => $usecache, |
|
|
108 |
'groups_with_subgroups'=> groups_with_subgroups($group, $subgroup), |
97 |
); |
109 |
); |
98 |
if ($filter) { |
|
|
99 |
while ( my ($k, $v) = each %$filter ) { |
100 |
$template->param( "filter_$k" => $v ) if $v; |
101 |
} |
102 |
} |
103 |
} |
110 |
} |
104 |
|
111 |
|
105 |
elsif ( $phase eq 'Delete Saved') { |
112 |
elsif ( $phase eq 'Delete Saved') { |
Lines 113-142
elsif ( $phase eq 'Delete Saved') {
Link Here
|
113 |
|
120 |
|
114 |
elsif ( $phase eq 'Show SQL'){ |
121 |
elsif ( $phase eq 'Show SQL'){ |
115 |
|
122 |
|
116 |
my $id = $input->param('reports'); |
123 |
my $id = $input->param('reports'); |
117 |
my ($sql,$type,$reportname,$notes) = get_saved_report($id); |
124 |
my $report = get_saved_report($id); |
118 |
$template->param( |
125 |
$template->param( |
119 |
'id' => $id, |
126 |
'id' => $id, |
120 |
'reportname' => $reportname, |
127 |
'reportname' => $report->{report_name}, |
121 |
'notes' => $notes, |
128 |
'notes' => $report->{notes}, |
122 |
'sql' => $sql, |
129 |
'sql' => $report->{savedsql}, |
123 |
'showsql' => 1, |
130 |
'showsql' => 1, |
124 |
); |
131 |
); |
125 |
} |
132 |
} |
126 |
|
133 |
|
127 |
elsif ( $phase eq 'Edit SQL'){ |
134 |
elsif ( $phase eq 'Edit SQL'){ |
128 |
|
135 |
|
129 |
my $id = $input->param('reports'); |
136 |
my $id = $input->param('reports'); |
130 |
my ($sql,$type,$reportname,$notes, $cache_expiry, $public) = get_saved_report($id); |
137 |
my $report = get_saved_report($id); |
|
|
138 |
my $group = $report->{report_group}; |
139 |
my $subgroup = $report->{report_subgroup}; |
131 |
$template->param( |
140 |
$template->param( |
132 |
'sql' => $sql, |
141 |
'sql' => $report->{savedsql}, |
133 |
'reportname' => $reportname, |
142 |
'reportname' => $report->{report_name}, |
134 |
'notes' => $notes, |
143 |
'groups_with_subgroups' => groups_with_subgroups($group, $subgroup), |
|
|
144 |
'notes' => $report->{notes}, |
135 |
'id' => $id, |
145 |
'id' => $id, |
136 |
'cache_expiry' => $cache_expiry, |
146 |
'cache_expiry' => $report->{cache_expiry}, |
137 |
'public' => $public, |
147 |
'public' => $report->{public}, |
138 |
'usecache' => $usecache, |
148 |
'usecache' => $usecache, |
139 |
'editsql' => 1, |
149 |
'editsql' => 1, |
140 |
); |
150 |
); |
141 |
} |
151 |
} |
142 |
|
152 |
|
Lines 144-149
elsif ( $phase eq 'Update SQL'){
Link Here
|
144 |
my $id = $input->param('id'); |
154 |
my $id = $input->param('id'); |
145 |
my $sql = $input->param('sql'); |
155 |
my $sql = $input->param('sql'); |
146 |
my $reportname = $input->param('reportname'); |
156 |
my $reportname = $input->param('reportname'); |
|
|
157 |
my $group = $input->param('group'); |
158 |
my $subgroup = $input->param('subgroup'); |
147 |
my $notes = $input->param('notes'); |
159 |
my $notes = $input->param('notes'); |
148 |
my $cache_expiry = $input->param('cache_expiry'); |
160 |
my $cache_expiry = $input->param('cache_expiry'); |
149 |
my $cache_expiry_units = $input->param('cache_expiry_units'); |
161 |
my $cache_expiry_units = $input->param('cache_expiry_units'); |
Lines 177-192
elsif ( $phase eq 'Update SQL'){
Link Here
|
177 |
'errors' => \@errors, |
189 |
'errors' => \@errors, |
178 |
'sql' => $sql, |
190 |
'sql' => $sql, |
179 |
); |
191 |
); |
180 |
} |
192 |
} else { |
181 |
else { |
193 |
update_sql( $id, { |
182 |
update_sql( $id, $sql, $reportname, $notes, $cache_expiry, $public ); |
194 |
sql => $sql, |
|
|
195 |
name => $reportname, |
196 |
group => $group, |
197 |
subgroup => $subgroup, |
198 |
notes => $notes, |
199 |
cache_expiry => $cache_expiry, |
200 |
public => $public, |
201 |
} ); |
183 |
$template->param( |
202 |
$template->param( |
184 |
'save_successful' => 1, |
203 |
'save_successful' => 1, |
185 |
'reportname' => $reportname, |
204 |
'reportname' => $reportname, |
186 |
'id' => $id, |
205 |
'id' => $id, |
187 |
); |
206 |
); |
188 |
} |
207 |
} |
189 |
|
|
|
190 |
} |
208 |
} |
191 |
|
209 |
|
192 |
elsif ($phase eq 'retrieve results') { |
210 |
elsif ($phase eq 'retrieve results') { |
Lines 228-234
elsif ( $phase eq 'Report on this Area' ) {
Link Here
|
228 |
# they have choosen a new report and the area to report on |
246 |
# they have choosen a new report and the area to report on |
229 |
$template->param( |
247 |
$template->param( |
230 |
'build2' => 1, |
248 |
'build2' => 1, |
231 |
'area' => $input->param('areas'), |
249 |
'area' => $input->param('area'), |
232 |
'types' => get_report_types(), |
250 |
'types' => get_report_types(), |
233 |
'cache_expiry' => $cache_expiry, |
251 |
'cache_expiry' => $cache_expiry, |
234 |
'public' => $input->param('public'), |
252 |
'public' => $input->param('public'), |
Lines 275-316
elsif ( $phase eq 'Choose these criteria' ) {
Link Here
|
275 |
my $area = $input->param('area'); |
293 |
my $area = $input->param('area'); |
276 |
my $type = $input->param('type'); |
294 |
my $type = $input->param('type'); |
277 |
my $column = $input->param('column'); |
295 |
my $column = $input->param('column'); |
278 |
my @definitions = $input->param('definition'); |
296 |
my @definitions = $input->param('definition'); |
279 |
my $definition = join (',',@definitions); |
297 |
my $definition = join (',',@definitions); |
280 |
my @criteria = $input->param('criteria_column'); |
298 |
my @criteria = $input->param('criteria_column'); |
281 |
my $query_criteria; |
299 |
my $query_criteria; |
282 |
foreach my $crit (@criteria) { |
300 |
foreach my $crit (@criteria) { |
283 |
my $value = $input->param( $crit . "_value" ); |
301 |
my $value = $input->param( $crit . "_value" ); |
284 |
|
302 |
|
285 |
# If value is not defined, then it may be range values |
303 |
# If value is not defined, then it may be range values |
286 |
if (!defined $value) { |
304 |
if (!defined $value) { |
287 |
|
305 |
|
288 |
my $fromvalue = $input->param( "from_" . $crit . "_value" ); |
306 |
my $fromvalue = $input->param( "from_" . $crit . "_value" ); |
289 |
my $tovalue = $input->param( "to_" . $crit . "_value" ); |
307 |
my $tovalue = $input->param( "to_" . $crit . "_value" ); |
290 |
|
308 |
|
291 |
# If the range values are dates |
309 |
# If the range values are dates |
292 |
if ($fromvalue =~ C4::Dates->regexp('syspref') && $tovalue =~ C4::Dates->regexp('syspref')) { |
310 |
if ($fromvalue =~ C4::Dates->regexp('syspref') && $tovalue =~ C4::Dates->regexp('syspref')) { |
293 |
$fromvalue = C4::Dates->new($fromvalue)->output("iso"); |
311 |
$fromvalue = C4::Dates->new($fromvalue)->output("iso"); |
294 |
$tovalue = C4::Dates->new($tovalue)->output("iso"); |
312 |
$tovalue = C4::Dates->new($tovalue)->output("iso"); |
295 |
} |
313 |
} |
296 |
|
314 |
|
297 |
if ($fromvalue && $tovalue) { |
315 |
if ($fromvalue && $tovalue) { |
298 |
$query_criteria .= " AND $crit >= '$fromvalue' AND $crit <= '$tovalue'"; |
316 |
$query_criteria .= " AND $crit >= '$fromvalue' AND $crit <= '$tovalue'"; |
299 |
} |
317 |
} |
300 |
|
318 |
|
301 |
} else { |
|
|
302 |
|
303 |
# If value is a date |
304 |
if ($value =~ C4::Dates->regexp('syspref')) { |
305 |
$value = C4::Dates->new($value)->output("iso"); |
306 |
} |
307 |
# don't escape runtime parameters, they'll be at runtime |
308 |
if ($value =~ /<<.*>>/) { |
309 |
$query_criteria .= " AND $crit=$value"; |
310 |
} else { |
319 |
} else { |
311 |
$query_criteria .= " AND $crit='$value'"; |
320 |
|
|
|
321 |
# If value is a date |
322 |
if ($value =~ C4::Dates->regexp('syspref')) { |
323 |
$value = C4::Dates->new($value)->output("iso"); |
324 |
} |
325 |
# don't escape runtime parameters, they'll be at runtime |
326 |
if ($value =~ /<<.*>>/) { |
327 |
$query_criteria .= " AND $crit=$value"; |
328 |
} else { |
329 |
$query_criteria .= " AND $crit='$value'"; |
330 |
} |
312 |
} |
331 |
} |
313 |
} |
|
|
314 |
} |
332 |
} |
315 |
$template->param( |
333 |
$template->param( |
316 |
'build5' => 1, |
334 |
'build5' => 1, |
Lines 412-417
elsif ( $phase eq 'Build report' ) {
Link Here
|
412 |
build_query( \@columns, $query_criteria, $query_orderby, $area, $totals, $definition ); |
430 |
build_query( \@columns, $query_criteria, $query_orderby, $area, $totals, $definition ); |
413 |
$template->param( |
431 |
$template->param( |
414 |
'showreport' => 1, |
432 |
'showreport' => 1, |
|
|
433 |
'area' => $area, |
415 |
'sql' => $sql, |
434 |
'sql' => $sql, |
416 |
'type' => $type, |
435 |
'type' => $type, |
417 |
'cache_expiry' => $input->param('cache_expiry'), |
436 |
'cache_expiry' => $input->param('cache_expiry'), |
Lines 420-442
elsif ( $phase eq 'Build report' ) {
Link Here
|
420 |
} |
439 |
} |
421 |
|
440 |
|
422 |
elsif ( $phase eq 'Save' ) { |
441 |
elsif ( $phase eq 'Save' ) { |
423 |
# Save the report that has just been built |
442 |
# Save the report that has just been built |
|
|
443 |
my $area = $input->param('area'); |
424 |
my $sql = $input->param('sql'); |
444 |
my $sql = $input->param('sql'); |
425 |
my $type = $input->param('type'); |
445 |
my $type = $input->param('type'); |
426 |
$template->param( |
446 |
$template->param( |
427 |
'save' => 1, |
447 |
'save' => 1, |
|
|
448 |
'area' => $area, |
428 |
'sql' => $sql, |
449 |
'sql' => $sql, |
429 |
'type' => $type, |
450 |
'type' => $type, |
430 |
'cache_expiry' => $input->param('cache_expiry'), |
451 |
'cache_expiry' => $input->param('cache_expiry'), |
431 |
'public' => $input->param('public'), |
452 |
'public' => $input->param('public'), |
|
|
453 |
'groups_with_subgroups' => groups_with_subgroups($area), # in case we have a report group that matches area |
432 |
); |
454 |
); |
433 |
} |
455 |
} |
434 |
|
456 |
|
435 |
elsif ( $phase eq 'Save Report' ) { |
457 |
elsif ( $phase eq 'Save Report' ) { |
436 |
# save the sql pasted in by a user |
458 |
# save the sql pasted in by a user |
437 |
my $sql = $input->param('sql'); |
459 |
my $area = $input->param('area'); |
438 |
my $name = $input->param('reportname'); |
460 |
my $group = $input->param('group'); |
439 |
my $type = $input->param('types'); |
461 |
my $subgroup = $input->param('subgroup'); |
|
|
462 |
my $sql = $input->param('sql'); |
463 |
my $name = $input->param('reportname'); |
464 |
my $type = $input->param('types'); |
440 |
my $notes = $input->param('notes'); |
465 |
my $notes = $input->param('notes'); |
441 |
my $cache_expiry = $input->param('cache_expiry'); |
466 |
my $cache_expiry = $input->param('cache_expiry'); |
442 |
my $cache_expiry_units = $input->param('cache_expiry_units'); |
467 |
my $cache_expiry_units = $input->param('cache_expiry_units'); |
Lines 454-460
elsif ( $phase eq 'Save Report' ) {
Link Here
|
454 |
} |
479 |
} |
455 |
} |
480 |
} |
456 |
# check $cache_expiry isnt too large, Memcached::set requires it to be less than 30 days or it will be treated as if it were an absolute time stamp |
481 |
# check $cache_expiry isnt too large, Memcached::set requires it to be less than 30 days or it will be treated as if it were an absolute time stamp |
457 |
if( $cache_expiry >= 2592000 ){ |
482 |
if( $cache_expiry && $cache_expiry >= 2592000 ){ |
458 |
push @errors, {cache_expiry => $cache_expiry}; |
483 |
push @errors, {cache_expiry => $cache_expiry}; |
459 |
} |
484 |
} |
460 |
## FIXME this is AFTER entering a name to save the report under |
485 |
## FIXME this is AFTER entering a name to save the report under |
Lines 462-468
elsif ( $phase eq 'Save Report' ) {
Link Here
|
462 |
push @errors, {sqlerr => $1}; |
487 |
push @errors, {sqlerr => $1}; |
463 |
} |
488 |
} |
464 |
elsif ($sql !~ /^(SELECT)/i) { |
489 |
elsif ($sql !~ /^(SELECT)/i) { |
465 |
push @errors, {queryerr => 1}; |
490 |
push @errors, {queryerr => "No SELECT"}; |
466 |
} |
491 |
} |
467 |
if (@errors) { |
492 |
if (@errors) { |
468 |
$template->param( |
493 |
$template->param( |
Lines 476-632
elsif ( $phase eq 'Save Report' ) {
Link Here
|
476 |
); |
501 |
); |
477 |
} |
502 |
} |
478 |
else { |
503 |
else { |
479 |
my $id = save_report( $borrowernumber, $sql, $name, $type, $notes, $cache_expiry, $public ); |
504 |
save_report( { |
480 |
$template->param( |
505 |
borrowernumber => $borrowernumber, |
481 |
'save_successful' => 1, |
506 |
sql => $sql, |
482 |
'reportname' => $name, |
507 |
name => $name, |
483 |
'id' => $id, |
508 |
area => $area, |
484 |
); |
509 |
group => $group, |
|
|
510 |
subgroup => $subgroup, |
511 |
type => $type, |
512 |
notes => $notes, |
513 |
cache_expiry => $cache_expiry, |
514 |
public => $public, |
515 |
} ); |
516 |
$template->param( 'save_successful' => 1, ); |
485 |
} |
517 |
} |
486 |
} |
518 |
} |
487 |
|
519 |
|
488 |
elsif ($phase eq 'Run this report'){ |
520 |
elsif ($phase eq 'Run this report'){ |
489 |
# execute a saved report |
521 |
# execute a saved report |
490 |
my $limit = 20; # page size. # TODO: move to DB or syspref? |
522 |
my $limit = 20; # page size. # TODO: move to DB or syspref? |
491 |
my $offset = 0; |
523 |
my $offset = 0; |
492 |
my $report = $input->param('reports'); |
524 |
my $report_id = $input->param('reports'); |
493 |
my @sql_params = $input->param('sql_params'); |
525 |
my @sql_params = $input->param('sql_params'); |
494 |
# offset algorithm |
526 |
# offset algorithm |
495 |
if ($input->param('page')) { |
527 |
if ($input->param('page')) { |
496 |
$offset = ($input->param('page') - 1) * $limit; |
528 |
$offset = ($input->param('page') - 1) * $limit; |
497 |
} |
529 |
} |
498 |
my ($sql,$type,$name,$notes) = get_saved_report($report); |
530 |
|
499 |
unless ($sql) { |
531 |
my ( $sql, $type, $name, $notes ); |
500 |
push @errors, {no_sql_for_id=>$report}; |
532 |
if (my $report = get_saved_report($report_id)) { |
501 |
} |
533 |
$sql = $report->{savedsql}; |
502 |
my @rows = (); |
534 |
$name = $report->{report_name}; |
503 |
# if we have at least 1 parameter, and it's not filled, then don't execute but ask for parameters |
535 |
$notes = $report->{notes}; |
504 |
if ($sql =~ /<</ && !@sql_params) { |
536 |
|
505 |
# split on ??. Each odd (2,4,6,...) entry should be a parameter to fill |
537 |
my @rows = (); |
506 |
my @split = split /<<|>>/,$sql; |
538 |
# if we have at least 1 parameter, and it's not filled, then don't execute but ask for parameters |
507 |
my @tmpl_parameters; |
539 |
if ($sql =~ /<</ && !@sql_params) { |
508 |
for(my $i=0;$i<($#split/2);$i++) { |
540 |
# split on ??. Each odd (2,4,6,...) entry should be a parameter to fill |
509 |
my ($text,$authorised_value) = split /\|/,$split[$i*2+1]; |
541 |
my @split = split /<<|>>/,$sql; |
510 |
my $input; |
542 |
my @tmpl_parameters; |
511 |
if ($authorised_value eq "date") { |
543 |
for(my $i=0;$i<($#split/2);$i++) { |
512 |
$input = 'date'; |
544 |
my ($text,$authorised_value) = split /\|/,$split[$i*2+1]; |
513 |
} |
545 |
my $input; |
514 |
elsif ($authorised_value) { |
546 |
if ($authorised_value eq "date") { |
515 |
my $dbh=C4::Context->dbh; |
547 |
$input = 'date'; |
516 |
my @authorised_values; |
|
|
517 |
my %authorised_lib; |
518 |
# builds list, depending on authorised value... |
519 |
if ( $authorised_value eq "branches" ) { |
520 |
my $branches = GetBranchesLoop(); |
521 |
foreach my $thisbranch (@$branches) { |
522 |
push @authorised_values, $thisbranch->{value}; |
523 |
$authorised_lib{$thisbranch->{value}} = $thisbranch->{branchname}; |
524 |
} |
525 |
} |
548 |
} |
526 |
elsif ( $authorised_value eq "itemtypes" ) { |
549 |
elsif ($authorised_value) { |
527 |
my $sth = $dbh->prepare("SELECT itemtype,description FROM itemtypes ORDER BY description"); |
550 |
my $dbh=C4::Context->dbh; |
528 |
$sth->execute; |
551 |
my @authorised_values; |
529 |
while ( my ( $itemtype, $description ) = $sth->fetchrow_array ) { |
552 |
my %authorised_lib; |
530 |
push @authorised_values, $itemtype; |
553 |
# builds list, depending on authorised value... |
531 |
$authorised_lib{$itemtype} = $description; |
554 |
if ( $authorised_value eq "branches" ) { |
|
|
555 |
my $branches = GetBranchesLoop(); |
556 |
foreach my $thisbranch (@$branches) { |
557 |
push @authorised_values, $thisbranch->{value}; |
558 |
$authorised_lib{$thisbranch->{value}} = $thisbranch->{branchname}; |
559 |
} |
532 |
} |
560 |
} |
533 |
} |
561 |
elsif ( $authorised_value eq "itemtypes" ) { |
534 |
elsif ( $authorised_value eq "cn_source" ) { |
562 |
my $sth = $dbh->prepare("SELECT itemtype,description FROM itemtypes ORDER BY description"); |
535 |
my $class_sources = GetClassSources(); |
563 |
$sth->execute; |
536 |
my $default_source = C4::Context->preference("DefaultClassificationSource"); |
564 |
while ( my ( $itemtype, $description ) = $sth->fetchrow_array ) { |
537 |
foreach my $class_source (sort keys %$class_sources) { |
565 |
push @authorised_values, $itemtype; |
538 |
next unless $class_sources->{$class_source}->{'used'} or |
566 |
$authorised_lib{$itemtype} = $description; |
539 |
($class_source eq $default_source); |
567 |
} |
540 |
push @authorised_values, $class_source; |
|
|
541 |
$authorised_lib{$class_source} = $class_sources->{$class_source}->{'description'}; |
542 |
} |
568 |
} |
543 |
} |
569 |
elsif ( $authorised_value eq "cn_source" ) { |
544 |
elsif ( $authorised_value eq "categorycode" ) { |
570 |
my $class_sources = GetClassSources(); |
545 |
my $sth = $dbh->prepare("SELECT categorycode, description FROM categories ORDER BY description"); |
571 |
my $default_source = C4::Context->preference("DefaultClassificationSource"); |
546 |
$sth->execute; |
572 |
foreach my $class_source (sort keys %$class_sources) { |
547 |
while ( my ( $categorycode, $description ) = $sth->fetchrow_array ) { |
573 |
next unless $class_sources->{$class_source}->{'used'} or |
548 |
push @authorised_values, $categorycode; |
574 |
($class_source eq $default_source); |
549 |
$authorised_lib{$categorycode} = $description; |
575 |
push @authorised_values, $class_source; |
|
|
576 |
$authorised_lib{$class_source} = $class_sources->{$class_source}->{'description'}; |
577 |
} |
550 |
} |
578 |
} |
|
|
579 |
elsif ( $authorised_value eq "categorycode" ) { |
580 |
my $sth = $dbh->prepare("SELECT categorycode, description FROM categories ORDER BY description"); |
581 |
$sth->execute; |
582 |
while ( my ( $categorycode, $description ) = $sth->fetchrow_array ) { |
583 |
push @authorised_values, $categorycode; |
584 |
$authorised_lib{$categorycode} = $description; |
585 |
} |
586 |
|
587 |
#---- "true" authorised value |
588 |
} |
589 |
else { |
590 |
my $authorised_values_sth = $dbh->prepare("SELECT authorised_value,lib FROM authorised_values WHERE category=? ORDER BY lib"); |
551 |
|
591 |
|
552 |
#---- "true" authorised value |
592 |
$authorised_values_sth->execute( $authorised_value); |
553 |
} |
|
|
554 |
else { |
555 |
my $authorised_values_sth = $dbh->prepare("SELECT authorised_value,lib FROM authorised_values WHERE category=? ORDER BY lib"); |
556 |
|
557 |
$authorised_values_sth->execute( $authorised_value); |
558 |
|
593 |
|
559 |
while ( my ( $value, $lib ) = $authorised_values_sth->fetchrow_array ) { |
594 |
while ( my ( $value, $lib ) = $authorised_values_sth->fetchrow_array ) { |
560 |
push @authorised_values, $value; |
595 |
push @authorised_values, $value; |
561 |
$authorised_lib{$value} = $lib; |
596 |
$authorised_lib{$value} = $lib; |
562 |
# For item location, we show the code and the libelle |
597 |
# For item location, we show the code and the libelle |
563 |
$authorised_lib{$value} = $lib; |
598 |
$authorised_lib{$value} = $lib; |
|
|
599 |
} |
564 |
} |
600 |
} |
565 |
} |
601 |
$input =CGI::scrolling_list( # FIXME: factor out scrolling_list |
566 |
$input =CGI::scrolling_list( # FIXME: factor out scrolling_list |
602 |
-name => "sql_params", |
567 |
-name => "sql_params", |
603 |
-values => \@authorised_values, |
568 |
-values => \@authorised_values, |
|
|
569 |
# -default => $value, |
604 |
# -default => $value, |
570 |
-labels => \%authorised_lib, |
605 |
-labels => \%authorised_lib, |
571 |
-override => 1, |
606 |
-override => 1, |
572 |
-size => 1, |
607 |
-size => 1, |
573 |
-multiple => 0, |
608 |
-multiple => 0, |
574 |
-tabindex => 1, |
609 |
-tabindex => 1, |
575 |
); |
610 |
); |
576 |
|
611 |
push @tmpl_parameters, {'entry' => $text, 'input' => $input }; |
577 |
} else { |
612 |
} |
578 |
$input = "<input type='text' name='sql_params'/>"; |
|
|
579 |
} |
613 |
} |
580 |
push @tmpl_parameters, {'entry' => $text, 'input' => $input }; |
614 |
$template->param('sql' => $sql, |
581 |
} |
615 |
'name' => $name, |
582 |
$template->param('sql' => $sql, |
616 |
'sql_params' => \@tmpl_parameters, |
583 |
'name' => $name, |
617 |
'enter_params' => 1, |
584 |
'sql_params' => \@tmpl_parameters, |
618 |
'reports' => $report, |
585 |
'enter_params' => 1, |
619 |
); |
586 |
'reports' => $report, |
|
|
587 |
); |
588 |
} else { |
589 |
# OK, we have parameters, or there are none, we run the report |
590 |
# if there were parameters, replace before running |
591 |
# split on ??. Each odd (2,4,6,...) entry should be a parameter to fill |
592 |
my @split = split /<<|>>/,$sql; |
593 |
my @tmpl_parameters; |
594 |
for(my $i=0;$i<$#split/2;$i++) { |
595 |
my $quoted = C4::Context->dbh->quote($sql_params[$i]); |
596 |
# if there are special regexp chars, we must \ them |
597 |
$split[$i*2+1] =~ s/(\||\?|\.|\*|\(|\)|\%)/\\$1/g; |
598 |
$sql =~ s/<<$split[$i*2+1]>>/$quoted/; |
599 |
} |
600 |
my ($sth, $errors) = execute_query($sql, $offset, $limit); |
601 |
my $total = nb_rows($sql) || 0; |
602 |
unless ($sth) { |
603 |
die "execute_query failed to return sth for report $report: $sql"; |
604 |
} else { |
620 |
} else { |
605 |
my $headref = $sth->{NAME} || []; |
621 |
# OK, we have parameters, or there are none, we run the report |
606 |
my @headers = map { +{ cell => $_ } } @$headref; |
622 |
# if there were parameters, replace before running |
607 |
$template->param(header_row => \@headers); |
623 |
# split on ??. Each odd (2,4,6,...) entry should be a parameter to fill |
608 |
while (my $row = $sth->fetchrow_arrayref()) { |
624 |
my @split = split /<<|>>/,$sql; |
609 |
my @cells = map { +{ cell => $_ } } @$row; |
625 |
my @tmpl_parameters; |
610 |
push @rows, { cells => \@cells }; |
626 |
for(my $i=0;$i<$#split/2;$i++) { |
|
|
627 |
my $quoted = C4::Context->dbh->quote($sql_params[$i]); |
628 |
# if there are special regexp chars, we must \ them |
629 |
$split[$i*2+1] =~ s/(\||\?|\.|\*|\(|\)|\%)/\\$1/g; |
630 |
$sql =~ s/<<$split[$i*2+1]>>/$quoted/; |
631 |
} |
632 |
my ($sth, $errors) = execute_query($sql, $offset, $limit); |
633 |
my $total = nb_rows($sql) || 0; |
634 |
unless ($sth) { |
635 |
die "execute_query failed to return sth for report $report: $sql"; |
636 |
} else { |
637 |
my $headref = $sth->{NAME} || []; |
638 |
my @headers = map { +{ cell => $_ } } @$headref; |
639 |
$template->param(header_row => \@headers); |
640 |
while (my $row = $sth->fetchrow_arrayref()) { |
641 |
my @cells = map { +{ cell => $_ } } @$row; |
642 |
push @rows, { cells => \@cells }; |
643 |
} |
611 |
} |
644 |
} |
612 |
} |
|
|
613 |
|
645 |
|
614 |
my $totpages = int($total/$limit) + (($total % $limit) > 0 ? 1 : 0); |
646 |
my $totpages = int($total/$limit) + (($total % $limit) > 0 ? 1 : 0); |
615 |
my $url = "/cgi-bin/koha/reports/guided_reports.pl?reports=$report&phase=Run%20this%20report"; |
647 |
my $url = "/cgi-bin/koha/reports/guided_reports.pl?reports=$report&phase=Run%20this%20report"; |
616 |
if (@sql_params) { |
648 |
if (@sql_params) { |
617 |
$url = join('&sql_params=', $url, map { URI::Escape::uri_escape($_) } @sql_params); |
649 |
$url = join('&sql_params=', $url, map { URI::Escape::uri_escape($_) } @sql_params); |
|
|
650 |
} |
651 |
$template->param( |
652 |
'results' => \@rows, |
653 |
'sql' => $sql, |
654 |
'id' => $report, |
655 |
'execute' => 1, |
656 |
'name' => $name, |
657 |
'notes' => $notes, |
658 |
'errors' => $errors, |
659 |
'pagination_bar' => pagination_bar($url, $totpages, $input->param('page')), |
660 |
'unlimited_total' => $total, |
661 |
); |
618 |
} |
662 |
} |
619 |
$template->param( |
663 |
} |
620 |
'results' => \@rows, |
664 |
else { |
621 |
'sql' => $sql, |
665 |
push @errors, { no_sql_for_id => $report_id }; |
622 |
'id' => $report, |
|
|
623 |
'execute' => 1, |
624 |
'name' => $name, |
625 |
'notes' => $notes, |
626 |
'errors' => $errors, |
627 |
'pagination_bar' => pagination_bar($url, $totpages, $input->param('page')), |
628 |
'unlimited_total' => $total, |
629 |
); |
630 |
} |
666 |
} |
631 |
} |
667 |
} |
632 |
|
668 |
|
Lines 676-691
elsif ($phase eq 'Export'){
Link Here
|
676 |
); |
712 |
); |
677 |
} |
713 |
} |
678 |
|
714 |
|
679 |
elsif ($phase eq 'Create report from SQL') { |
715 |
elsif ( $phase eq 'Create report from SQL' ) { |
680 |
# allow the user to paste in sql |
716 |
|
681 |
if ($input->param('sql')) { |
717 |
my ($group, $subgroup); |
|
|
718 |
# allow the user to paste in sql |
719 |
if ( $input->param('sql') ) { |
720 |
$group = $input->param('report_group'); |
721 |
$subgroup = $input->param('report_subgroup'); |
682 |
$template->param( |
722 |
$template->param( |
683 |
'sql' => $input->param('sql'), |
723 |
'sql' => $input->param('sql'), |
684 |
'reportname' => $input->param('reportname'), |
724 |
'reportname' => $input->param('reportname'), |
685 |
'notes' => $input->param('notes'), |
725 |
'notes' => $input->param('notes'), |
686 |
); |
726 |
); |
687 |
} |
727 |
} |
688 |
$template->param('create' => 1, 'public' => '0', 'cache_expiry' => 300, 'usecache' => $usecache); |
728 |
$template->param( |
|
|
729 |
'create' => 1, |
730 |
'groups_with_subgroups' => groups_with_subgroups($group, $subgroup), |
731 |
'public' => '0', |
732 |
'cache_expiry' => 300, |
733 |
'usecache' => $usecache, |
734 |
); |
689 |
} |
735 |
} |
690 |
|
736 |
|
691 |
elsif ($phase eq 'Create Compound Report'){ |
737 |
elsif ($phase eq 'Create Compound Report'){ |
Lines 724-726
$template->param( 'referer' => $input->referer(),
Link Here
|
724 |
); |
770 |
); |
725 |
|
771 |
|
726 |
output_html_with_http_headers $input, $cookie, $template->output; |
772 |
output_html_with_http_headers $input, $cookie, $template->output; |
|
|
773 |
|
774 |
sub groups_with_subgroups { |
775 |
my ($group, $subgroup) = @_; |
776 |
|
777 |
my $groups_with_subgroups = get_report_groups(); |
778 |
my @g_sg; |
779 |
while (my ($g_id, $v) = each %$groups_with_subgroups) { |
780 |
my @subgroups; |
781 |
if (my $sg = $v->{subgroups}) { |
782 |
while (my ($sg_id, $n) = each %$sg) { |
783 |
push @subgroups, { |
784 |
id => $sg_id, |
785 |
name => $n, |
786 |
selected => ($group && $g_id eq $group && $subgroup && $sg_id eq $subgroup ), |
787 |
}; |
788 |
} |
789 |
} |
790 |
push @g_sg, { |
791 |
id => $g_id, |
792 |
name => $v->{name}, |
793 |
selected => ($group && $g_id eq $group), |
794 |
subgroups => \@subgroups, |
795 |
}; |
796 |
} |
797 |
return \@g_sg; |
798 |
} |