Bug 14788: Add unit tests for GetTopIssues
[koha.git] / catalogue / detail.pl
blob30ce2c2243170bcd6e66fc9568e7d6baa215d1b9
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 strict;
20 use warnings;
22 use CGI qw ( -utf8 );
23 use C4::Acquisition qw( GetHistory );
24 use C4::Auth;
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::Branch;
32 use C4::Reserves;
33 use C4::Members; # to use GetMember
34 use C4::Serials;
35 use C4::XISBN qw(get_xisbns get_biblionumber_from_isbn);
36 use C4::External::Amazon;
37 use C4::Search; # enabled_staff_search_views
38 use C4::Tags qw(get_tags);
39 use C4::VirtualShelves;
40 use C4::XSLT;
41 use C4::Images;
42 use Koha::DateUtils;
43 use C4::HTML5Media;
44 use C4::CourseReserves qw(GetItemCourseReservesInfo);
45 use C4::Acquisition qw(GetOrdersByBiblionumber);
47 my $query = CGI->new();
49 my $analyze = $query->param('analyze');
51 my ( $template, $borrowernumber, $cookie, $flags ) = get_template_and_user(
53 template_name => 'catalogue/detail.tt',
54 query => $query,
55 type => "intranet",
56 authnotrequired => 0,
57 flagsrequired => { catalogue => 1 },
61 my $biblionumber = $query->param('biblionumber');
62 my $record = GetMarcBiblio($biblionumber);
64 if ( not defined $record ) {
65 # biblionumber invalid -> report and exit
66 $template->param( unknownbiblionumber => 1,
67 biblionumber => $biblionumber );
68 output_html_with_http_headers $query, $cookie, $template->output;
69 exit;
72 if($query->cookie("holdfor")){
73 my $holdfor_patron = GetMember('borrowernumber' => $query->cookie("holdfor"));
74 $template->param(
75 holdfor => $query->cookie("holdfor"),
76 holdfor_surname => $holdfor_patron->{'surname'},
77 holdfor_firstname => $holdfor_patron->{'firstname'},
78 holdfor_cardnumber => $holdfor_patron->{'cardnumber'},
82 my $fw = GetFrameworkCode($biblionumber);
83 my $showallitems = $query->param('showallitems');
84 my $marcflavour = C4::Context->preference("marcflavour");
86 # XSLT processing of some stuff
87 if (C4::Context->preference("XSLTDetailsDisplay") ) {
88 $template->param('XSLTDetailsDisplay' =>'1',
89 'XSLTBloc' => XSLTParse4Display($biblionumber, $record, "XSLTDetailsDisplay") );
92 $template->param( 'SpineLabelShowPrintOnBibDetails' => C4::Context->preference("SpineLabelShowPrintOnBibDetails") );
93 $template->param( ocoins => GetCOinSBiblio($record) );
95 # some useful variables for enhanced content;
96 # in each case, we're grabbing the first value we find in
97 # the record and normalizing it
98 my $upc = GetNormalizedUPC($record,$marcflavour);
99 my $ean = GetNormalizedEAN($record,$marcflavour);
100 my $oclc = GetNormalizedOCLCNumber($record,$marcflavour);
101 my $isbn = GetNormalizedISBN(undef,$record,$marcflavour);
103 $template->param(
104 normalized_upc => $upc,
105 normalized_ean => $ean,
106 normalized_oclc => $oclc,
107 normalized_isbn => $isbn,
110 my $marcnotesarray = GetMarcNotes( $record, $marcflavour );
111 my $marcisbnsarray = GetMarcISBN( $record, $marcflavour );
112 my $marcauthorsarray = GetMarcAuthors( $record, $marcflavour );
113 my $marcsubjctsarray = GetMarcSubjects( $record, $marcflavour );
114 my $marcseriesarray = GetMarcSeries($record,$marcflavour);
115 my $marcurlsarray = GetMarcUrls ($record,$marcflavour);
116 my $marchostsarray = GetMarcHosts($record,$marcflavour);
117 my $subtitle = GetRecordValue('subtitle', $record, $fw);
119 # Get Branches, Itemtypes and Locations
120 my $branches = GetBranches();
121 my $itemtypes = GetItemTypes();
122 my $dbh = C4::Context->dbh;
124 my @all_items = GetItemsInfo( $biblionumber );
125 my @items;
126 for my $itm (@all_items) {
127 push @items, $itm unless ( $itm->{itemlost} && GetHideLostItemsPreference($borrowernumber) && !$showallitems);
130 # flag indicating existence of at least one item linked via a host record
131 my $hostrecords;
132 # adding items linked via host biblios
133 my @hostitems = GetHostItemsInfo($record);
134 if (@hostitems){
135 $hostrecords =1;
136 push (@items,@hostitems);
139 my $dat = &GetBiblioData($biblionumber);
141 #coping with subscriptions
142 my $subscriptionsnumber = CountSubscriptionFromBiblionumber($biblionumber);
143 my @subscriptions = SearchSubscriptions({ biblionumber => $biblionumber, orderby => 'title' });
144 my @subs;
146 foreach my $subscription (@subscriptions) {
147 my %cell;
148 my $serials_to_display;
149 $cell{subscriptionid} = $subscription->{subscriptionid};
150 $cell{subscriptionnotes} = $subscription->{internalnotes};
151 $cell{missinglist} = $subscription->{missinglist};
152 $cell{librariannote} = $subscription->{librariannote};
153 $cell{branchcode} = $subscription->{branchcode};
154 $cell{branchname} = GetBranchName($subscription->{branchcode});
155 $cell{hasalert} = $subscription->{hasalert};
156 $cell{callnumber} = $subscription->{callnumber};
157 $cell{closed} = $subscription->{closed};
158 #get the three latest serials.
159 $serials_to_display = $subscription->{staffdisplaycount};
160 $serials_to_display = C4::Context->preference('StaffSerialIssueDisplayCount') unless $serials_to_display;
161 $cell{staffdisplaycount} = $serials_to_display;
162 $cell{latestserials} =
163 GetLatestSerials( $subscription->{subscriptionid}, $serials_to_display );
164 push @subs, \%cell;
168 # Get acquisition details
169 if ( C4::Context->preference('AcquisitionDetails') ) {
170 my $orders = C4::Acquisition::GetHistory( biblionumber => $biblionumber, get_canceled_order => 1 );
171 $template->param(
172 orders => $orders,
176 if ( defined $dat->{'itemtype'} ) {
177 $dat->{imageurl} = getitemtypeimagelocation( 'intranet', $itemtypes->{ $dat->{itemtype} }{imageurl} );
180 $dat->{'count'} = scalar @all_items + @hostitems;
181 $dat->{'showncount'} = scalar @items + @hostitems;
182 $dat->{'hiddencount'} = scalar @all_items + @hostitems - scalar @items;
184 my $shelflocations = GetKohaAuthorisedValues('items.location', $fw);
185 my $collections = GetKohaAuthorisedValues('items.ccode' , $fw);
186 my $copynumbers = GetKohaAuthorisedValues('items.copynumber', $fw);
187 my (@itemloop, @otheritemloop, %itemfields);
188 my $norequests = 1;
189 my $authvalcode_items_itemlost = GetAuthValCode('items.itemlost',$fw);
190 my $authvalcode_items_damaged = GetAuthValCode('items.damaged', $fw);
192 my $analytics_flag;
193 my $materials_flag; # set this if the items have anything in the materials field
194 my $currentbranch = C4::Context->userenv ? C4::Context->userenv->{branch} : undef;
195 if ($currentbranch and C4::Context->preference('SeparateHoldings')) {
196 $template->param(SeparateHoldings => 1);
198 my $separatebranch = C4::Context->preference('SeparateHoldingsBranch') || 'homebranch';
199 foreach my $item (@items) {
200 my $itembranchcode = $item->{$separatebranch};
202 # can place holds defaults to yes
203 $norequests = 0 unless ( ( $item->{'notforloan'} > 0 ) || ( $item->{'itemnotforloan'} > 0 ) );
205 $item->{imageurl} = defined $item->{itype} ? getitemtypeimagelocation('intranet', $itemtypes->{ $item->{itype} }{imageurl})
206 : '';
208 $item->{datedue} = format_sqldatetime($item->{datedue});
209 # item damaged, lost, withdrawn loops
210 $item->{itemlostloop} = GetAuthorisedValues($authvalcode_items_itemlost, $item->{itemlost}) if $authvalcode_items_itemlost;
211 if ($item->{damaged}) {
212 $item->{itemdamagedloop} = GetAuthorisedValues($authvalcode_items_damaged, $item->{damaged}) if $authvalcode_items_damaged;
214 #get shelf location and collection code description if they are authorised value.
215 # same thing for copy number
216 my $shelfcode = $item->{'location'};
217 $item->{'location'} = $shelflocations->{$shelfcode} if ( defined( $shelfcode ) && defined($shelflocations) && exists( $shelflocations->{$shelfcode} ) );
218 my $ccode = $item->{'ccode'};
219 $item->{'ccode'} = $collections->{$ccode} if ( defined( $ccode ) && defined($collections) && exists( $collections->{$ccode} ) );
220 my $copynumber = $item->{'copynumber'};
221 $item->{'copynumber'} = $copynumbers->{$copynumber} if ( defined($copynumber) && defined($copynumbers) && exists( $copynumbers->{$copynumber} ) );
222 foreach (qw(ccode enumchron copynumber stocknumber itemnotes uri)) {
223 $itemfields{$_} = 1 if ( $item->{$_} );
226 # checking for holds
227 my ($reservedate,$reservedfor,$expectedAt,undef,$wait) = GetReservesFromItemnumber($item->{itemnumber});
228 my $ItemBorrowerReserveInfo = GetMemberDetails( $reservedfor, 0);
230 if (C4::Context->preference('HidePatronName')){
231 $item->{'hidepatronname'} = 1;
234 if ( defined $reservedate ) {
235 $item->{backgroundcolor} = 'reserved';
236 $item->{reservedate} = $reservedate;
237 $item->{ReservedForBorrowernumber} = $reservedfor;
238 $item->{ReservedForSurname} = $ItemBorrowerReserveInfo->{'surname'};
239 $item->{ReservedForFirstname} = $ItemBorrowerReserveInfo->{'firstname'};
240 $item->{ExpectedAtLibrary} = $branches->{$expectedAt}{branchname};
241 $item->{Reservedcardnumber} = $ItemBorrowerReserveInfo->{'cardnumber'};
242 # Check waiting status
243 $item->{waitingdate} = $wait;
247 # Check the transit status
248 my ( $transfertwhen, $transfertfrom, $transfertto ) = GetTransfers($item->{itemnumber});
249 if ( defined( $transfertwhen ) && ( $transfertwhen ne '' ) ) {
250 $item->{transfertwhen} = $transfertwhen;
251 $item->{transfertfrom} = $branches->{$transfertfrom}{branchname};
252 $item->{transfertto} = $branches->{$transfertto}{branchname};
253 $item->{nocancel} = 1;
256 # item has a host number if its biblio number does not match the current bib
257 if ($item->{biblionumber} ne $biblionumber){
258 $item->{hostbiblionumber} = $item->{biblionumber};
259 $item->{hosttitle} = GetBiblioData($item->{biblionumber})->{title};
262 #count if item is used in analytical bibliorecords
263 my $countanalytics= GetAnalyticsCount($item->{itemnumber});
264 if ($countanalytics > 0){
265 $analytics_flag=1;
266 $item->{countanalytics} = $countanalytics;
269 if (defined($item->{'materials'}) && $item->{'materials'} =~ /\S/){
270 $materials_flag = 1;
273 if ( C4::Context->preference('UseCourseReserves') ) {
274 $item->{'course_reserves'} = GetItemCourseReservesInfo( itemnumber => $item->{'itemnumber'} );
277 if ( C4::Context->preference('IndependentBranches') ) {
278 my $userenv = C4::Context->userenv();
279 if ( not C4::Context->IsSuperLibrarian()
280 and $userenv->{branch} ne $item->{homebranch} ) {
281 $item->{cannot_be_edited} = 1;
285 if ($currentbranch and $currentbranch ne "NO_LIBRARY_SET"
286 and C4::Context->preference('SeparateHoldings')) {
287 if ($itembranchcode and $itembranchcode eq $currentbranch) {
288 push @itemloop, $item;
289 } else {
290 push @otheritemloop, $item;
292 } else {
293 push @itemloop, $item;
297 # Display only one tab if one items list is empty
298 if (scalar(@itemloop) == 0 || scalar(@otheritemloop) == 0) {
299 $template->param(SeparateHoldings => 0);
300 if (scalar(@itemloop) == 0) {
301 @itemloop = @otheritemloop;
305 $template->param( norequests => $norequests );
306 $template->param(
307 MARCNOTES => $marcnotesarray,
308 MARCSUBJCTS => $marcsubjctsarray,
309 MARCAUTHORS => $marcauthorsarray,
310 MARCSERIES => $marcseriesarray,
311 MARCURLS => $marcurlsarray,
312 MARCISBNS => $marcisbnsarray,
313 MARCHOSTS => $marchostsarray,
314 subtitle => $subtitle,
315 itemdata_ccode => $itemfields{ccode},
316 itemdata_enumchron => $itemfields{enumchron},
317 itemdata_uri => $itemfields{uri},
318 itemdata_copynumber => $itemfields{copynumber},
319 itemdata_stocknumber => $itemfields{stocknumber},
320 volinfo => $itemfields{enumchron},
321 itemdata_itemnotes => $itemfields{itemnotes},
322 z3950_search_params => C4::Search::z3950_search_args($dat),
323 hostrecords => $hostrecords,
324 analytics_flag => $analytics_flag,
325 C4::Search::enabled_staff_search_views,
326 materials => $materials_flag,
329 if (C4::Context->preference("AlternateHoldingsField") && scalar @items == 0) {
330 my $fieldspec = C4::Context->preference("AlternateHoldingsField");
331 my $subfields = substr $fieldspec, 3;
332 my $holdingsep = C4::Context->preference("AlternateHoldingsSeparator") || ' ';
333 my @alternateholdingsinfo = ();
334 my @holdingsfields = $record->field(substr $fieldspec, 0, 3);
336 for my $field (@holdingsfields) {
337 my %holding = ( holding => '' );
338 my $havesubfield = 0;
339 for my $subfield ($field->subfields()) {
340 if ((index $subfields, $$subfield[0]) >= 0) {
341 $holding{'holding'} .= $holdingsep if (length $holding{'holding'} > 0);
342 $holding{'holding'} .= $$subfield[1];
343 $havesubfield++;
346 if ($havesubfield) {
347 push(@alternateholdingsinfo, \%holding);
351 $template->param(
352 ALTERNATEHOLDINGS => \@alternateholdingsinfo,
356 my @results = ( $dat, );
357 foreach ( keys %{$dat} ) {
358 $template->param( "$_" => defined $dat->{$_} ? $dat->{$_} : '' );
361 # does not work: my %views_enabled = map { $_ => 1 } $template->query(loop => 'EnableViews');
362 # method query not found?!?!
363 $template->param( AmazonTld => get_amazon_tld() ) if ( C4::Context->preference("AmazonCoverImages"));
364 $template->param(
365 itemloop => \@itemloop,
366 otheritemloop => \@otheritemloop,
367 biblionumber => $biblionumber,
368 ($analyze? 'analyze':'detailview') =>1,
369 subscriptions => \@subs,
370 subscriptionsnumber => $subscriptionsnumber,
371 subscriptiontitle => $dat->{title},
372 searchid => $query->param('searchid'),
375 # $debug and $template->param(debug_display => 1);
377 # Lists
379 if (C4::Context->preference("virtualshelves") ) {
380 $template->param( 'GetShelves' => GetBibliosShelves( $biblionumber ) );
383 # XISBN Stuff
384 if (C4::Context->preference("FRBRizeEditions")==1) {
385 eval {
386 $template->param(
387 XISBNS => get_xisbns($isbn)
390 if ($@) { warn "XISBN Failed $@"; }
393 if ( C4::Context->preference("LocalCoverImages") == 1 ) {
394 my @images = ListImagesForBiblio($biblionumber);
395 $template->{VARS}->{localimages} = \@images;
398 # HTML5 Media
399 if ( (C4::Context->preference("HTML5MediaEnabled") eq 'both') or (C4::Context->preference("HTML5MediaEnabled") eq 'staff') ) {
400 $template->param( C4::HTML5Media->gethtml5media($record));
403 # Displaying tags
405 my $tag_quantity;
406 if (C4::Context->preference('TagsEnabled') and $tag_quantity = C4::Context->preference('TagsShowOnDetail')) {
407 $template->param(
408 TagsEnabled => 1,
409 TagsShowOnDetail => $tag_quantity
411 $template->param(TagLoop => get_tags({biblionumber=>$biblionumber, approved=>1,
412 'sort'=>'-weight', limit=>$tag_quantity}));
415 #we only need to pass the number of holds to the template
416 my $holds = C4::Reserves::GetReservesFromBiblionumber({ biblionumber => $biblionumber, all_dates => 1 });
417 $template->param( holdcount => scalar ( @$holds ) );
419 my $StaffDetailItemSelection = C4::Context->preference('StaffDetailItemSelection');
420 if ($StaffDetailItemSelection) {
421 # Only enable item selection if user can execute at least one action
422 if (
423 $flags->{superlibrarian}
424 || (
425 ref $flags->{tools} eq 'HASH' && (
426 $flags->{tools}->{items_batchmod} # Modify selected items
427 || $flags->{tools}->{items_batchdel} # Delete selected items
430 || ( ref $flags->{tools} eq '' && $flags->{tools} )
433 $template->param(
434 StaffDetailItemSelection => $StaffDetailItemSelection );
438 my @allorders_using_biblio = GetOrdersByBiblionumber ($biblionumber);
439 my @deletedorders_using_biblio;
440 my @orders_using_biblio;
441 my @baskets_orders;
442 my @baskets_deletedorders;
444 foreach my $myorder (@allorders_using_biblio) {
445 my $basket = $myorder->{'basketno'};
446 if ((defined $myorder->{'datecancellationprinted'}) and ($myorder->{'datecancellationprinted'} ne '0000-00-00') ){
447 push @deletedorders_using_biblio, $myorder;
448 unless (grep(/^$basket$/, @baskets_deletedorders)){
449 push @baskets_deletedorders,$myorder->{'basketno'};
452 else {
453 push @orders_using_biblio, $myorder;
454 unless (grep(/^$basket$/, @baskets_orders)){
455 push @baskets_orders,$myorder->{'basketno'};
460 my $count_orders_using_biblio = scalar @orders_using_biblio ;
461 $template->param (countorders => $count_orders_using_biblio);
463 my $count_deletedorders_using_biblio = scalar @deletedorders_using_biblio ;
464 $template->param (countdeletedorders => $count_deletedorders_using_biblio);
466 $template->param (basketsorders => \@baskets_orders);
467 $template->param (basketsdeletedorders => \@baskets_deletedorders);
469 output_html_with_http_headers $query, $cookie, $template->output;