From b769763073b831c0eecd2750351c340d93a38bcc Mon Sep 17 00:00:00 2001
From: jeremy breuillard <jeremy.breuillard@biblibre.com>
Date: Mon, 7 Feb 2022 12:00:00 +0100
Subject: [PATCH] Bug 21330: Add xslt for authority normal view in OPAC

This patch provides a xslt stylesheet on authority normal view in OPAC. Have a look on opac-authoritiesdetail.pl

Test plan:
1)You need the file 'UNIMARCauthResults.xsl' -> get it in the patch from Bug 11083
2)For testing make sure to have at least one or more authorities defined

3)OPAC: Home > Authority search(Submit) > Authority search results
4)Click details on a result and notice the view
5)Apply patch
6)INTRA: Home > Administration > System preferences ->find "AuthorityXSLTOpacDetailsDisplay"
7)Write the path where your file is, ex: '/home/koha/src/UNIMARCauthResults.xsl'
8)Save changes
9)Repeat 3) and 4) and notice the display

This is a similar bug: Bug 30036
---
 .gitignore                                    |  2 +-
 ...spref-AuthorityXSLTOpacDetailsDisplay.perl |  9 ++
 installer/data/mysql/mandatory/sysprefs.sql   |  1 +
 .../admin/preferences/staff_interface.pref    |  4 +
 .../bootstrap/en/modules/opac-auth-detail.tt  | 96 ++-----------------
 opac/opac-authoritiesdetail.pl                | 29 +++++-
 6 files changed, 45 insertions(+), 96 deletions(-)
 create mode 100644 installer/data/mysql/atomicupdate/add-syspref-AuthorityXSLTOpacDetailsDisplay.perl

diff --git a/.gitignore b/.gitignore
index c70d94a1dd..5c9d3a90c0 100644
--- a/.gitignore
+++ b/.gitignore
@@ -4,4 +4,4 @@
 /pm_to_blib
 node_modules/
 koha-tmpl/opac-tmpl/bootstrap/css/maps/
-koha-tmpl/intranet-tmpl/prog/css/maps/
+koha-tmpl/intranet-tmpl/prog/css/maps/
\ No newline at end of file
diff --git a/installer/data/mysql/atomicupdate/add-syspref-AuthorityXSLTOpacDetailsDisplay.perl b/installer/data/mysql/atomicupdate/add-syspref-AuthorityXSLTOpacDetailsDisplay.perl
new file mode 100644
index 0000000000..cf56c1877d
--- /dev/null
+++ b/installer/data/mysql/atomicupdate/add-syspref-AuthorityXSLTOpacDetailsDisplay.perl
@@ -0,0 +1,9 @@
+$DBversion = 'XXX';
+if (CheckVersion($DBversion)) {
+    $dbh->do(q{
+        INSERT IGNORE INTO systempreferences (`variable`, `value`, `options`, `explanation`, `type`)
+        VALUES ('AuthorityXSLTOpacDetailsDisplay','','','Enable XSL stylesheet control over authority results page display on intranet','Free')
+    });
+
+    NewVersion($DBversion, '11083', 'Add syspref AuthorityXSLTOpacDetailsDisplay');
+}
\ No newline at end of file
diff --git a/installer/data/mysql/mandatory/sysprefs.sql b/installer/data/mysql/mandatory/sysprefs.sql
index dd83a00fd8..01cbd8f8e8 100644
--- a/installer/data/mysql/mandatory/sysprefs.sql
+++ b/installer/data/mysql/mandatory/sysprefs.sql
@@ -69,6 +69,7 @@ INSERT INTO systempreferences ( `variable`, `value`, `options`, `explanation`, `
 ('AuthorityMergeLimit','50',NULL,'Maximum number of biblio records updated immediately when an authority record has been modified.','integer'),
 ('AuthorityMergeMode','loose','loose|strict','Authority merge mode','Choice'),
 ('AuthoritySeparator','--','10','Used to separate a list of authorities in a display. Usually --','free'),
+('AuthorityXSLTOpacDetailsDisplay','','','Enable XSL stylesheet control over authority details page display on opac','Free'),
 ('AuthSuccessLog','0',NULL,'If enabled, log successful authentications','YesNo'),
 ('autoBarcode','OFF','incremental|annual|hbyymmincr|EAN13|OFF','Used to autogenerate a barcode: incremental will be of the form 1, 2, 3; annual of the form 2007-0001, 2007-0002; hbyymmincr of the form HB08010001 where HB=Home Branch','Choice'),
 ('AutoCreateAuthorities','0',NULL,'Automatically create authorities that do not exist when cataloging records.','YesNo'),
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/staff_interface.pref b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/staff_interface.pref
index 10c41c581d..4e63e68439 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/staff_interface.pref
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/staff_interface.pref
@@ -1,6 +1,10 @@
 Staff interface:
     Appearance:
         -
+            - 'Display authority details in the opac interface using XSLT stylesheet at: '
+            - pref: AuthorityXSLTOpacDetailsDisplay
+              class: file
+            - '<br />Options:<ul><li>Put a path to define a xslt file</li><li>Put an URL for an external specific stylesheet.</li></ul>{langcode} will be replaced with current interface language and {authtypecode} will be replaced by the authority type code'
             - "Display language selector on "
             - pref: StaffLangSelectorMode
               choices:
diff --git a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-auth-detail.tt b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-auth-detail.tt
index f0d9fbcc2d..9629aaf05f 100644
--- a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-auth-detail.tt
+++ b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-auth-detail.tt
@@ -50,17 +50,12 @@
                             </span>
                         </div>
 
-                        [% IF ( displayhierarchy ) %]
-                            <div id="hierarchies">
-                                [% PROCESS showhierarchy trees = loophierarchies %]
-                            </div>
-                        [% END %]
-
                         <h1>[% summary.mainentry | html %][% IF authtypetext %] ([% authtypetext | html %])[% END %]</h1>
 
                         <div class="usedin">
                             [% IF count %]<a href="/cgi-bin/koha/opac-search.pl?type=opac&amp;q=[% authid | uri %]&amp;idx=an,phr">Number of records used in: [% count | html %]</a>[% ELSE %]This authority is not used in any records.[% END %]
                         </div>
+                        <br>
 
                         <div class="authstanza">
                             [% FOREACH authorize IN summary.authorized %]
@@ -72,91 +67,12 @@
                             [% PROCESS otherscript headings=summary.otherscript wantcategory='preferred' %]
                         </div>
 
-                        [% IF summary.seefrom.size %]
-                            <div class="authstanza seefrom">
-                                <div class="authstanzaheading">Used for/see from:</div>
-                                <ul class="seefrom">
-                                    [% FOREACH seefro IN summary.seefrom %]
-                                        <li class="heading seefrom auth[% seefro.field | html %]">
-                                            [% IF seefro.type && seefro.type != 'seefrom' %]
-                                                <span class="label">[% PROCESS authtypelabel type=seefro.type | trim %]:</span>
-                                            [% END %]
-                                            <span class="seefrom">[% PROCESS authheadingdisplay heading=seefro.heading search=seefrosearch authid=seefro.authid %]</span>
-                                        </li>
-                                    [% END %]
-                                    [% PROCESS otherscript headings=summary.otherscript wantcategory='seefrom' %]
-                                </ul>
-                            </div>
-                        [% END %]
-
-                        [% IF summary.seealso.size %]
-                            <div class="authstanza seealso">
-                                <div class="authstanzaheading">See also:</div>
-                                <ul class="seelso">
-                                    [% FOREACH seeals IN summary.seealso %]
-                                        <li class="heading seealso auth[% seeals.field | html %]">
-                                            [% IF seeals.type && seeals.type != 'seealso' %]
-                                                <span class="label">[% PROCESS authtypelabel type=seeals.type | trim %]:</span>
-                                            [% END %]
-                                            <span class="seealso">[% PROCESS authheadingdisplay heading=seeals.heading search=seeals.search authid=seeals.authid %]</span>
-                                        </li>
-                                    [% END %]
-                                    [% PROCESS otherscript headings=summary.otherscript wantcategory='seealso' %]
-                                </ul>
-                            </div>
-                        [% END %]
-
-                        [% IF marcflavour == 'UNIMARC' && summary.otherscript %]
-                            <div class="authstanza">
-                                <div class="authstanzaheading">Other forms:</div>
-                                <ul>
-                                    [% FOREACH otherscrip IN summary.otherscript %]
-                                        <li>
-                                            [% PROCESS language lang=otherscrip.lang | trim %]:
-                                            <span class="otherscript">[% otherscrip.term | html %]</span>
-                                        </li>
-                                    [% END %]
-                                </ul>
-                            </div>
+                        [% IF authresult.html %]
+                            [% authresult.html | $raw %]
+                        [% ELSE %]
+                            [% PROCESS authresult summary=authresult.summary authid=authresult.authid %]
                         [% END %]
-
-                        [% IF ( MARCURLS ) %]
-                            <div class="authstanza online_resources">
-                                <span class="label">Online resources:</span>
-                                <ul>
-                                    [% FOREACH MARCurl IN MARCURLS %]
-                                        <li>[% IF ( MARCurl.part ) %][% MARCurl.part | html %]<br />[% END %]
-                                            [% IF ( Koha.Preference('OPACURLOpenInNewWindow') ) %]
-                                                <a href="[% MARCurl.MARCURL | url %]" title="[% MARCurl.MARCURL | html %]" target="_blank" rel="noreferrer">
-                                            [% ELSE %]
-                                                <a href="[% MARCurl.MARCURL | url %]" title="[% MARCurl.MARCURL | html %]">
-                                            [% END %]
-                                            [% MARCurl.linktext | html %]</a>
-                                            [% IF ( MARCurl.notes ) %]
-                                                <ul>
-                                                    [% FOREACH note IN MARCurl.notes %]
-                                                        <li>[% note.note | html %]</li>
-                                                    [% END %]
-                                                </ul>
-                                            [% END %]
-                                        </li>
-                                    [% END # /FOREACH MARCURLS %]
-                                </ul>
-                            </div>
-                        [% END # / IF MARCURLS %]
-
-                        <div id="authdescriptions" class="toptabs">
-                            <ul>
-                                <li id="tab_descriptions"><a href="#descriptions">Notes</a></li>
-                            </ul>
-                            <div id="descriptions">
-                                <div class="content_set">
-                                    [% FOREACH note IN summary.notes %]
-                                        <p class="note auth[% note.field | html %]">[% note.note | html %]</p>
-                                    [% END %]
-                                </div>
-                            </div>
-                        </div>
+                           
                     </div> <!-- / .#userauthdetails -->
                 </div> <!-- / .col-lg-10/12 -->
             </div> <!-- / .row -->
diff --git a/opac/opac-authoritiesdetail.pl b/opac/opac-authoritiesdetail.pl
index ed0a1c7014..ad12e90f2a 100755
--- a/opac/opac-authoritiesdetail.pl
+++ b/opac/opac-authoritiesdetail.pl
@@ -69,8 +69,8 @@ my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
 
 my $authid = $query->param('authid');
 $authid = int($authid);
-my $record = GetAuthority( $authid );
-if ( ! $record ) {
+my $authresult = GetAuthority( $authid );
+if ( ! $authresult ) {
     print $query->redirect("/cgi-bin/koha/errors/404.pl"); # escape early
     exit;
 }
@@ -86,7 +86,25 @@ if ($display_hierarchy){
 my $count = $authority ? $authority->get_usage_count : 0;
 
 my $authority_types = Koha::Authority::Types->search( {}, { order_by => ['authtypecode'] } );
-my $marcurlsarray = GetMarcUrls( $record, $marcflavour );
+my $marcurlsarray = GetMarcUrls( $authresult, $marcflavour );
+
+my $AuthorityXSLTOpacDetailsDisplay = C4::Context->preference('AuthorityXSLTOpacDetailsDisplay');
+    if ($authresult && $AuthorityXSLTOpacDetailsDisplay) {
+        my $lang = C4::Languages::getlanguage();
+        
+        my $xsl = $AuthorityXSLTOpacDetailsDisplay;
+        
+        $xsl =~ s/\{langcode\}/$lang/g;
+        $xsl =~ s/\{authtypecode\}/$authtypecode/g;
+        my $xslt_engine = Koha::XSLT::Base->new;
+        my $output = $xslt_engine->transform({ xml => $authority->marcxml, file => $xsl });
+        if ($xslt_engine->err) {
+            warn "XSL transformation failed ($xsl): " . $xslt_engine->err;
+            next;
+        }
+        $authresult->{html} = $output;
+        
+    }
 
 $template->param(
     authority_types => $authority_types,
@@ -94,6 +112,7 @@ $template->param(
     authid          => $authid,
     count           => $count,
     MARCURLS        => $marcurlsarray,
+    authresult      => $authresult,
 );
 
 # find the marc field/subfield used in biblio by this authority
@@ -114,7 +133,7 @@ if ($show_marc) {
     my @loop_data = ();
 
 # loop through each tag
-    my @fields    = $record->fields();
+    my @fields    = $authresult->fields();
     foreach my $field (@fields) {
         my @subfields_data;
 
@@ -166,7 +185,7 @@ if ($show_marc) {
     }
     $template->param( "Tab0XX" => \@loop_data );
 } else {
-    my $summary = BuildSummary($record, $authid, $authtypecode);
+    my $summary = BuildSummary($authresult, $authid, $authtypecode);
     $template->{VARS}->{'summary'} = $summary;
 }
 
-- 
2.17.1