Revert "Bug 28371: Passpreviously fetched branches and itemtypes through and fetch...
[koha-ffzg.git] / C4 / XSLT.pm
index dd8d26f..ca25d0d 100644 (file)
@@ -30,8 +30,7 @@ use Koha::AuthorisedValues;
 use Koha::ItemTypes;
 use Koha::XSLT::Base;
 use Koha::Libraries;
-
-
+use Koha::Recalls;
 
 my $engine; #XSLT Handler object
 my %authval_per_framework;
@@ -60,12 +59,11 @@ C4::XSLT - Functions for displaying XSLT-generated content
 =head2 transformMARCXML4XSLT
 
 Replaces codes with authorized values in a MARC::Record object
-Is only used in this module currently.
 
 =cut
 
 sub transformMARCXML4XSLT {
-    my ($biblionumber, $record) = @_;
+    my ($biblionumber, $record, $opac) = @_;
     my $frameworkcode = GetFrameworkCode($biblionumber) || '';
     my $tagslib = &GetMarcStructure(1, $frameworkcode, { unsafe => 1 });
     my @fields;
@@ -82,9 +80,9 @@ sub transformMARCXML4XSLT {
                 my @new_subfields = ();
                 for my $subfield ( $field->subfields() ) {
                     my ( $letter, $value ) = @$subfield;
-                    # Replace the field value with the authorised value *except* for MARC21/NORMARC field 942$n (suppression in opac)
+                    # Replace the field value with the authorised value *except* for MARC21 field 942$n (suppression in opac)
                     if ( !( $tag eq '942' && $subfield->[0] eq 'n' ) || $marcflavour eq 'UNIMARC' ) {
-                        $value = GetAuthorisedValueDesc( $tag, $letter, $value, '', $tagslib )
+                        $value = GetAuthorisedValueDesc( $tag, $letter, $value, '', $tagslib, undef, $opac )
                             if $av->{ $tag }->{ $letter };
                     }
                     push( @new_subfields, $letter, $value );
@@ -172,7 +170,7 @@ sub get_xslt_sysprefs {
                               TrackClicks opacthemes IdRef OpacSuppression
                               OPACResultsLibrary OPACShowOpenURL
                               OpenURLResolverURL OpenURLImageLocation
-                              resultsMaxItems resultsMaxItemsUnavailable
+                              OPACResultsMaxItems OPACResultsMaxItemsUnavailable OPACResultsUnavailableGroupingBy
                               OpenURLText OPACShowMusicalInscripts OPACPlayMusicalInscripts / )
     {
         my $sp = C4::Context->preference( $syspref );
@@ -254,6 +252,9 @@ sub XSLTParse4Display {
     my $variables    = $params->{xslt_variables};
     my $items_rs     = $params->{items_rs};
 
+    die "Mandatory \$params->{xsl_syspref} was not provided, called with biblionumber $params->{biblionumber}"
+        if not defined $params->{xsl_syspref};
+
     my $xslfilename = get_xsl_filename( $xslsyspref);
 
     # grab the XML, run it through our stylesheet, push it out to the browser
@@ -267,9 +268,10 @@ sub XSLTParse4Display {
     my $xmlrecord = $record->as_xml(C4::Context->preference('marcflavour'));
 
     $variables ||= {};
+    my $biblio;
     if (C4::Context->preference('OPACShowOpenURL')) {
         my @biblio_itemtypes;
-        my $biblio = Koha::Biblios->find($biblionumber);
+        $biblio //= Koha::Biblios->find($biblionumber);
         if (C4::Context->preference('item-level_itypes')) {
             @biblio_itemtypes = $biblio->items->get_column("itype");
         } else {
@@ -282,8 +284,10 @@ sub XSLTParse4Display {
             $variables->{OpenURLResolverURL} = $biblio->get_openurl;
         }
     }
+
     my $varxml = "<variables>\n";
     while (my ($key, $value) = each %$variables) {
+        $value //= q{};
         $varxml .= "<variable name=\"$key\">$value</variable>\n";
     }
     $varxml .= "</variables>\n";
@@ -340,7 +344,7 @@ sub buildKohaItemsNamespace {
     my $ccodes =
       { map { $_->{authorised_value} => $_->{opac_description} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => "", kohafield => 'items.ccode' } ) };
 
-    my %branches = map { $_->branchcode => $_->branchname } Koha::Libraries->search({}, { order_by => 'branchname' });
+    my %branches = map { $_->branchcode => $_->branchname } Koha::Libraries->search({}, { order_by => 'branchname' })->as_list;
 
     my $itemtypes = { map { $_->{itemtype} => $_ } @{ Koha::ItemTypes->search->unblessed } };
     my $xml = '';
@@ -350,14 +354,24 @@ sub buildKohaItemsNamespace {
     while ( my $item = $items->next ) {
         my $status;
         my $substatus = '';
+        my $recalls_count;
 
-        if ($item->has_pending_hold) {
+        if ( C4::Context->preference('UseRecalls') ) {
+            $recalls_count = Koha::Recalls->search({ itemnumber => $item->itemnumber, status => 'waiting' })->count;
+        }
+
+        if ($recalls_count) {
+            # recalls take priority over holds
+            $status = 'other';
+            $substatus = 'Recall waiting';
+        }
+        elsif ( $item->has_pending_hold ) {
             $status = 'other';
             $substatus = 'Pending hold';
         }
         elsif ( $item->holds->waiting->count ) {
             $status = 'other';
-            $substatus = 'Waiting';
+            $substatus = 'Hold waiting';
         }
         elsif ($item->get_transfer) {
             $status = 'other';
@@ -397,13 +411,13 @@ sub buildKohaItemsNamespace {
         else {
             $status = "available";
         }
-        my $homebranch     = xml_escape($branches{$item->homebranch});
-        my $holdingbranch  = xml_escape($branches{$item->holdingbranch});
+        my $homebranch     = C4::Koha::xml_escape($branches{$item->homebranch});
+        my $holdingbranch  = C4::Koha::xml_escape($branches{$item->holdingbranch});
         my $resultbranch   = C4::Context->preference('OPACResultsLibrary') eq 'homebranch' ? $homebranch : $holdingbranch;
-        my $location       = xml_escape($item->location && exists $shelflocations->{$item->location} ? $shelflocations->{$item->location} : $item->location);
-        my $ccode          = xml_escape($item->ccode    && exists $ccodes->{$item->ccode}            ? $ccodes->{$item->ccode}            : $item->ccode);
-        my $itemcallnumber = xml_escape($item->itemcallnumber);
-        my $stocknumber    = xml_escape($item->stocknumber);
+        my $location       = C4::Koha::xml_escape($item->location && exists $shelflocations->{$item->location} ? $shelflocations->{$item->location} : $item->location);
+        my $ccode          = C4::Koha::xml_escape($item->ccode    && exists $ccodes->{$item->ccode}            ? $ccodes->{$item->ccode}            : $item->ccode);
+        my $itemcallnumber = C4::Koha::xml_escape($item->itemcallnumber);
+        my $stocknumber    = C4::Koha::xml_escape($item->stocknumber);
         $xml .=
             "<item>"
           . "<homebranch>$homebranch</homebranch>"