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
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 use C4
::Reports
::Guided
;
25 use C4
::Auth qw
/:DEFAULT get_session/;
27 use C4
::Dates qw
/format_date/;
29 use C4
::Branch
; # XXX subfield_is_koha_internal_p
30 use C4
::Koha qw
/IsAuthorisedValueCategory/;
38 Script to control the guided report creation
43 my $usecache = C4
::Context
->ismemcached;
45 my $phase = $input->param('phase');
47 if ( $phase eq 'Build new' or $phase eq 'Delete Saved' ) {
48 $flagsrequired = 'create_reports';
50 elsif ( $phase eq 'Use saved' ) {
51 $flagsrequired = 'execute_reports';
56 my ( $template, $borrowernumber, $cookie ) = get_template_and_user
(
58 template_name
=> "reports/guided_reports_start.tmpl",
62 flagsrequired
=> { reports
=> $flagsrequired },
66 my $session = $cookie ? get_session
($cookie->value) : undef;
69 if ( $input->param("filter_set") ) {
71 $filter->{$_} = $input->param("filter_$_") foreach qw
/date author keyword group subgroup/;
72 $session->param('report_filter', $filter) if $session;
73 $template->param( 'filter_set' => 1 );
76 $filter = $session->param('report_filter');
82 $template->param( 'start' => 1 );
85 elsif ( $phase eq 'Build new' ) {
87 $template->param( 'build1' => 1 );
88 my $areas = get_report_areas
();
90 'areas' => [map { id
=> $_->[0], name
=> $_->[1] }, @
$areas],
91 'usecache' => $usecache,
92 'cache_expiry' => 300,
95 } elsif ( $phase eq 'Use saved' ) {
98 # get list of reports and display them
99 my $group = $input->param('group');
100 my $subgroup = $input->param('subgroup');
101 $filter->{group
} = $group;
102 $filter->{subgroup
} = $subgroup;
105 'savedreports' => get_saved_reports
($filter),
106 'usecache' => $usecache,
107 'groups_with_subgroups'=> groups_with_subgroups
($group, $subgroup),
111 elsif ( $phase eq 'Delete Multiple') {
112 my @ids = $input->param('ids');
113 delete_report
( @ids );
114 print $input->redirect("/cgi-bin/koha/reports/guided_reports.pl?phase=Use%20saved");
118 elsif ( $phase eq 'Delete Saved') {
120 # delete a report from the saved reports list
121 my $ids = $input->param('reports');
123 print $input->redirect("/cgi-bin/koha/reports/guided_reports.pl?phase=Use%20saved");
127 elsif ( $phase eq 'Show SQL'){
129 my $id = $input->param('reports');
130 my $report = get_saved_report
($id);
133 'reportname' => $report->{report_name
},
134 'notes' => $report->{notes
},
135 'sql' => $report->{savedsql
},
140 elsif ( $phase eq 'Edit SQL'){
141 my $id = $input->param('reports');
142 my $report = get_saved_report
($id);
143 my $group = $report->{report_group
};
144 my $subgroup = $report->{report_subgroup
};
146 'sql' => $report->{savedsql
},
147 'reportname' => $report->{report_name
},
148 'groups_with_subgroups' => groups_with_subgroups
($group, $subgroup),
149 'notes' => $report->{notes
},
151 'cache_expiry' => $report->{cache_expiry
},
152 'public' => $report->{public
},
153 'usecache' => $usecache,
158 elsif ( $phase eq 'Update SQL'){
159 my $id = $input->param('id');
160 my $sql = $input->param('sql');
161 my $reportname = $input->param('reportname');
162 my $group = $input->param('group');
163 my $subgroup = $input->param('subgroup');
164 my $notes = $input->param('notes');
165 my $cache_expiry = $input->param('cache_expiry');
166 my $cache_expiry_units = $input->param('cache_expiry_units');
167 my $public = $input->param('public');
168 my $save_anyway = $input->param('save_anyway');
172 # if we have the units, then we came from creating a report from SQL and thus need to handle converting units
173 if( $cache_expiry_units ){
174 if( $cache_expiry_units eq "minutes" ){
176 } elsif( $cache_expiry_units eq "hours" ){
177 $cache_expiry *= 3600; # 60 * 60
178 } elsif( $cache_expiry_units eq "days" ){
179 $cache_expiry *= 86400; # 60 * 60 * 24
182 # 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
183 if( $cache_expiry >= 2592000 ){
184 push @errors, {cache_expiry
=> $cache_expiry};
187 create_non_existing_group_and_subgroup
($input, $group, $subgroup);
189 if ($sql =~ /;?\W?(UPDATE|DELETE|DROP|INSERT|SHOW|CREATE)\W/i) {
190 push @errors, {sqlerr
=> $1};
192 elsif ($sql !~ /^(SELECT)/i) {
193 push @errors, {queryerr
=> 1};
198 'errors' => \
@errors,
203 # Check defined SQL parameters for authorised value validity
204 my $problematic_authvals = ValidateSQLParameters
($sql);
206 if ( scalar @
$problematic_authvals > 0 && not $save_anyway ) {
207 # There's at least one problematic parameter, report to the
208 # GUI and provide all user input for further actions
212 'reportname' => $reportname,
214 'subgroup' => $subgroup,
216 'cache_expiry' => $cache_expiry,
217 'cache_expiry_units' => $cache_expiry_units,
219 'problematic_authvals' => $problematic_authvals,
220 'warn_authval_problem' => 1,
225 # No params problem found or asked to save anyway
230 subgroup
=> $subgroup,
232 cache_expiry
=> $cache_expiry,
236 'save_successful' => 1,
237 'reportname' => $reportname,
244 elsif ($phase eq 'retrieve results') {
245 my $id = $input->param('id');
246 my ($results,$name,$notes) = format_results
($id);
250 'results' => $results,
256 elsif ( $phase eq 'Report on this Area' ) {
257 my $cache_expiry_units = $input->param('cache_expiry_units'),
258 my $cache_expiry = $input->param('cache_expiry');
260 # we need to handle converting units
261 if( $cache_expiry_units eq "minutes" ){
263 } elsif( $cache_expiry_units eq "hours" ){
264 $cache_expiry *= 3600; # 60 * 60
265 } elsif( $cache_expiry_units eq "days" ){
266 $cache_expiry *= 86400; # 60 * 60 * 24
268 # 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
269 if( $cache_expiry >= 2592000 ){ # oops, over the limit of 30 days
270 # report error to user
274 'areas' => get_report_areas
(),
275 'cache_expiry' => $cache_expiry,
276 'usecache' => $usecache,
277 'public' => $input->param('public'),
280 # they have choosen a new report and the area to report on
283 'area' => $input->param('area'),
284 'types' => get_report_types
(),
285 'cache_expiry' => $cache_expiry,
286 'public' => $input->param('public'),
291 elsif ( $phase eq 'Choose this type' ) {
292 # they have chosen type and area
293 # get area and type and pass them to the template
294 my $area = $input->param('area');
295 my $type = $input->param('types');
300 columns
=> get_columns
($area,$input),
301 'cache_expiry' => $input->param('cache_expiry'),
302 'public' => $input->param('public'),
306 elsif ( $phase eq 'Choose these columns' ) {
307 # we now know type, area, and columns
308 # next step is the constraints
309 my $area = $input->param('area');
310 my $type = $input->param('type');
311 my @columns = $input->param('columns');
312 my $column = join( ',', @columns );
318 definitions
=> get_from_dictionary
($area),
319 criteria
=> get_criteria
($area,$input),
320 'cache_expiry' => $input->param('cache_expiry'),
321 'cache_expiry_units' => $input->param('cache_expiry_units'),
322 'public' => $input->param('public'),
326 elsif ( $phase eq 'Choose these criteria' ) {
327 my $area = $input->param('area');
328 my $type = $input->param('type');
329 my $column = $input->param('column');
330 my @definitions = $input->param('definition');
331 my $definition = join (',',@definitions);
332 my @criteria = $input->param('criteria_column');
334 foreach my $crit (@criteria) {
335 my $value = $input->param( $crit . "_value" );
337 # If value is not defined, then it may be range values
338 if (!defined $value) {
340 my $fromvalue = $input->param( "from_" . $crit . "_value" );
341 my $tovalue = $input->param( "to_" . $crit . "_value" );
343 # If the range values are dates
344 if ($fromvalue =~ C4
::Dates
->regexp('syspref') && $tovalue =~ C4
::Dates
->regexp('syspref')) {
345 $fromvalue = C4
::Dates
->new($fromvalue)->output("iso");
346 $tovalue = C4
::Dates
->new($tovalue)->output("iso");
349 if ($fromvalue && $tovalue) {
350 $query_criteria .= " AND $crit >= '$fromvalue' AND $crit <= '$tovalue'";
356 if ($value =~ C4
::Dates
->regexp('syspref')) {
357 $value = C4
::Dates
->new($value)->output("iso");
359 # don't escape runtime parameters, they'll be at runtime
360 if ($value =~ /<<.*>>/) {
361 $query_criteria .= " AND $crit=$value";
363 $query_criteria .= " AND $crit='$value'";
372 'definition' => $definition,
373 'criteriastring' => $query_criteria,
374 'cache_expiry' => $input->param('cache_expiry'),
375 'cache_expiry_units' => $input->param('cache_expiry_units'),
376 'public' => $input->param('public'),
380 my @columns = split( ',', $column );
383 # build structue for use by tmpl_loop to choose columns to order by
384 # need to do something about the order of the order :)
385 # we also want to use the %columns hash to get the plain english names
386 foreach my $col (@columns) {
387 my %total = (name
=> $col);
388 my @selects = map {+{ value
=> $_ }} (qw(sum min max avg count));
389 $total{'select'} = \
@selects;
390 push @total_by, \
%total;
393 $template->param( 'total_by' => \
@total_by );
396 elsif ( $phase eq 'Choose these operations' ) {
397 my $area = $input->param('area');
398 my $type = $input->param('type');
399 my $column = $input->param('column');
400 my $criteria = $input->param('criteria');
401 my $definition = $input->param('definition');
402 my @total_by = $input->param('total_by');
404 foreach my $total (@total_by) {
405 my $value = $input->param( $total . "_tvalue" );
406 $totals .= "$value($total),";
414 'criteriastring' => $criteria,
416 'definition' => $definition,
417 'cache_expiry' => $input->param('cache_expiry'),
418 'public' => $input->param('public'),
422 my @columns = split( ',', $column );
425 # build structue for use by tmpl_loop to choose columns to order by
426 # need to do something about the order of the order :)
427 foreach my $col (@columns) {
428 my %order = (name
=> $col);
429 my @selects = map {+{ value
=> $_ }} (qw(asc desc));
430 $order{'select'} = \
@selects;
431 push @order_by, \
%order;
434 $template->param( 'order_by' => \
@order_by );
437 elsif ( $phase eq 'Build report' ) {
439 # now we have all the info we need and can build the sql
440 my $area = $input->param('area');
441 my $type = $input->param('type');
442 my $column = $input->param('column');
443 my $crit = $input->param('criteria');
444 my $totals = $input->param('totals');
445 my $definition = $input->param('definition');
446 my $query_criteria=$crit;
447 # split the columns up by ,
448 my @columns = split( ',', $column );
449 my @order_by = $input->param('order_by');
452 foreach my $order (@order_by) {
453 my $value = $input->param( $order . "_ovalue" );
454 if ($query_orderby) {
455 $query_orderby .= ",$order $value";
458 $query_orderby = " ORDER BY $order $value";
464 build_query
( \
@columns, $query_criteria, $query_orderby, $area, $totals, $definition );
470 'cache_expiry' => $input->param('cache_expiry'),
471 'public' => $input->param('public'),
475 elsif ( $phase eq 'Save' ) {
476 # Save the report that has just been built
477 my $area = $input->param('area');
478 my $sql = $input->param('sql');
479 my $type = $input->param('type');
485 'cache_expiry' => $input->param('cache_expiry'),
486 'public' => $input->param('public'),
487 'groups_with_subgroups' => groups_with_subgroups
($area), # in case we have a report group that matches area
491 elsif ( $phase eq 'Save Report' ) {
492 # save the sql pasted in by a user
493 my $area = $input->param('area');
494 my $group = $input->param('group');
495 my $subgroup = $input->param('subgroup');
496 my $sql = $input->param('sql');
497 my $name = $input->param('reportname');
498 my $type = $input->param('types');
499 my $notes = $input->param('notes');
500 my $cache_expiry = $input->param('cache_expiry');
501 my $cache_expiry_units = $input->param('cache_expiry_units');
502 my $public = $input->param('public');
503 my $save_anyway = $input->param('save_anyway');
506 # if we have the units, then we came from creating a report from SQL and thus need to handle converting units
507 if( $cache_expiry_units ){
508 if( $cache_expiry_units eq "minutes" ){
510 } elsif( $cache_expiry_units eq "hours" ){
511 $cache_expiry *= 3600; # 60 * 60
512 } elsif( $cache_expiry_units eq "days" ){
513 $cache_expiry *= 86400; # 60 * 60 * 24
516 # 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
517 if( $cache_expiry && $cache_expiry >= 2592000 ){
518 push @errors, {cache_expiry
=> $cache_expiry};
521 create_non_existing_group_and_subgroup
($input, $group, $subgroup);
523 ## FIXME this is AFTER entering a name to save the report under
524 if ($sql =~ /;?\W?(UPDATE|DELETE|DROP|INSERT|SHOW|CREATE)\W/i) {
525 push @errors, {sqlerr
=> $1};
527 elsif ($sql !~ /^(SELECT)/i) {
528 push @errors, {queryerr
=> "No SELECT"};
533 'errors' => \
@errors,
535 'reportname'=> $name,
538 'cache_expiry' => $cache_expiry,
542 # Check defined SQL parameters for authorised value validity
543 my $problematic_authvals = ValidateSQLParameters
($sql);
545 if ( scalar @
$problematic_authvals > 0 && not $save_anyway ) {
546 # There's at least one problematic parameter, report to the
547 # GUI and provide all user input for further actions
551 'subgroup' => $subgroup,
553 'reportname' => $name,
556 'cache_expiry' => $cache_expiry,
557 'cache_expiry_units' => $cache_expiry_units,
559 'problematic_authvals' => $problematic_authvals,
560 'warn_authval_problem' => 1,
564 # No params problem found or asked to save anyway
565 my $id = save_report
( {
566 borrowernumber
=> $borrowernumber,
571 subgroup
=> $subgroup,
574 cache_expiry
=> $cache_expiry,
578 'save_successful' => 1,
579 'reportname' => $name,
586 elsif ($phase eq 'Run this report'){
587 # execute a saved report
588 my $limit = $input->param('limit') || 20;
590 my $report_id = $input->param('reports');
591 my @sql_params = $input->param('sql_params');
593 if ($input->param('page')) {
594 $offset = ($input->param('page') - 1) * $limit;
599 'report_id' => $report_id,
602 my ( $sql, $type, $name, $notes );
603 if (my $report = get_saved_report
($report_id)) {
604 $sql = $report->{savedsql
};
605 $name = $report->{report_name
};
606 $notes = $report->{notes
};
609 # if we have at least 1 parameter, and it's not filled, then don't execute but ask for parameters
610 if ($sql =~ /<</ && !@sql_params) {
611 # split on ??. Each odd (2,4,6,...) entry should be a parameter to fill
612 my @split = split /<<|>>/,$sql;
615 for(my $i=0;$i<($#split/2);$i++) {
616 my ($text,$authorised_value) = split /\|/,$split[$i*2+1];
619 if ( not defined $authorised_value ) {
620 # no authorised value input, provide a text box
622 } elsif ( $authorised_value eq "date" ) {
623 # require a date, provide a date picker
626 # defined $authorised_value, and not 'date'
627 my $dbh=C4
::Context
->dbh;
628 my @authorised_values;
630 # builds list, depending on authorised value...
631 if ( $authorised_value eq "branches" ) {
632 my $branches = GetBranchesLoop
();
633 foreach my $thisbranch (@
$branches) {
634 push @authorised_values, $thisbranch->{value
};
635 $authorised_lib{$thisbranch->{value
}} = $thisbranch->{branchname
};
638 elsif ( $authorised_value eq "itemtypes" ) {
639 my $sth = $dbh->prepare("SELECT itemtype,description FROM itemtypes ORDER BY description");
641 while ( my ( $itemtype, $description ) = $sth->fetchrow_array ) {
642 push @authorised_values, $itemtype;
643 $authorised_lib{$itemtype} = $description;
646 elsif ( $authorised_value eq "cn_source" ) {
647 my $class_sources = GetClassSources
();
648 my $default_source = C4
::Context
->preference("DefaultClassificationSource");
649 foreach my $class_source (sort keys %$class_sources) {
650 next unless $class_sources->{$class_source}->{'used'} or
651 ($class_source eq $default_source);
652 push @authorised_values, $class_source;
653 $authorised_lib{$class_source} = $class_sources->{$class_source}->{'description'};
656 elsif ( $authorised_value eq "categorycode" ) {
657 my $sth = $dbh->prepare("SELECT categorycode, description FROM categories ORDER BY description");
659 while ( my ( $categorycode, $description ) = $sth->fetchrow_array ) {
660 push @authorised_values, $categorycode;
661 $authorised_lib{$categorycode} = $description;
664 #---- "true" authorised value
667 if ( IsAuthorisedValueCategory
($authorised_value) ) {
669 SELECT authorised_value,lib
670 FROM authorised_values
674 my $authorised_values_sth = $dbh->prepare($query);
675 $authorised_values_sth->execute( $authorised_value);
677 while ( my ( $value, $lib ) = $authorised_values_sth->fetchrow_array ) {
678 push @authorised_values, $value;
679 $authorised_lib{$value} = $lib;
680 # For item location, we show the code and the libelle
681 $authorised_lib{$value} = $lib;
684 # not exists $authorised_value_categories{$authorised_value})
685 push @authval_errors, {'entry' => $text,
686 'auth_val' => $authorised_value };
687 # tell the template there's an error
688 $template->param( auth_val_error
=> 1 );
689 # skip scrolling list creation and params push
695 $input =CGI
::scrolling_list
( # FIXME: factor out scrolling_list
696 -name
=> "sql_params",
697 -id
=> "sql_params_".$labelid,
698 -values => \
@authorised_values,
699 # -default => $value,
700 -labels
=> \
%authorised_lib,
708 push @tmpl_parameters, {'entry' => $text, 'input' => $input, 'labelid' => $labelid };
710 $template->param('sql' => $sql,
712 'sql_params' => \
@tmpl_parameters,
713 'auth_val_errors' => \
@authval_errors,
715 'reports' => $report_id,
718 # OK, we have parameters, or there are none, we run the report
719 # if there were parameters, replace before running
720 # split on ??. Each odd (2,4,6,...) entry should be a parameter to fill
721 my @split = split /<<|>>/,$sql;
723 for(my $i=0;$i<$#split/2;$i++) {
724 my $quoted = C4
::Context
->dbh->quote($sql_params[$i]);
725 # if there are special regexp chars, we must \ them
726 $split[$i*2+1] =~ s/(\||\?|\.|\*|\(|\)|\%)/\\$1/g;
727 $sql =~ s/<<$split[$i*2+1]>>/$quoted/;
729 my ($sth, $errors) = execute_query
($sql, $offset, $limit);
730 my $total = nb_rows
($sql) || 0;
732 die "execute_query failed to return sth for report $report_id: $sql";
734 my $headers= header_cell_loop
($sth);
735 $template->param(header_row
=> $headers);
736 while (my $row = $sth->fetchrow_arrayref()) {
737 my @cells = map { +{ cell
=> $_ } } @
$row;
738 push @rows, { cells
=> \
@cells };
742 my $totpages = int($total/$limit) + (($total % $limit) > 0 ?
1 : 0);
743 my $url = "/cgi-bin/koha/reports/guided_reports.pl?reports=$report_id&phase=Run%20this%20report&limit=$limit";
745 $url = join('&sql_params=', $url, map { URI
::Escape
::uri_escape
($_) } @sql_params);
755 'pagination_bar' => pagination_bar
($url, $totpages, $input->param('page')),
756 'unlimited_total' => $total,
757 'sql_params' => \
@sql_params,
762 push @errors, { no_sql_for_id
=> $report_id };
766 elsif ($phase eq 'Export'){
767 binmode STDOUT
, ':encoding(UTF-8)';
769 # export results to tab separated text or CSV
770 my $sql = $input->param('sql'); # FIXME: use sql from saved report ID#, not new user-supplied SQL!
771 my $format = $input->param('format');
772 my ($sth, $q_errors) = execute_query
($sql);
773 unless ($q_errors and @
$q_errors) {
774 print $input->header( -type
=> 'application/octet-stream',
775 -attachment
=>"reportresults.$format"
777 if ($format eq 'tab') {
778 print join("\t", header_cell_values
($sth)), "\n";
779 while (my $row = $sth->fetchrow_arrayref()) {
780 print join("\t", @
$row), "\n";
783 my $csv = Text
::CSV
->new({binary
=> 1});
784 $csv or die "Text::CSV->new({binary => 1}) FAILED: " . Text
::CSV
->error_diag();
785 if ($csv->combine(header_cell_values
($sth))) {
786 print $csv->string(), "\n";
788 push @
$q_errors, { combine
=> 'HEADER ROW: ' . $csv->error_diag() } ;
790 while (my $row = $sth->fetchrow_arrayref()) {
791 if ($csv->combine(@
$row)) {
792 print $csv->string(), "\n";
794 push @
$q_errors, { combine
=> $csv->error_diag() } ;
798 foreach my $err (@
$q_errors, @errors) {
799 print "# ERROR: " . (map {$_ . ": " . $err->{$_}} keys %$err) . "\n";
800 } # here we print all the non-fatal errors at the end. Not super smooth, but better than nothing.
806 'name' => 'Error exporting report!',
808 'errors' => $q_errors,
812 elsif ( $phase eq 'Create report from SQL' ) {
814 my ($group, $subgroup);
815 # allow the user to paste in sql
816 if ( $input->param('sql') ) {
817 $group = $input->param('report_group');
818 $subgroup = $input->param('report_subgroup');
820 'sql' => $input->param('sql') // '',
821 'reportname' => $input->param('reportname') // '',
822 'notes' => $input->param('notes') // '',
827 'groups_with_subgroups' => groups_with_subgroups
($group, $subgroup),
829 'cache_expiry' => 300,
830 'usecache' => $usecache,
834 elsif ($phase eq 'Create Compound Report'){
835 $template->param( 'savedreports' => get_saved_reports
(),
840 elsif ($phase eq 'Save Compound'){
841 my $master = $input->param('master');
842 my $subreport = $input->param('subreport');
843 my ($mastertables,$subtables) = create_compound
($master,$subreport);
844 $template->param( 'save_compound' => 1,
845 master
=>$mastertables,
850 # pass $sth, get back an array of names for the column headers
851 sub header_cell_values
{
852 my $sth = shift or return ();
854 foreach my $c (@
{$sth->{NAME
}}) {
855 #FIXME apparently DBI still needs a utf8 fix for this?
862 # pass $sth, get back a TMPL_LOOP-able set of names for the column headers
863 sub header_cell_loop
{
864 my @headers = map { +{ cell
=> $_ } } header_cell_values
(shift);
869 $template->{VARS
}->{'build' . $_} and $template->{VARS
}->{'buildx' . $_} and last;
871 $template->param( 'referer' => $input->referer(),
874 output_html_with_http_headers
$input, $cookie, $template->output;
876 sub groups_with_subgroups
{
877 my ($group, $subgroup) = @_;
879 my $groups_with_subgroups = get_report_groups
();
881 my @sorted_keys = sort {
882 $groups_with_subgroups->{$a}->{name
} cmp $groups_with_subgroups->{$b}->{name
}
883 } keys %$groups_with_subgroups;
884 foreach my $g_id (@sorted_keys) {
885 my $v = $groups_with_subgroups->{$g_id};
887 if (my $sg = $v->{subgroups
}) {
888 foreach my $sg_id (sort { $sg->{$a} cmp $sg->{$b} } keys %$sg) {
891 name
=> $sg->{$sg_id},
892 selected
=> ($group && $g_id eq $group && $subgroup && $sg_id eq $subgroup ),
899 selected
=> ($group && $g_id eq $group),
900 subgroups
=> \
@subgroups,
906 sub create_non_existing_group_and_subgroup
{
907 my ($input, $group, $subgroup) = @_;
909 if (defined $group and $group ne '') {
910 my $report_groups = C4
::Reports
::Guided
::get_report_groups
;
911 if (not exists $report_groups->{$group}) {
912 my $groupdesc = $input->param('groupdesc') // $group;
913 C4
::Koha
::AddAuthorisedValue
('REPORT_GROUP', $group, $groupdesc);
915 if (defined $subgroup and $subgroup ne '') {
916 if (not exists $report_groups->{$group}->{subgroups
}->{$subgroup}) {
917 my $subgroupdesc = $input->param('subgroupdesc') // $subgroup;
918 C4
::Koha
::AddAuthorisedValue
('REPORT_SUBGROUP', $subgroup, $subgroupdesc, $group);