Bug 10052 Followup for edithelp.tt and help-top.inc
[koha.git] / opac / opac-MARCdetail.pl
blob2a69244228f84f774459b5358fa19b436606ea32
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 $biblionumber = $biblio->{biblionumber};
65 my $record = GetMarcBiblio($biblionumber, 1);
66 if ( ! $record ) {
67 print $query->redirect("/cgi-bin/koha/errors/404.pl");
68 exit;
70 # open template
71 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
73 template_name => "opac-MARCdetail.tmpl",
74 query => $query,
75 type => "opac",
76 authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ),
77 debug => 1,
81 $template->param(
82 bibliotitle => $biblio->{title},
85 # get biblionumbers stored in the cart
86 my @cart_list;
88 if($query->cookie("bib_list")){
89 my $cart_list = $query->cookie("bib_list");
90 @cart_list = split(/\//, $cart_list);
91 if ( grep {$_ eq $biblionumber} @cart_list) {
92 $template->param( incart => 1 );
96 $template->param( 'AllowOnShelfHolds' => C4::Context->preference('AllowOnShelfHolds') );
97 $template->param( 'ItemsIssued' => CountItemsIssued( $biblionumber ) );
99 # adding the $RequestOnOpac param
100 my $RequestOnOpac;
101 if (C4::Context->preference("RequestOnOpac")) {
102 $RequestOnOpac = 1;
105 # fill arrays
106 my @loop_data = ();
107 my $tag;
109 # loop through each tab 0 through 9
110 for ( my $tabloop = 0 ; $tabloop <= 9 ; $tabloop++ ) {
112 # loop through each tag
113 my @loop_data = ();
114 my @subfields_data;
116 # deal with leader
117 unless ( $tagslib->{'000'}->{'@'}->{tab} ne $tabloop
118 or $tagslib->{'000'}->{'@'}->{hidden} > 0 )
120 my %subfield_data;
121 $subfield_data{marc_lib} = $tagslib->{'000'}->{'@'}->{lib};
122 $subfield_data{marc_value} = $record->leader();
123 $subfield_data{marc_subfield} = '@';
124 $subfield_data{marc_tag} = '000';
125 push( @subfields_data, \%subfield_data );
126 my %tag_data;
127 $tag_data{tag} = '000 -' . $tagslib->{'000'}->{lib};
128 my @tmp = @subfields_data;
129 $tag_data{subfield} = \@tmp;
130 push( @loop_data, \%tag_data );
131 undef @subfields_data;
133 my @fields = $record->fields();
134 for ( my $x_i = 0 ; $x_i <= $#fields ; $x_i++ ) {
136 # if tag <10, there's no subfield, use the "@" trick
137 if ( $fields[$x_i]->tag() < 10 ) {
138 next
139 if (
140 $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{tab} ne $tabloop );
141 next if ( $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{hidden} > 0 );
142 my %subfield_data;
143 $subfield_data{marc_lib} =
144 $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{lib};
145 $subfield_data{marc_value} = $fields[$x_i]->data();
146 $subfield_data{marc_subfield} = '@';
147 $subfield_data{marc_tag} = $fields[$x_i]->tag();
148 push( @subfields_data, \%subfield_data );
150 else {
151 my @subf = $fields[$x_i]->subfields;
152 my $previous = '';
153 # loop through each subfield
154 for my $i ( 0 .. $#subf ) {
155 $subf[$i][0] = "@" unless defined($subf[$i][0]);
156 my $sf_def = $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] };
157 next if ( ($sf_def->{tab}||0) != $tabloop );
158 next if ( ($sf_def->{hidden}||0) > 0 );
159 my %subfield_data;
160 $subfield_data{marc_lib} = ($sf_def->{lib} eq $previous) ? '--' : $sf_def->{lib};
161 $previous = $sf_def->{lib};
162 $subfield_data{link} = $sf_def->{link};
163 $subf[$i][1] =~ s/\n/<br\/>/g;
164 if ( $sf_def->{isurl} ) {
165 $subfield_data{marc_value} = "<a href=\"$subf[$i][1]\">$subf[$i][1]</a>";
167 elsif ( defined($sf_def->{kohafield}) && $sf_def->{kohafield} eq "biblioitems.isbn" ) {
168 $subfield_data{marc_value} = $subf[$i][1];
170 else {
171 if ( $sf_def->{authtypecode} ) {
172 $subfield_data{authority} = $fields[$x_i]->subfield(9);
174 $subfield_data{marc_value} = GetAuthorisedValueDesc( $fields[$x_i]->tag(),
175 $subf[$i][0], $subf[$i][1], '', $tagslib, '', 'opac' );
177 $subfield_data{marc_subfield} = $subf[$i][0];
178 $subfield_data{marc_tag} = $fields[$x_i]->tag();
179 push( @subfields_data, \%subfield_data );
182 if ( $#subfields_data >= 0 ) {
183 my %tag_data;
184 if ( ( $fields[$x_i]->tag() eq $fields[ $x_i - 1 ]->tag() )
185 && ( C4::Context->preference('LabelMARCView') eq 'economical' )
188 $tag_data{tag} = "";
190 else {
191 if ( C4::Context->preference('hide_marc') ) {
192 $tag_data{tag} = $tagslib->{ $fields[$x_i]->tag() }->{lib};
194 else {
195 $tag_data{tag} =
196 $fields[$x_i]->tag()
197 . ' '
198 . C4::Koha::display_marc_indicators($fields[$x_i])
199 . ' - '
200 . $tagslib->{ $fields[$x_i]->tag() }->{lib};
203 my @tmp = @subfields_data;
204 $tag_data{subfield} = \@tmp;
205 push( @loop_data, \%tag_data );
206 undef @subfields_data;
209 $template->param( "tab" . $tabloop . "XX" => \@loop_data );
213 # now, build item tab !
214 # the main difference is that datas are in lines and not in columns : thus, we build the <th> first, then the values...
215 # loop through each tag
216 # warning : we may have differents number of columns in each row. Thus, we first build a hash, complete it if necessary
217 # then construct template.
218 my @fields = $record->fields();
219 my %witness
220 ; #---- stores the list of subfields used at least once, with the "meaning" of the code
221 my @big_array;
222 foreach my $field (@fields) {
223 next if ( $field->tag() < 10 );
224 my @subf = $field->subfields;
225 my %this_row;
227 # loop through each subfield
228 for my $i ( 0 .. $#subf ) {
229 my $sf_def = $tagslib->{ $field->tag() }->{ $subf[$i][0] };
230 next if ( ($sf_def->{tab}||0) != 10 );
231 next if ( ($sf_def->{hidden}||0) > 0 );
232 $witness{ $subf[$i][0] } = $sf_def->{lib};
234 if ( $sf_def->{isurl} ) {
235 $this_row{ $subf[$i][0] } = "<a href=\"$subf[$i][1]\">$subf[$i][1]</a>";
237 elsif ( $sf_def->{kohafield} eq "biblioitems.isbn" ) {
238 $this_row{ $subf[$i][0] } = $subf[$i][1];
240 else {
241 $this_row{ $subf[$i][0] } = GetAuthorisedValueDesc( $field->tag(), $subf[$i][0],
242 $subf[$i][1], '', $tagslib, '', 'opac' );
245 if (%this_row) {
246 push( @big_array, \%this_row );
249 my ( $holdingbrtagf, $holdingbrtagsubf ) =
250 &GetMarcFromKohaField( "items.holdingbranch", $itemtype );
251 @big_array =
252 sort { ($a->{$holdingbrtagsubf}||'') cmp ($b->{$holdingbrtagsubf}||'') } @big_array;
254 #fill big_row with missing datas
255 foreach my $subfield_code ( keys(%witness) ) {
256 for ( my $i = 0 ; $i <= $#big_array ; $i++ ) {
257 $big_array[$i]{$subfield_code} = "&nbsp;"
258 unless ( $big_array[$i]{$subfield_code} );
262 # now, construct template !
263 my @item_value_loop;
264 my @header_value_loop;
265 for ( my $i = 0 ; $i <= $#big_array ; $i++ ) {
266 my $items_data;
267 foreach my $subfield_code ( keys(%witness) ) {
268 $items_data .= "<td>" . $big_array[$i]{$subfield_code} . "</td>";
270 my %row_data;
271 $row_data{item_value} = $items_data;
272 push( @item_value_loop, \%row_data );
275 foreach my $subfield_code ( keys(%witness) ) {
276 my %header_value;
277 $header_value{header_value} = $witness{$subfield_code};
278 push( @header_value_loop, \%header_value );
281 if(C4::Context->preference("ISBD")) {
282 $template->param(ISBD => 1);
285 #Export options
286 my $OpacExportOptions=C4::Context->preference("OpacExportOptions");
287 my @export_options = split(/\|/,$OpacExportOptions);
288 $template->{VARS}->{'export_options'} = \@export_options;
290 #Search for title in links
291 my $marcflavour = C4::Context->preference("marcflavour");
292 my $dat = TransformMarcToKoha( $dbh, $record );
293 my $isbn = GetNormalizedISBN(undef,$record,$marcflavour);
294 my $marccontrolnumber = GetMarcControlnumber ($record, $marcflavour);
295 my $marcissns = GetMarcISSN( $record, $marcflavour );
296 my $issn = $marcissns->[0] || '';
298 if (my $search_for_title = C4::Context->preference('OPACSearchForTitleIn')){
299 $dat->{title} =~ s/\/+$//; # remove trailing slash
300 $dat->{title} =~ s/\s+$//; # remove trailing space
301 $search_for_title = parametrized_url(
302 $search_for_title,
304 TITLE => $dat->{title},
305 AUTHOR => $dat->{author},
306 ISBN => $isbn,
307 ISSN => $issn,
308 CONTROLNUMBER => $marccontrolnumber,
309 BIBLIONUMBER => $biblionumber,
312 $template->param('OPACSearchForTitleIn' => $search_for_title);
315 $template->param(
316 item_loop => \@item_value_loop,
317 item_header_loop => \@header_value_loop,
318 biblionumber => $biblionumber,
321 output_html_with_http_headers $query, $cookie, $template->output;