Bug 18577: Add FIXME about the missing FK
[koha.git] / opac / opac-MARCdetail.pl
blobd758af28917982eb9af35bac00c9df73dd209d6e
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 qw ( -utf8 );
52 use MARC::Record;
53 use C4::Biblio;
54 use C4::Items;
55 use C4::Reserves;
56 use C4::Members;
57 use C4::Acquisition;
58 use C4::Koha;
59 use List::MoreUtils qw( any uniq );
60 use Koha::Patrons;
61 use Koha::RecordProcessor;
63 my $query = new CGI;
65 my $dbh = C4::Context->dbh;
67 my $biblionumber = $query->param('biblionumber');
68 if ( ! $biblionumber ) {
69 print $query->redirect("/cgi-bin/koha/errors/404.pl");
70 exit;
73 my $record = GetMarcBiblio($biblionumber, 1);
74 if ( ! $record ) {
75 print $query->redirect("/cgi-bin/koha/errors/404.pl");
76 exit;
79 my @all_items = GetItemsInfo($biblionumber);
80 my @items2hide;
81 if (scalar @all_items >= 1) {
82 push @items2hide, GetHiddenItemnumbers(@all_items);
84 if (scalar @items2hide == scalar @all_items ) {
85 print $query->redirect("/cgi-bin/koha/errors/404.pl");
86 exit;
90 my $framework = &GetFrameworkCode( $biblionumber );
91 my $tagslib = &GetMarcStructure( 0, $framework );
92 my ($tag_itemnumber,$subtag_itemnumber) = &GetMarcFromKohaField('items.itemnumber',$framework);
93 my $biblio = GetBiblioData($biblionumber);
95 my $record_processor = Koha::RecordProcessor->new({
96 filters => 'ViewPolicy',
97 options => {
98 interface => 'opac',
99 frameworkcode => $framework
102 $record_processor->process($record);
104 # open template
105 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
107 template_name => "opac-MARCdetail.tt",
108 query => $query,
109 type => "opac",
110 authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ),
111 debug => 1,
115 my ($bt_tag,$bt_subtag) = GetMarcFromKohaField('biblio.title',$framework);
116 $template->param(
117 bibliotitle => $biblio->{title},
118 ) if $tagslib->{$bt_tag}->{$bt_subtag}->{hidden} <= 0 && # <=0 OPAC visible.
119 $tagslib->{$bt_tag}->{$bt_subtag}->{hidden} > -8; # except -8;
121 # get biblionumbers stored in the cart
122 if(my $cart_list = $query->cookie("bib_list")){
123 my @cart_list = split(/\//, $cart_list);
124 if ( grep {$_ eq $biblionumber} @cart_list) {
125 $template->param( incart => 1 );
129 my $allow_onshelf_holds;
130 my $patron = Koha::Patrons->find( $loggedinuser )->unblessed;
131 for my $itm (@all_items) {
132 $allow_onshelf_holds = C4::Reserves::OnShelfHoldsAllowed($itm, $patron);
133 last if $allow_onshelf_holds;
136 $template->param( 'AllowOnShelfHolds' => $allow_onshelf_holds );
137 $template->param( 'ItemsIssued' => CountItemsIssued( $biblionumber ) );
139 # adding the $RequestOnOpac param
140 my $RequestOnOpac;
141 if (C4::Context->preference("RequestOnOpac")) {
142 $RequestOnOpac = 1;
145 # fill arrays
146 my @loop_data = ();
147 my $tag;
149 # loop through each tab 0 through 9
150 for ( my $tabloop = 0 ; $tabloop <= 9 ; $tabloop++ ) {
152 # loop through each tag
153 my @loop_data = ();
154 my @subfields_data;
156 # deal with leader
157 unless ( $tagslib->{'000'}->{'@'}->{tab} ne $tabloop
158 or $tagslib->{'000'}->{'@'}->{hidden} > 0 )
160 my %subfield_data;
161 $subfield_data{marc_lib} = $tagslib->{'000'}->{'@'}->{lib};
162 $subfield_data{marc_value} = $record->leader();
163 $subfield_data{marc_subfield} = '@';
164 $subfield_data{marc_tag} = '000';
165 push( @subfields_data, \%subfield_data );
166 my %tag_data;
167 $tag_data{tag} = '000 -' . $tagslib->{'000'}->{lib};
168 my @tmp = @subfields_data;
169 $tag_data{subfield} = \@tmp;
170 push( @loop_data, \%tag_data );
171 undef @subfields_data;
173 my @fields = $record->fields();
174 for ( my $x_i = 0 ; $x_i <= $#fields ; $x_i++ ) {
176 # if tag <10, there's no subfield, use the "@" trick
177 if ( $fields[$x_i]->tag() < 10 ) {
178 next
179 if (
180 $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{tab} ne $tabloop );
181 next if ( $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{hidden} > 0 );
182 my %subfield_data;
183 $subfield_data{marc_lib} =
184 $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{lib};
185 $subfield_data{marc_value} = $fields[$x_i]->data();
186 $subfield_data{marc_subfield} = '@';
187 $subfield_data{marc_tag} = $fields[$x_i]->tag();
188 push( @subfields_data, \%subfield_data );
190 else {
191 my @subf = $fields[$x_i]->subfields;
192 my $previous = '';
193 # loop through each subfield
194 for my $i ( 0 .. $#subf ) {
195 $subf[$i][0] = "@" unless defined($subf[$i][0]);
196 my $sf_def = $tagslib->{ $fields[$x_i]->tag() };
197 $sf_def = $sf_def->{ $subf[$i][0] } if defined($sf_def);
198 my ($tab,$hidden,$lib);
199 $tab = $sf_def->{tab} if defined($sf_def);
200 $tab = $tab // int($fields[$x_i]->tag()/100);
201 $hidden = $sf_def->{hidden} if defined($sf_def);
202 $hidden = $hidden // 0;
203 next if ( $tab != $tabloop );
204 next if ( $hidden > 0 );
205 my %subfield_data;
206 $lib = $sf_def->{lib} if defined($sf_def);
207 $lib = $lib // '--';
208 $subfield_data{marc_lib} = ($lib eq $previous) ? '--' : $lib;
209 $previous = $lib;
210 $subfield_data{link} = $sf_def->{link};
211 $subf[$i][1] =~ s/\n/<br\/>/g;
212 if ( $sf_def->{isurl} ) {
213 $subfield_data{marc_value} = "<a href=\"$subf[$i][1]\">$subf[$i][1]</a>";
215 elsif ( defined($sf_def->{kohafield}) && $sf_def->{kohafield} eq "biblioitems.isbn" ) {
216 $subfield_data{marc_value} = $subf[$i][1];
218 else {
219 if ( $sf_def->{authtypecode} ) {
220 $subfield_data{authority} = $fields[$x_i]->subfield(9);
222 $subfield_data{marc_value} = GetAuthorisedValueDesc( $fields[$x_i]->tag(),
223 $subf[$i][0], $subf[$i][1], '', $tagslib, '', 'opac' );
225 $subfield_data{marc_subfield} = $subf[$i][0];
226 $subfield_data{marc_tag} = $fields[$x_i]->tag();
227 push( @subfields_data, \%subfield_data );
230 if ( $#subfields_data >= 0 ) {
231 my %tag_data;
232 if ( ( $fields[$x_i]->tag() eq $fields[ $x_i - 1 ]->tag() )
233 && ( C4::Context->preference('LabelMARCView') eq 'economical' )
236 $tag_data{tag} = "";
238 else {
239 if ( C4::Context->preference('hide_marc') ) {
240 $tag_data{tag} = $tagslib->{ $fields[$x_i]->tag() }->{lib};
242 else {
243 my $sf_def = $tagslib->{ $fields[$x_i]->tag() };
244 my $lib;
245 $lib = $sf_def->{lib} if defined($sf_def);
246 $lib = $lib // '';
247 $tag_data{tag} = $fields[$x_i]->tag() . ' '
248 . C4::Koha::display_marc_indicators($fields[$x_i])
249 . " - $lib";
252 my @tmp = @subfields_data;
253 $tag_data{subfield} = \@tmp;
254 push( @loop_data, \%tag_data );
255 undef @subfields_data;
258 $template->param( "tab" . $tabloop . "XX" => \@loop_data );
262 # now, build item tab !
263 # the main difference is that datas are in lines and not in columns : thus, we build the <th> first, then the values...
264 # loop through each tag
265 # warning : we may have differents number of columns in each row. Thus, we first build a hash, complete it if necessary
266 # then construct template.
267 my @fields = $record->fields();
268 my %witness
269 ; #---- stores the list of subfields used at least once, with the "meaning" of the code
270 my @item_subfield_codes;
271 my @item_loop;
272 foreach my $field (@fields) {
273 next if ( $field->tag() < 10 );
274 next if ( ( $field->tag() eq $tag_itemnumber ) &&
275 ( any { $field->subfield($subtag_itemnumber) eq $_ }
276 @items2hide) );
277 my @subf = $field->subfields;
278 my $item;
280 # loop through each subfield
281 for my $i ( 0 .. $#subf ) {
282 my $sf_def = $tagslib->{ $field->tag() }->{ $subf[$i][0] };
283 next if ( ($sf_def->{tab}||0) != 10 );
284 next if ( ($sf_def->{hidden}||0) > 0 );
285 push @item_subfield_codes, $subf[$i][0];
286 $witness{ $subf[$i][0] } = $sf_def->{lib};
288 if ( $sf_def->{isurl} ) {
289 $item->{ $subf[$i][0] } = "<a href=\"$subf[$i][1]\">$subf[$i][1]</a>";
291 elsif ( $sf_def->{kohafield} eq "biblioitems.isbn" ) {
292 $item->{ $subf[$i][0] } = $subf[$i][1];
294 else {
295 $item->{ $subf[$i][0] } = GetAuthorisedValueDesc( $field->tag(), $subf[$i][0],
296 $subf[$i][1], '', $tagslib, '', 'opac' );
299 push @item_loop, $item if $item;
301 my ( $holdingbrtagf, $holdingbrtagsubf ) =
302 &GetMarcFromKohaField( "items.holdingbranch", $framework );
303 @item_loop =
304 sort { ($a->{$holdingbrtagsubf}||'') cmp ($b->{$holdingbrtagsubf}||'') } @item_loop;
306 @item_subfield_codes = uniq @item_subfield_codes;
307 # fill item info
308 my @item_header_loop;
309 for my $subfield_code ( @item_subfield_codes ) {
310 push @item_header_loop, $witness{$subfield_code};
311 for my $item_data ( @item_loop ) {
312 $item_data->{$subfield_code} ||= "&nbsp;"
316 if ( C4::Context->preference("OPACISBD") ) {
317 $template->param( ISBD => 1 );
320 #Search for title in links
321 my $marcflavour = C4::Context->preference("marcflavour");
322 my $dat = TransformMarcToKoha( $record );
323 my $isbn = GetNormalizedISBN(undef,$record,$marcflavour);
324 my $marccontrolnumber = GetMarcControlnumber ($record, $marcflavour);
325 my $marcissns = GetMarcISSN( $record, $marcflavour );
326 my $issn = $marcissns->[0] || '';
328 if (my $search_for_title = C4::Context->preference('OPACSearchForTitleIn')){
329 $dat->{title} =~ s/\/+$//; # remove trailing slash
330 $dat->{title} =~ s/\s+$//; # remove trailing space
331 $search_for_title = parametrized_url(
332 $search_for_title,
334 TITLE => $dat->{title},
335 AUTHOR => $dat->{author},
336 ISBN => $isbn,
337 ISSN => $issn,
338 CONTROLNUMBER => $marccontrolnumber,
339 BIBLIONUMBER => $biblionumber,
342 $template->param('OPACSearchForTitleIn' => $search_for_title);
345 $template->param(
346 item_loop => \@item_loop,
347 item_header_loop => \@item_header_loop,
348 item_subfield_codes => \@item_subfield_codes,
349 biblionumber => $biblionumber,
352 output_html_with_http_headers $query, $cookie, $template->output;