Bug 12728: Add StatisticsFields to sysprefs.sql
[koha.git] / opac / opac-MARCdetail.pl
blobf8fab90a6df283712e36b46635af83fbba7e2c21
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;
52 use MARC::Record;
53 use C4::Biblio;
54 use C4::Items;
55 use C4::Acquisition;
56 use C4::Koha;
57 use List::MoreUtils qw/any/;
59 my $query = new CGI;
61 my $dbh = C4::Context->dbh;
63 my $biblionumber = $query->param('biblionumber');
64 if ( ! $biblionumber ) {
65 print $query->redirect("/cgi-bin/koha/errors/404.pl");
66 exit;
69 my @all_items = GetItemsInfo($biblionumber);
70 my @items2hide;
71 if (scalar @all_items >= 1) {
72 push @items2hide, GetHiddenItemnumbers(@all_items);
74 if (scalar @items2hide == scalar @all_items ) {
75 print $query->redirect("/cgi-bin/koha/errors/404.pl");
76 exit;
80 my $itemtype = &GetFrameworkCode($biblionumber);
81 my $tagslib = &GetMarcStructure( 0, $itemtype );
82 my ($tag_itemnumber,$subtag_itemnumber) = &GetMarcFromKohaField('items.itemnumber',$itemtype);
83 my $biblio = GetBiblioData($biblionumber);
84 $biblionumber = $biblio->{biblionumber};
85 my $record = GetMarcBiblio($biblionumber, 1);
86 if ( ! $record ) {
87 print $query->redirect("/cgi-bin/koha/errors/404.pl");
88 exit;
90 # open template
91 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
93 template_name => "opac-MARCdetail.tt",
94 query => $query,
95 type => "opac",
96 authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ),
97 debug => 1,
101 $template->param(
102 bibliotitle => $biblio->{title},
105 # get biblionumbers stored in the cart
106 my @cart_list;
108 if($query->cookie("bib_list")){
109 my $cart_list = $query->cookie("bib_list");
110 @cart_list = split(/\//, $cart_list);
111 if ( grep {$_ eq $biblionumber} @cart_list) {
112 $template->param( incart => 1 );
116 $template->param( 'AllowOnShelfHolds' => C4::Context->preference('AllowOnShelfHolds') );
117 $template->param( 'ItemsIssued' => CountItemsIssued( $biblionumber ) );
119 # adding the $RequestOnOpac param
120 my $RequestOnOpac;
121 if (C4::Context->preference("RequestOnOpac")) {
122 $RequestOnOpac = 1;
125 # fill arrays
126 my @loop_data = ();
127 my $tag;
129 # loop through each tab 0 through 9
130 for ( my $tabloop = 0 ; $tabloop <= 9 ; $tabloop++ ) {
132 # loop through each tag
133 my @loop_data = ();
134 my @subfields_data;
136 # deal with leader
137 unless ( $tagslib->{'000'}->{'@'}->{tab} ne $tabloop
138 or $tagslib->{'000'}->{'@'}->{hidden} > 0 )
140 my %subfield_data;
141 $subfield_data{marc_lib} = $tagslib->{'000'}->{'@'}->{lib};
142 $subfield_data{marc_value} = $record->leader();
143 $subfield_data{marc_subfield} = '@';
144 $subfield_data{marc_tag} = '000';
145 push( @subfields_data, \%subfield_data );
146 my %tag_data;
147 $tag_data{tag} = '000 -' . $tagslib->{'000'}->{lib};
148 my @tmp = @subfields_data;
149 $tag_data{subfield} = \@tmp;
150 push( @loop_data, \%tag_data );
151 undef @subfields_data;
153 my @fields = $record->fields();
154 for ( my $x_i = 0 ; $x_i <= $#fields ; $x_i++ ) {
156 # if tag <10, there's no subfield, use the "@" trick
157 if ( $fields[$x_i]->tag() < 10 ) {
158 next
159 if (
160 $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{tab} ne $tabloop );
161 next if ( $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{hidden} > 0 );
162 my %subfield_data;
163 $subfield_data{marc_lib} =
164 $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{lib};
165 $subfield_data{marc_value} = $fields[$x_i]->data();
166 $subfield_data{marc_subfield} = '@';
167 $subfield_data{marc_tag} = $fields[$x_i]->tag();
168 push( @subfields_data, \%subfield_data );
170 else {
171 my @subf = $fields[$x_i]->subfields;
172 my $previous = '';
173 # loop through each subfield
174 for my $i ( 0 .. $#subf ) {
175 $subf[$i][0] = "@" unless defined($subf[$i][0]);
176 my $sf_def = $tagslib->{ $fields[$x_i]->tag() };
177 $sf_def = $sf_def->{ $subf[$i][0] } if defined($sf_def);
178 my ($tab,$hidden,$lib);
179 $tab = $sf_def->{tab} if defined($sf_def);
180 $tab = $tab // int($fields[$x_i]->tag()/100);
181 $hidden = $sf_def->{hidden} if defined($sf_def);
182 $hidden = $hidden // 0;
183 next if ( $tab != $tabloop );
184 next if ( $hidden > 0 );
185 my %subfield_data;
186 $lib = $sf_def->{lib} if defined($sf_def);
187 $lib = $lib // '--';
188 $subfield_data{marc_lib} = ($lib eq $previous) ? '--' : $lib;
189 $previous = $lib;
190 $subfield_data{link} = $sf_def->{link};
191 $subf[$i][1] =~ s/\n/<br\/>/g;
192 if ( $sf_def->{isurl} ) {
193 $subfield_data{marc_value} = "<a href=\"$subf[$i][1]\">$subf[$i][1]</a>";
195 elsif ( defined($sf_def->{kohafield}) && $sf_def->{kohafield} eq "biblioitems.isbn" ) {
196 $subfield_data{marc_value} = $subf[$i][1];
198 else {
199 if ( $sf_def->{authtypecode} ) {
200 $subfield_data{authority} = $fields[$x_i]->subfield(9);
202 $subfield_data{marc_value} = GetAuthorisedValueDesc( $fields[$x_i]->tag(),
203 $subf[$i][0], $subf[$i][1], '', $tagslib, '', 'opac' );
205 $subfield_data{marc_subfield} = $subf[$i][0];
206 $subfield_data{marc_tag} = $fields[$x_i]->tag();
207 push( @subfields_data, \%subfield_data );
210 if ( $#subfields_data >= 0 ) {
211 my %tag_data;
212 if ( ( $fields[$x_i]->tag() eq $fields[ $x_i - 1 ]->tag() )
213 && ( C4::Context->preference('LabelMARCView') eq 'economical' )
216 $tag_data{tag} = "";
218 else {
219 if ( C4::Context->preference('hide_marc') ) {
220 $tag_data{tag} = $tagslib->{ $fields[$x_i]->tag() }->{lib};
222 else {
223 my $sf_def = $tagslib->{ $fields[$x_i]->tag() };
224 my $lib;
225 $lib = $sf_def->{lib} if defined($sf_def);
226 $lib = $lib // '';
227 $tag_data{tag} = $fields[$x_i]->tag() . ' '
228 . C4::Koha::display_marc_indicators($fields[$x_i])
229 . " - $lib";
232 my @tmp = @subfields_data;
233 $tag_data{subfield} = \@tmp;
234 push( @loop_data, \%tag_data );
235 undef @subfields_data;
238 $template->param( "tab" . $tabloop . "XX" => \@loop_data );
242 # now, build item tab !
243 # the main difference is that datas are in lines and not in columns : thus, we build the <th> first, then the values...
244 # loop through each tag
245 # warning : we may have differents number of columns in each row. Thus, we first build a hash, complete it if necessary
246 # then construct template.
247 my @fields = $record->fields();
248 my %witness
249 ; #---- stores the list of subfields used at least once, with the "meaning" of the code
250 my @big_array;
251 foreach my $field (@fields) {
252 next if ( $field->tag() < 10 );
253 next if ( ( $field->tag() eq $tag_itemnumber ) &&
254 ( any { $field->subfield($subtag_itemnumber) eq $_ }
255 @items2hide) );
256 my @subf = $field->subfields;
257 my %this_row;
259 # loop through each subfield
260 for my $i ( 0 .. $#subf ) {
261 my $sf_def = $tagslib->{ $field->tag() }->{ $subf[$i][0] };
262 next if ( ($sf_def->{tab}||0) != 10 );
263 next if ( ($sf_def->{hidden}||0) > 0 );
264 $witness{ $subf[$i][0] } = $sf_def->{lib};
266 if ( $sf_def->{isurl} ) {
267 $this_row{ $subf[$i][0] } = "<a href=\"$subf[$i][1]\">$subf[$i][1]</a>";
269 elsif ( $sf_def->{kohafield} eq "biblioitems.isbn" ) {
270 $this_row{ $subf[$i][0] } = $subf[$i][1];
272 else {
273 $this_row{ $subf[$i][0] } = GetAuthorisedValueDesc( $field->tag(), $subf[$i][0],
274 $subf[$i][1], '', $tagslib, '', 'opac' );
277 if (%this_row) {
278 push( @big_array, \%this_row );
281 my ( $holdingbrtagf, $holdingbrtagsubf ) =
282 &GetMarcFromKohaField( "items.holdingbranch", $itemtype );
283 @big_array =
284 sort { ($a->{$holdingbrtagsubf}||'') cmp ($b->{$holdingbrtagsubf}||'') } @big_array;
286 #fill big_row with missing datas
287 foreach my $subfield_code ( keys(%witness) ) {
288 for ( my $i = 0 ; $i <= $#big_array ; $i++ ) {
289 $big_array[$i]{$subfield_code} = "&nbsp;"
290 unless ( $big_array[$i]{$subfield_code} );
294 # now, construct template !
295 my @item_value_loop;
296 my @header_value_loop;
297 for ( my $i = 0 ; $i <= $#big_array ; $i++ ) {
298 my $items_data;
299 foreach my $subfield_code ( keys(%witness) ) {
300 $items_data .= "<td>" . $big_array[$i]{$subfield_code} . "</td>";
302 my %row_data;
303 $row_data{item_value} = $items_data;
304 push( @item_value_loop, \%row_data );
307 foreach my $subfield_code ( keys(%witness) ) {
308 my %header_value;
309 $header_value{header_value} = $witness{$subfield_code};
310 push( @header_value_loop, \%header_value );
313 if(C4::Context->preference("ISBD")) {
314 $template->param(ISBD => 1);
317 #Export options
318 my $OpacExportOptions=C4::Context->preference("OpacExportOptions");
319 my @export_options = split(/\|/,$OpacExportOptions);
320 $template->{VARS}->{'export_options'} = \@export_options;
322 #Search for title in links
323 my $marcflavour = C4::Context->preference("marcflavour");
324 my $dat = TransformMarcToKoha( $dbh, $record );
325 my $isbn = GetNormalizedISBN(undef,$record,$marcflavour);
326 my $marccontrolnumber = GetMarcControlnumber ($record, $marcflavour);
327 my $marcissns = GetMarcISSN( $record, $marcflavour );
328 my $issn = $marcissns->[0] || '';
330 if (my $search_for_title = C4::Context->preference('OPACSearchForTitleIn')){
331 $dat->{title} =~ s/\/+$//; # remove trailing slash
332 $dat->{title} =~ s/\s+$//; # remove trailing space
333 $search_for_title = parametrized_url(
334 $search_for_title,
336 TITLE => $dat->{title},
337 AUTHOR => $dat->{author},
338 ISBN => $isbn,
339 ISSN => $issn,
340 CONTROLNUMBER => $marccontrolnumber,
341 BIBLIONUMBER => $biblionumber,
344 $template->param('OPACSearchForTitleIn' => $search_for_title);
347 $template->param(
348 item_loop => \@item_value_loop,
349 item_header_loop => \@header_value_loop,
350 biblionumber => $biblionumber,
353 output_html_with_http_headers $query, $cookie, $template->output;