Added coverage to unit test for TTParser
[koha.git] / opac / opac-MARCdetail.pl
blob2d9dc25241ff6ac8bf2fcc47b2c9de9365cfed10
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::Acquisition;
54 use C4::Koha;
56 my $query = new CGI;
58 my $dbh = C4::Context->dbh;
60 my $biblionumber = $query->param('biblionumber');
61 my $itemtype = &GetFrameworkCode($biblionumber);
62 my $tagslib = &GetMarcStructure( 0, $itemtype );
63 my $biblio = GetBiblioData($biblionumber);
64 my $record = GetMarcBiblio($biblionumber, 1);
65 if ( ! $record ) {
66 print $query->redirect("/cgi-bin/koha/errors/404.pl");
67 exit;
69 # open template
70 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
72 template_name => "opac-MARCdetail.tmpl",
73 query => $query,
74 type => "opac",
75 authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ),
76 debug => 1,
80 $template->param(
81 bibliotitle => $biblio->{title},
84 # get biblionumbers stored in the cart
85 my @cart_list;
87 if($query->cookie("bib_list")){
88 my $cart_list = $query->cookie("bib_list");
89 @cart_list = split(/\//, $cart_list);
90 if ( grep {$_ eq $biblionumber} @cart_list) {
91 $template->param( incart => 1 );
95 $template->param( 'AllowOnShelfHolds' => C4::Context->preference('AllowOnShelfHolds') );
96 $template->param( 'ItemsIssued' => CountItemsIssued( $biblionumber ) );
98 # adding the $RequestOnOpac param
99 my $RequestOnOpac;
100 if (C4::Context->preference("RequestOnOpac")) {
101 $RequestOnOpac = 1;
104 # fill arrays
105 my @loop_data = ();
106 my $tag;
108 # loop through each tab 0 through 9
109 for ( my $tabloop = 0 ; $tabloop <= 9 ; $tabloop++ ) {
111 # loop through each tag
112 my @loop_data = ();
113 my @subfields_data;
115 # deal with leader
116 unless ( $tagslib->{'000'}->{'@'}->{tab} ne $tabloop
117 or $tagslib->{'000'}->{'@'}->{hidden} > 0 )
119 my %subfield_data;
120 $subfield_data{marc_lib} = $tagslib->{'000'}->{'@'}->{lib};
121 $subfield_data{marc_value} = $record->leader();
122 $subfield_data{marc_subfield} = '@';
123 $subfield_data{marc_tag} = '000';
124 push( @subfields_data, \%subfield_data );
125 my %tag_data;
126 $tag_data{tag} = '000 -' . $tagslib->{'000'}->{lib};
127 my @tmp = @subfields_data;
128 $tag_data{subfield} = \@tmp;
129 push( @loop_data, \%tag_data );
130 undef @subfields_data;
132 my @fields = $record->fields();
133 for ( my $x_i = 0 ; $x_i <= $#fields ; $x_i++ ) {
135 # if tag <10, there's no subfield, use the "@" trick
136 if ( $fields[$x_i]->tag() < 10 ) {
137 next
138 if (
139 $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{tab} ne $tabloop );
140 next if ( $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{hidden} > 0 );
141 my %subfield_data;
142 $subfield_data{marc_lib} =
143 $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{lib};
144 $subfield_data{marc_value} = $fields[$x_i]->data();
145 $subfield_data{marc_subfield} = '@';
146 $subfield_data{marc_tag} = $fields[$x_i]->tag();
147 push( @subfields_data, \%subfield_data );
149 else {
150 my @subf = $fields[$x_i]->subfields;
151 my $previous = '';
152 # loop through each subfield
153 for my $i ( 0 .. $#subf ) {
154 $subf[$i][0] = "@" unless defined($subf[$i][0]);
155 my $sf_def = $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] };
156 next if ( ($sf_def->{tab}||0) != $tabloop );
157 next if ( ($sf_def->{hidden}||0) > 0 );
158 my %subfield_data;
159 $subfield_data{marc_lib} = ($sf_def->{lib} eq $previous) ? '--' : $sf_def->{lib};
160 $previous = $sf_def->{lib};
161 $subfield_data{link} = $sf_def->{link};
162 $subf[$i][1] =~ s/\n/<br\/>/g;
163 if ( $sf_def->{isurl} ) {
164 $subfield_data{marc_value} = "<a href=\"$subf[$i][1]\">$subf[$i][1]</a>";
166 elsif ( defined($sf_def->{kohafield}) && $sf_def->{kohafield} eq "biblioitems.isbn" ) {
167 $subfield_data{marc_value} = $subf[$i][1];
169 else {
170 if ( $sf_def->{authtypecode} ) {
171 $subfield_data{authority} = $fields[$x_i]->subfield(9);
173 $subfield_data{marc_value} = GetAuthorisedValueDesc( $fields[$x_i]->tag(),
174 $subf[$i][0], $subf[$i][1], '', $tagslib, '', 'opac' );
176 $subfield_data{marc_subfield} = $subf[$i][0];
177 $subfield_data{marc_tag} = $fields[$x_i]->tag();
178 push( @subfields_data, \%subfield_data );
181 if ( $#subfields_data >= 0 ) {
182 my %tag_data;
183 if ( ( $fields[$x_i]->tag() eq $fields[ $x_i - 1 ]->tag() )
184 && ( C4::Context->preference('LabelMARCView') eq 'economical' )
187 $tag_data{tag} = "";
189 else {
190 if ( C4::Context->preference('hide_marc') ) {
191 $tag_data{tag} = $tagslib->{ $fields[$x_i]->tag() }->{lib};
193 else {
194 $tag_data{tag} =
195 $fields[$x_i]->tag()
196 . ' '
197 . C4::Koha::display_marc_indicators($fields[$x_i])
198 . ' - '
199 . $tagslib->{ $fields[$x_i]->tag() }->{lib};
202 my @tmp = @subfields_data;
203 $tag_data{subfield} = \@tmp;
204 push( @loop_data, \%tag_data );
205 undef @subfields_data;
208 $template->param( "tab" . $tabloop . "XX" => \@loop_data );
212 # now, build item tab !
213 # the main difference is that datas are in lines and not in columns : thus, we build the <th> first, then the values...
214 # loop through each tag
215 # warning : we may have differents number of columns in each row. Thus, we first build a hash, complete it if necessary
216 # then construct template.
217 my @fields = $record->fields();
218 my %witness
219 ; #---- stores the list of subfields used at least once, with the "meaning" of the code
220 my @big_array;
221 foreach my $field (@fields) {
222 next if ( $field->tag() < 10 );
223 my @subf = $field->subfields;
224 my %this_row;
226 # loop through each subfield
227 for my $i ( 0 .. $#subf ) {
228 my $sf_def = $tagslib->{ $field->tag() }->{ $subf[$i][0] };
229 next if ( ($sf_def->{tab}||0) != 10 );
230 next if ( ($sf_def->{hidden}||0) > 0 );
231 $witness{ $subf[$i][0] } = $sf_def->{lib};
233 if ( $sf_def->{isurl} ) {
234 $this_row{ $subf[$i][0] } = "<a href=\"$subf[$i][1]\">$subf[$i][1]</a>";
236 elsif ( $sf_def->{kohafield} eq "biblioitems.isbn" ) {
237 $this_row{ $subf[$i][0] } = $subf[$i][1];
239 else {
240 $this_row{ $subf[$i][0] } = GetAuthorisedValueDesc( $field->tag(), $subf[$i][0],
241 $subf[$i][1], '', $tagslib, '', 'opac' );
244 if (%this_row) {
245 push( @big_array, \%this_row );
248 my ( $holdingbrtagf, $holdingbrtagsubf ) =
249 &GetMarcFromKohaField( "items.holdingbranch", $itemtype );
250 @big_array =
251 sort { ($a->{$holdingbrtagsubf}||'') cmp ($b->{$holdingbrtagsubf}||'') } @big_array;
253 #fill big_row with missing datas
254 foreach my $subfield_code ( keys(%witness) ) {
255 for ( my $i = 0 ; $i <= $#big_array ; $i++ ) {
256 $big_array[$i]{$subfield_code} = "&nbsp;"
257 unless ( $big_array[$i]{$subfield_code} );
261 # now, construct template !
262 my @item_value_loop;
263 my @header_value_loop;
264 for ( my $i = 0 ; $i <= $#big_array ; $i++ ) {
265 my $items_data;
266 foreach my $subfield_code ( keys(%witness) ) {
267 $items_data .= "<td>" . $big_array[$i]{$subfield_code} . "</td>";
269 my %row_data;
270 $row_data{item_value} = $items_data;
271 push( @item_value_loop, \%row_data );
274 foreach my $subfield_code ( keys(%witness) ) {
275 my %header_value;
276 $header_value{header_value} = $witness{$subfield_code};
277 push( @header_value_loop, \%header_value );
280 if(C4::Context->preference("ISBD")) {
281 $template->param(ISBD => 1);
284 #Export options
285 my $OpacExportOptions=C4::Context->preference("OpacExportOptions");
286 my @export_options = split(/\|/,$OpacExportOptions);
287 $template->{VARS}->{'export_options'} = \@export_options;
289 #Search for title in links
290 my $marcflavour = C4::Context->preference("marcflavour");
291 my $dat = TransformMarcToKoha( $dbh, $record );
292 my $isbn = GetNormalizedISBN(undef,$record,$marcflavour);
293 my $marccontrolnumber = GetMarcControlnumber ($record, $marcflavour);
294 my $marcissns = GetMarcISSN( $record, $marcflavour );
295 my $issn = $marcissns->[0] || '';
297 if (my $search_for_title = C4::Context->preference('OPACSearchForTitleIn')){
298 $dat->{author} ? $search_for_title =~ s/{AUTHOR}/$dat->{author}/g : $search_for_title =~ s/{AUTHOR}//g;
299 $dat->{title} =~ s/\/+$//; # remove trailing slash
300 $dat->{title} =~ s/\s+$//; # remove trailing space
301 $dat->{title} ? $search_for_title =~ s/{TITLE}/$dat->{title}/g : $search_for_title =~ s/{TITLE}//g;
302 $isbn ? $search_for_title =~ s/{ISBN}/$isbn/g : $search_for_title =~ s/{ISBN}//g;
303 $issn ? $search_for_title =~ s/{ISSN}/$issn/g : $search_for_title =~ s/{ISSN}//g;
304 $marccontrolnumber ? $search_for_title =~ s/{CONTROLNUMBER}/$marccontrolnumber/g : $search_for_title =~ s/{CONTROLNUMBER}//g;
305 $search_for_title =~ s/{BIBLIONUMBER}/$biblionumber/g;
306 $template->param('OPACSearchForTitleIn' => $search_for_title);
309 $template->param(
310 item_loop => \@item_value_loop,
311 item_header_loop => \@header_value_loop,
312 biblionumber => $biblionumber,
315 output_html_with_http_headers $query, $cookie, $template->output;