From 3d0783a9b04e27a38aad7b4a0397528d8238f439 Mon Sep 17 00:00:00 2001
From: Alex Arnaud <alex.arnaud@biblibre.com>
Date: Fri, 10 Nov 2017 14:46:02 +0000
Subject: [PATCH] Bug 17047 - Move ability to comment mana entities from search
 results table to report/subscription detail page

+ code refactoring
---
 C4/Reports/Guided.pm                               | 11 +++-
 C4/Serials.pm                                      |  6 ++
 Koha/SharedContent.pm                              | 12 +++-
 koha-tmpl/intranet-tmpl/prog/en/includes/mana.inc  | 44 +++++++++++++++
 .../en/includes/mana/mana-report-search-result.inc | 27 +--------
 .../mana/mana-subscription-search-result.inc       | 13 +----
 .../prog/en/includes/reports-toolbar.inc           | 66 ++++++++++++----------
 .../prog/en/includes/serials-toolbar.inc           | 35 ++++++++++++
 .../en/modules/reports/guided_reports_start.tt     | 20 -------
 .../intranet-tmpl/prog/js/subscription-add.js      | 45 ---------------
 reports/guided_reports.pl                          |  4 ++
 serials/subscription-detail.pl                     |  1 +
 svc/mana/share                                     |  2 +-
 13 files changed, 147 insertions(+), 139 deletions(-)
 create mode 100644 koha-tmpl/intranet-tmpl/prog/en/includes/mana.inc

diff --git a/C4/Reports/Guided.pm b/C4/Reports/Guided.pm
index d61b282..70d1f25 100644
--- a/C4/Reports/Guided.pm
+++ b/C4/Reports/Guided.pm
@@ -33,6 +33,7 @@ use C4::Log;
 
 use Koha::AuthorisedValues;
 use Koha::Patron::Categories;
+use Koha::SharedContent;
 
 BEGIN {
     require Exporter;
@@ -715,7 +716,15 @@ sub get_saved_report {
     } else {
         return;
     }
-    return $dbh->selectrow_hashref($query, undef, $report_arg);
+    my $report = $dbh->selectrow_hashref($query, undef, $report_arg);
+
+    if ( my $mana_id = $report->{mana_id} ) {
+        my $mana_report = Koha::SharedContent::manaGetRequestWithId(
+            'report', $mana_id, {usecomments => 1});
+        $report->{comments} = $mana_report->{data}->{comments};
+    }
+
+    return $report;
 }
 
 =head2 create_compound($masterID,$subreportID)
diff --git a/C4/Serials.pm b/C4/Serials.pm
index d7c44e8..abb08e9 100644
--- a/C4/Serials.pm
+++ b/C4/Serials.pm
@@ -300,6 +300,12 @@ sub GetSubscription {
     });
     $subscription->{additional_fields} = $additional_field_values->{$subscriptionid};
 
+    if ( my $mana_id = $subscription->{mana_id} ) {
+        my $mana_subscription = Koha::SharedContent::manaGetRequestWithId(
+            'subscription', $mana_id, {usecomments => 1});
+        $subscription->{comments} = $mana_subscription->{data}->{comments};
+    }
+
     return $subscription;
 }
 
diff --git a/Koha/SharedContent.pm b/Koha/SharedContent.pm
index c54a8e1..ca484c1 100644
--- a/Koha/SharedContent.pm
+++ b/Koha/SharedContent.pm
@@ -78,9 +78,11 @@ sub manaIncrementRequest {
 =cut
 
 sub manaPostRequest {
-    my ($lang, $loggedinuser, $resourceid, $resourcetype) = @_;
+    my ($lang, $loggedinuser, $resourceid, $resourcetype, $content) = @_;
 
-    my $content = prepareSharedData($lang, $loggedinuser, $resourceid, $resourcetype);
+    unless ( $content ) {
+        $content = prepareSharedData($lang, $loggedinuser, $resourceid, $resourcetype);
+    }
 
     my $result = manaRequest(buildRequest('post', $resourcetype, $content));
 
@@ -161,8 +163,12 @@ sub buildRequest {
 
     if ( $type eq 'getwithid' ) {
         my $id = shift;
+        my $params = shift;
+        $params = join '&',
+            map { defined $params->{$_} ? $_ . "=" . $params->{$_} : () }
+            keys %$params;
 
-        my $url = "$MANA_IP/$resource/$id.json";
+        my $url = "$MANA_IP/$resource/$id.json?$params";
         return HTTP::Request->new( GET => $url );
     }
 
diff --git a/koha-tmpl/intranet-tmpl/prog/en/includes/mana.inc b/koha-tmpl/intranet-tmpl/prog/en/includes/mana.inc
new file mode 100644
index 0000000..d57290c
--- /dev/null
+++ b/koha-tmpl/intranet-tmpl/prog/en/includes/mana.inc
@@ -0,0 +1,44 @@
+<script type="text/JavaScript">
+//<![CDATA[
+$(document).ready(function() {
+    function mana_increment(mana_id, resource, fieldvalue, stepvalue = 1) {
+        $.ajax( {
+            type: "POST",
+            url: "/cgi-bin/koha/svc/mana/increment",
+            data: {id: mana_id, resource: resource, field: fieldvalue, step: stepvalue},
+            datatype: "json",
+        })
+    }
+
+    function mana_comment( target_id, manamsg, resource_type ) {
+        $.ajax( {
+            type: "POST",
+            url: "/cgi-bin/koha/svc/mana/share",
+            data: {message: manamsg, resource: resource_type , resource_id: target_id},
+            datatype: "json",
+        })
+    }
+
+    $(document).on('click', 'ul li.mana-comment', function() {
+        id = $(this).attr('data-id');
+        mana_increment(id, 'resource_comment', 'nb');
+    });
+
+    $(document).on('click', 'ul li.mana-other-comment', function() {
+        $('#mana-comment-box').modal('show');
+    });
+
+    $(document).on('click', '#mana-send-comment', function() {
+        var resource_type = $('#mana-resource').val();
+        var resource_id = $('#mana-resource-id').val();
+        var comment = $("#mana-comment").val();
+        mana_comment(resource_id, comment, resource_type);
+        $("#mana-comment-box").modal("hide");
+    });
+
+    $(document).on('click', '#mana-comment-close', function() {
+        $("#mana-comment-box").modal("hide");
+    });
+});
+//]]>
+</script>
\ No newline at end of file
diff --git a/koha-tmpl/intranet-tmpl/prog/en/includes/mana/mana-report-search-result.inc b/koha-tmpl/intranet-tmpl/prog/en/includes/mana/mana-report-search-result.inc
index 312af44..b95600c 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/includes/mana/mana-report-search-result.inc
+++ b/koha-tmpl/intranet-tmpl/prog/en/includes/mana/mana-report-search-result.inc
@@ -24,6 +24,7 @@ $(document).ready(function() {
 });
 //]]>
 </script>
+[% INCLUDE 'mana.inc' %]
 
 [% IF statuscode == "200" AND reports %]
     <table id="mana_results_datatable" width=100%>
@@ -68,14 +69,6 @@ $(document).ready(function() {
                     [% UNLESS search_only %]
                         <td>
                             <button class="mana-use" id="mana-use-[% report.id %]"><i class="fa fa-inbox"></i> Use</button>
-                            <select class="mana-actions" id="mana-actions-[% report.id %]">
-                                <option selected disabled>Report mistake</option>
-                                [% FOREACH comment IN report.comments %]
-                                    <option value="[% comment.id %]">[% comment.message %] ([% comment.nb %])</option>
-                                [% END %]
-                                    <option>other</option>
-                            </select>
-                            <button hidden class="actionreport2" hidden> Cancel</button>
                         </td>
                     [% END %]
                   </tr>
@@ -86,21 +79,3 @@ $(document).ready(function() {
 [% ELSE %]
     <h4> [% msg %]  statuscode: [% statuscode %]</h4>
 [% END %]
-
-<div id="comment_box" class="modal" tabindex="-1" role="dialog" aria-labelledby="mana_search_result_label" style="display: none;">
-    <div class="modal-dialog modal-lg" style="width: 30%">
-        <div class="modal-content" style="">
-            <div class="modal-header">
-                <button type="button" id="commentCloseButton" class="closebtn"  aria-hidden="true">×</button>
-                <h3 id="mana_submit_comment"> Please enter a new commment (max 35 caracters)</h3>
-            </div>
-            <div class="modal-body">
-                <form>
-                    <input hidden id="selected_id" value="">
-                    <input type="text" id="manamsg">
-                </form>
-                <button id="CommentButton"> Comment </button>
-            </div>
-        </div>
-    </div>
-</div>
diff --git a/koha-tmpl/intranet-tmpl/prog/en/includes/mana/mana-subscription-search-result.inc b/koha-tmpl/intranet-tmpl/prog/en/includes/mana/mana-subscription-search-result.inc
index 30e10ca..34c3f42 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/includes/mana/mana-subscription-search-result.inc
+++ b/koha-tmpl/intranet-tmpl/prog/en/includes/mana/mana-subscription-search-result.inc
@@ -2,7 +2,7 @@
 [% USE Koha %]
 [% USE AuthorisedValues %]
 [% USE Branches %]
-
+[% INCLUDE 'mana.inc' %]
 <script type="text/javascript">
 //<![CDATA[
 $(document).ready(function() {
@@ -10,17 +10,6 @@ $(document).ready(function() {
         id = $(this).attr('id');
         mana_use(id.substr(9));
     });
-
-    $(document).on('change', 'select.mana-actions', function() {
-        report_id = $(this).attr('id').substr(13);
-        if ($(this).val() == 'other') {
-            $('input#selected_id').val(report_id);
-            $('#comment_box').modal('show');
-        } else {
-            comment_id = $(this).val();
-            mana_increment(comment_id, 'resource_comment', 'nb');
-        }
-    });
 });
 //]]>
 </script>
diff --git a/koha-tmpl/intranet-tmpl/prog/en/includes/reports-toolbar.inc b/koha-tmpl/intranet-tmpl/prog/en/includes/reports-toolbar.inc
index d6c57b8..e6b80a9 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/includes/reports-toolbar.inc
+++ b/koha-tmpl/intranet-tmpl/prog/en/includes/reports-toolbar.inc
@@ -1,3 +1,4 @@
+[% INCLUDE 'mana.inc' %]
 <div id="toolbar" class="btn-toolbar">
     [% IF ( CAN_user_reports_create_reports ) %]
         <div class="btn-group">
@@ -49,6 +50,40 @@
             </div>
         [% END %]
 
+        [% IF ( mana_id && Koha.Preference('Mana') == 1 ) %]
+            <div class="btn-group">
+                <button class="btn btn-default btn-sm dropdown-toggle" data-toggle="dropdown"> Report mistake <span class="caret"></span></button>
+                <ul class="dropdown-menu">
+                    [% FOREACH c IN mana_comments %]
+                        <li class="mana-comment" data-id="[% c.id %]">
+                            <a href="#">[% c.message %] ([% c.nb %])</a>
+                        </li>
+                    [% END %]
+                    <li role="separator" class="divider"></li>
+                    <li class="mana-other-comment"><a href="#">Other</a> </li>
+                </ul>
+            </div>
+
+            <div id="mana-comment-box" class="modal" tabindex="-1" role="dialog" aria-labelledby="mana_search_result_label" style="display: none;">
+                <div class="modal-dialog modal-lg" style="width: 30%">
+                    <div class="modal-content" style="">
+                        <div class="modal-header">
+                            <button type="button" id="mana-comment-close" class="closebtn"  aria-hidden="true">×</button>
+                            <h3 id="mana_submit_comment"> Please enter a new commment (max 35 caracters)</h3>
+                        </div>
+                        <div class="modal-body">
+                            <input hidden id="mana-resource" value="report">
+                            <input hidden id="mana-resource-id" value="[% mana_id %]">
+                            <div>
+                                <input type="text" maxlength="35" size="35" id="mana-comment">
+                            </div>
+                            <button id="mana-send-comment"> Comment </button>
+                        </div>
+                    </div>
+                </div>
+            </div>
+        [% END %]
+
         [% IF ( execute ) %]
             <div class="btn-group">
                 <button class="btn btn-default btn-sm dropdown-toggle" data-toggle="dropdown" id="format"><i class="fa fa-upload"></i> Download <span class="caret"></span></button>
@@ -96,21 +131,6 @@
         });
     });
 
-    $('#search_form').on( "submit", function(event) {
-        event.preventDefault();
-        mana_search($(this).children("#mana_search_field").val());
-    });
-
-    function mana_increment(mana_id, resourcename, fieldvalue, stepvalue = 1){
-        $.ajax( {
-            type: "POST",
-            url: "/cgi-bin/koha/svc/mana/increment",
-            data: {id: mana_id, field: fieldvalue, resource: resourcename, step: stepvalue},
-            datatype: "json",
-        }).done( function() {
-        }).fail( function(){ alert("") });
-    }
-
     function mana_use( mana_id ){
         $.ajax( {
             type:"POST",
@@ -158,13 +178,6 @@
 
             $( "select[class='actionreport1']" ).show();
             $( "button[class='actionreport2']" ).hide();
-            $("#CommentButton").on("click", function(){
-                var resource_type = "report";
-                var target_id = $("#selected_id").val();
-                var manamsg = $("#manamsg").val();
-                mana_comment(target_id, manamsg, resource_type);
-                $("#comment_box").modal("hide");
-            });
 
             $(".showbutton").on("click", function(){
                 $(this).parent().hide();
@@ -194,13 +207,4 @@
         }).fail( function( result ){
         });
     }
-
-    function mana_comment( target_id, manamsg, resource_type ){
-        $.ajax( {
-            type: "POST",
-            url: "/cgi-bin/koha/svc/mana/share",
-            data: {message: manamsg, resource: resource_type , resource_id: target_id},
-            datatype: "json",
-        })
-    }
 </script>
diff --git a/koha-tmpl/intranet-tmpl/prog/en/includes/serials-toolbar.inc b/koha-tmpl/intranet-tmpl/prog/en/includes/serials-toolbar.inc
index c4399ea..7554bd4 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/includes/serials-toolbar.inc
+++ b/koha-tmpl/intranet-tmpl/prog/en/includes/serials-toolbar.inc
@@ -1,3 +1,4 @@
+[% INCLUDE 'mana.inc' %]
 <script type="text/javascript">
     //<![CDATA[
 
@@ -98,6 +99,40 @@
                 [% END %]
             [% END %]
         [% END %]
+
+        [% IF ( mana_id && Koha.Preference('Mana') == 1 ) %]
+            <div class="btn-group">
+                <button class="btn btn-default btn-sm dropdown-toggle" data-toggle="dropdown"> Report mistake <span class="caret"></span></button>
+                <ul class="dropdown-menu">
+                    [% FOREACH c IN mana_comments %]
+                        <li class="mana-comment" data-id="[% c.id %]">
+                            <a href="#">[% c.message %] ([% c.nb %])</a>
+                        </li>
+                    [% END %]
+                    <li role="separator" class="divider"></li>
+                    <li class="mana-other-comment"><a href="#">Other</a> </li>
+                </ul>
+            </div>
+
+            <div id="mana-comment-box" class="modal" tabindex="-1" role="dialog" aria-labelledby="mana_search_result_label" style="display: none;">
+                <div class="modal-dialog modal-lg" style="width: 30%">
+                    <div class="modal-content" style="">
+                        <div class="modal-header">
+                            <button type="button" id="mana-comment-close" class="closebtn"  aria-hidden="true">×</button>
+                            <h3 id="mana_submit_comment"> Please enter a new commment (max 35 caracters)</h3>
+                        </div>
+                        <div class="modal-body">
+                            <input hidden id="mana-resource" value="subscription">
+                            <input hidden id="mana-resource-id" value="[% mana_id %]">
+                            <div>
+                                <input type="text" maxlength="35" size="35" id="mana-comment">
+                            </div>
+                            <button id="mana-send-comment"> Comment </button>
+                        </div>
+                    </div>
+                </div>
+            </div>
+        [% END %]
     </div>
 [% ELSIF CAN_user_serials_create_subscription %]
     <div id="toolbar" class="btn-toolbar">
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 9f57758..5baf8a9 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
@@ -130,26 +130,6 @@ canned reports and writing custom SQL reports.</p>
     </div>
 [% END %]
 
-<div id="mana_search_result" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="mana_search_result_label" style="width: 100%; left:0%; margin-left: auto; display: none;">
-    <div class="modal-dialog modal-lg">
-        <div class="modal-content">
-            <div class="modal-header">
-                <button type="button" class="closebtn" data-dismiss="modal" aria-hidden="true">×</button>
-                <h3 id="mana_search_result_label"> Mana Search</h3>
-            </div>
-            <div>
-                <form id="search_form" style="margin-left: 5%">
-                    Please enter a few key words:
-                    <input type=text id=mana_search_field>
-                    <input type=button class="mana_search_button" value="Search">
-                </form>
-                <div class="modal-body">
-                </div>
-            </div>
-        </div>
-    </div>
-</div>
-
 [% IF report_converted %]
     <div class="dialog message">
         The report "[% report_converted %]" has been converted.
diff --git a/koha-tmpl/intranet-tmpl/prog/js/subscription-add.js b/koha-tmpl/intranet-tmpl/prog/js/subscription-add.js
index 0958dfa..0eb311b 100644
--- a/koha-tmpl/intranet-tmpl/prog/js/subscription-add.js
+++ b/koha-tmpl/intranet-tmpl/prog/js/subscription-add.js
@@ -428,51 +428,6 @@ function mana_search() {
             $("#mana_search").html( result );
         }
         $("#mana_search").show();
-        $( "select[class='actionreport1']" ).show();
-        $( "button[class='actionreport2']" ).hide();
-
-        $("#CommentButton").on("click", function(){
-            var resource_type = "subscription";
-            var target_id = $("#selected_id").val();
-            var manamsg = $("#manamsg").val();
-            mana_comment(target_id, manamsg, resource_type);
-            $("#comment_box").modal("hide");
-        });
-
-        $("#commentCloseButton").on("click", function(){
-            $("#comment_box").modal("hide");
-        });
-
-        $(".actionreport1").on("click", function(){
-            $("#selectedcomment").val($(this).val());
-            $(this).parent("select").hide();
-            $(this).parent("select").next().show();
-        });
-
-        $(".actionreport2").on("click", function(){
-            $(this).hide();
-            $(this).prev().show();
-            mana_increment($("#selectedcomment").val(), 'resource_comment', 'nb', -1);
-        });
-
-    })
-}
-
-function mana_comment( target_id, manamsg, resource_type ){
-    $.ajax( {
-        type: "POST",
-        url: "/cgi-bin/koha/svc/mana/share",
-        data: {message: manamsg, resource: resource_type , resource_id: target_id},
-        datatype: "json",
-    })
-}
-
-function mana_increment(mana_id, resource, fieldvalue, stepvalue = 1){
-    $.ajax( {
-        type: "POST",
-        url: "/cgi-bin/koha/svc/mana/increment",
-        data: {id: mana_id, resource: resource, field: fieldvalue, step: stepvalue},
-        datatype: "json",
     })
 }
 
diff --git a/reports/guided_reports.pl b/reports/guided_reports.pl
index 37785ea..153da44 100755
--- a/reports/guided_reports.pl
+++ b/reports/guided_reports.pl
@@ -182,6 +182,8 @@ elsif ( $phase eq 'Show SQL'){
 	'sql'     => $report->{savedsql},
 	'showsql' => 1,
         'mana_success' => scalar $input->param('mana_success'),
+        'mana_id' => $report->{mana_id},
+        'mana_comments' => $report->{comments}
     );
 }
 
@@ -200,6 +202,8 @@ elsif ( $phase eq 'Edit SQL'){
         'public' => $report->{public},
         'usecache' => $usecache,
         'editsql'    => 1,
+        'mana_id' => $report->{mana_id},
+        'mana_comments' => $report->{comments}
     );
 }
 
diff --git a/serials/subscription-detail.pl b/serials/subscription-detail.pl
index ffb458e..6eed722 100755
--- a/serials/subscription-detail.pl
+++ b/serials/subscription-detail.pl
@@ -177,6 +177,7 @@ $template->param(
     (uc(C4::Context->preference("marcflavour"))) => 1,
     show_acquisition_details => defined $tmpl_infos->{ordered_exists} || defined $tmpl_infos->{spent_exists} ? 1 : 0,
     basketno => $order->{basketno},
+    mana_comments => $subs->{comments},
     %$tmpl_infos,
 );
 
diff --git a/svc/mana/share b/svc/mana/share
index 87de9cf..cced006 100755
--- a/svc/mana/share
+++ b/svc/mana/share
@@ -44,7 +44,7 @@ my $content;
 $content->{resource_id} = $input->param("resource_id");
 $content->{resource_type} = $input->param("resource");
 $content->{message} = $input->param("message");
-Koha::SharedContent::manaPostRequest('resource_comment', $content);
+Koha::SharedContent::manaPostRequest('', undef, undef, 'resource_comment', $content);
 my $package = "Koha::".ucfirst($input->param('resource'));
 my $resource;
 my $result;
-- 
2.7.4