Revert "Bug 25828: Update cpanfile for 20.05 release cycle"
[koha.git] / opac / opac-MARCdetail.pl
blobb835f1bdea1fabbc5a6a483249ad2c45f6bf3cde
1 #!/usr/bin/perl
3 # This file is part of Koha.
5 # Copyright (C) 2000-2002 Katipo Communications
6 # Parts Copyright (C) 2010 BibLibre
7 # Parts Copyright (C) 2013 Mark Tompsett
9 # Koha is free software; you can redistribute it and/or modify it
10 # under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 3 of the License, or
12 # (at your option) any later version.
14 # Koha is distributed in the hope that it will be useful, but
15 # WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # along with Koha; if not, see <http://www.gnu.org/licenses>.
23 =head1 NAME
25 opac-MARCdetail.pl : script to show a biblio in MARC format
27 =head1 SYNOPSIS
29 =cut
31 =head1 DESCRIPTION
33 This script needs a biblionumber as parameter
35 It shows the biblio in a (nice) MARC format depending on MARC
36 parameters tables.
38 The template is in <templates_dir>/catalogue/MARCdetail.tt.
39 this template must be divided into 11 "tabs".
41 The first 10 tabs present the biblio, the 11th one presents
42 the items attached to the biblio
44 =cut
46 use Modern::Perl;
48 use C4::Auth;
49 use C4::Context;
50 use C4::Output;
51 use CGI qw ( -utf8 );
52 use MARC::Record;
53 use C4::Biblio;
54 use C4::Items;
55 use C4::Reserves;
56 use C4::Members;
57 use C4::Acquisition;
58 use C4::Koha;
59 use List::MoreUtils qw( any uniq );
60 use Koha::Biblios;
61 use Koha::CirculationRules;
62 use Koha::Items;
63 use Koha::ItemTypes;
64 use Koha::Patrons;
65 use Koha::RecordProcessor;
66 use Koha::DateUtils;
68 my $query = CGI->new();
70 my $biblionumber = $query->param('biblionumber');
71 if ( ! $biblionumber ) {
72 print $query->redirect("/cgi-bin/koha/errors/404.pl");
73 exit;
75 $biblionumber = int($biblionumber);
77 # open template
78 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
80 template_name => "opac-MARCdetail.tt",
81 query => $query,
82 type => "opac",
83 authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ),
84 debug => 1,
88 my $patron = Koha::Patrons->find( $loggedinuser );
89 my $borcat = q{};
90 if ( C4::Context->preference('OpacHiddenItemsExceptions') ) {
91 # we need to fetch the borrower info here, so we can pass the category
92 $borcat = $patron ? $patron->categorycode : $borcat;
95 my $record = GetMarcBiblio({
96 biblionumber => $biblionumber,
97 embed_items => 1,
98 opac => 1,
99 borcat => $borcat });
100 if ( ! $record ) {
101 print $query->redirect("/cgi-bin/koha/errors/404.pl");
102 exit;
105 my @all_items = GetItemsInfo($biblionumber);
106 my $biblio = Koha::Biblios->find( $biblionumber );
107 my $framework = $biblio ? $biblio->frameworkcode : q{};
108 my $tagslib = &GetMarcStructure( 0, $framework );
109 my ($tag_itemnumber,$subtag_itemnumber) = &GetMarcFromKohaField( 'items.itemnumber' );
110 my @nonhiddenitems = $record->field($tag_itemnumber);
111 if (scalar @all_items >= 1 && scalar @nonhiddenitems == 0) {
112 print $query->redirect("/cgi-bin/koha/errors/404.pl");
113 exit;
116 my $record_processor = Koha::RecordProcessor->new({
117 filters => 'ViewPolicy',
118 options => {
119 interface => 'opac',
120 frameworkcode => $framework
123 $record_processor->process($record);
125 # get biblionumbers stored in the cart
126 if(my $cart_list = $query->cookie("bib_list")){
127 my @cart_list = split(/\//, $cart_list);
128 if ( grep {$_ eq $biblionumber} @cart_list) {
129 $template->param( incart => 1 );
133 my ($bt_tag,$bt_subtag) = GetMarcFromKohaField( 'biblio.title' );
134 $template->param(
135 bibliotitle => $biblio->title,
136 ) if $tagslib->{$bt_tag}->{$bt_subtag}->{hidden} <= 0 && # <=0 OPAC visible.
137 $tagslib->{$bt_tag}->{$bt_subtag}->{hidden} > -8; # except -8;
139 my $allow_onshelf_holds;
140 for my $itm (@all_items) {
141 my $item = Koha::Items->find( $itm->{itemnumber} );
142 $allow_onshelf_holds = Koha::CirculationRules->get_onshelfholds_policy( { item => $item, patron => $patron } );
143 last if $allow_onshelf_holds;
146 if( $allow_onshelf_holds || CountItemsIssued($biblionumber) || $biblio->has_items_waiting_or_intransit ) {
147 $template->param( ReservableItems => 1 );
150 # adding the $RequestOnOpac param
151 my $RequestOnOpac;
152 if (C4::Context->preference("RequestOnOpac")) {
153 $RequestOnOpac = 1;
156 # fill arrays
157 my @loop_data = ();
158 my $tag;
160 # loop through each tab 0 through 9
161 for ( my $tabloop = 0 ; $tabloop <= 9 ; $tabloop++ ) {
163 # loop through each tag
164 my @loop_data = ();
165 my @subfields_data;
167 # deal with leader
168 unless ( $tagslib->{'000'}->{'@'}->{tab} ne $tabloop
169 or $tagslib->{'000'}->{'@'}->{hidden} > 0 )
171 my %subfield_data;
172 $subfield_data{marc_lib} = $tagslib->{'000'}->{'@'}->{lib};
173 $subfield_data{marc_value} = $record->leader();
174 $subfield_data{marc_subfield} = '@';
175 $subfield_data{marc_tag} = '000';
176 push( @subfields_data, \%subfield_data );
177 my %tag_data;
178 $tag_data{tag} = '000 -' . $tagslib->{'000'}->{lib};
179 my @tmp = @subfields_data;
180 $tag_data{subfield} = \@tmp;
181 push( @loop_data, \%tag_data );
182 undef @subfields_data;
184 my @fields = $record->fields();
185 for ( my $x_i = 0 ; $x_i <= $#fields ; $x_i++ ) {
187 # if tag <10, there's no subfield, use the "@" trick
188 if ( $fields[$x_i]->tag() < 10 ) {
189 next
190 if (
191 $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{tab} ne $tabloop );
192 next if ( $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{hidden} > 0 );
193 my %subfield_data;
194 $subfield_data{marc_lib} =
195 $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{lib};
196 $subfield_data{marc_value} = $fields[$x_i]->data();
197 $subfield_data{marc_subfield} = '@';
198 $subfield_data{marc_tag} = $fields[$x_i]->tag();
199 push( @subfields_data, \%subfield_data );
201 else {
202 my @subf = $fields[$x_i]->subfields;
203 my $previous = '';
204 # loop through each subfield
205 for my $i ( 0 .. $#subf ) {
206 $subf[$i][0] = "@" unless defined($subf[$i][0]);
207 my $sf_def = $tagslib->{ $fields[$x_i]->tag() };
208 $sf_def = $sf_def->{ $subf[$i][0] } if defined($sf_def);
209 my ($tab,$hidden,$lib);
210 $tab = $sf_def->{tab} if defined($sf_def);
211 $tab = $tab // int($fields[$x_i]->tag()/100);
212 $hidden = $sf_def->{hidden} if defined($sf_def);
213 $hidden = $hidden // 0;
214 next if ( $tab != $tabloop );
215 next if ( $hidden > 0 );
216 my %subfield_data;
217 $lib = $sf_def->{lib} if defined($sf_def);
218 $lib = $lib // '--';
219 $subfield_data{marc_lib} = ($lib eq $previous) ? '--' : $lib;
220 $previous = $lib;
221 $subfield_data{link} = $sf_def->{link};
222 $subf[$i][1] =~ s/\n/<br\/>/g;
223 if ( $sf_def->{isurl} ) {
224 $subfield_data{marc_value} = "<a href=\"$subf[$i][1]\">$subf[$i][1]</a>";
226 elsif ( defined($sf_def->{kohafield}) && $sf_def->{kohafield} eq "biblioitems.isbn" ) {
227 $subfield_data{marc_value} = $subf[$i][1];
229 else {
230 if ( $sf_def->{authtypecode} ) {
231 $subfield_data{authority} = $fields[$x_i]->subfield(9);
233 $subfield_data{marc_value} = GetAuthorisedValueDesc( $fields[$x_i]->tag(),
234 $subf[$i][0], $subf[$i][1], '', $tagslib, '', 'opac' );
236 $subfield_data{marc_subfield} = $subf[$i][0];
237 $subfield_data{marc_tag} = $fields[$x_i]->tag();
238 push( @subfields_data, \%subfield_data );
241 if ( $#subfields_data >= 0 ) {
242 my %tag_data;
243 if ( ( $fields[$x_i]->tag() eq $fields[ $x_i - 1 ]->tag() )
244 && ( C4::Context->preference('LabelMARCView') eq 'economical' )
247 $tag_data{tag} = "";
249 else {
250 if ( C4::Context->preference('hide_marc') ) {
251 $tag_data{tag} = $tagslib->{ $fields[$x_i]->tag() }->{lib};
253 else {
254 my $sf_def = $tagslib->{ $fields[$x_i]->tag() };
255 my $lib;
256 $lib = $sf_def->{lib} if defined($sf_def);
257 $lib = $lib // '';
258 $tag_data{tag} = $fields[$x_i]->tag() . ' '
259 . C4::Koha::display_marc_indicators($fields[$x_i])
260 . " - $lib";
263 my @tmp = @subfields_data;
264 $tag_data{subfield} = \@tmp;
265 push( @loop_data, \%tag_data );
266 undef @subfields_data;
269 $template->param( "tab" . $tabloop . "XX" => \@loop_data );
273 # now, build item tab !
274 # the main difference is that datas are in lines and not in columns : thus, we build the <th> first, then the values...
275 # loop through each tag
276 # warning : we may have differents number of columns in each row. Thus, we first build a hash, complete it if necessary
277 # then construct template.
278 # $record has already had all the item fields filtered above.
279 my @fields = $record->fields();
280 my %witness
281 ; #---- stores the list of subfields used at least once, with the "meaning" of the code
282 my @item_subfield_codes;
283 my @item_loop;
284 foreach my $field (@fields) {
285 next if ( $field->tag() < 10 );
286 my @subf = $field->subfields;
287 my $item;
289 # loop through each subfield
290 for my $i ( 0 .. $#subf ) {
291 my $sf_def = $tagslib->{ $field->tag() }->{ $subf[$i][0] };
292 next if ( ($sf_def->{tab}||0) != 10 );
293 next if ( ($sf_def->{hidden}||0) > 0 );
295 push @item_subfield_codes, $subf[$i][0];
296 $witness{ $subf[$i][0] } = $sf_def->{lib};
298 # Allow repeatables (BZ 13574)
299 if( $item->{$subf[$i][0]} ) {
300 $item->{$subf[$i][0]} .= ' | ';
301 } else {
302 $item->{$subf[$i][0]} = q{};
305 if ( $sf_def->{isurl} ) {
306 $item->{ $subf[$i][0] } .= "<a href=\"$subf[$i][1]\">$subf[$i][1]</a>";
308 elsif ( $sf_def->{kohafield} eq "biblioitems.isbn" ) {
309 $item->{ $subf[$i][0] } .= $subf[$i][1];
311 else {
312 $item->{ $subf[$i][0] } .= GetAuthorisedValueDesc( $field->tag(), $subf[$i][0],
313 $subf[$i][1], '', $tagslib, '', 'opac' );
316 my $kohafield = $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{kohafield};
317 $item->{ $subf[$i][0] } = output_pref( { str => $item->{ $subf[$i][0] }, dateonly => 1 } )
318 if grep { $kohafield eq $_ }
319 qw( items.dateaccessioned items.onloan items.datelastseen items.datelastborrowed items.replacementpricedate );
322 push @item_loop, $item if $item;
324 my ( $holdingbrtagf, $holdingbrtagsubf ) =
325 &GetMarcFromKohaField( "items.holdingbranch" );
326 @item_loop =
327 sort { ($a->{$holdingbrtagsubf}||'') cmp ($b->{$holdingbrtagsubf}||'') } @item_loop;
329 @item_subfield_codes = uniq @item_subfield_codes;
330 # fill item info
331 my @item_header_loop;
332 for my $subfield_code ( @item_subfield_codes ) {
333 push @item_header_loop, $witness{$subfield_code};
334 for my $item_data ( @item_loop ) {
335 $item_data->{$subfield_code} ||= "&nbsp;"
339 if ( C4::Context->preference("OPACISBD") ) {
340 $template->param( ISBD => 1 );
343 #Search for title in links
344 my $marcflavour = C4::Context->preference("marcflavour");
345 my $dat = TransformMarcToKoha( $record );
346 my $isbn = GetNormalizedISBN(undef,$record,$marcflavour);
347 my $marccontrolnumber = GetMarcControlnumber ($record, $marcflavour);
348 my $marcissns = GetMarcISSN( $record, $marcflavour );
349 my $issn = $marcissns->[0] || '';
351 if (my $search_for_title = C4::Context->preference('OPACSearchForTitleIn')){
352 $dat->{title} =~ s/\/+$//; # remove trailing slash
353 $dat->{title} =~ s/\s+$//; # remove trailing space
354 $search_for_title = parametrized_url(
355 $search_for_title,
357 TITLE => $dat->{title},
358 AUTHOR => $dat->{author},
359 ISBN => $isbn,
360 ISSN => $issn,
361 CONTROLNUMBER => $marccontrolnumber,
362 BIBLIONUMBER => $biblionumber,
365 $template->param('OPACSearchForTitleIn' => $search_for_title);
368 if( C4::Context->preference('ArticleRequests') ) {
369 my $itemtype = Koha::ItemTypes->find($biblio->itemtype);
370 my $artreqpossible = $patron
371 ? $biblio->can_article_request( $patron )
372 : $itemtype
373 ? $itemtype->may_article_request
374 : q{};
375 $template->param( artreqpossible => $artreqpossible );
378 $template->param(
379 item_loop => \@item_loop,
380 item_header_loop => \@item_header_loop,
381 item_subfield_codes => \@item_subfield_codes,
382 biblio => $biblio,
385 output_html_with_http_headers $query, $cookie, $template->output;