From 751051f1e73cabcf8c0491c233f8852bd551d567 Mon Sep 17 00:00:00 2001
From: Owen Leonard <oleonard@myacpl.org>
Date: Thu, 24 Sep 2020 14:23:11 +0000
Subject: [PATCH] Bug 5697: Automatic linking in guided reports

This patch modifies the output of reports so that if certain columns are
returned the data in the colums automatically offer menus of actions:

- borrowernumber: View, edit, check out
- cardnumber: Check out
- itemnumber: View, edit
- biblionumber: View, edit

Like the menu of batch operations, this functionality is available only
if the column is returned with its original name (e.g. biblionumber, not
"biblionumber AS `Biblio number`).

To test, apply the patch and run a report which will return examples of
each kind of data. I used:

SELECT biblio.biblionumber , biblio.title, items.itemnumber,
items.itemcallnumber, items.barcode, borrowers.firstname,
borrowers.surname, borrowers.borrowernumber, borrowers.cardnumber
FROM issues
LEFT JOIN borrowers ON borrowers.borrowernumber=issues.borrowernumber
LEFT JOIN items ON issues.itemnumber=items.itemnumber
LEFT JOIN biblio ON items.biblionumber=biblio.biblionumber
ORDER BY RAND() LIMIT 50

When the report runs you should see that the data in each affected
column is displayed as a link, each of which should trigger the correct
menu. Test each case to confirm that the correct page is opened.

Test the "toggle data menus" control. Clicking it should hide and show
the menus in the report results.
---
 .../en/modules/reports/guided_reports_start.tt     | 86 +++++++++++++++++++---
 1 file changed, 75 insertions(+), 11 deletions(-)

diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/reports/guided_reports_start.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/reports/guided_reports_start.tt
index 153d7b7ced..644a1daee1 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/reports/guided_reports_start.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/reports/guided_reports_start.tt
@@ -54,6 +54,7 @@
         resize:  vertical;
     }
     #mana_search_errortext { font-family: monospace; font-weight: bold; }
+    .data-plain { display: none; }
 </style>
 [% Asset.css("css/reports.css") | $raw %]
 [% Asset.css("lib/d3c3/c3.min.css") | $raw %]
@@ -930,7 +931,7 @@
                                 <textarea style="display:none" name="cardnumberlist" id="cardnumberlist">[% FOREACH result IN results %][% FOREACH cells IN result.cells %][% place = loop.index %][% IF header_row.$place.cell == 'cardnumber' || header_types.item(header_row.$place.cell) == 'cardnumber' %][% SET batch_cardnumbers = 1 %][% cells.cell | html %][% END %][% END %][% END %]</textarea>
                             </form>
 
-                            [% IF ( batch_biblionumbers || batch_itemnumbers || batch_cardnumbers ) %]
+                                [% IF ( batch_biblionumbers || batch_itemnumbers || batch_cardnumbers ) %]
                                 <p>
                                     <div class="btn-group">
                                         <button class="btn btn-default dropdown-toggle" type="button" id="batch_mod_menu" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
@@ -975,6 +976,11 @@
                                             [% END %]
                                         </ul> <!-- /.dropdown-menu -->
                                     </div> <!-- /.dropdown -->
+                                    <a href="#" id="toggle_auto_links">
+                                        <i class="fa fa-eye autolink" style="display:none"></i>
+                                        <i class="fa fa-eye-slash autolink"></i>
+                                        Toggle data menus
+                                    </a>
                                 </p>
                             [% END # /IF ( batch_biblionumbers || batch_itemnumbers || batch_cardnumbers ) %]
                         [% END # UNLESS ( errors ) %]
@@ -1006,19 +1012,33 @@
 
                     <div class="pages">[% pagination_bar | $raw %]</div>
                     [% UNLESS ( errors ) %]
-                        <table>
-                            <tr>
-                                [% FOREACH header_ro IN header_row %]
-                                    <th>[% header_ro.cell | html %]</th>
-                                [% END %]
-                            </tr>
-                            [% FOREACH result IN results %]
+                        <table id="report_results">
+                            <thead>
                                 <tr>
-                                    [% FOREACH cells IN result.cells %]
-                                        <td>[% cells.cell | $raw %]</td>
+                                    [% FOREACH header_ro IN header_row %]
+                                        [% IF header_ro.has_itemnumbers && ( header_ro.cell == 'itemnumber' || header_types.item( header_ro.cell ) == 'itemnumber' ) %]
+                                            <th class="itemnumber">[% header_ro.cell | html %]</th>
+                                        [% ELSIF header_ro.has_biblionumbers && ( header_ro.cell == 'biblionumber' || header_types.item( header_ro.cell ) == 'biblionumber' ) %]
+                                            <th class="biblionumber">[% header_ro.cell | html %]</th>
+                                        [% ELSIF header_ro.cell == 'cardnumber' || header_types.item( header_ro.cell ) == 'cardnumber' %]
+                                            <th class="cardnumber">[% header_ro.cell | html %]</th>
+                                        [% ELSIF header_ro.cell == 'borrowernumber' || header_types.item( header_ro.cell ) == 'borrowernumber' %]
+                                            <th class="borrowernumber">[% header_ro.cell | html %]</th>
+                                        [% ELSE %]
+                                            <th>[% header_ro.cell | html %]</th>
+                                        [% END %]
                                     [% END %]
                                 </tr>
-                            [% END %]
+                            </thead>
+                            <tbody>
+                                [% FOREACH result IN results %]
+                                    <tr>
+                                        [% FOREACH cells IN result.cells %]
+                                            <td>[% cells.cell | $raw %]</td>
+                                        [% END %]
+                                    </tr>
+                                [% END %]
+                            </tbody>
                         </table>
                     [% END %]
 
@@ -1802,6 +1822,50 @@
                 e.preventDefault();
                 addToList();
             });
+
+            $(".limitselect").on("click", function(){
+                var limit = $(this).data("limit");
+                $("#limit").val( limit );
+                $("#limitselect").submit();
+            });
+
+            if( $("#report_results").length > 0 ){
+                $("#report_results").dataTable($.extend(true, {}, dataTablesDefaults, {
+                    "dom": 't',
+                    "paginate": false,
+                    "ordering": false,
+                    "columnDefs": [
+                        {
+                            targets: "biblionumber",
+                            render: function ( data ) {
+                                return '<span class="data-plain">' + data + '</span><div class="btn-group dropup autolink"><a class="btn btn-link dropdown-toggle" role="button" data-toggle="dropdown" href="#">' + data + ' <b class="caret"></b></a><ul class="dropdown-menu pull-right" role="menu"><li><a target="_blank" href="/cgi-bin/koha/catalogue/detail.pl?biblionumber=' + data + '"><i class="fa fa-window-restore"></i> View record</a></li><li role="separator" class="divider"></li><li><a target="_blank" href="/cgi-bin/koha/cataloguing/addbiblio.pl?biblionumber=' + data + '"><i class="fa fa-pencil"></i> Edit record</a></li></ul></div>';
+                            }
+                        },
+                        {
+                            targets: "itemnumber",
+                            render: function ( data ) {
+                                return '<span class="data-plain">' + data + '</span><div class="btn-group dropup autolink"><a class="btn btn-link dropdown-toggle" role="button" data-toggle="dropdown" href="#">' + data + ' <b class="caret"></b></a><ul class="dropdown-menu pull-right" role="menu"><li><a target="_blank" href="/cgi-bin/koha/catalogue/moredetail.pl?itemnumber=' + data + '"><i class="fa fa-window-restore"></i> View item</a></li><li role="separator" class="divider"></li><li><a target="_blank" href="/cgi-bin/koha/cataloguing/additem.pl?op=edititem&itemnumber=' + data + '#edititem"><i class="fa fa-pencil"></i> Edit item</a></li></ul></div>';
+                            }
+                        },
+                        {
+                            targets: "borrowernumber",
+                            render: function ( data ) {
+                                return '<span class="data-plain">' + data + '</span><div class="btn-group dropup autolink"><a class="btn btn-link dropdown-toggle" role="button" data-toggle="dropdown" href="#">' + data + '  <b class="caret"></b></a><ul class="dropdown-menu pull-right" role="menu"><li><a target="_blank" href="/cgi-bin/koha/members/moremember.pl?borrowernumber=' + data + '"><i class="fa fa-window-restore"></i> View patron</a></li><li role="separator" class="divider"></li><li><a target="_blank" href="/cgi-bin/koha/members/memberentry.pl?borrowernumber=' + data + '"><i class="fa fa-pencil"></i> Edit patron</a></li><li role="separator" class="divider"></li><li><a target="_blank" href="/cgi-bin/koha/circ/circulation.pl?borrowernumber=' + data + '"><i class="fa fa-window-restore"></i> Check out</a></li></ul></div>';
+                            }
+                        },
+                        {
+                            targets: "cardnumber",
+                            render: function ( data ) {
+                                return '<span class="data-plain">' + data + '</span><div class="btn-group dropup autolink"><a class="btn btn-link dropdown-toggle" role="button" data-toggle="dropdown" href="#">' + data + ' <b class="caret"></b></a><ul class="dropdown-menu pull-right" role="menu"><li><a target="_blank" href="/cgi-bin/koha/circ/circulation.pl?findborrower=' + data + '"><i class="fa fa-window-restore"></i> Check out</a></li></ul></div>';
+                            }
+                        },
+                    ]
+                }));
+            }
+            $("#toggle_auto_links").on("click", function(e){
+                e.preventDefault();
+                $(".data-plain,.autolink").toggle();
+            });
         });
 
         function addColumn() {
-- 
2.11.0