[Koha-patches] [PATCH 4/6] 5575, comment 9 = fix XSLT display

paul.poulain at biblibre.com paul.poulain at biblibre.com
Wed Jan 19 21:05:34 CET 2011


From: Paul Poulain <paul.poulain at biblibre.com>

This branch adds a new feature for the XSLT display : you can have your own XSLT
for both opac & staff & for both results & detail.

You can use either a fully qualified xslt (with http://) or a relative one
(../../koha-tmpl/...)

The merge was wrong on some parts & the syspref had to be modified as it's no more a boolean but a string
---
 C4/Search.pm                                       |    9 +--
 C4/XSLT.pm                                         |   59 +++++++++++---------
 catalogue/detail.pl                                |    8 ++-
 .../prog/en/modules/admin/preferences/opac.pref    |   10 +--
 .../en/modules/admin/preferences/staff-client.pref |   14 ++---
 .../prog/en/modules/catalogue/detail.tmpl          |    2 +-
 .../opac-tmpl/prog/en/modules/opac-results.tmpl    |    2 +-
 opac/opac-detail.pl                                |    6 ++-
 8 files changed, 58 insertions(+), 52 deletions(-)

diff --git a/C4/Search.pm b/C4/Search.pm
index eca115c..f61c645 100644
--- a/C4/Search.pm
+++ b/C4/Search.pm
@@ -1683,11 +1683,10 @@ sub searchResults {
         # XSLT processing of some stuff
 	use C4::Charset;
 	SetUTF8Flag($marcrecord);
-        if (!$scan && $search_context eq 'opac' && C4::Context->preference("OPACXSLTResultsDisplay")) {
-            # FIXME note that XSLTResultsDisplay (use of XSLT to format staff interface bib search results)
-            # is not implemented yet
-            $oldbiblio->{XSLTResultsRecord} = XSLTParse4Display($oldbiblio->{biblionumber}, $marcrecord, 'Results', 
-                                                                $search_context);
+        $debug && warn $marcrecord->as_formatted;
+
+        if ( C4::Context->preference($search_context."XSLTResultsDisplay") && !$scan ) {
+            $oldbiblio->{$search_context."XSLTResultsRecord"} = XSLTParse4Display( $biblionumber, $marcrecord, C4::Context->preference($search_context."XSLTResultsDisplay") );
         }
 
         # last check for norequest : if itemtype is notforloan, it can't be reserved either, whatever the items
diff --git a/C4/XSLT.pm b/C4/XSLT.pm
index 5616c84..23f7df6 100644
--- a/C4/XSLT.pm
+++ b/C4/XSLT.pm
@@ -31,6 +31,7 @@ use C4::Output qw//;
 use Encode;
 use XML::LibXML;
 use XML::LibXSLT;
+use LWP::Simple;
 
 use vars qw($VERSION @ISA @EXPORT);
 
@@ -40,6 +41,7 @@ BEGIN {
     @ISA = qw(Exporter);
     @EXPORT = qw(
         &XSLTParse4Display
+      &GetURI
     );
 }
 
@@ -49,9 +51,22 @@ C4::XSLT - Functions for displaying XSLT-generated content
 
 =head1 FUNCTIONS
 
-=head2 transformMARCXML4XSLT
+=head1 GetURI
 
-Replaces codes with authorized values in a MARC::Record object
+=head2 GetURI file and returns the xslt as a string
+
+=cut
+
+sub GetURI {
+    my ($uri) = @_;
+    my $string;
+    $string = get $uri ;
+    return $string;
+}
+
+=head1 transformMARCXML4XSLT
+
+=head2 replaces codes with authorized values in a MARC::Record object
 
 =cut
 
@@ -120,11 +135,10 @@ sub getAuthorisedValues4MARCSubfields {
 my $stylesheet;
 
 sub XSLTParse4Display {
-    my ( $biblionumber, $orig_record, $xsl_suffix, $interface ) = @_;
-    $interface = 'opac' unless $interface;
+    my ( $biblionumber, $orig_record, $xslfilename ) = @_;
     # grab the XML, run it through our stylesheet, push it out to the browser
     my $record = transformMARCXML4XSLT( $biblionumber, $orig_record );
-	my $itemsimageurl = GetKohaImageurlFromAuthorisedValues( "CCODE", $orig_record->field('099')->subfield("t")) || '';
+	my $itemsimageurl = GetKohaImageurlFromAuthorisedValues( "CCODE", $orig_record->subfield("099", "t")) || '';
 	my $logoxml = "<logo>" . $itemsimageurl . "</logo>\n";
     #return $record->as_formatted();
     my $itemsxml  = buildKohaItemsNamespace($biblionumber);
@@ -142,31 +156,22 @@ sub XSLTParse4Display {
 
     my $parser = XML::LibXML->new();
     # don't die when you find &, >, etc
-    $parser->recover_silently(0);
+    $parser->recover_silently(1);
     my $source = $parser->parse_string($xmlrecord);
-    unless ( $stylesheet ) {
+    unless ( $stylesheet->{$xslfilename} ) {
         my $xslt = XML::LibXSLT->new();
-        my $xslfile;
-        if ($interface eq 'intranet') {
-            $xslfile = C4::Context->config('intrahtdocs') . 
-                      '/' . C4::Context->preference("template") . 
-                      '/' . C4::Output::_current_language() .
-                      '/xslt/' .
-                      C4::Context->preference('marcflavour') .
-                      "slim2intranet$xsl_suffix.xsl";
+        my $style_doc;
+        if ( $xslfilename =~ /http:/ ) {
+            my $xsltstring = GetURI($xslfilename);
+            $style_doc = $parser->parse_string($xsltstring);
         } else {
-            $xslfile = C4::Context->config('opachtdocs') . 
-                      '/' . C4::Context->preference("opacthemes") . 
-                      '/' . C4::Output::_current_language() .
-                      '/xslt/' .
-                      C4::Context->preference('marcflavour') .
-                      "slim2OPAC$xsl_suffix.xsl";
+            use Cwd;
+            $style_doc = $parser->parse_file($xslfilename);
         }
-        my $style_doc = $parser->parse_file($xslfile);
-        $stylesheet = $xslt->parse_stylesheet($style_doc);
+        $stylesheet->{$xslfilename} = $xslt->parse_stylesheet($style_doc);
     }
-    my $results = $stylesheet->transform($source);
-    my $newxmlrecord = $stylesheet->output_string($results);
+    my $results      = $stylesheet->{$xslfilename}->transform($source);
+    my $newxmlrecord = $stylesheet->{$xslfilename}->output_string($results);
     return $newxmlrecord;
 }
 
@@ -212,8 +217,8 @@ sub buildKohaItemsNamespace {
         } else {
             $status = "available";
         }
-        my $homebranch = xml_escape($branches->{$item->{homebranch}}->{'branchname'});
-	my $itemcallnumber = xml_escape($item->{itemcallnumber});
+        my $homebranch = $branches->{ $item->{homebranch} }->{'branchname'};
+        my $itemcallnumber = $item->{itemcallnumber} || '';
         my $itemlocation = GetAuthorisedValueByCode( "LOC", $item->{location}) || '';
         $xml.= "<item><homebranch>$homebranch</homebranch>".
 		"<status>$status</status>".
diff --git a/catalogue/detail.pl b/catalogue/detail.pl
index 10b58da..8269f02 100755
--- a/catalogue/detail.pl
+++ b/catalogue/detail.pl
@@ -59,9 +59,11 @@ my $marcflavour      = C4::Context->preference("marcflavour");
 my $record           = GetMarcBiblio($biblionumber);
 
 # XSLT processing of some stuff
-if (C4::Context->preference("XSLTDetailsDisplay") ) {
-    $template->param('XSLTDetailsDisplay' =>'1',
-        'XSLTBloc' => XSLTParse4Display($biblionumber, $record, 'Detail','intranet') );
+if ( C4::Context->preference("IntranetXSLTDetailsDisplay") ) {
+    $template->param(
+        'IntranetXSLTDetailsDisplay' => '1',
+        'XSLTBloc'           => XSLTParse4Display( $biblionumber, $record, C4::Context->preference('IntranetXSLTDetailsDisplay') )
+    );
 }
 
 $template->param( 'SpineLabelShowPrintOnBibDetails' => C4::Context->preference("SpineLabelShowPrintOnBibDetails") );
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/opac.pref b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/opac.pref
index 7fc48d3..5bd8018 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/opac.pref
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/opac.pref
@@ -44,15 +44,13 @@ OPAC:
         -
             - Show biblio records on OPAC result page
             - pref: OPACXSLTResultsDisplay
-              choices:
-                  yes: using XSLT stylesheets.
-                  no: normally.
+              class: long
+            - as xslt (../koha-tmpl/opac-tmpl/prog/en/xslt/MARC21slim2OPACResults.xsl for default xslt, empty for no xslt)
         -
             - Show item details pages on the OPAC
             - pref: OPACXSLTDetailsDisplay
-              choices:
-                  yes: using XSLT stylesheets.
-                  no: normally.
+              class: long
+            - as xslt (../koha-tmpl/opac-tmpl/prog/en/xslt/MARC21slim2OPACDetail.xsl for default xslt, empty for no xslt)
         -
             - On pages displayed with XSLT stylesheets on the OPAC,
             - pref: DisplayOPACiconsXSLT
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/staff-client.pref b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/staff-client.pref
index ab546f1..2160540 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/staff-client.pref
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/staff-client.pref
@@ -46,17 +46,15 @@ Staff Client:
               type: textarea
               class: code
         -   
-            - Show biblio records on result page in the staff client
+            - Show biblio records on result page in the staff client using 
             - pref: XSLTResultsDisplay
-              choices:
-                  yes: using XSLT stylesheets.
-                  no: normally.
+              class: long
+            - as xslt (../koha-tmpl/intranet-tmpl/prog/en/xslt/MARC21slim2intranetResults.xsl for default xslt, empty for no xslt)
         -   
-            - Show item details pages in the staff client
+            - Show item details pages in the staff client using
             - pref: XSLTDetailsDisplay
-              choices:
-                  yes: using XSLT stylesheets.
-                  no: normally.              
+              class: long
+            - as xslt (../koha-tmpl/intranet-tmpl/prog/en/xslt/MARC21slim2intranetDetails.xsl for default xslt, empty for no xslt)
         -
             - Use the Yahoo UI libraries
             - pref: yuipath
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/detail.tmpl b/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/detail.tmpl
index dd3b7ab..7c13db1 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/detail.tmpl
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/detail.tmpl
@@ -71,7 +71,7 @@ function verify_images() {
         <!-- /TMPL_IF -->
     <!-- /TMPL_IF -->
 
-    <!-- TMPL_IF NAME="XSLTDetailsDisplay" -->
+    <!-- TMPL_IF NAME="IntranetXSLTDetailsDisplay" -->
         <!-- TMPL_VAR NAME="XSLTBloc" -->
 
         <!-- TMPL_IF NAME="GetShelves" -->
diff --git a/koha-tmpl/opac-tmpl/prog/en/modules/opac-results.tmpl b/koha-tmpl/opac-tmpl/prog/en/modules/opac-results.tmpl
index cfd5e36..41f5777 100644
--- a/koha-tmpl/opac-tmpl/prog/en/modules/opac-results.tmpl
+++ b/koha-tmpl/opac-tmpl/prog/en/modules/opac-results.tmpl
@@ -396,7 +396,7 @@ $(document).ready(function(){
                 <!-- /TMPL_IF -->
 
 				<!-- TMPL_IF NAME="OPACXSLTResultsDisplay" -->
-                <!-- TMPL_VAR NAME="XSLTResultsRecord" -->
+                <!-- TMPL_VAR NAME="opacXSLTResultsRecord" -->
 				<!-- TMPL_ELSE -->
 				<!-- TMPL_IF name="BiblioDefaultViewmarc" --><a class="title" href="/cgi-bin/koha/opac-MARCdetail.pl?biblionumber=<!-- TMPL_VAR NAME="biblionumber" ESCAPE="URL" -->" title="View details for this title">
                                 <!-- TMPL_ELSE -->
diff --git a/opac/opac-detail.pl b/opac/opac-detail.pl
index 43b71b9..76e1525 100755
--- a/opac/opac-detail.pl
+++ b/opac/opac-detail.pl
@@ -40,6 +40,10 @@ use C4::Review;
 use C4::Members;
 use C4::VirtualShelves;
 use C4::XSLT;
+use List::MoreUtils qw/any none/;
+use 5.10.0;
+
+#use Switch;
 
 BEGIN {
 	if (C4::Context->preference('BakerTaylorEnabled')) {
@@ -73,7 +77,7 @@ if ( ! $record ) {
 $template->param( biblionumber => $biblionumber );
 # XSLT processing of some stuff
 if (C4::Context->preference("OPACXSLTDetailsDisplay") ) {
-    $template->param( 'XSLTBloc' => XSLTParse4Display($biblionumber, $record, 'Detail', 'opac') );
+    $template->param( 'XSLTBloc' => XSLTParse4Display( $biblionumber, $record, C4::Context->preference("OPACXSLTDetailsDisplay") ) );
 }
 
 $template->param('OPACShowCheckoutName' => C4::Context->preference("OPACShowCheckoutName") ); 
-- 
1.7.1



More information about the Koha-patches mailing list