From cddf33a967b87b30a142929b2f22c9c61a25bd13 Mon Sep 17 00:00:00 2001
From: Baptiste Wojtkowski <baptiste.wojtkowski@biblibre.com>
Date: Wed, 31 May 2017 12:27:59 +0000
Subject: [PATCH] Bug 17047: Minor changes

- search on reports module is now also validated by pressing enter.
- AutoShare now initialized with nothing autmaticaly shared.
- To create a report you now have to click on New report => New report from Mana.
- Error messages are displayed in case of fail
- Correct the display of sql reports containing "'"
---
 .../atomicupdate/mana_03-add_mana_autoshare.sql    |   2 +-
 .../en/includes/mana/mana-report-search-result.inc |   2 +-
 .../prog/en/includes/reports-toolbar.inc           | 148 ++++++++++++++++++++-
 .../en/modules/reports/guided_reports_start.tt     | 127 ++----------------
 .../prog/en/modules/serials/subscription-add.tt    |  21 ++-
 reports/guided_reports.pl                          |   1 +
 svc/mana/share                                     |  19 +++
 7 files changed, 198 insertions(+), 122 deletions(-)

diff --git a/installer/data/mysql/atomicupdate/mana_03-add_mana_autoshare.sql b/installer/data/mysql/atomicupdate/mana_03-add_mana_autoshare.sql
index 353ffc1..f5cc363 100644
--- a/installer/data/mysql/atomicupdate/mana_03-add_mana_autoshare.sql
+++ b/installer/data/mysql/atomicupdate/mana_03-add_mana_autoshare.sql
@@ -1,2 +1,2 @@
 INSERT INTO systempreferences ( `variable`, `value`, `options`, `explanation`, `type` ) VALUES
-('AutoShareWithMana','subscription','','defines datas automatically shared with mana','multiple');
+('AutoShareWithMana','','','defines datas automatically shared with mana','multiple');
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 455f453..2de9fea 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
@@ -30,7 +30,7 @@
                     [% END %]
                     <input hidden class="rowid" value="[% report.id %]">
                     <td>[% IF ( report.report_name ) %][% report.report_name %][% END %]</td>
-                    <td title="[% report.savedsql %]"><div>
+                    <td title="[% report.savedsql |html %]"><div>
                         [% IF report.notes.length > 200 %]
                             [% report.notes.substr(0,200) %]<a class="showbutton">Show More</a></div><div hidden>
                         [% END %]
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 a425919..7ed481e 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/includes/reports-toolbar.inc
+++ b/koha-tmpl/intranet-tmpl/prog/en/includes/reports-toolbar.inc
@@ -5,6 +5,9 @@
             <ul class="dropdown-menu">
                 <li id="newmenuc"><a href="/cgi-bin/koha/reports/guided_reports.pl?phase=Build%20new">New guided report</a> </li>
                 <li id="newsql"><a href="/cgi-bin/koha/reports/guided_reports.pl?phase=Create%20report%20from%20SQL">New SQL report</a> </li>
+                [% IF Koha.Preference('Mana')==1 %]
+                <li id="newsql"><a href="" data-toggle="modal" data-target="#mana_search_result">New SQL from Mana</a> </li>
+                [% END %]
             </ul>
         </div>
     [% END %]
@@ -64,12 +67,155 @@
     [% END %]
 </div>
 
+
+[% IF Koha.Preference('Mana')==1 %]
+    <div id="mana_search_result" class="modal fade container-fluid" 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>
+[% END %]
+
+
+
 <script type="text/javascript">
 var MSG_CONFIRM_DELETE = _("Are you sure you want to delete this report? This cannot be undone.");
-
 $(document).ready(function(){
     $(".delete").on("click",function(){
         return confirmDelete(MSG_CONFIRM_DELETE);
     });
 });
 </script>
+
+<script type="text/javascript">
+    $(document).ready( function(){
+        $(".mana_search_button").on("click",function(){
+            mana_search($("#mana_search_field").val());
+        });
+    });
+
+    $('#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/addvaluetofield",
+            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",
+            url: "/cgi-bin/koha/svc/mana/use",
+            data: {id:mana_id, resource: 'report', saveinbase: 1},
+            dataType: "json",
+        })
+        .done( function (result){
+            if ( result.errmsg ){
+                alert( result.errmsg );
+            }
+            else{
+                window.location = ("/cgi-bin/koha/reports/guided_reports.pl?reports=").concat(result.id).concat("&amp;phase=Show%20SQL&mana_success=1");
+            }
+        })
+        .fail( function ( foo, msg, longmsg, bla ){
+        });
+    }
+
+    function mana_search( textquery ){
+        $.ajax({
+            type: "POST",
+            url: "/cgi-bin/koha/svc/mana/search",
+            data: {biblionumber: $("#biblionumber").val(), resource: 'report', id: textquery, usecomments: 1},
+            dataType: "html",
+        })
+        .done( function( result ) {
+            $("#mana_search_result .modal-body").html(result);
+            $("#mana_search_result_label").text("Results from Mana Knowledge Base");
+            $("#mana_results_datatable").dataTable($.extend(true, {}, dataTablesDefaults,{
+                "sPaginationType":"four_button",
+                "autoWidth": false,
+                "columnDefs": [
+                    { "width": "35%", "targets": 1 }
+                ],
+                "aoColumnDefs": [
+                    { 'bSortable': false, "bSearchable": false, 'aTargets': [ 'NoSort' ] },
+                    { "sType": "title-string", "aTargets" : [ "title-string" ] },
+                    { 'sType': "anti-the", 'aTargets' : [ 'anti-the'] }
+                ]
+            }));
+            if($("td.dataTables_empty").length == 0){
+                 $("#mana_search").show();
+            }
+
+            $( "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();
+                $(this).parent().next().show();
+            });
+
+            $("a[class='hidebutton']").on("click", function(){
+                $(this).parent().hide();
+                $(this).parent().prev().show();
+            });
+
+            $("#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);
+            });
+        }).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/modules/reports/guided_reports_start.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/reports/guided_reports_start.tt
index 624e232..3d6acc9 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
@@ -81,10 +81,6 @@ $(".goback").on("click",function(e){
     window.history.back();
 });
 
-$(".mana_search_button").on("click",function(){
-    mana_search($(this).prev().val());
-});
-
 $(".ShareButton").on("click", function(){
     if($(this).closest("tr").find(".report_notes").text().length < 20 || $(this).closest("tr").find(".report_name").text().length < 20){
         $("#shared_infos").hide();
@@ -101,9 +97,6 @@ $(".ShareButton").on("click", function(){
     }
 });
 
-$('#search_form').submit(function () {
-    return false;
-});
 
 $("#addColumn").on("click",function(){
     addColumn();
@@ -283,104 +276,6 @@ $("#delColumn").on("click",function(){
 [% END %]
 });
 
-function mana_increment(mana_id, resourcename, fieldvalue, stepvalue = 1){
-    $.ajax( {
-        type: "POST",
-        url: "/cgi-bin/koha/svc/mana/addvaluetofield",
-        data: {id: mana_id, field: fieldvalue, resource: resourcename, step: stepvalue},
-        datatype: "json",
-    }).done( function() {
-    }).fail( function(){ });
-}
-
-function mana_use( mana_id ){
-    $.ajax( {
-        type:"POST",
-        url: "/cgi-bin/koha/svc/mana/use",
-        data: {id:mana_id, resource: 'report', saveinbase: 1},
-        dataType: "json",
-    })
-    .done( function (result){
-        window.location = ("/cgi-bin/koha/reports/guided_reports.pl?reports=").concat(result.id).concat("&amp;phase=Show%20SQL");
-    })
-    .fail( function ( foo, msg, longmsg){
-    });
-}
-
-function mana_search( textquery ){
-    $.ajax({
-        type: "POST",
-        url: "/cgi-bin/koha/svc/mana/search",
-        data: {biblionumber: $("#biblionumber").val(), resource: 'report', id: textquery, usecomments: 1},
-        dataType: "html",
-    })
-    .done( function( result ) {
-        $("#mana_search_result .modal-body").html(result);
-        $("#mana_search_result_label").text("Results from Mana Knowledge Base");
-        $("#mana_results_datatable").dataTable($.extend(true, {}, dataTablesDefaults,{
-            "sPaginationType":"four_button",
-            "autoWidth": false,
-            "columnDefs": [
-                { "width": "35%", "targets": 1 }
-            ],
-            "aoColumnDefs": [
-                { 'bSortable': false, "bSearchable": false, 'aTargets': [ 'NoSort' ] },
-                { "sType": "title-string", "aTargets" : [ "title-string" ] },
-                { 'sType': "anti-the", 'aTargets' : [ 'anti-the'] }
-            ]
-        }));
-        if($("td.dataTables_empty").length == 0){
-             $("#mana_search").show();
-        }
-
-        $( "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();
-            $(this).parent().next().show();
-        });
-
-        $("a[class='hidebutton']").on("click", function(){
-            $(this).parent().hide();
-            $(this).parent().prev().show();
-        });
-
-        $("#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);
-        });
-    }).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>
@@ -492,6 +387,13 @@ canned reports and writing custom SQL reports.</p>
     </div>
 </div>
 
+[% IF report_converted %]
+    <div class="dialog message">
+        The report "[% report_converted %]" has been converted.
+    </div>
+[% END %]
+
+
 [% IF ( saved1 ) %]
 [% IF ( savedreports ) %]<h1>Saved reports</h1>
 
@@ -516,14 +418,8 @@ canned reports and writing custom SQL reports.</p>
     </div>
     [% END %]
 
-   [% IF (Koha.Preference('Mana') == 1) %]
-    <div id="mana_search" class="dialog message">
-        <p> You want more reports? Check the Mana Knowledge Base <p> <a style="cursor:pointer" data-toggle="modal" data-target="#mana_search_result">Quick Search</a></p>
-    </div>
-    [% END %]
-
 </script>
-<h1> [% savedreport.sql %]</h1>
+
 <form action="/cgi-bin/koha/reports/guided_reports.pl" id="reports_form" method="post">
 <input type="hidden" name="phase" value="Delete Multiple" />
         <table id="table_reports">
@@ -554,7 +450,7 @@ canned reports and writing custom SQL reports.</p>
                             [% IF ( CAN_user_reports_delete_reports ) %] <!-- not break CSS -->
                                 <input type="checkbox" name="ids" value="[% savedreport.id %]" />
                             [% END %]
-                        <input hidden class="report_sql" value="[% savedreport.savedsql %]">
+                        <input hidden class="report_sql" value="[% savedreport.savedsql |html %]">
                         </td>
                         <td class="report_id"><label for="ids">[% savedreport.id %]</label></td>
                         <td class="report_name">
@@ -1280,6 +1176,11 @@ Sub report:<select name="subreport">
 [% END %]
 
 [% IF ( showsql ) %]
+    [% IF mana_success %]
+        <fieldset>
+            Shared Successfully, thanks for your contribution!
+        </fieldset>
+    [% END %]
 <fieldset class="rows">
     <legend>[% reportname %]</legend>
     <ol>
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/serials/subscription-add.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/serials/subscription-add.tt
index 2f80be3..360f977 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/serials/subscription-add.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/serials/subscription-add.tt
@@ -406,6 +406,9 @@ function show_page_2() {
 }
 
 function mana_search() {
+    $("#mana_search").html("<p>Mana kb is being asked for your subscription..</p>");
+    $("#mana_search").show();
+
     $.ajax({
         type: "POST",
         url: "/cgi-bin/koha/svc/mana/search",
@@ -414,6 +417,7 @@ function mana_search() {
     })
     .done( function( result ) {
         $("#mana_search_result .modal-body").html(result);
+debugger;
         $("#mana_search_result_label").text("Results from Mana Knowledge Base");
         $("#mana_results_datatable").dataTable($.extend(true, {}, dataTablesDefaults, {
             "sPaginationType": "four_button",
@@ -429,8 +433,15 @@ function mana_search() {
             ]
         }));
         if( $("#mana_results_datatable").length && $("td.dataTables_empty").length == 0){
-            $("#mana_search").show();
+            $("#mana_search").html("<p>Subscription found on Mana Knowledge Base:</p><p> <a style='cursor:pointer' data-toggle='modal' data-target='#mana_search_result'>Quick fill</a></p>");
+        }
+        else if ( $("#mana_results_datatable").length ){
+            $("#mana_search").html("<p>No subscription found on Mana Knowledge Base :(</p><p> Please feel free to share you pattern with all others librarians once you are done</p>");
         }
+        else{
+            $("#mana_search").html( result );
+        }
+        $("#mana_search").show();
         $( "select[class='actionreport1']" ).show();
         $( "button[class='actionreport2']" ).hide();
 
@@ -458,7 +469,9 @@ function mana_search() {
             mana_increment($("#selectedcomment").val(), 'resource_comment', 'nb', -1);
         });
 
-    }).fail(function(result){
+    }).fail(function(result, msg, longmsg){
+        console.log( msg );
+        console.log( longmsg );
     });
 }
 
@@ -566,9 +579,6 @@ $(document).ready(function() {
 
 
     mana_search();
-    $("#myid").on("click", function(){
-        debugger;
-    })
     $("#displayexample").hide();
     $("#mana_search_result").modal("hide");
     $("#aqbooksellerid").on('keypress', function(e) {
@@ -914,7 +924,6 @@ $(document).ready(function() {
                 [% END %]
 
                         <div hidden id="mana_search" class="dialog message">
-                            <p>Subscription found on Mana Knowledge Base:</p><p> <a style="cursor:pointer" data-toggle="modal" data-target="#mana_search_result">Quick fill</a></p>
                         </div>
                         <div id="subscription_form_planning">
                             <fieldset class="rows">
diff --git a/reports/guided_reports.pl b/reports/guided_reports.pl
index 0ef602d..bad2a11 100755
--- a/reports/guided_reports.pl
+++ b/reports/guided_reports.pl
@@ -181,6 +181,7 @@ elsif ( $phase eq 'Show SQL'){
         'notes'      => $report->{notes},
 	'sql'     => $report->{savedsql},
 	'showsql' => 1,
+        'mana_success' => $input->param('mana_success'),
     );
 }
 
diff --git a/svc/mana/share b/svc/mana/share
index a294bd0..47db3e1 100755
--- a/svc/mana/share
+++ b/svc/mana/share
@@ -45,3 +45,22 @@ $content->{resource_id} = $input->param("resource_id");
 $content->{resource_type} = $input->param("resource");
 $content->{message} = $input->param("message");
 Koha::SharedContent::manaPostRequest('resource_comment', $content);
+    my $package = "Koha::".ucfirst($input->param('resource'));
+    my $resource;
+    my $result;
+eval{
+    $result = Koha::SharedContent::manaGetRequestWithId($input->param('resource'), $input->param('id') );
+};
+if ( $@ or $result->{code} == 500 ){
+    $resource->{errmsg} =  "Error: mana access got broken, please try again later\n\n ( error: $@ )";
+}
+else{
+    if ( $input->param( 'saveinbase' )) {
+        $resource = { id => $package->new_from_mana($result->{data})->id };
+    }
+    else{
+        $resource = $result->{data};
+    }
+}
+
+print(to_json($resource));
-- 
2.7.4