Bug 5230: Call number ranges in export don't give expected results.
[koha.git] / opac / opac-MARCdetail.pl
blobfd909f5e8138b10646415f087ef17ad3b402d1f8
1 #!/usr/bin/perl
3 # Copyright 2000-2002 Katipo Communications
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 =head1 NAME
22 MARCdetail.pl : script to show a biblio in MARC format
24 =head1 SYNOPSIS
26 =cut
28 =head1 DESCRIPTION
30 This script needs a biblionumber as parameter
32 It shows the biblio in a (nice) MARC format depending on MARC
33 parameters tables.
35 The template is in <templates_dir>/catalogue/MARCdetail.tmpl.
36 this template must be divided into 11 "tabs".
38 The first 10 tabs present the biblio, the 11th one presents
39 the items attached to the biblio
41 =cut
43 use strict;
44 use warnings;
46 use C4::Auth;
47 use C4::Context;
48 use C4::Output;
49 use CGI;
50 use MARC::Record;
51 use C4::Biblio;
52 use C4::Acquisition;
53 use C4::Koha;
55 my $query = new CGI;
57 my $dbh = C4::Context->dbh;
59 my $biblionumber = $query->param('biblionumber');
60 my $itemtype = &GetFrameworkCode($biblionumber);
61 my $tagslib = &GetMarcStructure( 0, $itemtype );
62 my $biblio = GetBiblioData($biblionumber);
63 my $record = GetMarcBiblio($biblionumber);
64 if ( ! $record ) {
65 print $query->redirect("/cgi-bin/koha/errors/404.pl");
66 exit;
68 # open template
69 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
71 template_name => "opac-MARCdetail.tmpl",
72 query => $query,
73 type => "opac",
74 authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ),
75 debug => 1,
79 $template->param(
80 bibliotitle => $biblio->{title},
83 $template->param( 'AllowOnShelfHolds' => C4::Context->preference('AllowOnShelfHolds') );
84 $template->param( 'ItemsIssued' => CountItemsIssued( $biblionumber ) );
86 # adding the $RequestOnOpac param
87 my $RequestOnOpac;
88 if (C4::Context->preference("RequestOnOpac")) {
89 $RequestOnOpac = 1;
92 # fill arrays
93 my @loop_data = ();
94 my $tag;
96 # loop through each tab 0 through 9
97 for ( my $tabloop = 0 ; $tabloop <= 10 ; $tabloop++ ) {
99 # loop through each tag
100 my @loop_data = ();
101 my @subfields_data;
103 # deal with leader
104 unless ( $tagslib->{'000'}->{'@'}->{tab} ne $tabloop
105 or $tagslib->{'000'}->{'@'}->{hidden} > 0 )
107 my %subfield_data;
108 $subfield_data{marc_lib} = $tagslib->{'000'}->{'@'}->{lib};
109 $subfield_data{marc_value} = $record->leader();
110 $subfield_data{marc_subfield} = '@';
111 $subfield_data{marc_tag} = '000';
112 push( @subfields_data, \%subfield_data );
113 my %tag_data;
114 $tag_data{tag} = '000 -' . $tagslib->{'000'}->{lib};
115 my @tmp = @subfields_data;
116 $tag_data{subfield} = \@tmp;
117 push( @loop_data, \%tag_data );
118 undef @subfields_data;
120 my @fields = $record->fields();
121 for ( my $x_i = 0 ; $x_i <= $#fields ; $x_i++ ) {
123 # if tag <10, there's no subfield, use the "@" trick
124 if ( $fields[$x_i]->tag() < 10 ) {
125 next
126 if (
127 $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{tab} ne $tabloop );
128 next if ( $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{hidden} > 0 );
129 my %subfield_data;
130 $subfield_data{marc_lib} =
131 $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{lib};
132 $subfield_data{marc_value} = $fields[$x_i]->data();
133 $subfield_data{marc_subfield} = '@';
134 $subfield_data{marc_tag} = $fields[$x_i]->tag();
135 push( @subfields_data, \%subfield_data );
137 else {
138 my @subf = $fields[$x_i]->subfields;
139 my $previous = '';
140 # loop through each subfield
141 for my $i ( 0 .. $#subf ) {
142 $subf[$i][0] = "@" unless $subf[$i][0];
143 my $sf_def = $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] };
144 next if ( $sf_def->{tab} ne $tabloop );
145 next if ( $sf_def->{hidden} > 0 );
146 my %subfield_data;
147 $subfield_data{marc_lib} = ($sf_def->{lib} eq $previous) ? '--' : $sf_def->{lib};
148 $previous = $sf_def->{lib};
149 $subfield_data{link} = $sf_def->{link};
150 $subf[$i][1] =~ s/\n/<br\/>/g;
151 if ( $sf_def->{isurl} ) {
152 $subfield_data{marc_value} = "<a href=\"$subf[$i][1]\">$subf[$i][1]</a>";
154 elsif ( defined($sf_def->{kohafield}) && $sf_def->{kohafield} eq "biblioitems.isbn" ) {
155 $subfield_data{marc_value} = $subf[$i][1];
157 else {
158 if ( $sf_def->{authtypecode} ) {
159 $subfield_data{authority} = $fields[$x_i]->subfield(9);
161 $subfield_data{marc_value} = GetAuthorisedValueDesc( $fields[$x_i]->tag(),
162 $subf[$i][0], $subf[$i][1], '', $tagslib, '', 'opac' );
164 $subfield_data{marc_subfield} = $subf[$i][0];
165 $subfield_data{marc_tag} = $fields[$x_i]->tag();
166 push( @subfields_data, \%subfield_data );
169 if ( $#subfields_data >= 0 ) {
170 my %tag_data;
171 if ( ( $fields[$x_i]->tag() eq $fields[ $x_i - 1 ]->tag() )
172 && ( C4::Context->preference('LabelMARCView') eq 'economical' )
175 $tag_data{tag} = "";
177 else {
178 if ( C4::Context->preference('hide_marc') ) {
179 $tag_data{tag} = $tagslib->{ $fields[$x_i]->tag() }->{lib};
181 else {
182 $tag_data{tag} =
183 $fields[$x_i]->tag()
184 . ' '
185 . C4::Koha::display_marc_indicators($fields[$x_i])
186 . ' - '
187 . $tagslib->{ $fields[$x_i]->tag() }->{lib};
190 my @tmp = @subfields_data;
191 $tag_data{subfield} = \@tmp;
192 push( @loop_data, \%tag_data );
193 undef @subfields_data;
196 $template->param( $tabloop . "XX" => \@loop_data );
200 # now, build item tab !
201 # the main difference is that datas are in lines and not in columns : thus, we build the <th> first, then the values...
202 # loop through each tag
203 # warning : we may have differents number of columns in each row. Thus, we first build a hash, complete it if necessary
204 # then construct template.
205 my @fields = $record->fields();
206 my %witness
207 ; #---- stores the list of subfields used at least once, with the "meaning" of the code
208 my @big_array;
209 foreach my $field (@fields) {
210 next if ( $field->tag() < 10 );
211 my @subf = $field->subfields;
212 my %this_row;
214 # loop through each subfield
215 for my $i ( 0 .. $#subf ) {
216 my $sf_def = $tagslib->{ $field->tag() }->{ $subf[$i][0] };
217 next if ( $sf_def->{tab} ne 10 );
218 next if ( $sf_def->{hidden} > 0 );
219 $witness{ $subf[$i][0] } = $sf_def->{lib};
221 if ( $sf_def->{isurl} ) {
222 $this_row{ $subf[$i][0] } = "<a href=\"$subf[$i][1]\">$subf[$i][1]</a>";
224 elsif ( $sf_def->{kohafield} eq "biblioitems.isbn" ) {
225 $this_row{ $subf[$i][0] } = $subf[$i][1];
227 else {
228 $this_row{ $subf[$i][0] } = GetAuthorisedValueDesc( $field->tag(), $subf[$i][0],
229 $subf[$i][1], '', $tagslib, '', 'opac' );
232 if (%this_row) {
233 push( @big_array, \%this_row );
236 my ( $holdingbrtagf, $holdingbrtagsubf ) =
237 &GetMarcFromKohaField( "items.holdingbranch", $itemtype );
238 @big_array =
239 sort { $a->{$holdingbrtagsubf} cmp $b->{$holdingbrtagsubf} } @big_array;
241 #fill big_row with missing datas
242 foreach my $subfield_code ( keys(%witness) ) {
243 for ( my $i = 0 ; $i <= $#big_array ; $i++ ) {
244 $big_array[$i]{$subfield_code} = "&nbsp;"
245 unless ( $big_array[$i]{$subfield_code} );
249 # now, construct template !
250 my @item_value_loop;
251 my @header_value_loop;
252 for ( my $i = 0 ; $i <= $#big_array ; $i++ ) {
253 my $items_data;
254 foreach my $subfield_code ( keys(%witness) ) {
255 $items_data .= "<td>" . $big_array[$i]{$subfield_code} . "</td>";
257 my %row_data;
258 $row_data{item_value} = $items_data;
259 push( @item_value_loop, \%row_data );
262 foreach my $subfield_code ( keys(%witness) ) {
263 my %header_value;
264 $header_value{header_value} = $witness{$subfield_code};
265 push( @header_value_loop, \%header_value );
268 if(C4::Context->preference("ISBD")) {
269 $template->param(ISBD => 1);
272 #Search for title in links
273 if (my $search_for_title = C4::Context->preference('OPACSearchForTitleIn')){
274 $biblio->{author} ? $search_for_title =~ s/{AUTHOR}/$biblio->{author}/g : $search_for_title =~ s/{AUTHOR}//g;
275 $biblio->{title} =~ s/\/+$//; # remove trailing slash
276 $biblio->{title} =~ s/\s+$//; # remove trailing space
277 $biblio->{title} ? $search_for_title =~ s/{TITLE}/$biblio->{title}/g : $search_for_title =~ s/{TITLE}//g;
278 $biblio->{isbn} ? $search_for_title =~ s/{ISBN}/$biblio->{isbn}/g : $search_for_title =~ s/{ISBN}//g;
279 $template->param('OPACSearchForTitleIn' => $search_for_title);
282 $template->param(
283 item_loop => \@item_value_loop,
284 item_header_loop => \@header_value_loop,
285 biblionumber => $biblionumber,
288 output_html_with_http_headers $query, $cookie, $template->output;