From d0921299eafac9f12ddc65cd2b1bd84703fc297e Mon Sep 17 00:00:00 2001
From: axel Amghar <axel.amghar@biblibre.com>
Date: Mon, 29 Apr 2019 11:27:49 +0200
Subject: [PATCH] Bug 18829: Elasticsearch - Put a better display for the view
 of ES indexed record

To test:
- apply the patch
- go to global sysPref
- make sure that SearchEngine have "ElasticSearch" as value
- Search whatever you want in Search the catalog and select a record
- Search the link (Ctrl f) : "Elasticsearch Record :" , click on the link
- make sure that the pop-up open and you should see the elasticsearch result in JSON (the pop-up look the same as MARC preview)
- note fields are alphabetized
- from the command line delete the es record:
    curl -XDELETE es:9200/koha_kohadev_biblios/data/5
- click the preview link, it syas record not found
- check the logs - you see the error on missing record

Signed-off-by: Fridolin Somers <fridolin.somers@biblibre.com>

Signed-off-by: Katrin Fischer <katrin.fischer.83@web.de>
---
 catalogue/showelastic.pl                      | 76 +++++++++++++++++++
 .../prog/en/modules/catalogue/detail.tt       | 34 ++++++++-
 .../prog/en/modules/catalogue/showelastic.tt  | 35 +++++++++
 3 files changed, 143 insertions(+), 2 deletions(-)
 create mode 100755 catalogue/showelastic.pl
 create mode 100644 koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/showelastic.tt

diff --git a/catalogue/showelastic.pl b/catalogue/showelastic.pl
new file mode 100755
index 0000000000..1b9318cc3a
--- /dev/null
+++ b/catalogue/showelastic.pl
@@ -0,0 +1,76 @@
+#!/usr/bin/perl
+
+# Koha library project  www.koha-community.org
+
+# Copyright 2020 Koha Development Team
+#
+# This file is part of Koha.
+#
+# Koha is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# Koha is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with Koha; if not, see <http://www.gnu.org/licenses>.
+
+
+use Modern::Perl;
+
+# standard or CPAN modules used
+use CGI qw(:standard -utf8);
+use DBI;
+use Encode;
+use JSON;
+use Try::Tiny;
+
+# Koha modules used
+use C4::Context;
+use C4::Output;
+use C4::Auth;
+use C4::Biblio;
+use C4::ImportBatch;
+use C4::XSLT ;
+use Koha::SearchEngine::Elasticsearch;
+use LWP::Simple qw/get/;
+
+my $input= new CGI;
+my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
+  {
+    template_name   => "catalogue/showelastic.tt",
+    query           => $input,
+    type            => "intranet",
+    flagsrequired   => { catalogue => 1  },
+    debug           => 1,
+  }
+);
+
+my $biblionumber = $input->param('id');
+
+my $es = Koha::SearchEngine::Elasticsearch->new({index=>'biblios'});
+
+my $es_record;
+my @es_fields;
+
+try {
+    $es_record = $es->get_elasticsearch()->get({
+        index => $es->index_name,
+        type  => 'data',
+        id    => $biblionumber,
+    });
+}
+catch{
+    @es_fields = ("Error fetching record: see logs for details");
+    warn $_;
+};
+
+for my $field (sort keys %{$es_record} ){
+    push @es_fields, { $field, $es_record->{$field} };
+};
+$template->param( esrecord => to_json( \@es_fields ) );
+output_html_with_http_headers $input, $cookie, $template->output;
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/detail.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/detail.tt
index a4452f51be..4d4ccccb6e 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/detail.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/detail.tt
@@ -157,6 +157,10 @@
           </span>
         [% END %]
 
+        [% IF ( Koha.Preference('SearchEngine') == 'Elasticsearch' ) %]
+            <span id="catalogue_detail_elastic_record" class="results_summary"><span class="label">Elasticsearch Record :</span> <a href="/cgi-bin/koha/catalogue/showelastic.pl?id=[% biblionumber | uri %]" title="ELASTIC" class="previewElastic">Show</a></span>
+        [% END %]
+
         [% IF ( holdcount ) %]
             <span class="results_summary">
                 <span class="label">Holds:</span>
@@ -1127,6 +1131,23 @@ Note that permanent location is a code, and location may be an authval.
     </div>
 </div>
 
+<div id="elasticPreview" class="modal" tabindex="-1" role="dialog" aria-labelledby="elasticPreviewLabel" aria-hidden="true">
+    <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="elasticPreviewLabel">Elastic record</h3>
+    </div>
+    <div class="modal-body">
+        <div id="loading"> <img src="[% interface | html %]/[% theme | html %]/img/spinner-small.gif" alt="" /> Loading </div>
+    </div>
+    <div class="modal-footer">
+        <button class="btn btn-default" data-dismiss="modal" aria-hidden="true">Close</button>
+    </div>
+    </div>
+    </div>
+</div>
+
             </main>
         </div> <!-- /.col-sm-10.col-sm-push-2 -->
 
@@ -1719,10 +1740,18 @@ Note that permanent location is a code, and location may be an authval.
                 var page = $(this).attr("href");
                 $("#marcPreview .modal-body").load(page + " table");
                 $('#marcPreview').modal({show:true});
+
             });
-            $("#marcPreview").on("hidden.bs.modal", function(){
-                $("#marcPreview .modal-body").html("<div id=\"loading\"><img src=\"[% interface | html %]/[% theme | html %]/img/spinner-small.gif\" alt=\"\" /> "+_("Loading")+"</div>");
+
+           [% IF ( Koha.Preference('SearchEngine') == 'Elasticsearch' ) %]
+            $("body").on("click",".previewElastic", function(e){
+                e.preventDefault();
+                var pageElastic = $(this).attr("href");
+                $("#elasticPreview .modal-body").load(pageElastic);
+                $('#elasticPreview').modal({show:true});
             });
+           [% END %]
+
             [% IF ( Koha.Preference('NovelistSelectStaffEnabled') && Koha.Preference('NovelistSelectStaffProfile') && ( normalized_isbn || normalized_upc ) ) %]
                 novSelect.loadContentForQuery({
                     ClientIdentifier : '[% IF normalized_isbn %][% normalized_isbn | html %][% ELSE %][% normalized_upc | html %][% END %]',
@@ -2430,5 +2459,6 @@ Note that permanent location is a code, and location may be an authval.
             });
         [% END %]
     </script>
+
 [% END %]
 [% INCLUDE 'intranet-bottom.inc' %]
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/showelastic.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/showelastic.tt
new file mode 100644
index 0000000000..cf818aded9
--- /dev/null
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/showelastic.tt
@@ -0,0 +1,35 @@
+[% INCLUDE 'doc-head-open.inc' %]
+[% USE raw %]
+<title>Koha &rsaquo; Cataloging &rsaquo; MARC import</title>
+<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
+</head>
+<body id="catalog_showelastic" class="catalog">
+    <pre id="main" style="direction: ltr;">
+
+    </pre>
+</body>
+<footer>
+    <style>
+        textarea
+        {
+            width: 100%;
+            min-height: 40em;
+            background-color : white;
+            resize:none;
+        }
+    </style>
+    <script>
+        display();
+
+        function display() {
+            let myJSON = JSON.stringify([% esrecord | $raw %], null, '\t');
+            let decode = decodeURIComponent(escape(myJSON));
+            let para = document.createElement('textarea');
+            para.disabled="yes";
+            para.textContent = decode;
+            document.getElementById('main').appendChild(para);
+        }
+
+    </script>
+</footer>
+</html>
-- 
2.30.2