Bug 17642: use get_description_by_koha_field when needed
[koha.git] / catalogue / detail.pl
index e971ce7..09aa55c 100755 (executable)
 # along with Koha; if not, see <http://www.gnu.org/licenses>.
 
 
-use strict;
-use warnings;
+use Modern::Perl;
 
 use CGI qw ( -utf8 );
+use HTML::Entities;
 use C4::Acquisition qw( GetHistory );
 use C4::Auth;
 use C4::Koha;
@@ -28,7 +28,6 @@ use C4::Output;
 use C4::Biblio;
 use C4::Items;
 use C4::Circulation;
-use C4::Branch;
 use C4::Reserves;
 use C4::Members; # to use GetMember
 use C4::Serials;
@@ -42,6 +41,7 @@ use Koha::DateUtils;
 use C4::HTML5Media;
 use C4::CourseReserves qw(GetItemCourseReservesInfo);
 use C4::Acquisition qw(GetOrdersByBiblionumber);
+use Koha::AuthorisedValues;
 use Koha::Virtualshelves;
 
 my $query = CGI->new();
@@ -59,6 +59,7 @@ my ( $template, $borrowernumber, $cookie, $flags ) = get_template_and_user(
 );
 
 my $biblionumber = $query->param('biblionumber');
+$biblionumber = HTML::Entities::encode($biblionumber);
 my $record       = GetMarcBiblio($biblionumber);
 
 if ( not defined $record ) {
@@ -84,9 +85,18 @@ my $showallitems = $query->param('showallitems');
 my $marcflavour  = C4::Context->preference("marcflavour");
 
 # XSLT processing of some stuff
-if (C4::Context->preference("XSLTDetailsDisplay") ) {
-    $template->param('XSLTDetailsDisplay' =>'1',
-        'XSLTBloc' => XSLTParse4Display($biblionumber, $record, "XSLTDetailsDisplay") );
+my $xslfile = C4::Context->preference('XSLTDetailsDisplay');
+my $lang   = $xslfile ? C4::Languages::getlanguage()  : undef;
+my $sysxml = $xslfile ? C4::XSLT::get_xslt_sysprefs() : undef;
+
+if ( $xslfile ) {
+    $template->param(
+        XSLTDetailsDisplay => '1',
+        XSLTBloc => XSLTParse4Display(
+                        $biblionumber, $record, "XSLTDetailsDisplay",
+                        1, undef, $sysxml, $xslfile, $lang
+                    )
+    );
 }
 
 $template->param( 'SpineLabelShowPrintOnBibDetails' => C4::Context->preference("SpineLabelShowPrintOnBibDetails") );
@@ -117,7 +127,6 @@ my $marchostsarray  = GetMarcHosts($record,$marcflavour);
 my $subtitle         = GetRecordValue('subtitle', $record, $fw);
 
 # Get Branches, Itemtypes and Locations
-my $branches = GetBranches();
 my $itemtypes = GetItemTypes();
 my $dbh = C4::Context->dbh;
 
@@ -151,7 +160,6 @@ foreach my $subscription (@subscriptions) {
     $cell{missinglist}       = $subscription->{missinglist};
     $cell{librariannote}     = $subscription->{librariannote};
     $cell{branchcode}        = $subscription->{branchcode};
-    $cell{branchname}        = GetBranchName($subscription->{branchcode});
     $cell{hasalert}          = $subscription->{hasalert};
     $cell{callnumber}        = $subscription->{callnumber};
     $cell{closed}            = $subscription->{closed};
@@ -181,13 +189,34 @@ $dat->{'count'} = scalar @all_items + @hostitems;
 $dat->{'showncount'} = scalar @items + @hostitems;
 $dat->{'hiddencount'} = scalar @all_items + @hostitems - scalar @items;
 
-my $shelflocations = GetKohaAuthorisedValues('items.location', $fw);
-my $collections    = GetKohaAuthorisedValues('items.ccode'   , $fw);
-my $copynumbers    = GetKohaAuthorisedValues('items.copynumber', $fw);
+my $shelflocations =
+  { map { $_->authorised_value => $_->lib } Koha::AuthorisedValues->search_by_koha_field( { frameworkcode => $fw, kohafield => 'items.location' } ) };
+my $collections =
+  { map { $_->authorised_value => $_->lib } Koha::AuthorisedValues->search_by_koha_field( { frameworkcode => $fw, kohafield => 'items.ccode' } ) };
+my $copynumbers =
+  { map { $_->authorised_value => $_->lib } Koha::AuthorisedValues->search_by_koha_field( { frameworkcode => $fw, kohafield => 'items.copynumber' } ) };
 my (@itemloop, @otheritemloop, %itemfields);
 my $norequests = 1;
-my $authvalcode_items_itemlost = GetAuthValCode('items.itemlost',$fw);
-my $authvalcode_items_damaged  = GetAuthValCode('items.damaged', $fw);
+
+my $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw, kohafield => 'items.itemlost', authorised_value => { not => undef } });
+if ( $mss->count ) {
+    $template->param( itemlostloop => GetAuthorisedValues( $mss->next->authorised_value ) );
+}
+$mss = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw, kohafield => 'items.damaged', authorised_value => { not => undef } });
+if ( $mss->count ) {
+    $template->param( itemdamagedloop => GetAuthorisedValues( $mss->next->authorised_value ) );
+}
+
+$mss = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw, kohafield => 'items.materials', authorised_value => { not => undef } });
+my %materials_map;
+if ($mss->count) {
+    my $materials_authvals = GetAuthorisedValues($mss->next->authorised_value);
+    if ($materials_authvals) {
+        foreach my $value (@$materials_authvals) {
+            $materials_map{$value->{authorised_value}} = $value->{lib};
+        }
+    }
+}
 
 my $analytics_flag;
 my $materials_flag; # set this if the items have anything in the materials field
@@ -206,11 +235,7 @@ foreach my $item (@items) {
                                                : '';
 
     $item->{datedue} = format_sqldatetime($item->{datedue});
-    # item damaged, lost, withdrawn loops
-    $item->{itemlostloop} = GetAuthorisedValues($authvalcode_items_itemlost, $item->{itemlost}) if $authvalcode_items_itemlost;
-    if ($item->{damaged}) {
-        $item->{itemdamagedloop} = GetAuthorisedValues($authvalcode_items_damaged, $item->{damaged}) if $authvalcode_items_damaged;
-    }
+
     #get shelf location and collection code description if they are authorised value.
     # same thing for copy number
     my $shelfcode = $item->{'location'};
@@ -237,7 +262,7 @@ foreach my $item (@items) {
         $item->{ReservedForBorrowernumber}     = $reservedfor;
         $item->{ReservedForSurname}     = $ItemBorrowerReserveInfo->{'surname'};
         $item->{ReservedForFirstname}   = $ItemBorrowerReserveInfo->{'firstname'};
-        $item->{ExpectedAtLibrary}      = $branches->{$expectedAt}{branchname};
+        $item->{ExpectedAtLibrary}      = $expectedAt;
         $item->{Reservedcardnumber}             = $ItemBorrowerReserveInfo->{'cardnumber'};
         # Check waiting status
         $item->{waitingdate} = $wait;
@@ -248,8 +273,8 @@ foreach my $item (@items) {
     my ( $transfertwhen, $transfertfrom, $transfertto ) = GetTransfers($item->{itemnumber});
     if ( defined( $transfertwhen ) && ( $transfertwhen ne '' ) ) {
         $item->{transfertwhen} = $transfertwhen;
-        $item->{transfertfrom} = $branches->{$transfertfrom}{branchname};
-        $item->{transfertto}   = $branches->{$transfertto}{branchname};
+        $item->{transfertfrom} = $transfertfrom;
+        $item->{transfertto}   = $transfertto;
         $item->{nocancel} = 1;
     }
 
@@ -275,7 +300,10 @@ foreach my $item (@items) {
     }
 
     if (defined($item->{'materials'}) && $item->{'materials'} =~ /\S/){
-       $materials_flag = 1;
+        $materials_flag = 1;
+        if (defined $materials_map{ $item->{materials} }) {
+            $item->{materials} = $materials_map{ $item->{materials} };
+        }
     }
 
     if ( C4::Context->preference('UseCourseReserves') ) {
@@ -378,7 +406,7 @@ $template->param(
     subscriptions       => \@subs,
     subscriptionsnumber => $subscriptionsnumber,
     subscriptiontitle   => $dat->{title},
-    searchid            => $query->param('searchid'),
+    searchid            => scalar $query->param('searchid'),
 );
 
 # $debug and $template->param(debug_display => 1);