Bug 19198: Prevent the test to fail randomly
[koha.git] / opac / opac-MARCdetail.pl
blob9605cdd7e61b8df325534f5959590066210f8bc2
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::Patrons;
62 use Koha::RecordProcessor;
64 my $query = new CGI;
66 my $dbh = C4::Context->dbh;
68 my $biblionumber = $query->param('biblionumber');
69 if ( ! $biblionumber ) {
70 print $query->redirect("/cgi-bin/koha/errors/404.pl");
71 exit;
74 my $record = GetMarcBiblio({
75 biblionumber => $biblionumber,
76 embed_items => 1 });
77 if ( ! $record ) {
78 print $query->redirect("/cgi-bin/koha/errors/404.pl");
79 exit;
82 my @all_items = GetItemsInfo($biblionumber);
83 my @items2hide;
84 if (scalar @all_items >= 1) {
85 push @items2hide, GetHiddenItemnumbers(@all_items);
87 if (scalar @items2hide == scalar @all_items ) {
88 print $query->redirect("/cgi-bin/koha/errors/404.pl");
89 exit;
93 my $framework = &GetFrameworkCode( $biblionumber );
94 my $tagslib = &GetMarcStructure( 0, $framework );
95 my ($tag_itemnumber,$subtag_itemnumber) = &GetMarcFromKohaField('items.itemnumber',$framework);
96 my $biblio = Koha::Biblios->find( $biblionumber );
98 my $record_processor = Koha::RecordProcessor->new({
99 filters => 'ViewPolicy',
100 options => {
101 interface => 'opac',
102 frameworkcode => $framework
105 $record_processor->process($record);
107 # open template
108 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
110 template_name => "opac-MARCdetail.tt",
111 query => $query,
112 type => "opac",
113 authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ),
114 debug => 1,
118 my ($bt_tag,$bt_subtag) = GetMarcFromKohaField('biblio.title',$framework);
119 $template->param(
120 bibliotitle => $biblio->title,
121 ) if $tagslib->{$bt_tag}->{$bt_subtag}->{hidden} <= 0 && # <=0 OPAC visible.
122 $tagslib->{$bt_tag}->{$bt_subtag}->{hidden} > -8; # except -8;
124 # get biblionumbers stored in the cart
125 if(my $cart_list = $query->cookie("bib_list")){
126 my @cart_list = split(/\//, $cart_list);
127 if ( grep {$_ eq $biblionumber} @cart_list) {
128 $template->param( incart => 1 );
132 my $allow_onshelf_holds;
133 my $patron = Koha::Patrons->find( $loggedinuser );
134 for my $itm (@all_items) {
135 $allow_onshelf_holds = C4::Reserves::OnShelfHoldsAllowed( $itm, ( $patron ? $patron->unblessed : {} ) );
136 last if $allow_onshelf_holds;
139 $template->param( 'AllowOnShelfHolds' => $allow_onshelf_holds );
140 $template->param( 'ItemsIssued' => CountItemsIssued( $biblionumber ) );
142 # adding the $RequestOnOpac param
143 my $RequestOnOpac;
144 if (C4::Context->preference("RequestOnOpac")) {
145 $RequestOnOpac = 1;
148 # fill arrays
149 my @loop_data = ();
150 my $tag;
152 # loop through each tab 0 through 9
153 for ( my $tabloop = 0 ; $tabloop <= 9 ; $tabloop++ ) {
155 # loop through each tag
156 my @loop_data = ();
157 my @subfields_data;
159 # deal with leader
160 unless ( $tagslib->{'000'}->{'@'}->{tab} ne $tabloop
161 or $tagslib->{'000'}->{'@'}->{hidden} > 0 )
163 my %subfield_data;
164 $subfield_data{marc_lib} = $tagslib->{'000'}->{'@'}->{lib};
165 $subfield_data{marc_value} = $record->leader();
166 $subfield_data{marc_subfield} = '@';
167 $subfield_data{marc_tag} = '000';
168 push( @subfields_data, \%subfield_data );
169 my %tag_data;
170 $tag_data{tag} = '000 -' . $tagslib->{'000'}->{lib};
171 my @tmp = @subfields_data;
172 $tag_data{subfield} = \@tmp;
173 push( @loop_data, \%tag_data );
174 undef @subfields_data;
176 my @fields = $record->fields();
177 for ( my $x_i = 0 ; $x_i <= $#fields ; $x_i++ ) {
179 # if tag <10, there's no subfield, use the "@" trick
180 if ( $fields[$x_i]->tag() < 10 ) {
181 next
182 if (
183 $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{tab} ne $tabloop );
184 next if ( $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{hidden} > 0 );
185 my %subfield_data;
186 $subfield_data{marc_lib} =
187 $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{lib};
188 $subfield_data{marc_value} = $fields[$x_i]->data();
189 $subfield_data{marc_subfield} = '@';
190 $subfield_data{marc_tag} = $fields[$x_i]->tag();
191 push( @subfields_data, \%subfield_data );
193 else {
194 my @subf = $fields[$x_i]->subfields;
195 my $previous = '';
196 # loop through each subfield
197 for my $i ( 0 .. $#subf ) {
198 $subf[$i][0] = "@" unless defined($subf[$i][0]);
199 my $sf_def = $tagslib->{ $fields[$x_i]->tag() };
200 $sf_def = $sf_def->{ $subf[$i][0] } if defined($sf_def);
201 my ($tab,$hidden,$lib);
202 $tab = $sf_def->{tab} if defined($sf_def);
203 $tab = $tab // int($fields[$x_i]->tag()/100);
204 $hidden = $sf_def->{hidden} if defined($sf_def);
205 $hidden = $hidden // 0;
206 next if ( $tab != $tabloop );
207 next if ( $hidden > 0 );
208 my %subfield_data;
209 $lib = $sf_def->{lib} if defined($sf_def);
210 $lib = $lib // '--';
211 $subfield_data{marc_lib} = ($lib eq $previous) ? '--' : $lib;
212 $previous = $lib;
213 $subfield_data{link} = $sf_def->{link};
214 $subf[$i][1] =~ s/\n/<br\/>/g;
215 if ( $sf_def->{isurl} ) {
216 $subfield_data{marc_value} = "<a href=\"$subf[$i][1]\">$subf[$i][1]</a>";
218 elsif ( defined($sf_def->{kohafield}) && $sf_def->{kohafield} eq "biblioitems.isbn" ) {
219 $subfield_data{marc_value} = $subf[$i][1];
221 else {
222 if ( $sf_def->{authtypecode} ) {
223 $subfield_data{authority} = $fields[$x_i]->subfield(9);
225 $subfield_data{marc_value} = GetAuthorisedValueDesc( $fields[$x_i]->tag(),
226 $subf[$i][0], $subf[$i][1], '', $tagslib, '', 'opac' );
228 $subfield_data{marc_subfield} = $subf[$i][0];
229 $subfield_data{marc_tag} = $fields[$x_i]->tag();
230 push( @subfields_data, \%subfield_data );
233 if ( $#subfields_data >= 0 ) {
234 my %tag_data;
235 if ( ( $fields[$x_i]->tag() eq $fields[ $x_i - 1 ]->tag() )
236 && ( C4::Context->preference('LabelMARCView') eq 'economical' )
239 $tag_data{tag} = "";
241 else {
242 if ( C4::Context->preference('hide_marc') ) {
243 $tag_data{tag} = $tagslib->{ $fields[$x_i]->tag() }->{lib};
245 else {
246 my $sf_def = $tagslib->{ $fields[$x_i]->tag() };
247 my $lib;
248 $lib = $sf_def->{lib} if defined($sf_def);
249 $lib = $lib // '';
250 $tag_data{tag} = $fields[$x_i]->tag() . ' '
251 . C4::Koha::display_marc_indicators($fields[$x_i])
252 . " - $lib";
255 my @tmp = @subfields_data;
256 $tag_data{subfield} = \@tmp;
257 push( @loop_data, \%tag_data );
258 undef @subfields_data;
261 $template->param( "tab" . $tabloop . "XX" => \@loop_data );
265 # now, build item tab !
266 # the main difference is that datas are in lines and not in columns : thus, we build the <th> first, then the values...
267 # loop through each tag
268 # warning : we may have differents number of columns in each row. Thus, we first build a hash, complete it if necessary
269 # then construct template.
270 my @fields = $record->fields();
271 my %witness
272 ; #---- stores the list of subfields used at least once, with the "meaning" of the code
273 my @item_subfield_codes;
274 my @item_loop;
275 foreach my $field (@fields) {
276 next if ( $field->tag() < 10 );
277 next if ( ( $field->tag() eq $tag_itemnumber ) &&
278 ( any { $field->subfield($subtag_itemnumber) eq $_ }
279 @items2hide) );
280 my @subf = $field->subfields;
281 my $item;
283 # loop through each subfield
284 for my $i ( 0 .. $#subf ) {
285 my $sf_def = $tagslib->{ $field->tag() }->{ $subf[$i][0] };
286 next if ( ($sf_def->{tab}||0) != 10 );
287 next if ( ($sf_def->{hidden}||0) > 0 );
288 push @item_subfield_codes, $subf[$i][0];
289 $witness{ $subf[$i][0] } = $sf_def->{lib};
291 if ( $sf_def->{isurl} ) {
292 $item->{ $subf[$i][0] } = "<a href=\"$subf[$i][1]\">$subf[$i][1]</a>";
294 elsif ( $sf_def->{kohafield} eq "biblioitems.isbn" ) {
295 $item->{ $subf[$i][0] } = $subf[$i][1];
297 else {
298 $item->{ $subf[$i][0] } = GetAuthorisedValueDesc( $field->tag(), $subf[$i][0],
299 $subf[$i][1], '', $tagslib, '', 'opac' );
302 push @item_loop, $item if $item;
304 my ( $holdingbrtagf, $holdingbrtagsubf ) =
305 &GetMarcFromKohaField( "items.holdingbranch", $framework );
306 @item_loop =
307 sort { ($a->{$holdingbrtagsubf}||'') cmp ($b->{$holdingbrtagsubf}||'') } @item_loop;
309 @item_subfield_codes = uniq @item_subfield_codes;
310 # fill item info
311 my @item_header_loop;
312 for my $subfield_code ( @item_subfield_codes ) {
313 push @item_header_loop, $witness{$subfield_code};
314 for my $item_data ( @item_loop ) {
315 $item_data->{$subfield_code} ||= "&nbsp;"
319 if ( C4::Context->preference("OPACISBD") ) {
320 $template->param( ISBD => 1 );
323 #Search for title in links
324 my $marcflavour = C4::Context->preference("marcflavour");
325 my $dat = TransformMarcToKoha( $record );
326 my $isbn = GetNormalizedISBN(undef,$record,$marcflavour);
327 my $marccontrolnumber = GetMarcControlnumber ($record, $marcflavour);
328 my $marcissns = GetMarcISSN( $record, $marcflavour );
329 my $issn = $marcissns->[0] || '';
331 if (my $search_for_title = C4::Context->preference('OPACSearchForTitleIn')){
332 $dat->{title} =~ s/\/+$//; # remove trailing slash
333 $dat->{title} =~ s/\s+$//; # remove trailing space
334 $search_for_title = parametrized_url(
335 $search_for_title,
337 TITLE => $dat->{title},
338 AUTHOR => $dat->{author},
339 ISBN => $isbn,
340 ISSN => $issn,
341 CONTROLNUMBER => $marccontrolnumber,
342 BIBLIONUMBER => $biblionumber,
345 $template->param('OPACSearchForTitleIn' => $search_for_title);
348 $template->param(
349 item_loop => \@item_loop,
350 item_header_loop => \@item_header_loop,
351 item_subfield_codes => \@item_subfield_codes,
352 biblionumber => $biblionumber,
355 output_html_with_http_headers $query, $cookie, $template->output;