Bug 23050: Fix creation of biblio tab's id
[koha.git] / catalogue / detail.pl
blobd176f1088ed77506586c8196d67bd4de41d75736
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 });
73 my @tab_plugins = Koha::Plugins->new()->GetPlugins({
74 method => 'intranet_catalog_biblio_tab',
75 });
76 my @tabs;
77 foreach my $tab_plugin (@tab_plugins) {
78 my @biblio_tabs = $tab_plugin->intranet_catalog_biblio_tab();
79 foreach my $tab (@biblio_tabs) {
80 $tab->{id} = 'tab-' . $tab->{title};
81 $tab->{id} =~ s/[^0-9A-Za-z]+/-/g;
82 push @tabs, $tab,
86 $template->param(
87 plugins => \@plugins,
88 tabs => \@tabs,
92 my $biblionumber = $query->param('biblionumber');
93 $biblionumber = HTML::Entities::encode($biblionumber);
94 my $record = GetMarcBiblio({ biblionumber => $biblionumber });
95 my $biblio = Koha::Biblios->find( $biblionumber );
96 $template->param( 'biblio', $biblio );
98 if ( not defined $record ) {
99 # biblionumber invalid -> report and exit
100 $template->param( unknownbiblionumber => 1,
101 biblionumber => $biblionumber );
102 output_html_with_http_headers $query, $cookie, $template->output;
103 exit;
106 if($query->cookie("holdfor")){
107 my $holdfor_patron = Koha::Patrons->find( $query->cookie("holdfor") );
108 $template->param(
109 # FIXME Should pass the patron object
110 holdfor => $query->cookie("holdfor"),
111 holdfor_surname => $holdfor_patron->surname,
112 holdfor_firstname => $holdfor_patron->firstname,
113 holdfor_cardnumber => $holdfor_patron->cardnumber,
117 my $fw = GetFrameworkCode($biblionumber);
118 my $showallitems = $query->param('showallitems');
119 my $marcflavour = C4::Context->preference("marcflavour");
121 # XSLT processing of some stuff
122 my $xslfile = C4::Context->preference('XSLTDetailsDisplay') || "default";
123 my $lang = $xslfile ? C4::Languages::getlanguage() : undef;
124 my $sysxml = $xslfile ? C4::XSLT::get_xslt_sysprefs() : undef;
126 if ( $xslfile ) {
127 $template->param(
128 XSLTDetailsDisplay => '1',
129 XSLTBloc => XSLTParse4Display(
130 $biblionumber, $record, "XSLTDetailsDisplay",
131 1, undef, $sysxml, $xslfile, $lang
136 $template->param( 'SpineLabelShowPrintOnBibDetails' => C4::Context->preference("SpineLabelShowPrintOnBibDetails") );
137 $template->param( ocoins => $biblio->get_coins );
139 # some useful variables for enhanced content;
140 # in each case, we're grabbing the first value we find in
141 # the record and normalizing it
142 my $upc = GetNormalizedUPC($record,$marcflavour);
143 my $ean = GetNormalizedEAN($record,$marcflavour);
144 my $oclc = GetNormalizedOCLCNumber($record,$marcflavour);
145 my $isbn = GetNormalizedISBN(undef,$record,$marcflavour);
147 $template->param(
148 normalized_upc => $upc,
149 normalized_ean => $ean,
150 normalized_oclc => $oclc,
151 normalized_isbn => $isbn,
154 my $marcnotesarray = GetMarcNotes( $record, $marcflavour );
155 my $marcisbnsarray = GetMarcISBN( $record, $marcflavour );
156 my $marcauthorsarray = GetMarcAuthors( $record, $marcflavour );
157 my $marcsubjctsarray = GetMarcSubjects( $record, $marcflavour );
158 my $marcseriesarray = GetMarcSeries($record,$marcflavour);
159 my $marcurlsarray = GetMarcUrls ($record,$marcflavour);
160 my $marchostsarray = GetMarcHosts($record,$marcflavour);
162 my $itemtypes = { map { $_->{itemtype} => $_ } @{ Koha::ItemTypes->search->unblessed } };
164 my $dbh = C4::Context->dbh;
166 my @all_items = GetItemsInfo( $biblionumber );
167 my @items;
168 my $patron = Koha::Patrons->find( $borrowernumber );
169 for my $itm (@all_items) {
170 push @items, $itm unless ( $itm->{itemlost} && $patron->category->hidelostitems && !$showallitems);
173 # flag indicating existence of at least one item linked via a host record
174 my $hostrecords;
175 # adding items linked via host biblios
176 my @hostitems = GetHostItemsInfo($record);
177 if (@hostitems){
178 $hostrecords =1;
179 push (@items,@hostitems);
182 my $dat = &GetBiblioData($biblionumber);
184 #coping with subscriptions
185 my $subscriptionsnumber = CountSubscriptionFromBiblionumber($biblionumber);
186 my @subscriptions = SearchSubscriptions({ biblionumber => $biblionumber, orderby => 'title' });
187 my @subs;
189 foreach my $subscription (@subscriptions) {
190 my %cell;
191 my $serials_to_display;
192 $cell{subscriptionid} = $subscription->{subscriptionid};
193 $cell{subscriptionnotes} = $subscription->{internalnotes};
194 $cell{missinglist} = $subscription->{missinglist};
195 $cell{librariannote} = $subscription->{librariannote};
196 $cell{branchcode} = $subscription->{branchcode};
197 $cell{hasalert} = $subscription->{hasalert};
198 $cell{callnumber} = $subscription->{callnumber};
199 $cell{location} = $subscription->{location};
200 $cell{closed} = $subscription->{closed};
201 #get the three latest serials.
202 $serials_to_display = $subscription->{staffdisplaycount};
203 $serials_to_display = C4::Context->preference('StaffSerialIssueDisplayCount') unless $serials_to_display;
204 $cell{staffdisplaycount} = $serials_to_display;
205 $cell{latestserials} =
206 GetLatestSerials( $subscription->{subscriptionid}, $serials_to_display );
207 push @subs, \%cell;
211 # Get acquisition details
212 if ( C4::Context->preference('AcquisitionDetails') ) {
213 my $orders = Koha::Acquisition::Orders->search(
214 { biblionumber => $biblionumber },
216 join => 'basketno',
217 order_by => 'basketno.booksellerid'
219 ); # GetHistory sorted by aqbooksellerid, but does it make sense?
221 $template->param(
222 orders => $orders,
226 if ( defined $dat->{'itemtype'} ) {
227 $dat->{imageurl} = getitemtypeimagelocation( 'intranet', $itemtypes->{ $dat->{itemtype} }{imageurl} );
230 $dat->{'count'} = scalar @all_items + @hostitems;
231 $dat->{'showncount'} = scalar @items + @hostitems;
232 $dat->{'hiddencount'} = scalar @all_items + @hostitems - scalar @items;
234 my $shelflocations =
235 { map { $_->{authorised_value} => $_->{lib} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => $fw, kohafield => 'items.location' } ) };
236 my $collections =
237 { map { $_->{authorised_value} => $_->{lib} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => $fw, kohafield => 'items.ccode' } ) };
238 my $copynumbers =
239 { map { $_->{authorised_value} => $_->{lib} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => $fw, kohafield => 'items.copynumber' } ) };
240 my (@itemloop, @otheritemloop, %itemfields);
241 my $norequests = 1;
243 my $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw, kohafield => 'items.itemlost', authorised_value => [ -and => {'!=' => undef }, {'!=' => ''}] });
244 if ( $mss->count ) {
245 $template->param( itemlostloop => GetAuthorisedValues( $mss->next->authorised_value ) );
247 $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw, kohafield => 'items.damaged', authorised_value => [ -and => {'!=' => undef }, {'!=' => ''}] });
248 if ( $mss->count ) {
249 $template->param( itemdamagedloop => GetAuthorisedValues( $mss->next->authorised_value ) );
251 $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw, kohafield => 'items.withdrawn', authorised_value => { not => undef } });
252 if ( $mss->count ) {
253 $template->param( itemwithdrawnloop => GetAuthorisedValues( $mss->next->authorised_value) );
256 $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw, kohafield => 'items.materials', authorised_value => [ -and => {'!=' => undef }, {'!=' => ''}] });
257 my %materials_map;
258 if ($mss->count) {
259 my $materials_authvals = GetAuthorisedValues($mss->next->authorised_value);
260 if ($materials_authvals) {
261 foreach my $value (@$materials_authvals) {
262 $materials_map{$value->{authorised_value}} = $value->{lib};
267 my $analytics_flag;
268 my $materials_flag; # set this if the items have anything in the materials field
269 my $currentbranch = C4::Context->userenv ? C4::Context->userenv->{branch} : undef;
270 if ($currentbranch and C4::Context->preference('SeparateHoldings')) {
271 $template->param(SeparateHoldings => 1);
273 my $separatebranch = C4::Context->preference('SeparateHoldingsBranch') || 'homebranch';
274 foreach my $item (@items) {
275 my $itembranchcode = $item->{$separatebranch};
277 # can place holds defaults to yes
278 $norequests = 0 unless ( ( $item->{'notforloan'} > 0 ) || ( $item->{'itemnotforloan'} > 0 ) );
280 $item->{imageurl} = defined $item->{itype} ? getitemtypeimagelocation('intranet', $itemtypes->{ $item->{itype} }{imageurl})
281 : '';
283 $item->{datedue} = format_sqldatetime($item->{datedue});
285 #get shelf location and collection code description if they are authorised value.
286 # same thing for copy number
287 my $shelfcode = $item->{'location'};
288 $item->{'location'} = $shelflocations->{$shelfcode} if ( defined( $shelfcode ) && defined($shelflocations) && exists( $shelflocations->{$shelfcode} ) );
289 my $ccode = $item->{'ccode'};
290 $item->{'ccode'} = $collections->{$ccode} if ( defined( $ccode ) && defined($collections) && exists( $collections->{$ccode} ) );
291 my $copynumber = $item->{'copynumber'};
292 $item->{'copynumber'} = $copynumbers->{$copynumber} if ( defined($copynumber) && defined($copynumbers) && exists( $copynumbers->{$copynumber} ) );
293 foreach (qw(ccode enumchron copynumber stocknumber itemnotes itemnotes_nonpublic uri)) {
294 $itemfields{$_} = 1 if ( $item->{$_} );
297 # checking for holds
298 my $item_object = Koha::Items->find( $item->{itemnumber} );
299 my $holds = $item_object->current_holds;
300 if ( my $first_hold = $holds->next ) {
301 my $patron = Koha::Patrons->find( $first_hold->borrowernumber );
302 $item->{backgroundcolor} = 'reserved';
303 $item->{reservedate} = $first_hold->reservedate;
304 $item->{ReservedFor} = $patron,
305 $item->{ExpectedAtLibrary} = $first_hold->branchcode;
306 # Check waiting status
307 $item->{waitingdate} = $first_hold->waitingdate;
310 if ( my $checkout = $item_object->checkout ) {
311 $item->{CheckedOutFor} = $checkout->patron;
314 # Check the transit status
315 my ( $transfertwhen, $transfertfrom, $transfertto ) = GetTransfers($item->{itemnumber});
316 if ( defined( $transfertwhen ) && ( $transfertwhen ne '' ) ) {
317 $item->{transfertwhen} = $transfertwhen;
318 $item->{transfertfrom} = $transfertfrom;
319 $item->{transfertto} = $transfertto;
320 $item->{nocancel} = 1;
323 foreach my $f (qw( itemnotes )) {
324 if ($item->{$f}) {
325 $item->{$f} =~ s|\n|<br />|g;
326 $itemfields{$f} = 1;
330 #item has a host number if its biblio number does not match the current bib
332 if ($item->{biblionumber} ne $biblionumber){
333 $item->{hostbiblionumber} = $item->{biblionumber};
334 $item->{hosttitle} = GetBiblioData($item->{biblionumber})->{title};
338 if ( $analyze ) {
339 # count if item is used in analytical bibliorecords
340 # The 'countanalytics' flag is only used in the templates if analyze is set
341 my $countanalytics = C4::Context->preference('EasyAnalyticalRecords') ? GetAnalyticsCount($item->{itemnumber}) : 0;
342 if ($countanalytics > 0){
343 $analytics_flag=1;
344 $item->{countanalytics} = $countanalytics;
348 if (defined($item->{'materials'}) && $item->{'materials'} =~ /\S/){
349 $materials_flag = 1;
350 if (defined $materials_map{ $item->{materials} }) {
351 $item->{materials} = $materials_map{ $item->{materials} };
355 if ( C4::Context->preference('UseCourseReserves') ) {
356 $item->{'course_reserves'} = GetItemCourseReservesInfo( itemnumber => $item->{'itemnumber'} );
359 if ( C4::Context->preference('IndependentBranches') ) {
360 my $userenv = C4::Context->userenv();
361 if ( not C4::Context->IsSuperLibrarian()
362 and $userenv->{branch} ne $item->{homebranch} ) {
363 $item->{cannot_be_edited} = 1;
367 if ($currentbranch and $currentbranch ne "NO_LIBRARY_SET"
368 and C4::Context->preference('SeparateHoldings')) {
369 if ($itembranchcode and $itembranchcode eq $currentbranch) {
370 push @itemloop, $item;
371 } else {
372 push @otheritemloop, $item;
374 } else {
375 push @itemloop, $item;
379 # Display only one tab if one items list is empty
380 if (scalar(@itemloop) == 0 || scalar(@otheritemloop) == 0) {
381 $template->param(SeparateHoldings => 0);
382 if (scalar(@itemloop) == 0) {
383 @itemloop = @otheritemloop;
387 $template->param( norequests => $norequests );
388 $template->param(
389 MARCNOTES => $marcnotesarray,
390 MARCSUBJCTS => $marcsubjctsarray,
391 MARCAUTHORS => $marcauthorsarray,
392 MARCSERIES => $marcseriesarray,
393 MARCURLS => $marcurlsarray,
394 MARCISBNS => $marcisbnsarray,
395 MARCHOSTS => $marchostsarray,
396 itemdata_ccode => $itemfields{ccode},
397 itemdata_enumchron => $itemfields{enumchron},
398 itemdata_uri => $itemfields{uri},
399 itemdata_copynumber => $itemfields{copynumber},
400 itemdata_stocknumber => $itemfields{stocknumber},
401 volinfo => $itemfields{enumchron},
402 itemdata_itemnotes => $itemfields{itemnotes},
403 itemdata_nonpublicnotes => $itemfields{itemnotes_nonpublic},
404 z3950_search_params => C4::Search::z3950_search_args($dat),
405 hostrecords => $hostrecords,
406 analytics_flag => $analytics_flag,
407 C4::Search::enabled_staff_search_views,
408 materials => $materials_flag,
411 if (C4::Context->preference("AlternateHoldingsField") && scalar @items == 0) {
412 my $fieldspec = C4::Context->preference("AlternateHoldingsField");
413 my $subfields = substr $fieldspec, 3;
414 my $holdingsep = C4::Context->preference("AlternateHoldingsSeparator") || ' ';
415 my @alternateholdingsinfo = ();
416 my @holdingsfields = $record->field(substr $fieldspec, 0, 3);
418 for my $field (@holdingsfields) {
419 my %holding = ( holding => '' );
420 my $havesubfield = 0;
421 for my $subfield ($field->subfields()) {
422 if ((index $subfields, $$subfield[0]) >= 0) {
423 $holding{'holding'} .= $holdingsep if (length $holding{'holding'} > 0);
424 $holding{'holding'} .= $$subfield[1];
425 $havesubfield++;
428 if ($havesubfield) {
429 push(@alternateholdingsinfo, \%holding);
433 $template->param(
434 ALTERNATEHOLDINGS => \@alternateholdingsinfo,
438 my @results = ( $dat, );
439 foreach ( keys %{$dat} ) {
440 $template->param( "$_" => defined $dat->{$_} ? $dat->{$_} : '' );
443 # does not work: my %views_enabled = map { $_ => 1 } $template->query(loop => 'EnableViews');
444 # method query not found?!?!
445 $template->param( AmazonTld => get_amazon_tld() ) if ( C4::Context->preference("AmazonCoverImages"));
446 $template->param(
447 itemloop => \@itemloop,
448 otheritemloop => \@otheritemloop,
449 biblionumber => $biblionumber,
450 ($analyze? 'analyze':'detailview') =>1,
451 subscriptions => \@subs,
452 subscriptionsnumber => $subscriptionsnumber,
453 subscriptiontitle => $dat->{title},
454 searchid => scalar $query->param('searchid'),
457 # $debug and $template->param(debug_display => 1);
459 # Lists
461 if (C4::Context->preference("virtualshelves") ) {
462 my $shelves = Koha::Virtualshelves->search(
464 biblionumber => $biblionumber,
465 category => 2,
468 join => 'virtualshelfcontents',
471 $template->param( 'shelves' => $shelves );
474 # XISBN Stuff
475 if (C4::Context->preference("FRBRizeEditions")==1) {
476 eval {
477 $template->param(
478 XISBNS => scalar get_xisbns($isbn, $biblionumber)
481 if ($@) { warn "XISBN Failed $@"; }
484 if ( C4::Context->preference("LocalCoverImages") == 1 ) {
485 my @images = ListImagesForBiblio($biblionumber);
486 $template->{VARS}->{localimages} = \@images;
489 # HTML5 Media
490 if ( (C4::Context->preference("HTML5MediaEnabled") eq 'both') or (C4::Context->preference("HTML5MediaEnabled") eq 'staff') ) {
491 $template->param( C4::HTML5Media->gethtml5media($record));
494 # Displaying tags
496 my $tag_quantity;
497 if (C4::Context->preference('TagsEnabled') and $tag_quantity = C4::Context->preference('TagsShowOnDetail')) {
498 $template->param(
499 TagsEnabled => 1,
500 TagsShowOnDetail => $tag_quantity
502 $template->param(TagLoop => get_tags({biblionumber=>$biblionumber, approved=>1,
503 'sort'=>'-weight', limit=>$tag_quantity}));
506 #we only need to pass the number of holds to the template
507 my $holds = $biblio->holds;
508 $template->param( holdcount => $holds->count );
510 my $StaffDetailItemSelection = C4::Context->preference('StaffDetailItemSelection');
511 if ($StaffDetailItemSelection) {
512 # Only enable item selection if user can execute at least one action
513 if (
514 $flags->{superlibrarian}
515 || (
516 ref $flags->{tools} eq 'HASH' && (
517 $flags->{tools}->{items_batchmod} # Modify selected items
518 || $flags->{tools}->{items_batchdel} # Delete selected items
521 || ( ref $flags->{tools} eq '' && $flags->{tools} )
524 $template->param(
525 StaffDetailItemSelection => $StaffDetailItemSelection );
529 my @allorders_using_biblio = GetOrdersByBiblionumber ($biblionumber);
530 my @deletedorders_using_biblio;
531 my @orders_using_biblio;
532 my @baskets_orders;
533 my @baskets_deletedorders;
535 foreach my $myorder (@allorders_using_biblio) {
536 my $basket = $myorder->{'basketno'};
537 if ((defined $myorder->{'datecancellationprinted'}) and ($myorder->{'datecancellationprinted'} ne '0000-00-00') ){
538 push @deletedorders_using_biblio, $myorder;
539 unless (grep(/^$basket$/, @baskets_deletedorders)){
540 push @baskets_deletedorders,$myorder->{'basketno'};
543 else {
544 push @orders_using_biblio, $myorder;
545 unless (grep(/^$basket$/, @baskets_orders)){
546 push @baskets_orders,$myorder->{'basketno'};
551 $template->param(biblio => $biblio);
553 my $count_orders_using_biblio = scalar @orders_using_biblio ;
554 $template->param (countorders => $count_orders_using_biblio);
556 my $count_deletedorders_using_biblio = scalar @deletedorders_using_biblio ;
557 $template->param (countdeletedorders => $count_deletedorders_using_biblio);
559 $template->param (basketsorders => \@baskets_orders);
560 $template->param (basketsdeletedorders => \@baskets_deletedorders);
562 output_html_with_http_headers $query, $cookie, $template->output;