Bug 8377: Followup move style in a css file and do not pass template to a pm
[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     # format some item fields for display
201     if ( defined $item->{'publictype'} ) {
202         $item->{ $item->{'publictype'} } = 1;
203     }
204     $item->{imageurl} = defined $item->{itype} ? getitemtypeimagelocation('intranet', $itemtypes->{ $item->{itype} }{imageurl})
205                                                : '';
206
207         foreach (qw(datelastseen onloan)) {
208                 $item->{$_} = format_date($item->{$_});
209     }
210     $item->{datedue} = format_sqldatetime($item->{datedue});
211     # item damaged, lost, withdrawn loops
212     $item->{itemlostloop} = GetAuthorisedValues($authvalcode_items_itemlost, $item->{itemlost}) if $authvalcode_items_itemlost;
213     if ($item->{damaged}) {
214         $item->{itemdamagedloop} = GetAuthorisedValues($authvalcode_items_damaged, $item->{damaged}) if $authvalcode_items_damaged;
215     }
216     #get shelf location and collection code description if they are authorised value.
217     # same thing for copy number
218     my $shelfcode = $item->{'location'};
219     $item->{'location'} = $shelflocations->{$shelfcode} if ( defined( $shelfcode ) && defined($shelflocations) && exists( $shelflocations->{$shelfcode} ) );
220     my $ccode = $item->{'ccode'};
221     $item->{'ccode'} = $collections->{$ccode} if ( defined( $ccode ) && defined($collections) && exists( $collections->{$ccode} ) );
222     my $copynumber = $item->{'copynumber'};
223     $item->{'copynumber'} = $copynumbers->{$copynumber} if ( defined($copynumber) && defined($copynumbers) && exists( $copynumbers->{$copynumber} ) );
224     foreach (qw(ccode enumchron copynumber itemnotes uri)) {
225         $itemfields{$_} = 1 if ( $item->{$_} );
226     }
227
228     # checking for holds
229     my ($reservedate,$reservedfor,$expectedAt) = GetReservesFromItemnumber($item->{itemnumber});
230     my $ItemBorrowerReserveInfo = GetMemberDetails( $reservedfor, 0);
231     
232     if (C4::Context->preference('HidePatronName')){
233         $item->{'hidepatronname'} = 1;
234     }
235
236     if ( defined $reservedate ) {
237         $item->{backgroundcolor} = 'reserved';
238         $item->{reservedate}     = format_date($reservedate);
239         $item->{ReservedForBorrowernumber}     = $reservedfor;
240         $item->{ReservedForSurname}     = $ItemBorrowerReserveInfo->{'surname'};
241         $item->{ReservedForFirstname}   = $ItemBorrowerReserveInfo->{'firstname'};
242         $item->{ExpectedAtLibrary}      = $branches->{$expectedAt}{branchname};
243         $item->{Reservedcardnumber}             = $ItemBorrowerReserveInfo->{'cardnumber'};
244     }
245
246         # Check the transit status
247     my ( $transfertwhen, $transfertfrom, $transfertto ) = GetTransfers($item->{itemnumber});
248     if ( defined( $transfertwhen ) && ( $transfertwhen ne '' ) ) {
249         $item->{transfertwhen} = format_date($transfertwhen);
250         $item->{transfertfrom} = $branches->{$transfertfrom}{branchname};
251         $item->{transfertto}   = $branches->{$transfertto}{branchname};
252         $item->{nocancel} = 1;
253     }
254
255     # FIXME: move this to a pm, check waiting status for holds
256     my $sth2 = $dbh->prepare("SELECT * FROM reserves WHERE borrowernumber=? AND itemnumber=? AND found='W'");
257     $sth2->execute($item->{ReservedForBorrowernumber},$item->{itemnumber});
258     while (my $wait_hashref = $sth2->fetchrow_hashref) {
259         $item->{waitingdate} = format_date($wait_hashref->{waitingdate});
260     }
261
262     # item has a host number if its biblio number does not match the current bib
263     if ($item->{biblionumber} ne $biblionumber){
264         $item->{hostbiblionumber} = $item->{biblionumber};
265         $item->{hosttitle} = GetBiblioData($item->{biblionumber})->{title};
266     }
267         
268     #count if item is used in analytical bibliorecords
269     my $countanalytics= GetAnalyticsCount($item->{itemnumber});
270     if ($countanalytics > 0){
271         $analytics_flag=1;
272         $item->{countanalytics} = $countanalytics;
273     }
274
275     if (defined($item->{'materials'}) && $item->{'materials'} =~ /\S/){
276         $materials_flag = 1;
277     }
278
279     if ($currentbranch and $currentbranch ne "NO_LIBRARY_SET"
280     and C4::Context->preference('SeparateHoldings')) {
281         if ($itembranchcode and $itembranchcode eq $currentbranch) {
282             push @itemloop, $item;
283         } else {
284             push @otheritemloop, $item;
285         }
286     } else {
287         push @itemloop, $item;
288     }
289 }
290
291 $template->param( norequests => $norequests );
292 $template->param(
293         MARCNOTES   => $marcnotesarray,
294         MARCSUBJCTS => $marcsubjctsarray,
295         MARCAUTHORS => $marcauthorsarray,
296         MARCSERIES  => $marcseriesarray,
297         MARCURLS => $marcurlsarray,
298     MARCISBNS => $marcisbnsarray,
299         MARCHOSTS => $marchostsarray,
300         subtitle    => $subtitle,
301         itemdata_ccode      => $itemfields{ccode},
302         itemdata_enumchron  => $itemfields{enumchron},
303         itemdata_uri        => $itemfields{uri},
304         itemdata_copynumber => $itemfields{copynumber},
305         volinfo                         => $itemfields{enumchron},
306     itemdata_itemnotes  => $itemfields{itemnotes},
307         z3950_search_params     => C4::Search::z3950_search_args($dat),
308     holdcount           => $holdcount,
309         hostrecords         => $hostrecords,
310         analytics_flag  => $analytics_flag,
311         C4::Search::enabled_staff_search_views,
312         materials       => $materials_flag,
313 );
314
315 if (C4::Context->preference("AlternateHoldingsField") && scalar @items == 0) {
316     my $fieldspec = C4::Context->preference("AlternateHoldingsField");
317     my $subfields = substr $fieldspec, 3;
318     my $holdingsep = C4::Context->preference("AlternateHoldingsSeparator") || ' ';
319     my @alternateholdingsinfo = ();
320     my @holdingsfields = $record->field(substr $fieldspec, 0, 3);
321
322     for my $field (@holdingsfields) {
323         my %holding = ( holding => '' );
324         my $havesubfield = 0;
325         for my $subfield ($field->subfields()) {
326             if ((index $subfields, $$subfield[0]) >= 0) {
327                 $holding{'holding'} .= $holdingsep if (length $holding{'holding'} > 0);
328                 $holding{'holding'} .= $$subfield[1];
329                 $havesubfield++;
330             }
331         }
332         if ($havesubfield) {
333             push(@alternateholdingsinfo, \%holding);
334         }
335     }
336
337     $template->param(
338         ALTERNATEHOLDINGS   => \@alternateholdingsinfo,
339         );
340 }
341
342 my @results = ( $dat, );
343 foreach ( keys %{$dat} ) {
344     $template->param( "$_" => defined $dat->{$_} ? $dat->{$_} : '' );
345 }
346
347 # does not work: my %views_enabled = map { $_ => 1 } $template->query(loop => 'EnableViews');
348 # method query not found?!?!
349 $template->param( AmazonTld => get_amazon_tld() ) if ( C4::Context->preference("AmazonCoverImages"));
350 $template->param(
351     itemloop        => \@itemloop,
352     otheritemloop   => \@otheritemloop,
353     biblionumber        => $biblionumber,
354     ($analyze? 'analyze':'detailview') =>1,
355     subscriptions       => \@subs,
356     subscriptionsnumber => $subscriptionsnumber,
357     subscriptiontitle   => $dat->{title},
358 );
359
360 # $debug and $template->param(debug_display => 1);
361
362 # Lists
363
364 if (C4::Context->preference("virtualshelves") ) {
365    $template->param( 'GetShelves' => GetBibliosShelves( $biblionumber ) );
366 }
367
368 # XISBN Stuff
369 if (C4::Context->preference("FRBRizeEditions")==1) {
370     eval {
371         $template->param(
372             XISBNS => get_xisbns($isbn)
373         );
374     };
375     if ($@) { warn "XISBN Failed $@"; }
376 }
377
378 if ( C4::Context->preference("LocalCoverImages") == 1 ) {
379     my @images = ListImagesForBiblio($biblionumber);
380     $template->{VARS}->{localimages} = \@images;
381 }
382
383 # HTML5 Media
384 if ( (C4::Context->preference("HTML5MediaEnabled") eq 'both') or (C4::Context->preference("HTML5MediaEnabled") eq 'staff') ) {
385     $template->param( C4::HTML5Media->gethtml5media($record));
386 }
387
388
389 # Get OPAC URL
390 if (C4::Context->preference('OPACBaseURL')){
391      $template->param( OpacUrl => C4::Context->preference('OPACBaseURL') );
392 }
393
394 # Displaying tags
395
396 my $tag_quantity;
397 if (C4::Context->preference('TagsEnabled') and $tag_quantity = C4::Context->preference('TagsShowOnDetail')) {
398     $template->param(
399         TagsEnabled => 1,
400         TagsShowOnDetail => $tag_quantity
401     );
402     $template->param(TagLoop => get_tags({biblionumber=>$biblionumber, approved=>1,
403                                 'sort'=>'-weight', limit=>$tag_quantity}));
404 }
405
406 output_html_with_http_headers $query, $cookie, $template->output;