Bug 9292 - Remove dead code related to 'publictype'
[koha.git] / catalogue / detail.pl
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 2 of the License, or (at your option) any later
8 # version.
9 #
10 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License along with
15 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
16 # Suite 330, Boston, MA  02111-1307 USA
17
18
19 use strict;
20 use warnings;
21
22 use CGI;
23 use C4::Auth;
24 use C4::Dates qw/format_date/;
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
45 # use Smart::Comments;
46
47 my $query = CGI->new();
48
49 my $analyze = $query->param('analyze');
50
51 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
52     {
53     template_name   =>  'catalogue/detail.tmpl',
54         query           => $query,
55         type            => "intranet",
56         authnotrequired => 0,
57         flagsrequired   => { catalogue => 1 },
58     }
59 );
60
61 my $biblionumber = $query->param('biblionumber');
62 my $record       = GetMarcBiblio($biblionumber);
63
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;
70 }
71
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'},
79     );
80 }
81
82 my $fw           = GetFrameworkCode($biblionumber);
83 my $showallitems = $query->param('showallitems');
84 my $marcflavour  = C4::Context->preference("marcflavour");
85
86 # XSLT processing of some stuff
87 if (C4::Context->preference("XSLTDetailsDisplay") ) {
88     $template->param('XSLTDetailsDisplay' =>'1',
89         'XSLTBloc' => XSLTParse4Display($biblionumber, $record, "XSLTDetailsDisplay") );
90 }
91
92 $template->param( 'SpineLabelShowPrintOnBibDetails' => C4::Context->preference("SpineLabelShowPrintOnBibDetails") );
93 $template->param( ocoins => GetCOinSBiblio($record) );
94
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);
102
103 $template->param(
104     normalized_upc => $upc,
105     normalized_ean => $ean,
106     normalized_oclc => $oclc,
107     normalized_isbn => $isbn,
108 );
109
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);
118
119 # Get Branches, Itemtypes and Locations
120 my $branches = GetBranches();
121 my $itemtypes = GetItemTypes();
122 my $dbh = C4::Context->dbh;
123
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);
128 }
129
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);
137 }
138
139 my $dat = &GetBiblioData($biblionumber);
140
141 # get count of holds
142 my ( $holdcount, $holds ) = GetReservesFromBiblionumber($biblionumber,1);
143
144 #coping with subscriptions
145 my $subscriptionsnumber = CountSubscriptionFromBiblionumber($biblionumber);
146 my @subscriptions       = GetSubscriptions( $dat->{title}, $dat->{issn}, undef, $biblionumber );
147 my @subs;
148
149 foreach my $subscription (@subscriptions) {
150     my %cell;
151         my $serials_to_display;
152     $cell{subscriptionid}    = $subscription->{subscriptionid};
153     $cell{subscriptionnotes} = $subscription->{internalnotes};
154     $cell{missinglist}       = $subscription->{missinglist};
155     $cell{librariannote}     = $subscription->{librariannote};
156     $cell{branchcode}        = $subscription->{branchcode};
157     $cell{branchname}        = GetBranchName($subscription->{branchcode});
158     $cell{hasalert}          = $subscription->{hasalert};
159     $cell{callnumber}        = $subscription->{callnumber};
160     $cell{closed}            = $subscription->{closed};
161     #get the three latest serials.
162         $serials_to_display = $subscription->{staffdisplaycount};
163         $serials_to_display = C4::Context->preference('StaffSerialIssueDisplayCount') unless $serials_to_display;
164         $cell{staffdisplaycount} = $serials_to_display;
165     $cell{latestserials} =
166       GetLatestSerials( $subscription->{subscriptionid}, $serials_to_display );
167     push @subs, \%cell;
168 }
169
170 if ( defined $dat->{'itemtype'} ) {
171     $dat->{imageurl} = getitemtypeimagelocation( 'intranet', $itemtypes->{ $dat->{itemtype} }{imageurl} );
172 }
173
174 $dat->{'count'} = scalar @all_items + @hostitems;
175 $dat->{'showncount'} = scalar @items + @hostitems;
176 $dat->{'hiddencount'} = scalar @all_items + @hostitems - scalar @items;
177
178 my $shelflocations = GetKohaAuthorisedValues('items.location', $fw);
179 my $collections    = GetKohaAuthorisedValues('items.ccode'   , $fw);
180 my $copynumbers    = GetKohaAuthorisedValues('items.copynumber', $fw);
181 my (@itemloop, @otheritemloop, %itemfields);
182 my $norequests = 1;
183 my $authvalcode_items_itemlost = GetAuthValCode('items.itemlost',$fw);
184 my $authvalcode_items_damaged  = GetAuthValCode('items.damaged', $fw);
185
186 my $analytics_flag;
187 my $materials_flag; # set this if the items have anything in the materials field
188 my $currentbranch = C4::Context->userenv ? C4::Context->userenv->{branch} : undef;
189 if ($currentbranch and C4::Context->preference('SeparateHoldings')) {
190     $template->param(SeparateHoldings => 1);
191 }
192 my $separatebranch = C4::Context->preference('SeparateHoldingsBranch') || 'homebranch';
193 foreach my $item (@items) {
194     my $itembranchcode = $item->{$separatebranch};
195     $item->{homebranch}        = GetBranchName($item->{homebranch});
196
197     # can place holds defaults to yes
198     $norequests = 0 unless ( ( $item->{'notforloan'} > 0 ) || ( $item->{'itemnotforloan'} > 0 ) );
199
200     $item->{imageurl} = defined $item->{itype} ? getitemtypeimagelocation('intranet', $itemtypes->{ $item->{itype} }{imageurl})
201                                                : '';
202
203         foreach (qw(datelastseen onloan)) {
204                 $item->{$_} = format_date($item->{$_});
205     }
206     $item->{datedue} = format_sqldatetime($item->{datedue});
207     # item damaged, lost, withdrawn loops
208     $item->{itemlostloop} = GetAuthorisedValues($authvalcode_items_itemlost, $item->{itemlost}) if $authvalcode_items_itemlost;
209     if ($item->{damaged}) {
210         $item->{itemdamagedloop} = GetAuthorisedValues($authvalcode_items_damaged, $item->{damaged}) if $authvalcode_items_damaged;
211     }
212     #get shelf location and collection code description if they are authorised value.
213     # same thing for copy number
214     my $shelfcode = $item->{'location'};
215     $item->{'location'} = $shelflocations->{$shelfcode} if ( defined( $shelfcode ) && defined($shelflocations) && exists( $shelflocations->{$shelfcode} ) );
216     my $ccode = $item->{'ccode'};
217     $item->{'ccode'} = $collections->{$ccode} if ( defined( $ccode ) && defined($collections) && exists( $collections->{$ccode} ) );
218     my $copynumber = $item->{'copynumber'};
219     $item->{'copynumber'} = $copynumbers->{$copynumber} if ( defined($copynumber) && defined($copynumbers) && exists( $copynumbers->{$copynumber} ) );
220     foreach (qw(ccode enumchron copynumber itemnotes uri)) {
221         $itemfields{$_} = 1 if ( $item->{$_} );
222     }
223
224     # checking for holds
225     my ($reservedate,$reservedfor,$expectedAt) = GetReservesFromItemnumber($item->{itemnumber});
226     my $ItemBorrowerReserveInfo = GetMemberDetails( $reservedfor, 0);
227     
228     if (C4::Context->preference('HidePatronName')){
229         $item->{'hidepatronname'} = 1;
230     }
231
232     if ( defined $reservedate ) {
233         $item->{backgroundcolor} = 'reserved';
234         $item->{reservedate}     = format_date($reservedate);
235         $item->{ReservedForBorrowernumber}     = $reservedfor;
236         $item->{ReservedForSurname}     = $ItemBorrowerReserveInfo->{'surname'};
237         $item->{ReservedForFirstname}   = $ItemBorrowerReserveInfo->{'firstname'};
238         $item->{ExpectedAtLibrary}      = $branches->{$expectedAt}{branchname};
239         $item->{Reservedcardnumber}             = $ItemBorrowerReserveInfo->{'cardnumber'};
240     }
241
242         # Check the transit status
243     my ( $transfertwhen, $transfertfrom, $transfertto ) = GetTransfers($item->{itemnumber});
244     if ( defined( $transfertwhen ) && ( $transfertwhen ne '' ) ) {
245         $item->{transfertwhen} = format_date($transfertwhen);
246         $item->{transfertfrom} = $branches->{$transfertfrom}{branchname};
247         $item->{transfertto}   = $branches->{$transfertto}{branchname};
248         $item->{nocancel} = 1;
249     }
250
251     # FIXME: move this to a pm, check waiting status for holds
252     my $sth2 = $dbh->prepare("SELECT * FROM reserves WHERE borrowernumber=? AND itemnumber=? AND found='W'");
253     $sth2->execute($item->{ReservedForBorrowernumber},$item->{itemnumber});
254     while (my $wait_hashref = $sth2->fetchrow_hashref) {
255         $item->{waitingdate} = format_date($wait_hashref->{waitingdate});
256     }
257
258     # item has a host number if its biblio number does not match the current bib
259     if ($item->{biblionumber} ne $biblionumber){
260         $item->{hostbiblionumber} = $item->{biblionumber};
261         $item->{hosttitle} = GetBiblioData($item->{biblionumber})->{title};
262     }
263         
264     #count if item is used in analytical bibliorecords
265     my $countanalytics= GetAnalyticsCount($item->{itemnumber});
266     if ($countanalytics > 0){
267         $analytics_flag=1;
268         $item->{countanalytics} = $countanalytics;
269     }
270
271     if (defined($item->{'materials'}) && $item->{'materials'} =~ /\S/){
272         $materials_flag = 1;
273     }
274
275     if ($currentbranch and $currentbranch ne "NO_LIBRARY_SET"
276     and C4::Context->preference('SeparateHoldings')) {
277         if ($itembranchcode and $itembranchcode eq $currentbranch) {
278             push @itemloop, $item;
279         } else {
280             push @otheritemloop, $item;
281         }
282     } else {
283         push @itemloop, $item;
284     }
285 }
286
287 $template->param( norequests => $norequests );
288 $template->param(
289         MARCNOTES   => $marcnotesarray,
290         MARCSUBJCTS => $marcsubjctsarray,
291         MARCAUTHORS => $marcauthorsarray,
292         MARCSERIES  => $marcseriesarray,
293         MARCURLS => $marcurlsarray,
294     MARCISBNS => $marcisbnsarray,
295         MARCHOSTS => $marchostsarray,
296         subtitle    => $subtitle,
297         itemdata_ccode      => $itemfields{ccode},
298         itemdata_enumchron  => $itemfields{enumchron},
299         itemdata_uri        => $itemfields{uri},
300         itemdata_copynumber => $itemfields{copynumber},
301         volinfo                         => $itemfields{enumchron},
302     itemdata_itemnotes  => $itemfields{itemnotes},
303         z3950_search_params     => C4::Search::z3950_search_args($dat),
304     holdcount           => $holdcount,
305         hostrecords         => $hostrecords,
306         analytics_flag  => $analytics_flag,
307         C4::Search::enabled_staff_search_views,
308         materials       => $materials_flag,
309 );
310
311 if (C4::Context->preference("AlternateHoldingsField") && scalar @items == 0) {
312     my $fieldspec = C4::Context->preference("AlternateHoldingsField");
313     my $subfields = substr $fieldspec, 3;
314     my $holdingsep = C4::Context->preference("AlternateHoldingsSeparator") || ' ';
315     my @alternateholdingsinfo = ();
316     my @holdingsfields = $record->field(substr $fieldspec, 0, 3);
317
318     for my $field (@holdingsfields) {
319         my %holding = ( holding => '' );
320         my $havesubfield = 0;
321         for my $subfield ($field->subfields()) {
322             if ((index $subfields, $$subfield[0]) >= 0) {
323                 $holding{'holding'} .= $holdingsep if (length $holding{'holding'} > 0);
324                 $holding{'holding'} .= $$subfield[1];
325                 $havesubfield++;
326             }
327         }
328         if ($havesubfield) {
329             push(@alternateholdingsinfo, \%holding);
330         }
331     }
332
333     $template->param(
334         ALTERNATEHOLDINGS   => \@alternateholdingsinfo,
335         );
336 }
337
338 my @results = ( $dat, );
339 foreach ( keys %{$dat} ) {
340     $template->param( "$_" => defined $dat->{$_} ? $dat->{$_} : '' );
341 }
342
343 # does not work: my %views_enabled = map { $_ => 1 } $template->query(loop => 'EnableViews');
344 # method query not found?!?!
345 $template->param( AmazonTld => get_amazon_tld() ) if ( C4::Context->preference("AmazonCoverImages"));
346 $template->param(
347     itemloop        => \@itemloop,
348     otheritemloop   => \@otheritemloop,
349     biblionumber        => $biblionumber,
350     ($analyze? 'analyze':'detailview') =>1,
351     subscriptions       => \@subs,
352     subscriptionsnumber => $subscriptionsnumber,
353     subscriptiontitle   => $dat->{title},
354 );
355
356 # $debug and $template->param(debug_display => 1);
357
358 # Lists
359
360 if (C4::Context->preference("virtualshelves") ) {
361    $template->param( 'GetShelves' => GetBibliosShelves( $biblionumber ) );
362 }
363
364 # XISBN Stuff
365 if (C4::Context->preference("FRBRizeEditions")==1) {
366     eval {
367         $template->param(
368             XISBNS => get_xisbns($isbn)
369         );
370     };
371     if ($@) { warn "XISBN Failed $@"; }
372 }
373
374 if ( C4::Context->preference("LocalCoverImages") == 1 ) {
375     my @images = ListImagesForBiblio($biblionumber);
376     $template->{VARS}->{localimages} = \@images;
377 }
378
379 # HTML5 Media
380 if ( (C4::Context->preference("HTML5MediaEnabled") eq 'both') or (C4::Context->preference("HTML5MediaEnabled") eq 'staff') ) {
381     $template->param( C4::HTML5Media->gethtml5media($record));
382 }
383
384
385 # Get OPAC URL
386 if (C4::Context->preference('OPACBaseURL')){
387      $template->param( OpacUrl => C4::Context->preference('OPACBaseURL') );
388 }
389
390 # Displaying tags
391
392 my $tag_quantity;
393 if (C4::Context->preference('TagsEnabled') and $tag_quantity = C4::Context->preference('TagsShowOnDetail')) {
394     $template->param(
395         TagsEnabled => 1,
396         TagsShowOnDetail => $tag_quantity
397     );
398     $template->param(TagLoop => get_tags({biblionumber=>$biblionumber, approved=>1,
399                                 'sort'=>'-weight', limit=>$tag_quantity}));
400 }
401
402 output_html_with_http_headers $query, $cookie, $template->output;