Bug 20434: Add UNIMARC field 214 and its subfields
[koha.git] / catalogue / detail.pl
blobb4d9bae646a3571de66538b67580f62715e77a32
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 });
80 my $biblio = Koha::Biblios->find( $biblionumber );
81 $template->param( 'biblio', $biblio );
83 if ( not defined $record ) {
84 # biblionumber invalid -> report and exit
85 $template->param( unknownbiblionumber => 1,
86 biblionumber => $biblionumber );
87 output_html_with_http_headers $query, $cookie, $template->output;
88 exit;
91 if($query->cookie("holdfor")){
92 my $holdfor_patron = Koha::Patrons->find( $query->cookie("holdfor") );
93 $template->param(
94 # FIXME Should pass the patron object
95 holdfor => $query->cookie("holdfor"),
96 holdfor_surname => $holdfor_patron->surname,
97 holdfor_firstname => $holdfor_patron->firstname,
98 holdfor_cardnumber => $holdfor_patron->cardnumber,
102 my $fw = GetFrameworkCode($biblionumber);
103 my $showallitems = $query->param('showallitems');
104 my $marcflavour = C4::Context->preference("marcflavour");
106 # XSLT processing of some stuff
107 my $xslfile = C4::Context->preference('XSLTDetailsDisplay') || "default";
108 my $lang = $xslfile ? C4::Languages::getlanguage() : undef;
109 my $sysxml = $xslfile ? C4::XSLT::get_xslt_sysprefs() : undef;
111 if ( $xslfile ) {
112 $template->param(
113 XSLTDetailsDisplay => '1',
114 XSLTBloc => XSLTParse4Display(
115 $biblionumber, $record, "XSLTDetailsDisplay",
116 1, undef, $sysxml, $xslfile, $lang
121 $template->param( 'SpineLabelShowPrintOnBibDetails' => C4::Context->preference("SpineLabelShowPrintOnBibDetails") );
122 $template->param( ocoins => $biblio->get_coins );
124 # some useful variables for enhanced content;
125 # in each case, we're grabbing the first value we find in
126 # the record and normalizing it
127 my $upc = GetNormalizedUPC($record,$marcflavour);
128 my $ean = GetNormalizedEAN($record,$marcflavour);
129 my $oclc = GetNormalizedOCLCNumber($record,$marcflavour);
130 my $isbn = GetNormalizedISBN(undef,$record,$marcflavour);
132 $template->param(
133 normalized_upc => $upc,
134 normalized_ean => $ean,
135 normalized_oclc => $oclc,
136 normalized_isbn => $isbn,
139 my $marcnotesarray = GetMarcNotes( $record, $marcflavour );
140 my $marcisbnsarray = GetMarcISBN( $record, $marcflavour );
141 my $marcauthorsarray = GetMarcAuthors( $record, $marcflavour );
142 my $marcsubjctsarray = GetMarcSubjects( $record, $marcflavour );
143 my $marcseriesarray = GetMarcSeries($record,$marcflavour);
144 my $marcurlsarray = GetMarcUrls ($record,$marcflavour);
145 my $marchostsarray = GetMarcHosts($record,$marcflavour);
147 my $itemtypes = { map { $_->{itemtype} => $_ } @{ Koha::ItemTypes->search->unblessed } };
149 my $dbh = C4::Context->dbh;
151 my @all_items = GetItemsInfo( $biblionumber );
152 my @items;
153 my $patron = Koha::Patrons->find( $borrowernumber );
154 for my $itm (@all_items) {
155 push @items, $itm unless ( $itm->{itemlost} && $patron->category->hidelostitems && !$showallitems);
158 # flag indicating existence of at least one item linked via a host record
159 my $hostrecords;
160 # adding items linked via host biblios
161 my @hostitems = GetHostItemsInfo($record);
162 if (@hostitems){
163 $hostrecords =1;
164 push (@items,@hostitems);
167 my $dat = &GetBiblioData($biblionumber);
169 #coping with subscriptions
170 my $subscriptionsnumber = CountSubscriptionFromBiblionumber($biblionumber);
171 my @subscriptions = SearchSubscriptions({ biblionumber => $biblionumber, orderby => 'title' });
172 my @subs;
174 foreach my $subscription (@subscriptions) {
175 my %cell;
176 my $serials_to_display;
177 $cell{subscriptionid} = $subscription->{subscriptionid};
178 $cell{subscriptionnotes} = $subscription->{internalnotes};
179 $cell{missinglist} = $subscription->{missinglist};
180 $cell{librariannote} = $subscription->{librariannote};
181 $cell{branchcode} = $subscription->{branchcode};
182 $cell{hasalert} = $subscription->{hasalert};
183 $cell{callnumber} = $subscription->{callnumber};
184 $cell{location} = $subscription->{location};
185 $cell{closed} = $subscription->{closed};
186 #get the three latest serials.
187 $serials_to_display = $subscription->{staffdisplaycount};
188 $serials_to_display = C4::Context->preference('StaffSerialIssueDisplayCount') unless $serials_to_display;
189 $cell{staffdisplaycount} = $serials_to_display;
190 $cell{latestserials} =
191 GetLatestSerials( $subscription->{subscriptionid}, $serials_to_display );
192 push @subs, \%cell;
196 # Get acquisition details
197 if ( C4::Context->preference('AcquisitionDetails') ) {
198 my $orders = Koha::Acquisition::Orders->search(
199 { biblionumber => $biblionumber },
201 join => 'basketno',
202 order_by => 'basketno.booksellerid'
204 ); # GetHistory sorted by aqbooksellerid, but does it make sense?
206 $template->param(
207 orders => $orders,
211 if ( defined $dat->{'itemtype'} ) {
212 $dat->{imageurl} = getitemtypeimagelocation( 'intranet', $itemtypes->{ $dat->{itemtype} }{imageurl} );
215 $dat->{'count'} = scalar @all_items + @hostitems;
216 $dat->{'showncount'} = scalar @items + @hostitems;
217 $dat->{'hiddencount'} = scalar @all_items + @hostitems - scalar @items;
219 my $shelflocations =
220 { map { $_->{authorised_value} => $_->{lib} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => $fw, kohafield => 'items.location' } ) };
221 my $collections =
222 { map { $_->{authorised_value} => $_->{lib} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => $fw, kohafield => 'items.ccode' } ) };
223 my $copynumbers =
224 { map { $_->{authorised_value} => $_->{lib} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => $fw, kohafield => 'items.copynumber' } ) };
225 my (@itemloop, @otheritemloop, %itemfields);
226 my $norequests = 1;
228 my $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw, kohafield => 'items.itemlost', authorised_value => [ -and => {'!=' => undef }, {'!=' => ''}] });
229 if ( $mss->count ) {
230 $template->param( itemlostloop => GetAuthorisedValues( $mss->next->authorised_value ) );
232 $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw, kohafield => 'items.damaged', authorised_value => [ -and => {'!=' => undef }, {'!=' => ''}] });
233 if ( $mss->count ) {
234 $template->param( itemdamagedloop => GetAuthorisedValues( $mss->next->authorised_value ) );
236 $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw, kohafield => 'items.withdrawn', authorised_value => { not => undef } });
237 if ( $mss->count ) {
238 $template->param( itemwithdrawnloop => GetAuthorisedValues( $mss->next->authorised_value) );
241 $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw, kohafield => 'items.materials', authorised_value => [ -and => {'!=' => undef }, {'!=' => ''}] });
242 my %materials_map;
243 if ($mss->count) {
244 my $materials_authvals = GetAuthorisedValues($mss->next->authorised_value);
245 if ($materials_authvals) {
246 foreach my $value (@$materials_authvals) {
247 $materials_map{$value->{authorised_value}} = $value->{lib};
252 my $analytics_flag;
253 my $materials_flag; # set this if the items have anything in the materials field
254 my $currentbranch = C4::Context->userenv ? C4::Context->userenv->{branch} : undef;
255 if ($currentbranch and C4::Context->preference('SeparateHoldings')) {
256 $template->param(SeparateHoldings => 1);
258 my $separatebranch = C4::Context->preference('SeparateHoldingsBranch') || 'homebranch';
259 foreach my $item (@items) {
260 my $itembranchcode = $item->{$separatebranch};
262 # can place holds defaults to yes
263 $norequests = 0 unless ( ( $item->{'notforloan'} > 0 ) || ( $item->{'itemnotforloan'} > 0 ) );
265 $item->{imageurl} = defined $item->{itype} ? getitemtypeimagelocation('intranet', $itemtypes->{ $item->{itype} }{imageurl})
266 : '';
268 $item->{datedue} = format_sqldatetime($item->{datedue});
270 #get shelf location and collection code description if they are authorised value.
271 # same thing for copy number
272 my $shelfcode = $item->{'location'};
273 $item->{'location'} = $shelflocations->{$shelfcode} if ( defined( $shelfcode ) && defined($shelflocations) && exists( $shelflocations->{$shelfcode} ) );
274 my $ccode = $item->{'ccode'};
275 $item->{'ccode'} = $collections->{$ccode} if ( defined( $ccode ) && defined($collections) && exists( $collections->{$ccode} ) );
276 my $copynumber = $item->{'copynumber'};
277 $item->{'copynumber'} = $copynumbers->{$copynumber} if ( defined($copynumber) && defined($copynumbers) && exists( $copynumbers->{$copynumber} ) );
278 foreach (qw(ccode enumchron copynumber stocknumber itemnotes itemnotes_nonpublic uri)) {
279 $itemfields{$_} = 1 if ( $item->{$_} );
282 # checking for holds
283 my $item_object = Koha::Items->find( $item->{itemnumber} );
284 my $holds = $item_object->current_holds;
285 if ( my $first_hold = $holds->next ) {
286 my $patron = Koha::Patrons->find( $first_hold->borrowernumber );
287 $item->{backgroundcolor} = 'reserved';
288 $item->{reservedate} = $first_hold->reservedate;
289 $item->{ReservedFor} = $patron,
290 $item->{ExpectedAtLibrary} = $first_hold->branchcode;
291 # Check waiting status
292 $item->{waitingdate} = $first_hold->waitingdate;
295 if ( my $checkout = $item_object->checkout ) {
296 $item->{CheckedOutFor} = $checkout->patron;
299 # Check the transit status
300 my ( $transfertwhen, $transfertfrom, $transfertto ) = GetTransfers($item->{itemnumber});
301 if ( defined( $transfertwhen ) && ( $transfertwhen ne '' ) ) {
302 $item->{transfertwhen} = $transfertwhen;
303 $item->{transfertfrom} = $transfertfrom;
304 $item->{transfertto} = $transfertto;
305 $item->{nocancel} = 1;
308 foreach my $f (qw( itemnotes )) {
309 if ($item->{$f}) {
310 $item->{$f} =~ s|\n|<br />|g;
311 $itemfields{$f} = 1;
315 #item has a host number if its biblio number does not match the current bib
317 if ($item->{biblionumber} ne $biblionumber){
318 $item->{hostbiblionumber} = $item->{biblionumber};
319 $item->{hosttitle} = GetBiblioData($item->{biblionumber})->{title};
323 if ( $analyze ) {
324 # count if item is used in analytical bibliorecords
325 # The 'countanalytics' flag is only used in the templates if analyze is set
326 my $countanalytics = C4::Context->preference('EasyAnalyticalRecords') ? GetAnalyticsCount($item->{itemnumber}) : 0;
327 if ($countanalytics > 0){
328 $analytics_flag=1;
329 $item->{countanalytics} = $countanalytics;
333 if (defined($item->{'materials'}) && $item->{'materials'} =~ /\S/){
334 $materials_flag = 1;
335 if (defined $materials_map{ $item->{materials} }) {
336 $item->{materials} = $materials_map{ $item->{materials} };
340 if ( C4::Context->preference('UseCourseReserves') ) {
341 $item->{'course_reserves'} = GetItemCourseReservesInfo( itemnumber => $item->{'itemnumber'} );
344 if ( C4::Context->preference('IndependentBranches') ) {
345 my $userenv = C4::Context->userenv();
346 if ( not C4::Context->IsSuperLibrarian()
347 and $userenv->{branch} ne $item->{homebranch} ) {
348 $item->{cannot_be_edited} = 1;
352 if ($currentbranch and $currentbranch ne "NO_LIBRARY_SET"
353 and C4::Context->preference('SeparateHoldings')) {
354 if ($itembranchcode and $itembranchcode eq $currentbranch) {
355 push @itemloop, $item;
356 } else {
357 push @otheritemloop, $item;
359 } else {
360 push @itemloop, $item;
364 # Display only one tab if one items list is empty
365 if (scalar(@itemloop) == 0 || scalar(@otheritemloop) == 0) {
366 $template->param(SeparateHoldings => 0);
367 if (scalar(@itemloop) == 0) {
368 @itemloop = @otheritemloop;
372 $template->param( norequests => $norequests );
373 $template->param(
374 MARCNOTES => $marcnotesarray,
375 MARCSUBJCTS => $marcsubjctsarray,
376 MARCAUTHORS => $marcauthorsarray,
377 MARCSERIES => $marcseriesarray,
378 MARCURLS => $marcurlsarray,
379 MARCISBNS => $marcisbnsarray,
380 MARCHOSTS => $marchostsarray,
381 itemdata_ccode => $itemfields{ccode},
382 itemdata_enumchron => $itemfields{enumchron},
383 itemdata_uri => $itemfields{uri},
384 itemdata_copynumber => $itemfields{copynumber},
385 itemdata_stocknumber => $itemfields{stocknumber},
386 volinfo => $itemfields{enumchron},
387 itemdata_itemnotes => $itemfields{itemnotes},
388 itemdata_nonpublicnotes => $itemfields{itemnotes_nonpublic},
389 z3950_search_params => C4::Search::z3950_search_args($dat),
390 hostrecords => $hostrecords,
391 analytics_flag => $analytics_flag,
392 C4::Search::enabled_staff_search_views,
393 materials => $materials_flag,
396 if (C4::Context->preference("AlternateHoldingsField") && scalar @items == 0) {
397 my $fieldspec = C4::Context->preference("AlternateHoldingsField");
398 my $subfields = substr $fieldspec, 3;
399 my $holdingsep = C4::Context->preference("AlternateHoldingsSeparator") || ' ';
400 my @alternateholdingsinfo = ();
401 my @holdingsfields = $record->field(substr $fieldspec, 0, 3);
403 for my $field (@holdingsfields) {
404 my %holding = ( holding => '' );
405 my $havesubfield = 0;
406 for my $subfield ($field->subfields()) {
407 if ((index $subfields, $$subfield[0]) >= 0) {
408 $holding{'holding'} .= $holdingsep if (length $holding{'holding'} > 0);
409 $holding{'holding'} .= $$subfield[1];
410 $havesubfield++;
413 if ($havesubfield) {
414 push(@alternateholdingsinfo, \%holding);
418 $template->param(
419 ALTERNATEHOLDINGS => \@alternateholdingsinfo,
423 my @results = ( $dat, );
424 foreach ( keys %{$dat} ) {
425 $template->param( "$_" => defined $dat->{$_} ? $dat->{$_} : '' );
428 # does not work: my %views_enabled = map { $_ => 1 } $template->query(loop => 'EnableViews');
429 # method query not found?!?!
430 $template->param( AmazonTld => get_amazon_tld() ) if ( C4::Context->preference("AmazonCoverImages"));
431 $template->param(
432 itemloop => \@itemloop,
433 otheritemloop => \@otheritemloop,
434 biblionumber => $biblionumber,
435 ($analyze? 'analyze':'detailview') =>1,
436 subscriptions => \@subs,
437 subscriptionsnumber => $subscriptionsnumber,
438 subscriptiontitle => $dat->{title},
439 searchid => scalar $query->param('searchid'),
442 # $debug and $template->param(debug_display => 1);
444 # Lists
446 if (C4::Context->preference("virtualshelves") ) {
447 my $shelves = Koha::Virtualshelves->search(
449 biblionumber => $biblionumber,
450 category => 2,
453 join => 'virtualshelfcontents',
456 $template->param( 'shelves' => $shelves );
459 # XISBN Stuff
460 if (C4::Context->preference("FRBRizeEditions")==1) {
461 eval {
462 $template->param(
463 XISBNS => scalar get_xisbns($isbn, $biblionumber)
466 if ($@) { warn "XISBN Failed $@"; }
469 if ( C4::Context->preference("LocalCoverImages") == 1 ) {
470 my @images = ListImagesForBiblio($biblionumber);
471 $template->{VARS}->{localimages} = \@images;
474 # HTML5 Media
475 if ( (C4::Context->preference("HTML5MediaEnabled") eq 'both') or (C4::Context->preference("HTML5MediaEnabled") eq 'staff') ) {
476 $template->param( C4::HTML5Media->gethtml5media($record));
479 # Displaying tags
481 my $tag_quantity;
482 if (C4::Context->preference('TagsEnabled') and $tag_quantity = C4::Context->preference('TagsShowOnDetail')) {
483 $template->param(
484 TagsEnabled => 1,
485 TagsShowOnDetail => $tag_quantity
487 $template->param(TagLoop => get_tags({biblionumber=>$biblionumber, approved=>1,
488 'sort'=>'-weight', limit=>$tag_quantity}));
491 #we only need to pass the number of holds to the template
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;