Bug 9117: Reports hierarchies management improvements
authorJulian Maurice <julian.maurice@biblibre.com>
Thu, 2 Aug 2012 14:03:32 +0000 (16:03 +0200)
committerJared Camins-Esakov <jcamins@cpbibliography.com>
Thu, 21 Mar 2013 22:54:09 +0000 (18:54 -0400)
- Filter by groups using tabs
- Filter by subgroups using a dropdown list under each tab
- Allow to create new group and subgroups when building a new report or
  editing an existing report (no need to go through admin AV interface)

+ Possibility to duplicate a report (new link in the table)

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Signed-off-by: Katrin Fischer <Katrin.Fischer.83@web.de>
Signed-off-by: Jared Camins-Esakov <jcamins@cpbibliography.com>
C4/Koha.pm
koha-tmpl/intranet-tmpl/prog/en/modules/reports/guided_reports_start.tt
reports/guided_reports.pl

index 4c01309..13ab94a 100644 (file)
@@ -63,6 +63,7 @@ BEGIN {
     &GetAuthorisedValueByCode
     &GetKohaImageurlFromAuthorisedValues
                &GetAuthValCode
+        &AddAuthorisedValue
                &GetNormalizedUPC
                &GetNormalizedISBN
                &GetNormalizedEAN
@@ -1226,6 +1227,26 @@ sub GetKohaAuthorisedValueLib {
   return $value;
 }
 
+=head2 AddAuthorisedValue
+
+    AddAuthorisedValue($category, $authorised_value, $lib, $lib_opac);
+
+Create a new authorised value.
+
+=cut
+
+sub AddAuthorisedValue {
+    my ($category, $authorised_value, $lib, $lib_opac) = @_;
+
+    my $dbh = C4::Context->dbh;
+    my $query = qq{
+        INSERT INTO authorised_values (category, authorised_value, lib, lib_opac)
+        VALUES (?,?,?,?)
+    };
+    my $sth = $dbh->prepare($query);
+    $sth->execute($category, $authorised_value, $lib, $lib_opac);
+}
+
 =head2 display_marc_indicators
 
   my $display_form = C4::Koha::display_marc_indicators($field);
index e1f4593..e2a5a19 100644 (file)
 dt_add_type_uk_date();
 [% END %]
 var group_subgroups = {};
-var no_subgroup_label = _( "(None)" );
+[% FOREACH group IN groups_with_subgroups %]
+    var gid = "[% group.id %]"
+    group_subgroups[gid] = new Array();
+    [% FOREACH subgroup IN group.subgroups %]
+        var sgid = "[% subgroup.id %]";
+        var sgname = "[% subgroup.name %]";
+        group_subgroups[gid].push([sgid, sgname]);
+    [% END %]
+[% END %]
+
 function load_group_subgroups () {
-    var group = $("#group").val();
+    var group = $("#group_select").val();
     var sg = $("#subgroup");
-    var has_subgroups = false;
-    $(sg).empty().append('<option value="">' + no_subgroup_label + '</option>');
+    $(sg).find('option[value!=""]').each(function() {
+        $(this).remove();
+    });
+    $(sg).hide();
     if (group) {
+        var select = $(sg).find('select')[0];
         $.each( group_subgroups[group], function(index, value) {
-                has_subgroups = true;
-            $('<option value="' + value[0] + '">' + value[1] + '</option>').appendTo(sg);
+            $('<option value="' + value[0] + '">' + value[1] + '</option>').appendTo(select);
         } );
-    }
-    if (has_subgroups) {
-        $(sg).show();
-    } else {
-        $(sg).hide();
+        $("#subgroup, #subgroup *").show();
     }
 }
+
 $(document).ready(function(){
+
+[% IF (saved1) %]
+    [% IF (dateformat == "metric") %]
+        dt_add_type_uk_date();
+    [% END %]
+    var rtable = $("#table_reports").dataTable($.extend(true, {}, dataTablesDefaults, {
+        'bAutoWidth': false,
+        'sDom': 't<"bottom pager"ilpf>',
+        'sPaginationType': 'four_button',
+        'aLengthMenu': [[10, 20, 50, 100, -1], [10, 20, 50, 100, "All"]],
+        'iDisplayLength': 20,
+        'aaSorting': [[ 1, "asc" ]],
+        'aoColumnDefs': [
+            { 'bSortable': false, 'bSearchable':false, 'aTargets': [-1, -2, -3, -4] },
+            { 'bSearchable': false, 'aTargets': [3, 4] }
+        ],
+        'oLanguage': {
+            'sZeroRecords': _('No matching reports found')
+        }
+    }));
+
+    var rtabs = $("#tabs").tabs();
+    rtabs.bind("tabsshow", function(e, ui) {
+        $("#subgroup_filter option").each(function() {
+            if($(this).val().length > 0) {
+                $(this).remove();
+            }
+        });
+        rtable.fnFilter('', 3);
+        rtable.fnFilter('', 4);
+        rtable.fnSetColumnVis(3, true);
+        rtable.fnSetColumnVis(4, true);
+
+        var g_id = $(ui.tab).attr('id')
+        var g_name = $(ui.tab).text();
+        if (g_id && g_id.length > 0) {
+            rtable.fnFilter(g_name, 3);
+            rtable.fnSetColumnVis(3, false);
+            for(var i in group_subgroups[g_id]) {
+                $("#subgroup_filter").append(
+                    '<option value="' + group_subgroups[g_id][i][0] + '">'
+                    + group_subgroups[g_id][i][1] + '</option>'
+                );
+            }
+            $("#subgroup_filter_block").show();
+        } else {
+            $("#subgroup_filter_block").hide();
+        }
+    });
+    $("#subgroup_filter_block").hide();
+
+    $("#subgroup_filter").change(function() {
+        var selected = $(this).find('option:selected');
+        var sg_id = $(selected).val();
+        var sg_name = $(selected).text();
+        if (sg_id.length > 0) {
+            rtable.fnFilter(sg_name, 4);
+            rtable.fnSetColumnVis(4, false);
+        } else {
+            rtable.fnFilter('', 4);
+            rtable.fnSetColumnVis(4, true);
+        }
+    });
+[% END %]
+
 [% IF ( showsql ) %]
     $("#sql").focus(function() {
         $(this).select();
@@ -66,20 +139,57 @@ $(document).ready(function(){
             return false;
         }
     });
-  // call the datatables plugin
-    $("#table_reports").dataTable($.extend(true, {}, dataTablesDefaults, {
-        "aoColumnDefs": [
-            { "aTargets": [ -1, -2, -3, -4 ], "bSortable": false, "bSearchable": false },
-        ],
-        "aaSorting": [[ 1, "asc" ]],
-        "aLengthMenu": [[10, 20, 50, 100, -1], [10, 20, 50, 100, "All"]],
-        "iDisplayLength": 20,
-        "sPaginationType": "four_button"
-    }));
 [% END %]
   // redefine the datepicker's default because reports require ISO
   // http://jqueryui.com/demos/datepicker/#option-dateFormat
   $( ".datepicker" ).datepicker( "option", "dateFormat", "yy-mm-dd" );
+
+[% IF (create || editsql || save) %]
+    $("#select_group").change(function() {
+        if($(this).attr('checked')) {
+            $("#group_input").attr('disabled', 'disabled');
+            $("#groupdesc_input").attr('disabled', 'disabled');
+            $("#group_select").attr('disabled', false);
+            if ($("#group_select").val().length > 0) {
+                $("#select_subgroup").attr('checked', 'checked');
+                $("#select_subgroup").change();
+                $("#subgroup, #subgroup *").show();
+            } else {
+                $("#subgroup").hide();
+            }
+        }
+    });
+    $("#create_group").change(function() {
+        if($(this).attr('checked')) {
+            $("#group_input").attr('disabled', false);
+            $("#groupdesc_input").attr('disabled', false);
+            $("#group_select").attr('disabled', 'disabled');
+            $("#create_subgroup").attr('checked', 'checked').change();
+            $("#subgroup_select").hide();
+            $("#subgroup input[type='radio']").hide();
+            $("#subgroup label[for]").hide();
+            $("#subgroup_input").show();
+            $("#subgroupdesc_input").show();
+            $("#subgroup").show();
+        }
+    });
+    $("#select_subgroup").change(function() {
+        if($(this).attr('checked')) {
+            $("#subgroup_select").attr('disabled', false);
+            $("#subgroup_input").attr('disabled', 'disabled');
+            $("#subgroupdesc_input").attr('disabled', 'disabled');
+        }
+    });
+    $("#create_subgroup").change(function() {
+        if($(this).attr('checked')) {
+            $("#subgroup_input").attr('disabled', false);
+            $("#subgroupdesc_input").attr('disabled', false);
+            $("#subgroup_select").attr('disabled', 'disabled');
+        }
+    });
+    $("#select_group").change();
+    $("#select_subgroup").change();
+[% END %]
 });
 //]]>
 </script>
@@ -149,61 +259,78 @@ canned reports and writing custom SQL reports.</p>
 [% IF ( saved1 ) %]
 [% IF ( savedreports ) %]<h1>Saved reports</h1>
 
-<table id="table_reports" class="tablesorter">
-<thead>
- <tr>
-  <th>ID</th>
-  <th>Report name</th>
-  <th>Type</th>
-  <th>Area</th>
-  <th>Group</th>
-  <th>Subgroup</th>
-  <th>Notes</th>
-  <th>Author</th>
-  <th>Creation date</th>
-  <th>Public</th>
-  [% IF (usecache) %] <th>Cache expiry (seconds)</th> [% END %]
-  <th>Saved results</th>
-  <th>Saved SQL</th>
-  <th>&nbsp;</th>
-  <th>&nbsp;</th>
- </tr>
-</thead>
-<tbody>
-[% FOREACH savedreport IN savedreports %]
-[% UNLESS ( loop.odd ) %]<tr class="odd">[% ELSE %]<tr>[% END %]
-<td>[% savedreport.id %]</td>
-<td>[% savedreport.report_name %]</td>
-<td>[% savedreport.type %]</td>
-<td>[% savedreport.areaname %]</td>
-<td>[% savedreport.groupname %]</td>
-<td>[% savedreport.subgroupname %]</td>
-<td>[% savedreport.notes %]</td>
-<td>[% savedreport.borrowersurname %][% IF ( savedreport.borrowerfirstname ) %], [% savedreport.borrowerfirstname %][% END %] ([% savedreport.borrowernumber %])</td>
-<td>[% savedreport.date_created %]</td>
-[% IF (savedreport.public) %]
-<td>Yes</td>
-[% ELSE %]
-<td>No</td>
-[% END %]
-[% IF (usecache) %] <td>[% savedreport.cache_expiry %]</td> [% END %]
-<td>[% IF ( savedreport.date_run ) %]<a href="/cgi-bin/koha/reports/guided_reports.pl?phase=retrieve%20results&amp;id=[% savedreport.id %]">[% savedreport.date_run %]</a>[% END %]
-</td>
-    <td>
-        <a href="/cgi-bin/koha/reports/guided_reports.pl?reports=[% savedreport.id %]&amp;phase=Show%20SQL">Show</a> 
-        [% IF ( CAN_user_reports_create_reports ) %]
-            &nbsp; <a href="/cgi-bin/koha/reports/guided_reports.pl?reports=[% savedreport.id %]&amp;phase=Edit%20SQL">Edit</a>
+<div id="tabs" class="toptabs">
+    <ul>
+        <li><a href="#reports">All</a></li>
+        [% FOREACH group IN groups_with_subgroups %]
+            <li><a id="[% group.id %]" href="#reports">[% group.name %]</a></li>
         [% END %]
-    </td>
-<td><a href="/cgi-bin/koha/reports/guided_reports.pl?reports=[% savedreport.id %]&amp;phase=Run%20this%20report">Run</a>
-<a href="/cgi-bin/koha/tools/scheduler.pl?id=[% savedreport.id %]">Schedule</a></td>
-    [% IF ( CAN_user_reports_create_reports ) %]
-        <td><a class="confirmdelete" title="Delete this saved report" href="/cgi-bin/koha/reports/guided_reports.pl?reports=[% savedreport.id %]&amp;phase=Delete%20Saved">Delete</a></td>
-    [% END %]
-</tr>
-[% END %]
-</tbody>
-</table>
+    </ul>
+    <div id="reports">
+        <div id="subgroup_filter_block">
+            <label for="subgroup_filter">Subgroup:</label>
+            <select id="subgroup_filter">
+                <option value="">All</option>
+            </select>
+        </div>
+        <table id="table_reports">
+            <thead>
+                <tr>
+                    <th>ID</th>
+                    <th>Report name</th>
+                    <th>Type</th>
+                    <th>Group</th>
+                    <th>Subgroup</th>
+                    <th>Notes</th>
+                    <th>Author</th>
+                    <th>Creation date</th>
+                    <th>Public</th>
+                    [% IF (usecache) %] <th>Cache expiry (seconds)</th> [% END %]
+                    <th>Saved results</th>
+                    <th>Saved SQL</th>
+                    <th>&nbsp;</th>
+                    <th>&nbsp;</th>
+                </tr>
+            </thead>
+            <tbody>
+                [% FOREACH savedreport IN savedreports %]
+                    [% UNLESS ( loop.odd ) %]<tr class="odd">[% ELSE %]<tr>[% END %]
+                        <td>[% savedreport.id %]</td>
+                        <td>[% savedreport.report_name %]</td>
+                        <td>[% savedreport.type %]</td>
+                        <td>[% savedreport.groupname %]</td>
+                        <td>[% savedreport.subgroupname %]</td>
+                        <td>[% savedreport.notes %]</td>
+                        <td>[% savedreport.borrowersurname %][% IF ( savedreport.borrowerfirstname ) %], [% savedreport.borrowerfirstname %][% END %] ([% savedreport.borrowernumber %])</td>
+                        <td>[% savedreport.date_created %]</td>
+                        [% IF (savedreport.public) %]
+                        <td>Yes</td>
+                        [% ELSE %]
+                        <td>No</td>
+                        [% END %]
+                        [% IF (usecache) %] <td>[% savedreport.cache_expiry %]</td> [% END %]
+                        <td>[% IF ( savedreport.date_run ) %]<a href="/cgi-bin/koha/reports/guided_reports.pl?phase=retrieve%20results&amp;id=[% savedreport.id %]">[% savedreport.date_run %]</a>[% END %]
+                        </td>
+                        <td>
+                            <a href="/cgi-bin/koha/reports/guided_reports.pl?reports=[% savedreport.id %]&amp;phase=Show%20SQL">Show</a>
+                            [% IF ( CAN_user_reports_create_reports ) %]
+                                <a href="/cgi-bin/koha/reports/guided_reports.pl?reports=[% savedreport.id %]&amp;phase=Edit%20SQL">Edit</a>
+                                <a title="Duplicate this saved report" href="/cgi-bin/koha/reports/guided_reports.pl?phase=Create report from SQL&sql=[% savedreport.savedsql |uri %]&reportname=[% savedreport.report_name |uri %]&notes=[% savedreport.notes |uri %]">Duplicate</a>
+                            [% END %]
+                        </td>
+                        <td>
+                            <a href="/cgi-bin/koha/reports/guided_reports.pl?reports=[% savedreport.id %]&amp;phase=Run%20this%20report">Run</a>
+                            <a href="/cgi-bin/koha/tools/scheduler.pl?id=[% savedreport.id %]">Schedule</a>
+                        </td>
+                        [% IF ( CAN_user_reports_create_reports ) %]
+                            <td><a class="confirmdelete" title="Delete this saved report" href="/cgi-bin/koha/reports/guided_reports.pl?reports=[% savedreport.id %]&amp;phase=Delete%20Saved">Delete</a></td>
+                        [% END %]
+                    </tr>
+                [% END %]
+            </tbody>
+        </table>
+    </div>
+</div>
 [% ELSE %]<div class="dialog message">
     [% IF (filter_set) %]
     <h4>No saved reports match your criteria. </h4>
@@ -515,31 +642,7 @@ canned reports and writing custom SQL reports.</p>
 <legend>Save your custom report</legend>
 <ol>
     <li><label for="reportname">Report name: </label><input type="text" id="reportname" name="reportname" /></li>
-    [% IF groups_with_subgroups %]
-    <li><label for="group">Report group: </label><select name="group" id="group" onChange="load_group_subgroups();">
-        [% FOR g IN groups_with_subgroups %]
-            [% IF g.selected %]
-    <option value="[% g.id %]" selected>[% g.name %]</option>
-            [% ELSE %]
-    <option value="[% g.id %]">[% g.name %]</option>
-            [% END %]
-    <script type="text/javascript">
-        var g_sg = new Array();
-            [% FOR sg IN g.subgroups %]
-        g_sg.push(["[% sg.id %]", "[% sg.name %]"]);
-                [% IF sg.selected %]
-        $(document).ready(function() {
-            $("#subgroup").val("[% sg.id %]");
-        });
-                [% END %]
-            [% END %]
-        group_subgroups["[% g.id %]"] = g_sg;
-    </script>
-        [% END %]
-    </select></li>
-    <li><label for="subgroup">Report subgroup: </label><select name="subgroup" id="subgroup">
-    </select></li>
-    [% END %]
+    [% PROCESS group_and_subgroup_selection %]
     <li><label for="notes">Notes:</label> <textarea name="notes" id="notes"></textarea></li>
 </ol></fieldset>
 <fieldset class="action"><input type="hidden" name="phase" value="Save Report" />
@@ -635,31 +738,8 @@ $(document).ready(function() {
         [% IF ( reportname ) %]<input type="text" id="reportname" name="reportname" value="[% reportname %]" />
         [% ELSE %]<input type="text" id="reportname" name="reportname" />[% END %] 
     </li>
-    [% IF groups_with_subgroups %]
-    <li><label for="group">Report group: </label><select name="group" id="group" onChange="load_group_subgroups();">
-        [% FOR g IN groups_with_subgroups %]
-            [% IF g.selected %]
-    <option value="[% g.id %]" selected>[% g.name %]</option>
-            [% ELSE %]
-    <option value="[% g.id %]">[% g.name %]</option>
-            [% END %]
-    <script type="text/javascript">
-        var g_sg = new Array();
-            [% FOR sg IN g.subgroups %]
-        g_sg.push(["[% sg.id %]", "[% sg.name %]"]);
-                [% IF sg.selected %]
-        $(document).ready(function() {
-            $("#subgroup").val("[% sg.id %]");
-        });
-                [% END %]
-            [% END %]
-        group_subgroups["[% g.id %]"] = g_sg;
-    </script>
-        [% END %]
-    </select></li>
-    <li><label for="subgroup">Report subgroup: </label><select name="subgroup" id="subgroup">
-    </select></li>
-    [% END %]
+    [% PROCESS group_and_subgroup_selection %]
+
 [% IF (public) %]
   <li><label for="public">Report is public:</label><select id="public" name="public"> <option value="0">No (default)</option> <option value="1" selected="selected">Yes</public> </select></li>
 [% ELSE %]
@@ -744,11 +824,6 @@ Sub report:<select name="subreport">
 [% END %]
 
 [% IF ( editsql ) %]
-<script type="text/javascript">
-$(document).ready(function() {
-    load_group_subgroups();
-});
-</script>
 <form action="/cgi-bin/koha/reports/guided_reports.pl" method="post">
 <input type="hidden" name="phase" value="Update SQL" />
 <input type="hidden" name="id" value="[% id %]"/>
@@ -756,31 +831,7 @@ $(document).ready(function() {
 <legend>Edit SQL report</legend>
 <ol>
 <li><label for="reportname">Report name:</label><input type="text" id="reportname" name="reportname" value="[% reportname %]" size="50" /></li>
-    [% IF groups_with_subgroups %]
-    <li><label for="group">Report group: </label><select name="group" id="group" onChange="load_group_subgroups();">
-        [% FOR g IN groups_with_subgroups %]
-            [% IF g.selected %]
-    <option value="[% g.id %]" selected>[% g.name %]</option>
-            [% ELSE %]
-    <option value="[% g.id %]">[% g.name %]</option>
-            [% END %]
-    <script type="text/javascript">
-        var g_sg = new Array();
-            [% FOR sg IN g.subgroups %]
-        g_sg.push(["[% sg.id %]", "[% sg.name %]"]);
-                [% IF sg.selected %]
-        $(document).ready(function() {
-            $("#subgroup").val("[% sg.id %]");
-        });
-                [% END %]
-            [% END %]
-        group_subgroups["[% g.id %]"] = g_sg;
-    </script>
-        [% END %]
-    </select></li>
-    <li><label for="subgroup">Report subgroup: </label><select name="subgroup" id="subgroup">
-    </select></li>
-    [% END %]
+[% PROCESS group_and_subgroup_selection %]
 [% IF (public) %]
   <li><label for="public">Report is public:</label><select id="public" name="public"> <option value="0">No (default)</option> <option value="1" selected="selected">Yes</public> </select></li>
 [% ELSE %]
@@ -848,43 +899,12 @@ $(document).ready(function() {
 
 [% IF ( saved1 ) %]
 <div id="saved-reports-filter">
-<script type="text/javascript">
-$(document).ready(function() {
-    no_subgroup_label = _( "-- All --" );
-    load_group_subgroups();
-});
-</script>
 <form action="/cgi-bin/koha/reports/guided_reports.pl" method="get">
   <input type="hidden" name="phase" value="Use saved" />
   <input type="hidden" name="filter_set" value="1" />
   <fieldset class="brief">
   <h3>Filter</h3>
   <ol>
-    <li><label for="group">Choose Group and Subgroup: </label>
-    <select name="group" id="group" onChange="load_group_subgroups();">
-        <option value="">-- All --</option>
-    [% FOR g IN groups_with_subgroups %]
-        [% IF g.selected %]
-        <option value="[% g.id %]" selected>[% g.name %]</option>
-        [% ELSE %]
-        <option value="[% g.id %]">[% g.name %]</option>
-        [% END %]
-        <script type="text/javascript">
-            var g_sg = new Array();
-        [% FOR sg IN g.subgroups %]
-            g_sg.push(["[% sg.id %]", "[% sg.name %]"]);
-            [% IF sg.selected %]
-            $(document).ready(function() {
-                $("#subgroup").val("[% sg.id %]");
-            });
-            [% END %]
-        [% END %]
-            group_subgroups["[% g.id %]"] = g_sg;
-        </script>
-    [% END %]
-    </select>
-    <select name="subgroup" id="subgroup"></select>
-    </li>
     <li><label for="filter_date">Date:</label> <input type="text" id="filter_date" name="filter_date" size="10" value="[% filter_date %]" class="datepicker" />
     <div class="hint">[% INCLUDE 'date-format.inc' %]</div>
 
@@ -920,3 +940,56 @@ $("#column_submit").submit(function() {
 //]]>
 </script>
 [% INCLUDE 'intranet-bottom.inc' %]
+
+
+[% BLOCK group_and_subgroup_selection %]
+    <li id="group">
+        <label>Report group:</label>
+        <input type="radio" name="select_or_create_group"
+            id="select_group" checked="checked" />
+        <label for="select_group" style="float:none">Select</label>
+        <select name="group" onChange="load_group_subgroups();" id="group_select">
+            <option value="">(None)</option>
+            [% FOREACH group IN groups_with_subgroups %]
+                [% IF (group.selected) %]
+                    <option value="[% group.id %]" selected="selected">
+                [% ELSE %]
+                    <option value="[% group.id %]">
+                [% END %]
+                    [% group.name %]
+                </option>
+            [% END %]
+        </select>
+        <input type="radio" name="select_or_create_group" id="create_group" />
+        <label for="create_group" style="float:none">or create</label>
+        <input type="text" name="group" id="group_input" title="Group code" placeholder="Code" />
+        <input type="text" name="groupdesc" id="groupdesc_input" title="Group name" placeholder="Name" />
+    </li>
+    <li id="subgroup">
+        <label>Report subgroup:</label>
+        <input type="radio" name="select_or_create_subgroup"
+            id="select_subgroup" checked="checked" />
+        <label for="select_subgroup" style="float:none">Select</label>
+        <select name="subgroup" id="subgroup_select">
+            <option value="">(None)</option>
+            [% FOREACH group IN groups_with_subgroups %]
+                [% IF (group.selected) %]
+                    [% FOREACH subgroup IN group.subgroups %]
+                        [% IF (subgroup.selected) %]
+                            <option value="[% subgroup.id %]" selected="selected">
+                        [% ELSE %]
+                            <option value="[% subgroup.id %]">
+                        [% END %]
+                            [% subgroup.name %]
+                        </option>
+                    [% END %]
+                [% END %]
+            [% END %]
+        </select>
+        <input type="radio" name="select_or_create_subgroup"
+            id="create_subgroup" />
+        <label for="create_subgroup" style="float:none">or create</label>
+        <input type="text" name="subgroup" id="subgroup_input" title="Subgroup code" placeholder="Code" />
+        <input type="text" name="subgroupdesc" id="subgroupdesc_input" title="Subgroup name" placeholder="Name" />
+    </li>
+[% END %]
index 23d85a9..0171cb4 100755 (executable)
@@ -17,8 +17,6 @@
 # with Koha; if not, write to the Free Software Foundation, Inc.,
 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 
-use strict;
-use warnings;
 
 use CGI;
 use Text::CSV;
@@ -106,6 +104,7 @@ elsif ( $phase eq 'Build new' ) {
         'savedreports' => get_saved_reports($filter),
         'usecache' => $usecache,
         'groups_with_subgroups'=> groups_with_subgroups($group, $subgroup),
+        dateformat => C4::Context->preference('dateformat'),
     );
 }
 
@@ -178,6 +177,8 @@ elsif ( $phase eq 'Update SQL'){
       push @errors, {cache_expiry => $cache_expiry};
     }
 
+    create_non_existing_group_and_subgroup($input, $group, $subgroup);
+
     if ($sql =~ /;?\W?(UPDATE|DELETE|DROP|INSERT|SHOW|CREATE)\W/i) {
         push @errors, {sqlerr => $1};
     }
@@ -482,6 +483,9 @@ elsif ( $phase eq 'Save Report' ) {
     if( $cache_expiry && $cache_expiry >= 2592000 ){
       push @errors, {cache_expiry => $cache_expiry};
     }
+
+    create_non_existing_group_and_subgroup($input, $group, $subgroup);
+
     ## FIXME this is AFTER entering a name to save the report under
     if ($sql =~ /;?\W?(UPDATE|DELETE|DROP|INSERT|SHOW|CREATE)\W/i) {
         push @errors, {sqlerr => $1};
@@ -735,9 +739,9 @@ elsif ( $phase eq 'Create report from SQL' ) {
         $group = $input->param('report_group');
         $subgroup  = $input->param('report_subgroup');
         $template->param(
-            'sql'           => $input->param('sql'),
-            'reportname'    => $input->param('reportname'),
-            'notes'         => $input->param('notes'),
+            'sql'           => $input->param('sql') // '',
+            'reportname'    => $input->param('reportname') // '',
+            'notes'         => $input->param('notes') // '',
         );
     }
     $template->param(
@@ -790,13 +794,17 @@ sub groups_with_subgroups {
 
     my $groups_with_subgroups = get_report_groups();
     my @g_sg;
-    while (my ($g_id, $v) = each %$groups_with_subgroups) {
+    my @sorted_keys = sort {
+        $groups_with_subgroups->{$a}->{name} cmp $groups_with_subgroups->{$b}->{name}
+    } keys %$groups_with_subgroups;
+    foreach my $g_id (@sorted_keys) {
+        my $v = $groups_with_subgroups->{$g_id};
         my @subgroups;
         if (my $sg = $v->{subgroups}) {
-            while (my ($sg_id, $n) = each %$sg) {
+            foreach my $sg_id (sort { $sg->{$a} cmp $sg->{$b} } keys %$sg) {
                 push @subgroups, {
                     id => $sg_id,
-                    name => $n,
+                    name => $sg->{$sg_id},
                     selected => ($group && $g_id eq $group && $subgroup && $sg_id eq $subgroup ),
                 };
             }
@@ -810,3 +818,21 @@ sub groups_with_subgroups {
     }
     return \@g_sg;
 }
+
+sub create_non_existing_group_and_subgroup {
+    my ($input, $group, $subgroup) = @_;
+
+    if (defined $group and $group ne '') {
+        my $report_groups = C4::Reports::Guided::get_report_groups;
+        if (not exists $report_groups->{$group}) {
+            my $groupdesc = $input->param('groupdesc') // $group;
+            C4::Koha::AddAuthorisedValue('REPORT_GROUP', $group, $groupdesc);
+        }
+        if (defined $subgroup and $subgroup ne '') {
+            if (not exists $report_groups->{$group}->{subgroups}->{$subgroup}) {
+                my $subgroupdesc = $input->param('subgroupdesc') // $subgroup;
+                C4::Koha::AddAuthorisedValue('REPORT_SUBGROUP', $subgroup, $subgroupdesc, $group);
+            }
+        }
+    }
+}