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>.
23 use C4::Acquisition qw( GetHistory );
27 use C4::Serials; #uses getsubscriptionfrom biblionumber
34 use C4::XISBN qw(get_xisbns);
35 use C4::External::Amazon;
36 use C4::Search; # enabled_staff_search_views
37 use C4::Tags qw(get_tags);
42 use C4::CourseReserves qw(GetItemCourseReservesInfo);
43 use C4::Acquisition qw(GetOrdersByBiblionumber);
44 use Koha::AuthorisedValues;
49 use Koha::Virtualshelves;
52 my $query = CGI->new();
54 my $analyze = $query->param('analyze');
56 my ( $template, $borrowernumber, $cookie, $flags ) = get_template_and_user(
58 template_name => 'catalogue/detail.tt',
62 flagsrequired => { catalogue => 1 },
66 # Determine if we should be offering any enhancement plugin buttons
67 if ( C4::Context->preference('UseKohaPlugins') &&
68 C4::Context->config('enable_plugins') ) {
69 my @plugins = Koha::Plugins->new()->GetPlugins({
70 method => 'intranet_catalog_biblio_enhancements'
72 # Only pass plugins that can offer a toolbar button
73 @plugins = grep { $_->get_toolbar_button } @plugins;
79 my $biblionumber = $query->param('biblionumber');
80 $biblionumber = HTML::Entities::encode($biblionumber);
81 my $record = GetMarcBiblio({ biblionumber => $biblionumber });
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;
91 if($query->cookie("holdfor")){
92 my $holdfor_patron = Koha::Patrons->find( $query->cookie("holdfor") );
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');
108 my $lang = $xslfile ? C4::Languages::getlanguage() : undef;
109 my $sysxml = $xslfile ? C4::XSLT::get_xslt_sysprefs() : undef;
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 => GetCOinSBiblio($record) );
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);
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);
146 my $subtitle = GetRecordValue('subtitle', $record, $fw);
148 my $itemtypes = { map { $_->{itemtype} => $_ } @{ Koha::ItemTypes->search->unblessed } };
150 my $dbh = C4::Context->dbh;
152 my @all_items = GetItemsInfo( $biblionumber );
154 my $patron = Koha::Patrons->find( $borrowernumber );
155 for my $itm (@all_items) {
156 push @items, $itm unless ( $itm->{itemlost} && $patron->category->hidelostitems && !$showallitems);
159 # flag indicating existence of at least one item linked via a host record
161 # adding items linked via host biblios
162 my @hostitems = GetHostItemsInfo($record);
165 push (@items,@hostitems);
168 my $dat = &GetBiblioData($biblionumber);
170 #coping with subscriptions
171 my $subscriptionsnumber = CountSubscriptionFromBiblionumber($biblionumber);
172 my @subscriptions = SearchSubscriptions({ biblionumber => $biblionumber, orderby => 'title' });
175 foreach my $subscription (@subscriptions) {
177 my $serials_to_display;
178 $cell{subscriptionid} = $subscription->{subscriptionid};
179 $cell{subscriptionnotes} = $subscription->{internalnotes};
180 $cell{missinglist} = $subscription->{missinglist};
181 $cell{librariannote} = $subscription->{librariannote};
182 $cell{branchcode} = $subscription->{branchcode};
183 $cell{hasalert} = $subscription->{hasalert};
184 $cell{callnumber} = $subscription->{callnumber};
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 );
196 # Get acquisition details
197 if ( C4::Context->preference('AcquisitionDetails') ) {
198 my $orders = C4::Acquisition::GetHistory( biblionumber => $biblionumber, get_canceled_order => 1 );
204 if ( defined $dat->{'itemtype'} ) {
205 $dat->{imageurl} = getitemtypeimagelocation( 'intranet', $itemtypes->{ $dat->{itemtype} }{imageurl} );
208 $dat->{'count'} = scalar @all_items + @hostitems;
209 $dat->{'showncount'} = scalar @items + @hostitems;
210 $dat->{'hiddencount'} = scalar @all_items + @hostitems - scalar @items;
213 { map { $_->{authorised_value} => $_->{lib} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => $fw, kohafield => 'items.location' } ) };
215 { map { $_->{authorised_value} => $_->{lib} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => $fw, kohafield => 'items.ccode' } ) };
217 { map { $_->{authorised_value} => $_->{lib} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => $fw, kohafield => 'items.copynumber' } ) };
218 my (@itemloop, @otheritemloop, %itemfields);
221 my $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw, kohafield => 'items.itemlost', authorised_value => [ -and => {'!=' => undef }, {'!=' => ''}] });
223 $template->param( itemlostloop => GetAuthorisedValues( $mss->next->authorised_value ) );
225 $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw, kohafield => 'items.damaged', authorised_value => [ -and => {'!=' => undef }, {'!=' => ''}] });
227 $template->param( itemdamagedloop => GetAuthorisedValues( $mss->next->authorised_value ) );
229 $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw, kohafield => 'items.withdrawn', authorised_value => { not => undef } });
231 $template->param( itemwithdrawnloop => GetAuthorisedValues( $mss->next->authorised_value) );
234 $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw, kohafield => 'items.materials', authorised_value => [ -and => {'!=' => undef }, {'!=' => ''}] });
237 my $materials_authvals = GetAuthorisedValues($mss->next->authorised_value);
238 if ($materials_authvals) {
239 foreach my $value (@$materials_authvals) {
240 $materials_map{$value->{authorised_value}} = $value->{lib};
246 my $materials_flag; # set this if the items have anything in the materials field
247 my $currentbranch = C4::Context->userenv ? C4::Context->userenv->{branch} : undef;
248 if ($currentbranch and C4::Context->preference('SeparateHoldings')) {
249 $template->param(SeparateHoldings => 1);
251 my $separatebranch = C4::Context->preference('SeparateHoldingsBranch') || 'homebranch';
252 foreach my $item (@items) {
253 my $itembranchcode = $item->{$separatebranch};
255 # can place holds defaults to yes
256 $norequests = 0 unless ( ( $item->{'notforloan'} > 0 ) || ( $item->{'itemnotforloan'} > 0 ) );
258 $item->{imageurl} = defined $item->{itype} ? getitemtypeimagelocation('intranet', $itemtypes->{ $item->{itype} }{imageurl})
261 $item->{datedue} = format_sqldatetime($item->{datedue});
263 #get shelf location and collection code description if they are authorised value.
264 # same thing for copy number
265 my $shelfcode = $item->{'location'};
266 $item->{'location'} = $shelflocations->{$shelfcode} if ( defined( $shelfcode ) && defined($shelflocations) && exists( $shelflocations->{$shelfcode} ) );
267 my $ccode = $item->{'ccode'};
268 $item->{'ccode'} = $collections->{$ccode} if ( defined( $ccode ) && defined($collections) && exists( $collections->{$ccode} ) );
269 my $copynumber = $item->{'copynumber'};
270 $item->{'copynumber'} = $copynumbers->{$copynumber} if ( defined($copynumber) && defined($copynumbers) && exists( $copynumbers->{$copynumber} ) );
271 foreach (qw(ccode enumchron copynumber stocknumber itemnotes itemnotes_nonpublic uri)) {
272 $itemfields{$_} = 1 if ( $item->{$_} );
276 my $item_object = Koha::Items->find( $item->{itemnumber} );
277 my $holds = $item_object->current_holds;
278 if ( my $first_hold = $holds->next ) {
279 my $patron = Koha::Patrons->find( $first_hold->borrowernumber );
280 $item->{backgroundcolor} = 'reserved';
281 $item->{reservedate} = $first_hold->reservedate;
282 $item->{ReservedFor} = $patron,
283 $item->{ExpectedAtLibrary} = $first_hold->branchcode;
284 # Check waiting status
285 $item->{waitingdate} = $first_hold->waitingdate;
288 if ( my $checkout = $item_object->checkout ) {
289 $item->{CheckedOutFor} = $checkout->patron;
292 # Check the transit status
293 my ( $transfertwhen, $transfertfrom, $transfertto ) = GetTransfers($item->{itemnumber});
294 if ( defined( $transfertwhen ) && ( $transfertwhen ne '' ) ) {
295 $item->{transfertwhen} = $transfertwhen;
296 $item->{transfertfrom} = $transfertfrom;
297 $item->{transfertto} = $transfertto;
298 $item->{nocancel} = 1;
301 foreach my $f (qw( itemnotes )) {
303 $item->{$f} =~ s|\n|<br />|g;
308 #item has a host number if its biblio number does not match the current bib
310 if ($item->{biblionumber} ne $biblionumber){
311 $item->{hostbiblionumber} = $item->{biblionumber};
312 $item->{hosttitle} = GetBiblioData($item->{biblionumber})->{title};
317 # count if item is used in analytical bibliorecords
318 # The 'countanalytics' flag is only used in the templates if analyze is set
319 my $countanalytics = C4::Context->preference('EasyAnalyticalRecords') ? GetAnalyticsCount($item->{itemnumber}) : 0;
320 if ($countanalytics > 0){
322 $item->{countanalytics} = $countanalytics;
326 if (defined($item->{'materials'}) && $item->{'materials'} =~ /\S/){
328 if (defined $materials_map{ $item->{materials} }) {
329 $item->{materials} = $materials_map{ $item->{materials} };
333 if ( C4::Context->preference('UseCourseReserves') ) {
334 $item->{'course_reserves'} = GetItemCourseReservesInfo( itemnumber => $item->{'itemnumber'} );
337 if ( C4::Context->preference('IndependentBranches') ) {
338 my $userenv = C4::Context->userenv();
339 if ( not C4::Context->IsSuperLibrarian()
340 and $userenv->{branch} ne $item->{homebranch} ) {
341 $item->{cannot_be_edited} = 1;
345 if ($currentbranch and $currentbranch ne "NO_LIBRARY_SET"
346 and C4::Context->preference('SeparateHoldings')) {
347 if ($itembranchcode and $itembranchcode eq $currentbranch) {
348 push @itemloop, $item;
350 push @otheritemloop, $item;
353 push @itemloop, $item;
357 # Display only one tab if one items list is empty
358 if (scalar(@itemloop) == 0 || scalar(@otheritemloop) == 0) {
359 $template->param(SeparateHoldings => 0);
360 if (scalar(@itemloop) == 0) {
361 @itemloop = @otheritemloop;
365 $template->param( norequests => $norequests );
367 MARCNOTES => $marcnotesarray,
368 MARCSUBJCTS => $marcsubjctsarray,
369 MARCAUTHORS => $marcauthorsarray,
370 MARCSERIES => $marcseriesarray,
371 MARCURLS => $marcurlsarray,
372 MARCISBNS => $marcisbnsarray,
373 MARCHOSTS => $marchostsarray,
374 subtitle => $subtitle,
375 itemdata_ccode => $itemfields{ccode},
376 itemdata_enumchron => $itemfields{enumchron},
377 itemdata_uri => $itemfields{uri},
378 itemdata_copynumber => $itemfields{copynumber},
379 itemdata_stocknumber => $itemfields{stocknumber},
380 volinfo => $itemfields{enumchron},
381 itemdata_itemnotes => $itemfields{itemnotes},
382 itemdata_nonpublicnotes => $itemfields{itemnotes_nonpublic},
383 z3950_search_params => C4::Search::z3950_search_args($dat),
384 hostrecords => $hostrecords,
385 analytics_flag => $analytics_flag,
386 C4::Search::enabled_staff_search_views,
387 materials => $materials_flag,
390 if (C4::Context->preference("AlternateHoldingsField") && scalar @items == 0) {
391 my $fieldspec = C4::Context->preference("AlternateHoldingsField");
392 my $subfields = substr $fieldspec, 3;
393 my $holdingsep = C4::Context->preference("AlternateHoldingsSeparator") || ' ';
394 my @alternateholdingsinfo = ();
395 my @holdingsfields = $record->field(substr $fieldspec, 0, 3);
397 for my $field (@holdingsfields) {
398 my %holding = ( holding => '' );
399 my $havesubfield = 0;
400 for my $subfield ($field->subfields()) {
401 if ((index $subfields, $$subfield[0]) >= 0) {
402 $holding{'holding'} .= $holdingsep if (length $holding{'holding'} > 0);
403 $holding{'holding'} .= $$subfield[1];
408 push(@alternateholdingsinfo, \%holding);
413 ALTERNATEHOLDINGS => \@alternateholdingsinfo,
417 my @results = ( $dat, );
418 foreach ( keys %{$dat} ) {
419 $template->param( "$_" => defined $dat->{$_} ? $dat->{$_} : '' );
422 # does not work: my %views_enabled = map { $_ => 1 } $template->query(loop => 'EnableViews');
423 # method query not found?!?!
424 $template->param( AmazonTld => get_amazon_tld() ) if ( C4::Context->preference("AmazonCoverImages"));
426 itemloop => \@itemloop,
427 otheritemloop => \@otheritemloop,
428 biblionumber => $biblionumber,
429 ($analyze? 'analyze':'detailview') =>1,
430 subscriptions => \@subs,
431 subscriptionsnumber => $subscriptionsnumber,
432 subscriptiontitle => $dat->{title},
433 searchid => scalar $query->param('searchid'),
436 # $debug and $template->param(debug_display => 1);
440 if (C4::Context->preference("virtualshelves") ) {
441 my $shelves = Koha::Virtualshelves->search(
443 biblionumber => $biblionumber,
447 join => 'virtualshelfcontents',
450 $template->param( 'shelves' => $shelves );
454 if (C4::Context->preference("FRBRizeEditions")==1) {
457 XISBNS => scalar get_xisbns($isbn)
460 if ($@) { warn "XISBN Failed $@"; }
463 if ( C4::Context->preference("LocalCoverImages") == 1 ) {
464 my @images = ListImagesForBiblio($biblionumber);
465 $template->{VARS}->{localimages} = \@images;
469 if ( (C4::Context->preference("HTML5MediaEnabled") eq 'both') or (C4::Context->preference("HTML5MediaEnabled") eq 'staff') ) {
470 $template->param( C4::HTML5Media->gethtml5media($record));
476 if (C4::Context->preference('TagsEnabled') and $tag_quantity = C4::Context->preference('TagsShowOnDetail')) {
479 TagsShowOnDetail => $tag_quantity
481 $template->param(TagLoop => get_tags({biblionumber=>$biblionumber, approved=>1,
482 'sort'=>'-weight', limit=>$tag_quantity}));
485 #we only need to pass the number of holds to the template
486 my $biblio = Koha::Biblios->find( $biblionumber );
487 my $holds = $biblio->holds;
488 $template->param( holdcount => $holds->count );
490 my $StaffDetailItemSelection = C4::Context->preference('StaffDetailItemSelection');
491 if ($StaffDetailItemSelection) {
492 # Only enable item selection if user can execute at least one action
494 $flags->{superlibrarian}
496 ref $flags->{tools} eq 'HASH' && (
497 $flags->{tools}->{items_batchmod} # Modify selected items
498 || $flags->{tools}->{items_batchdel} # Delete selected items
501 || ( ref $flags->{tools} eq '' && $flags->{tools} )
505 StaffDetailItemSelection => $StaffDetailItemSelection );
509 my @allorders_using_biblio = GetOrdersByBiblionumber ($biblionumber);
510 my @deletedorders_using_biblio;
511 my @orders_using_biblio;
513 my @baskets_deletedorders;
515 foreach my $myorder (@allorders_using_biblio) {
516 my $basket = $myorder->{'basketno'};
517 if ((defined $myorder->{'datecancellationprinted'}) and ($myorder->{'datecancellationprinted'} ne '0000-00-00') ){
518 push @deletedorders_using_biblio, $myorder;
519 unless (grep(/^$basket$/, @baskets_deletedorders)){
520 push @baskets_deletedorders,$myorder->{'basketno'};
524 push @orders_using_biblio, $myorder;
525 unless (grep(/^$basket$/, @baskets_orders)){
526 push @baskets_orders,$myorder->{'basketno'};
531 my $count_orders_using_biblio = scalar @orders_using_biblio ;
532 $template->param (countorders => $count_orders_using_biblio);
534 my $count_deletedorders_using_biblio = scalar @deletedorders_using_biblio ;
535 $template->param (countdeletedorders => $count_deletedorders_using_biblio);
537 $template->param (basketsorders => \@baskets_orders);
538 $template->param (basketsdeletedorders => \@baskets_deletedorders);
540 output_html_with_http_headers $query, $cookie, $template->output;