Bug 22495: Restore Elasticsearch mapping for su-geo
[koha.git] / catalogue / detail.pl
blob154d63260a5eb0783c532e13314e9a0b02b0dd65
1 #!/usr/bin/perl
3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19 use Modern::Perl;
21 use CGI qw ( -utf8 );
22 use HTML::Entities;
23 use C4::Auth;
24 use C4::Context;
25 use C4::Koha;
26 use C4::Serials; #uses getsubscriptionfrom biblionumber
27 use C4::Output;
28 use C4::Biblio;
29 use C4::Items;
30 use C4::Circulation;
31 use C4::Reserves;
32 use C4::Serials;
33 use C4::XISBN qw(get_xisbns);
34 use C4::External::Amazon;
35 use C4::Search; # enabled_staff_search_views
36 use C4::Tags qw(get_tags);
37 use C4::XSLT;
38 use C4::Images;
39 use Koha::DateUtils;
40 use C4::HTML5Media;
41 use C4::CourseReserves qw(GetItemCourseReservesInfo);
42 use C4::Acquisition qw(GetOrdersByBiblionumber);
43 use Koha::AuthorisedValues;
44 use Koha::Biblios;
45 use Koha::Items;
46 use Koha::ItemTypes;
47 use Koha::Patrons;
48 use Koha::Virtualshelves;
49 use Koha::Plugins;
51 my $query = CGI->new();
53 my $analyze = $query->param('analyze');
55 my ( $template, $borrowernumber, $cookie, $flags ) = get_template_and_user(
57 template_name => 'catalogue/detail.tt',
58 query => $query,
59 type => "intranet",
60 authnotrequired => 0,
61 flagsrequired => { catalogue => 1 },
65 # Determine if we should be offering any enhancement plugin buttons
66 if ( C4::Context->preference('UseKohaPlugins') &&
67 C4::Context->config('enable_plugins') ) {
68 # Only pass plugins that can offer a toolbar button
69 my @plugins = Koha::Plugins->new()->GetPlugins({
70 method => 'intranet_catalog_biblio_enhancements_toolbar_button'
71 });
72 $template->param(
73 plugins => \@plugins
77 my $biblionumber = $query->param('biblionumber');
78 $biblionumber = HTML::Entities::encode($biblionumber);
79 my $record = GetMarcBiblio({ biblionumber => $biblionumber });
81 if ( not defined $record ) {
82 # biblionumber invalid -> report and exit
83 $template->param( unknownbiblionumber => 1,
84 biblionumber => $biblionumber );
85 output_html_with_http_headers $query, $cookie, $template->output;
86 exit;
89 if($query->cookie("holdfor")){
90 my $holdfor_patron = Koha::Patrons->find( $query->cookie("holdfor") );
91 $template->param(
92 # FIXME Should pass the patron object
93 holdfor => $query->cookie("holdfor"),
94 holdfor_surname => $holdfor_patron->surname,
95 holdfor_firstname => $holdfor_patron->firstname,
96 holdfor_cardnumber => $holdfor_patron->cardnumber,
100 my $fw = GetFrameworkCode($biblionumber);
101 my $showallitems = $query->param('showallitems');
102 my $marcflavour = C4::Context->preference("marcflavour");
104 # XSLT processing of some stuff
105 my $xslfile = C4::Context->preference('XSLTDetailsDisplay');
106 my $lang = $xslfile ? C4::Languages::getlanguage() : undef;
107 my $sysxml = $xslfile ? C4::XSLT::get_xslt_sysprefs() : undef;
109 if ( $xslfile ) {
110 $template->param(
111 XSLTDetailsDisplay => '1',
112 XSLTBloc => XSLTParse4Display(
113 $biblionumber, $record, "XSLTDetailsDisplay",
114 1, undef, $sysxml, $xslfile, $lang
119 $template->param( 'SpineLabelShowPrintOnBibDetails' => C4::Context->preference("SpineLabelShowPrintOnBibDetails") );
120 $template->param( ocoins => GetCOinSBiblio($record) );
122 # some useful variables for enhanced content;
123 # in each case, we're grabbing the first value we find in
124 # the record and normalizing it
125 my $upc = GetNormalizedUPC($record,$marcflavour);
126 my $ean = GetNormalizedEAN($record,$marcflavour);
127 my $oclc = GetNormalizedOCLCNumber($record,$marcflavour);
128 my $isbn = GetNormalizedISBN(undef,$record,$marcflavour);
130 $template->param(
131 normalized_upc => $upc,
132 normalized_ean => $ean,
133 normalized_oclc => $oclc,
134 normalized_isbn => $isbn,
137 my $marcnotesarray = GetMarcNotes( $record, $marcflavour );
138 my $marcisbnsarray = GetMarcISBN( $record, $marcflavour );
139 my $marcauthorsarray = GetMarcAuthors( $record, $marcflavour );
140 my $marcsubjctsarray = GetMarcSubjects( $record, $marcflavour );
141 my $marcseriesarray = GetMarcSeries($record,$marcflavour);
142 my $marcurlsarray = GetMarcUrls ($record,$marcflavour);
143 my $marchostsarray = GetMarcHosts($record,$marcflavour);
144 my $subtitle = GetRecordValue('subtitle', $record, $fw);
146 my $itemtypes = { map { $_->{itemtype} => $_ } @{ Koha::ItemTypes->search->unblessed } };
148 my $dbh = C4::Context->dbh;
150 my @all_items = GetItemsInfo( $biblionumber );
151 my @items;
152 my $patron = Koha::Patrons->find( $borrowernumber );
153 for my $itm (@all_items) {
154 push @items, $itm unless ( $itm->{itemlost} && $patron->category->hidelostitems && !$showallitems);
157 # flag indicating existence of at least one item linked via a host record
158 my $hostrecords;
159 # adding items linked via host biblios
160 my @hostitems = GetHostItemsInfo($record);
161 if (@hostitems){
162 $hostrecords =1;
163 push (@items,@hostitems);
166 my $dat = &GetBiblioData($biblionumber);
168 #coping with subscriptions
169 my $subscriptionsnumber = CountSubscriptionFromBiblionumber($biblionumber);
170 my @subscriptions = SearchSubscriptions({ biblionumber => $biblionumber, orderby => 'title' });
171 my @subs;
173 foreach my $subscription (@subscriptions) {
174 my %cell;
175 my $serials_to_display;
176 $cell{subscriptionid} = $subscription->{subscriptionid};
177 $cell{subscriptionnotes} = $subscription->{internalnotes};
178 $cell{missinglist} = $subscription->{missinglist};
179 $cell{librariannote} = $subscription->{librariannote};
180 $cell{branchcode} = $subscription->{branchcode};
181 $cell{hasalert} = $subscription->{hasalert};
182 $cell{callnumber} = $subscription->{callnumber};
183 $cell{closed} = $subscription->{closed};
184 #get the three latest serials.
185 $serials_to_display = $subscription->{staffdisplaycount};
186 $serials_to_display = C4::Context->preference('StaffSerialIssueDisplayCount') unless $serials_to_display;
187 $cell{staffdisplaycount} = $serials_to_display;
188 $cell{latestserials} =
189 GetLatestSerials( $subscription->{subscriptionid}, $serials_to_display );
190 push @subs, \%cell;
194 # Get acquisition details
195 if ( C4::Context->preference('AcquisitionDetails') ) {
196 my $orders = Koha::Acquisition::Orders->search(
197 { biblionumber => $biblionumber },
199 join => 'basketno',
200 order_by => 'basketno.booksellerid'
202 ); # GetHistory sorted by aqbooksellerid, but does it make sense?
204 $template->param(
205 orders => $orders,
209 if ( defined $dat->{'itemtype'} ) {
210 $dat->{imageurl} = getitemtypeimagelocation( 'intranet', $itemtypes->{ $dat->{itemtype} }{imageurl} );
213 $dat->{'count'} = scalar @all_items + @hostitems;
214 $dat->{'showncount'} = scalar @items + @hostitems;
215 $dat->{'hiddencount'} = scalar @all_items + @hostitems - scalar @items;
217 my $shelflocations =
218 { map { $_->{authorised_value} => $_->{lib} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => $fw, kohafield => 'items.location' } ) };
219 my $collections =
220 { map { $_->{authorised_value} => $_->{lib} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => $fw, kohafield => 'items.ccode' } ) };
221 my $copynumbers =
222 { map { $_->{authorised_value} => $_->{lib} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => $fw, kohafield => 'items.copynumber' } ) };
223 my (@itemloop, @otheritemloop, %itemfields);
224 my $norequests = 1;
226 my $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw, kohafield => 'items.itemlost', authorised_value => [ -and => {'!=' => undef }, {'!=' => ''}] });
227 if ( $mss->count ) {
228 $template->param( itemlostloop => GetAuthorisedValues( $mss->next->authorised_value ) );
230 $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw, kohafield => 'items.damaged', authorised_value => [ -and => {'!=' => undef }, {'!=' => ''}] });
231 if ( $mss->count ) {
232 $template->param( itemdamagedloop => GetAuthorisedValues( $mss->next->authorised_value ) );
234 $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw, kohafield => 'items.withdrawn', authorised_value => { not => undef } });
235 if ( $mss->count ) {
236 $template->param( itemwithdrawnloop => GetAuthorisedValues( $mss->next->authorised_value) );
239 $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw, kohafield => 'items.materials', authorised_value => [ -and => {'!=' => undef }, {'!=' => ''}] });
240 my %materials_map;
241 if ($mss->count) {
242 my $materials_authvals = GetAuthorisedValues($mss->next->authorised_value);
243 if ($materials_authvals) {
244 foreach my $value (@$materials_authvals) {
245 $materials_map{$value->{authorised_value}} = $value->{lib};
250 my $analytics_flag;
251 my $materials_flag; # set this if the items have anything in the materials field
252 my $currentbranch = C4::Context->userenv ? C4::Context->userenv->{branch} : undef;
253 if ($currentbranch and C4::Context->preference('SeparateHoldings')) {
254 $template->param(SeparateHoldings => 1);
256 my $separatebranch = C4::Context->preference('SeparateHoldingsBranch') || 'homebranch';
257 foreach my $item (@items) {
258 my $itembranchcode = $item->{$separatebranch};
260 # can place holds defaults to yes
261 $norequests = 0 unless ( ( $item->{'notforloan'} > 0 ) || ( $item->{'itemnotforloan'} > 0 ) );
263 $item->{imageurl} = defined $item->{itype} ? getitemtypeimagelocation('intranet', $itemtypes->{ $item->{itype} }{imageurl})
264 : '';
266 $item->{datedue} = format_sqldatetime($item->{datedue});
268 #get shelf location and collection code description if they are authorised value.
269 # same thing for copy number
270 my $shelfcode = $item->{'location'};
271 $item->{'location'} = $shelflocations->{$shelfcode} if ( defined( $shelfcode ) && defined($shelflocations) && exists( $shelflocations->{$shelfcode} ) );
272 my $ccode = $item->{'ccode'};
273 $item->{'ccode'} = $collections->{$ccode} if ( defined( $ccode ) && defined($collections) && exists( $collections->{$ccode} ) );
274 my $copynumber = $item->{'copynumber'};
275 $item->{'copynumber'} = $copynumbers->{$copynumber} if ( defined($copynumber) && defined($copynumbers) && exists( $copynumbers->{$copynumber} ) );
276 foreach (qw(ccode enumchron copynumber stocknumber itemnotes itemnotes_nonpublic uri)) {
277 $itemfields{$_} = 1 if ( $item->{$_} );
280 # checking for holds
281 my $item_object = Koha::Items->find( $item->{itemnumber} );
282 my $holds = $item_object->current_holds;
283 if ( my $first_hold = $holds->next ) {
284 my $patron = Koha::Patrons->find( $first_hold->borrowernumber );
285 $item->{backgroundcolor} = 'reserved';
286 $item->{reservedate} = $first_hold->reservedate;
287 $item->{ReservedFor} = $patron,
288 $item->{ExpectedAtLibrary} = $first_hold->branchcode;
289 # Check waiting status
290 $item->{waitingdate} = $first_hold->waitingdate;
293 if ( my $checkout = $item_object->checkout ) {
294 $item->{CheckedOutFor} = $checkout->patron;
297 # Check the transit status
298 my ( $transfertwhen, $transfertfrom, $transfertto ) = GetTransfers($item->{itemnumber});
299 if ( defined( $transfertwhen ) && ( $transfertwhen ne '' ) ) {
300 $item->{transfertwhen} = $transfertwhen;
301 $item->{transfertfrom} = $transfertfrom;
302 $item->{transfertto} = $transfertto;
303 $item->{nocancel} = 1;
306 foreach my $f (qw( itemnotes )) {
307 if ($item->{$f}) {
308 $item->{$f} =~ s|\n|<br />|g;
309 $itemfields{$f} = 1;
313 #item has a host number if its biblio number does not match the current bib
315 if ($item->{biblionumber} ne $biblionumber){
316 $item->{hostbiblionumber} = $item->{biblionumber};
317 $item->{hosttitle} = GetBiblioData($item->{biblionumber})->{title};
321 if ( $analyze ) {
322 # count if item is used in analytical bibliorecords
323 # The 'countanalytics' flag is only used in the templates if analyze is set
324 my $countanalytics = C4::Context->preference('EasyAnalyticalRecords') ? GetAnalyticsCount($item->{itemnumber}) : 0;
325 if ($countanalytics > 0){
326 $analytics_flag=1;
327 $item->{countanalytics} = $countanalytics;
331 if (defined($item->{'materials'}) && $item->{'materials'} =~ /\S/){
332 $materials_flag = 1;
333 if (defined $materials_map{ $item->{materials} }) {
334 $item->{materials} = $materials_map{ $item->{materials} };
338 if ( C4::Context->preference('UseCourseReserves') ) {
339 $item->{'course_reserves'} = GetItemCourseReservesInfo( itemnumber => $item->{'itemnumber'} );
342 if ( C4::Context->preference('IndependentBranches') ) {
343 my $userenv = C4::Context->userenv();
344 if ( not C4::Context->IsSuperLibrarian()
345 and $userenv->{branch} ne $item->{homebranch} ) {
346 $item->{cannot_be_edited} = 1;
350 if ($currentbranch and $currentbranch ne "NO_LIBRARY_SET"
351 and C4::Context->preference('SeparateHoldings')) {
352 if ($itembranchcode and $itembranchcode eq $currentbranch) {
353 push @itemloop, $item;
354 } else {
355 push @otheritemloop, $item;
357 } else {
358 push @itemloop, $item;
362 # Display only one tab if one items list is empty
363 if (scalar(@itemloop) == 0 || scalar(@otheritemloop) == 0) {
364 $template->param(SeparateHoldings => 0);
365 if (scalar(@itemloop) == 0) {
366 @itemloop = @otheritemloop;
370 $template->param( norequests => $norequests );
371 $template->param(
372 MARCNOTES => $marcnotesarray,
373 MARCSUBJCTS => $marcsubjctsarray,
374 MARCAUTHORS => $marcauthorsarray,
375 MARCSERIES => $marcseriesarray,
376 MARCURLS => $marcurlsarray,
377 MARCISBNS => $marcisbnsarray,
378 MARCHOSTS => $marchostsarray,
379 subtitle => $subtitle,
380 itemdata_ccode => $itemfields{ccode},
381 itemdata_enumchron => $itemfields{enumchron},
382 itemdata_uri => $itemfields{uri},
383 itemdata_copynumber => $itemfields{copynumber},
384 itemdata_stocknumber => $itemfields{stocknumber},
385 volinfo => $itemfields{enumchron},
386 itemdata_itemnotes => $itemfields{itemnotes},
387 itemdata_nonpublicnotes => $itemfields{itemnotes_nonpublic},
388 z3950_search_params => C4::Search::z3950_search_args($dat),
389 hostrecords => $hostrecords,
390 analytics_flag => $analytics_flag,
391 C4::Search::enabled_staff_search_views,
392 materials => $materials_flag,
395 if (C4::Context->preference("AlternateHoldingsField") && scalar @items == 0) {
396 my $fieldspec = C4::Context->preference("AlternateHoldingsField");
397 my $subfields = substr $fieldspec, 3;
398 my $holdingsep = C4::Context->preference("AlternateHoldingsSeparator") || ' ';
399 my @alternateholdingsinfo = ();
400 my @holdingsfields = $record->field(substr $fieldspec, 0, 3);
402 for my $field (@holdingsfields) {
403 my %holding = ( holding => '' );
404 my $havesubfield = 0;
405 for my $subfield ($field->subfields()) {
406 if ((index $subfields, $$subfield[0]) >= 0) {
407 $holding{'holding'} .= $holdingsep if (length $holding{'holding'} > 0);
408 $holding{'holding'} .= $$subfield[1];
409 $havesubfield++;
412 if ($havesubfield) {
413 push(@alternateholdingsinfo, \%holding);
417 $template->param(
418 ALTERNATEHOLDINGS => \@alternateholdingsinfo,
422 my @results = ( $dat, );
423 foreach ( keys %{$dat} ) {
424 $template->param( "$_" => defined $dat->{$_} ? $dat->{$_} : '' );
427 # does not work: my %views_enabled = map { $_ => 1 } $template->query(loop => 'EnableViews');
428 # method query not found?!?!
429 $template->param( AmazonTld => get_amazon_tld() ) if ( C4::Context->preference("AmazonCoverImages"));
430 $template->param(
431 itemloop => \@itemloop,
432 otheritemloop => \@otheritemloop,
433 biblionumber => $biblionumber,
434 ($analyze? 'analyze':'detailview') =>1,
435 subscriptions => \@subs,
436 subscriptionsnumber => $subscriptionsnumber,
437 subscriptiontitle => $dat->{title},
438 searchid => scalar $query->param('searchid'),
441 # $debug and $template->param(debug_display => 1);
443 # Lists
445 if (C4::Context->preference("virtualshelves") ) {
446 my $shelves = Koha::Virtualshelves->search(
448 biblionumber => $biblionumber,
449 category => 2,
452 join => 'virtualshelfcontents',
455 $template->param( 'shelves' => $shelves );
458 # XISBN Stuff
459 if (C4::Context->preference("FRBRizeEditions")==1) {
460 eval {
461 $template->param(
462 XISBNS => scalar get_xisbns($isbn)
465 if ($@) { warn "XISBN Failed $@"; }
468 if ( C4::Context->preference("LocalCoverImages") == 1 ) {
469 my @images = ListImagesForBiblio($biblionumber);
470 $template->{VARS}->{localimages} = \@images;
473 # HTML5 Media
474 if ( (C4::Context->preference("HTML5MediaEnabled") eq 'both') or (C4::Context->preference("HTML5MediaEnabled") eq 'staff') ) {
475 $template->param( C4::HTML5Media->gethtml5media($record));
478 # Displaying tags
480 my $tag_quantity;
481 if (C4::Context->preference('TagsEnabled') and $tag_quantity = C4::Context->preference('TagsShowOnDetail')) {
482 $template->param(
483 TagsEnabled => 1,
484 TagsShowOnDetail => $tag_quantity
486 $template->param(TagLoop => get_tags({biblionumber=>$biblionumber, approved=>1,
487 'sort'=>'-weight', limit=>$tag_quantity}));
490 #we only need to pass the number of holds to the template
491 my $biblio = Koha::Biblios->find( $biblionumber );
492 my $holds = $biblio->holds;
493 $template->param( holdcount => $holds->count );
495 my $StaffDetailItemSelection = C4::Context->preference('StaffDetailItemSelection');
496 if ($StaffDetailItemSelection) {
497 # Only enable item selection if user can execute at least one action
498 if (
499 $flags->{superlibrarian}
500 || (
501 ref $flags->{tools} eq 'HASH' && (
502 $flags->{tools}->{items_batchmod} # Modify selected items
503 || $flags->{tools}->{items_batchdel} # Delete selected items
506 || ( ref $flags->{tools} eq '' && $flags->{tools} )
509 $template->param(
510 StaffDetailItemSelection => $StaffDetailItemSelection );
514 my @allorders_using_biblio = GetOrdersByBiblionumber ($biblionumber);
515 my @deletedorders_using_biblio;
516 my @orders_using_biblio;
517 my @baskets_orders;
518 my @baskets_deletedorders;
520 foreach my $myorder (@allorders_using_biblio) {
521 my $basket = $myorder->{'basketno'};
522 if ((defined $myorder->{'datecancellationprinted'}) and ($myorder->{'datecancellationprinted'} ne '0000-00-00') ){
523 push @deletedorders_using_biblio, $myorder;
524 unless (grep(/^$basket$/, @baskets_deletedorders)){
525 push @baskets_deletedorders,$myorder->{'basketno'};
528 else {
529 push @orders_using_biblio, $myorder;
530 unless (grep(/^$basket$/, @baskets_orders)){
531 push @baskets_orders,$myorder->{'basketno'};
536 my $count_orders_using_biblio = scalar @orders_using_biblio ;
537 $template->param (countorders => $count_orders_using_biblio);
539 my $count_deletedorders_using_biblio = scalar @deletedorders_using_biblio ;
540 $template->param (countdeletedorders => $count_deletedorders_using_biblio);
542 $template->param (basketsorders => \@baskets_orders);
543 $template->param (basketsdeletedorders => \@baskets_deletedorders);
545 output_html_with_http_headers $query, $cookie, $template->output;