Bug 9261: (follow-up) fix various issues identified during QA
[koha.git] / opac / opac-MARCdetail.pl
blobd6be0a6442685c890f43b08c2d959f4fedbfd704
1 #!/usr/bin/perl
3 # Copyright 2000-2002 Katipo Communications
4 # Parts copyright 2010 BibLibre
6 # This file is part of Koha.
8 # Koha is free software; you can redistribute it and/or modify it under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
11 # version.
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License along
18 # with Koha; if not, write to the Free Software Foundation, Inc.,
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 =head1 NAME
23 MARCdetail.pl : script to show a biblio in MARC format
25 =head1 SYNOPSIS
27 =cut
29 =head1 DESCRIPTION
31 This script needs a biblionumber as parameter
33 It shows the biblio in a (nice) MARC format depending on MARC
34 parameters tables.
36 The template is in <templates_dir>/catalogue/MARCdetail.tmpl.
37 this template must be divided into 11 "tabs".
39 The first 10 tabs present the biblio, the 11th one presents
40 the items attached to the biblio
42 =cut
44 use strict;
45 use warnings;
47 use C4::Auth;
48 use C4::Context;
49 use C4::Output;
50 use CGI;
51 use MARC::Record;
52 use C4::Biblio;
53 use C4::Items;
54 use C4::Acquisition;
55 use C4::Koha;
56 use List::MoreUtils qw/any/;
58 my $query = new CGI;
60 my $dbh = C4::Context->dbh;
62 my $biblionumber = $query->param('biblionumber');
63 if ( ! $biblionumber ) {
64 print $query->redirect("/cgi-bin/koha/errors/404.pl");
65 exit;
68 my @all_items = GetItemsInfo($biblionumber);
69 my @items2hide;
70 if (scalar @all_items >= 1) {
71 push @items2hide, GetHiddenItemnumbers(@all_items);
73 if (scalar @items2hide == scalar @all_items ) {
74 print $query->redirect("/cgi-bin/koha/errors/404.pl");
75 exit;
79 my $itemtype = &GetFrameworkCode($biblionumber);
80 my $tagslib = &GetMarcStructure( 0, $itemtype );
81 my ($tag_itemnumber,$subtag_itemnumber) = &GetMarcFromKohaField('items.itemnumber',$itemtype);
82 my $biblio = GetBiblioData($biblionumber);
83 $biblionumber = $biblio->{biblionumber};
84 my $record = GetMarcBiblio($biblionumber, 1);
85 if ( ! $record ) {
86 print $query->redirect("/cgi-bin/koha/errors/404.pl");
87 exit;
89 # open template
90 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
92 template_name => "opac-MARCdetail.tmpl",
93 query => $query,
94 type => "opac",
95 authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ),
96 debug => 1,
100 $template->param(
101 bibliotitle => $biblio->{title},
104 # get biblionumbers stored in the cart
105 my @cart_list;
107 if($query->cookie("bib_list")){
108 my $cart_list = $query->cookie("bib_list");
109 @cart_list = split(/\//, $cart_list);
110 if ( grep {$_ eq $biblionumber} @cart_list) {
111 $template->param( incart => 1 );
115 $template->param( 'AllowOnShelfHolds' => C4::Context->preference('AllowOnShelfHolds') );
116 $template->param( 'ItemsIssued' => CountItemsIssued( $biblionumber ) );
118 # adding the $RequestOnOpac param
119 my $RequestOnOpac;
120 if (C4::Context->preference("RequestOnOpac")) {
121 $RequestOnOpac = 1;
124 # fill arrays
125 my @loop_data = ();
126 my $tag;
128 # loop through each tab 0 through 9
129 for ( my $tabloop = 0 ; $tabloop <= 9 ; $tabloop++ ) {
131 # loop through each tag
132 my @loop_data = ();
133 my @subfields_data;
135 # deal with leader
136 unless ( $tagslib->{'000'}->{'@'}->{tab} ne $tabloop
137 or $tagslib->{'000'}->{'@'}->{hidden} > 0 )
139 my %subfield_data;
140 $subfield_data{marc_lib} = $tagslib->{'000'}->{'@'}->{lib};
141 $subfield_data{marc_value} = $record->leader();
142 $subfield_data{marc_subfield} = '@';
143 $subfield_data{marc_tag} = '000';
144 push( @subfields_data, \%subfield_data );
145 my %tag_data;
146 $tag_data{tag} = '000 -' . $tagslib->{'000'}->{lib};
147 my @tmp = @subfields_data;
148 $tag_data{subfield} = \@tmp;
149 push( @loop_data, \%tag_data );
150 undef @subfields_data;
152 my @fields = $record->fields();
153 for ( my $x_i = 0 ; $x_i <= $#fields ; $x_i++ ) {
155 # if tag <10, there's no subfield, use the "@" trick
156 if ( $fields[$x_i]->tag() < 10 ) {
157 next
158 if (
159 $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{tab} ne $tabloop );
160 next if ( $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{hidden} > 0 );
161 my %subfield_data;
162 $subfield_data{marc_lib} =
163 $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{lib};
164 $subfield_data{marc_value} = $fields[$x_i]->data();
165 $subfield_data{marc_subfield} = '@';
166 $subfield_data{marc_tag} = $fields[$x_i]->tag();
167 push( @subfields_data, \%subfield_data );
169 else {
170 my @subf = $fields[$x_i]->subfields;
171 my $previous = '';
172 # loop through each subfield
173 for my $i ( 0 .. $#subf ) {
174 $subf[$i][0] = "@" unless defined($subf[$i][0]);
175 my $sf_def = $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] };
176 next if ( ($sf_def->{tab}||0) != $tabloop );
177 next if ( ($sf_def->{hidden}||0) > 0 );
178 my %subfield_data;
179 $subfield_data{marc_lib} = ($sf_def->{lib} eq $previous) ? '--' : $sf_def->{lib};
180 $previous = $sf_def->{lib};
181 $subfield_data{link} = $sf_def->{link};
182 $subf[$i][1] =~ s/\n/<br\/>/g;
183 if ( $sf_def->{isurl} ) {
184 $subfield_data{marc_value} = "<a href=\"$subf[$i][1]\">$subf[$i][1]</a>";
186 elsif ( defined($sf_def->{kohafield}) && $sf_def->{kohafield} eq "biblioitems.isbn" ) {
187 $subfield_data{marc_value} = $subf[$i][1];
189 else {
190 if ( $sf_def->{authtypecode} ) {
191 $subfield_data{authority} = $fields[$x_i]->subfield(9);
193 $subfield_data{marc_value} = GetAuthorisedValueDesc( $fields[$x_i]->tag(),
194 $subf[$i][0], $subf[$i][1], '', $tagslib, '', 'opac' );
196 $subfield_data{marc_subfield} = $subf[$i][0];
197 $subfield_data{marc_tag} = $fields[$x_i]->tag();
198 push( @subfields_data, \%subfield_data );
201 if ( $#subfields_data >= 0 ) {
202 my %tag_data;
203 if ( ( $fields[$x_i]->tag() eq $fields[ $x_i - 1 ]->tag() )
204 && ( C4::Context->preference('LabelMARCView') eq 'economical' )
207 $tag_data{tag} = "";
209 else {
210 if ( C4::Context->preference('hide_marc') ) {
211 $tag_data{tag} = $tagslib->{ $fields[$x_i]->tag() }->{lib};
213 else {
214 $tag_data{tag} =
215 $fields[$x_i]->tag()
216 . ' '
217 . C4::Koha::display_marc_indicators($fields[$x_i])
218 . ' - '
219 . $tagslib->{ $fields[$x_i]->tag() }->{lib};
222 my @tmp = @subfields_data;
223 $tag_data{subfield} = \@tmp;
224 push( @loop_data, \%tag_data );
225 undef @subfields_data;
228 $template->param( "tab" . $tabloop . "XX" => \@loop_data );
232 # now, build item tab !
233 # the main difference is that datas are in lines and not in columns : thus, we build the <th> first, then the values...
234 # loop through each tag
235 # warning : we may have differents number of columns in each row. Thus, we first build a hash, complete it if necessary
236 # then construct template.
237 my @fields = $record->fields();
238 my %witness
239 ; #---- stores the list of subfields used at least once, with the "meaning" of the code
240 my @big_array;
241 foreach my $field (@fields) {
242 next if ( $field->tag() < 10 );
243 next if ( ( $field->tag() eq $tag_itemnumber ) &&
244 ( any { $field->subfield($subtag_itemnumber) eq $_ }
245 @items2hide) );
246 my @subf = $field->subfields;
247 my %this_row;
249 # loop through each subfield
250 for my $i ( 0 .. $#subf ) {
251 my $sf_def = $tagslib->{ $field->tag() }->{ $subf[$i][0] };
252 next if ( ($sf_def->{tab}||0) != 10 );
253 next if ( ($sf_def->{hidden}||0) > 0 );
254 $witness{ $subf[$i][0] } = $sf_def->{lib};
256 if ( $sf_def->{isurl} ) {
257 $this_row{ $subf[$i][0] } = "<a href=\"$subf[$i][1]\">$subf[$i][1]</a>";
259 elsif ( $sf_def->{kohafield} eq "biblioitems.isbn" ) {
260 $this_row{ $subf[$i][0] } = $subf[$i][1];
262 else {
263 $this_row{ $subf[$i][0] } = GetAuthorisedValueDesc( $field->tag(), $subf[$i][0],
264 $subf[$i][1], '', $tagslib, '', 'opac' );
267 if (%this_row) {
268 push( @big_array, \%this_row );
271 my ( $holdingbrtagf, $holdingbrtagsubf ) =
272 &GetMarcFromKohaField( "items.holdingbranch", $itemtype );
273 @big_array =
274 sort { ($a->{$holdingbrtagsubf}||'') cmp ($b->{$holdingbrtagsubf}||'') } @big_array;
276 #fill big_row with missing datas
277 foreach my $subfield_code ( keys(%witness) ) {
278 for ( my $i = 0 ; $i <= $#big_array ; $i++ ) {
279 $big_array[$i]{$subfield_code} = "&nbsp;"
280 unless ( $big_array[$i]{$subfield_code} );
284 # now, construct template !
285 my @item_value_loop;
286 my @header_value_loop;
287 for ( my $i = 0 ; $i <= $#big_array ; $i++ ) {
288 my $items_data;
289 foreach my $subfield_code ( keys(%witness) ) {
290 $items_data .= "<td>" . $big_array[$i]{$subfield_code} . "</td>";
292 my %row_data;
293 $row_data{item_value} = $items_data;
294 push( @item_value_loop, \%row_data );
297 foreach my $subfield_code ( keys(%witness) ) {
298 my %header_value;
299 $header_value{header_value} = $witness{$subfield_code};
300 push( @header_value_loop, \%header_value );
303 if(C4::Context->preference("ISBD")) {
304 $template->param(ISBD => 1);
307 #Export options
308 my $OpacExportOptions=C4::Context->preference("OpacExportOptions");
309 my @export_options = split(/\|/,$OpacExportOptions);
310 $template->{VARS}->{'export_options'} = \@export_options;
312 #Search for title in links
313 my $marcflavour = C4::Context->preference("marcflavour");
314 my $dat = TransformMarcToKoha( $dbh, $record );
315 my $isbn = GetNormalizedISBN(undef,$record,$marcflavour);
316 my $marccontrolnumber = GetMarcControlnumber ($record, $marcflavour);
317 my $marcissns = GetMarcISSN( $record, $marcflavour );
318 my $issn = $marcissns->[0] || '';
320 if (my $search_for_title = C4::Context->preference('OPACSearchForTitleIn')){
321 $dat->{title} =~ s/\/+$//; # remove trailing slash
322 $dat->{title} =~ s/\s+$//; # remove trailing space
323 $search_for_title = parametrized_url(
324 $search_for_title,
326 TITLE => $dat->{title},
327 AUTHOR => $dat->{author},
328 ISBN => $isbn,
329 ISSN => $issn,
330 CONTROLNUMBER => $marccontrolnumber,
331 BIBLIONUMBER => $biblionumber,
334 $template->param('OPACSearchForTitleIn' => $search_for_title);
337 $template->param(
338 item_loop => \@item_value_loop,
339 item_header_loop => \@header_value_loop,
340 biblionumber => $biblionumber,
343 output_html_with_http_headers $query, $cookie, $template->output;