Bug 12721 - Prevent software error if incorrect fieldnames given in sypref Statistics...
[koha.git] / opac / opac-MARCdetail.pl
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 #       Copyright (C) 2000-2002 Katipo Communications
6 # Parts Copyright (C) 2010      BibLibre
7 # Parts Copyright (C) 2013      Mark Tompsett
8 #
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.
13 #
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.
18 #
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>.
21
22
23 =head1 NAME
24
25 opac-MARCdetail.pl : script to show a biblio in MARC format
26
27 =head1 SYNOPSIS
28
29 =cut
30
31 =head1 DESCRIPTION
32
33 This script needs a biblionumber as  parameter
34
35 It shows the biblio in a (nice) MARC format depending on MARC
36 parameters tables.
37
38 The template is in <templates_dir>/catalogue/MARCdetail.tt.
39 this template must be divided into 11 "tabs".
40
41 The first 10 tabs present the biblio, the 11th one presents
42 the items attached to the biblio
43
44 =cut
45
46 use Modern::Perl;
47
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/;
60
61 my $query = new CGI;
62
63 my $dbh = C4::Context->dbh;
64
65 my $biblionumber = $query->param('biblionumber');
66 if ( ! $biblionumber ) {
67     print $query->redirect("/cgi-bin/koha/errors/404.pl");
68     exit;
69 }
70
71 my @all_items = GetItemsInfo($biblionumber);
72 my @items2hide;
73 if (scalar @all_items >= 1) {
74     push @items2hide, GetHiddenItemnumbers(@all_items);
75
76     if (scalar @items2hide == scalar @all_items ) {
77         print $query->redirect("/cgi-bin/koha/errors/404.pl");
78         exit;
79     }
80 }
81
82 my $itemtype     = &GetFrameworkCode($biblionumber);
83 my $tagslib      = &GetMarcStructure( 0, $itemtype );
84 my ($tag_itemnumber,$subtag_itemnumber) = &GetMarcFromKohaField('items.itemnumber',$itemtype);
85 my $biblio = GetBiblioData($biblionumber);
86 $biblionumber = $biblio->{biblionumber};
87 my $record = GetMarcBiblio($biblionumber, 1);
88 if ( ! $record ) {
89     print $query->redirect("/cgi-bin/koha/errors/404.pl");
90     exit;
91 }
92 # open template
93 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
94     {
95         template_name   => "opac-MARCdetail.tt",
96         query           => $query,
97         type            => "opac",
98         authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ),
99         debug           => 1,
100     }
101 );
102
103 my ($bt_tag,$bt_subtag) = GetMarcFromKohaField('biblio.title',$itemtype);
104 $template->param(
105     bibliotitle => $biblio->{title},
106 ) if $tagslib->{$bt_tag}->{$bt_subtag}->{hidden} <= 0; #<=0 is OPAC visible.
107
108 # get biblionumbers stored in the cart
109 if(my $cart_list = $query->cookie("bib_list")){
110     my @cart_list = split(/\//, $cart_list);
111     if ( grep {$_ eq $biblionumber} @cart_list) {
112         $template->param( incart => 1 );
113     }
114 }
115
116 my $allow_onshelf_holds;
117 my $borrower = GetMember( 'borrowernumber' => $loggedinuser );
118 for my $itm (@all_items) {
119     $allow_onshelf_holds = C4::Reserves::OnShelfHoldsAllowed($itm, $borrower);
120     last if $allow_onshelf_holds;
121 }
122
123 $template->param( 'AllowOnShelfHolds' => $allow_onshelf_holds );
124 $template->param( 'ItemsIssued' => CountItemsIssued( $biblionumber ) );
125
126 # adding the $RequestOnOpac param
127 my $RequestOnOpac;
128 if (C4::Context->preference("RequestOnOpac")) {
129         $RequestOnOpac = 1;
130 }
131
132 # fill arrays
133 my @loop_data = ();
134 my $tag;
135
136 # loop through each tab 0 through 9
137 for ( my $tabloop = 0 ; $tabloop <= 9 ; $tabloop++ ) {
138
139     # loop through each tag
140     my @loop_data = ();
141     my @subfields_data;
142
143     # deal with leader
144     unless ( $tagslib->{'000'}->{'@'}->{tab} ne $tabloop
145         or $tagslib->{'000'}->{'@'}->{hidden} > 0 )
146     {
147         my %subfield_data;
148         $subfield_data{marc_lib}      = $tagslib->{'000'}->{'@'}->{lib};
149         $subfield_data{marc_value}    = $record->leader();
150         $subfield_data{marc_subfield} = '@';
151         $subfield_data{marc_tag}      = '000';
152         push( @subfields_data, \%subfield_data );
153         my %tag_data;
154         $tag_data{tag} = '000 -' . $tagslib->{'000'}->{lib};
155         my @tmp = @subfields_data;
156         $tag_data{subfield} = \@tmp;
157         push( @loop_data, \%tag_data );
158         undef @subfields_data;
159     }
160     my @fields = $record->fields();
161     for ( my $x_i = 0 ; $x_i <= $#fields ; $x_i++ ) {
162
163         # if tag <10, there's no subfield, use the "@" trick
164         if ( $fields[$x_i]->tag() < 10 ) {
165             next
166               if (
167                 $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{tab} ne $tabloop );
168             next if ( $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{hidden} > 0 );
169             my %subfield_data;
170             $subfield_data{marc_lib} =
171               $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{lib};
172             $subfield_data{marc_value}    = $fields[$x_i]->data();
173             $subfield_data{marc_subfield} = '@';
174             $subfield_data{marc_tag}      = $fields[$x_i]->tag();
175             push( @subfields_data, \%subfield_data );
176         }
177         else {
178             my @subf = $fields[$x_i]->subfields;
179             my $previous = '';
180             # loop through each subfield
181             for my $i ( 0 .. $#subf ) {
182                 $subf[$i][0] = "@" unless defined($subf[$i][0]);
183                 my $sf_def = $tagslib->{ $fields[$x_i]->tag() };
184                 $sf_def = $sf_def->{ $subf[$i][0] } if defined($sf_def);
185                 my ($tab,$hidden,$lib);
186                 $tab = $sf_def->{tab} if defined($sf_def);
187                 $tab = $tab // int($fields[$x_i]->tag()/100);
188                 $hidden = $sf_def->{hidden} if defined($sf_def);
189                 $hidden = $hidden // 0;
190                 next if ( $tab != $tabloop );
191                 next if ( $hidden > 0 );
192                 my %subfield_data;
193                 $lib = $sf_def->{lib} if defined($sf_def);
194                 $lib = $lib // '--';
195                 $subfield_data{marc_lib} = ($lib eq $previous) ?  '--' : $lib;
196                 $previous = $lib;
197                 $subfield_data{link} = $sf_def->{link};
198                 $subf[$i][1] =~ s/\n/<br\/>/g;
199                 if ( $sf_def->{isurl} ) {
200                     $subfield_data{marc_value} = "<a href=\"$subf[$i][1]\">$subf[$i][1]</a>";
201                 }
202                 elsif ( defined($sf_def->{kohafield}) && $sf_def->{kohafield} eq "biblioitems.isbn" ) {
203                     $subfield_data{marc_value} = $subf[$i][1];
204                 }
205                 else {
206                     if ( $sf_def->{authtypecode} ) {
207                         $subfield_data{authority} = $fields[$x_i]->subfield(9);
208                     }
209                     $subfield_data{marc_value} = GetAuthorisedValueDesc( $fields[$x_i]->tag(),
210                         $subf[$i][0], $subf[$i][1], '', $tagslib, '', 'opac' );
211                 }
212                 $subfield_data{marc_subfield} = $subf[$i][0];
213                 $subfield_data{marc_tag}      = $fields[$x_i]->tag();
214                 push( @subfields_data, \%subfield_data );
215             }
216         }
217         if ( $#subfields_data >= 0 ) {
218             my %tag_data;
219             if (   ( $fields[$x_i]->tag() eq $fields[ $x_i - 1 ]->tag() )
220                 && ( C4::Context->preference('LabelMARCView') eq 'economical' )
221               )
222             {
223                 $tag_data{tag} = "";
224             }
225             else {
226                 if ( C4::Context->preference('hide_marc') ) {
227                     $tag_data{tag} = $tagslib->{ $fields[$x_i]->tag() }->{lib};
228                 }
229                 else {
230                     my $sf_def = $tagslib->{ $fields[$x_i]->tag() };
231                     my $lib;
232                     $lib = $sf_def->{lib} if defined($sf_def);
233                     $lib = $lib // '';
234                     $tag_data{tag} = $fields[$x_i]->tag() . ' '
235                       . C4::Koha::display_marc_indicators($fields[$x_i])
236                       . " - $lib";
237                 }
238             }
239             my @tmp = @subfields_data;
240             $tag_data{subfield} = \@tmp;
241             push( @loop_data, \%tag_data );
242             undef @subfields_data;
243         }
244     }
245     $template->param( "tab" . $tabloop . "XX" => \@loop_data );
246 }
247
248
249 # now, build item tab !
250 # the main difference is that datas are in lines and not in columns : thus, we build the <th> first, then the values...
251 # loop through each tag
252 # warning : we may have differents number of columns in each row. Thus, we first build a hash, complete it if necessary
253 # then construct template.
254 my @fields = $record->fields();
255 my %witness
256   ; #---- stores the list of subfields used at least once, with the "meaning" of the code
257 my @big_array;
258 foreach my $field (@fields) {
259     next if ( $field->tag() < 10 );
260     next if ( ( $field->tag() eq $tag_itemnumber ) &&
261               ( any { $field->subfield($subtag_itemnumber) eq $_ }
262                    @items2hide) );
263     my @subf = $field->subfields;
264     my %this_row;
265
266     # loop through each subfield
267     for my $i ( 0 .. $#subf ) {
268         my $sf_def = $tagslib->{ $field->tag() }->{ $subf[$i][0] };
269         next if ( ($sf_def->{tab}||0) != 10 );
270         next if ( ($sf_def->{hidden}||0) > 0 );
271         $witness{ $subf[$i][0] } = $sf_def->{lib};
272
273         if ( $sf_def->{isurl} ) {
274             $this_row{ $subf[$i][0] } = "<a href=\"$subf[$i][1]\">$subf[$i][1]</a>";
275         }
276         elsif ( $sf_def->{kohafield} eq "biblioitems.isbn" ) {
277             $this_row{ $subf[$i][0] } = $subf[$i][1];
278         }
279         else {
280             $this_row{ $subf[$i][0] } = GetAuthorisedValueDesc( $field->tag(), $subf[$i][0],
281                 $subf[$i][1], '', $tagslib, '', 'opac' );
282         }
283     }
284     if (%this_row) {
285         push( @big_array, \%this_row );
286     }
287 }
288 my ( $holdingbrtagf, $holdingbrtagsubf ) =
289   &GetMarcFromKohaField( "items.holdingbranch", $itemtype );
290 @big_array =
291   sort { ($a->{$holdingbrtagsubf}||'') cmp ($b->{$holdingbrtagsubf}||'') } @big_array;
292
293 #fill big_row with missing datas
294 foreach my $subfield_code ( keys(%witness) ) {
295     for ( my $i = 0 ; $i <= $#big_array ; $i++ ) {
296         $big_array[$i]{$subfield_code} = "&nbsp;"
297           unless ( $big_array[$i]{$subfield_code} );
298     }
299 }
300
301 # now, construct template !
302 my @item_value_loop;
303 my @header_value_loop;
304 for ( my $i = 0 ; $i <= $#big_array ; $i++ ) {
305     my $items_data;
306     foreach my $subfield_code ( keys(%witness) ) {
307         $items_data .= "<td>" . $big_array[$i]{$subfield_code} . "</td>";
308     }
309     my %row_data;
310     $row_data{item_value} = $items_data;
311     push( @item_value_loop, \%row_data );
312 }
313
314 foreach my $subfield_code ( keys(%witness) ) {
315     my %header_value;
316     $header_value{header_value} = $witness{$subfield_code};
317     push( @header_value_loop, \%header_value );
318 }
319
320 if(C4::Context->preference("ISBD")) {
321         $template->param(ISBD => 1);
322 }
323
324 #Search for title in links
325 my $marcflavour  = C4::Context->preference("marcflavour");
326 my $dat = TransformMarcToKoha( $dbh, $record );
327 my $isbn = GetNormalizedISBN(undef,$record,$marcflavour);
328 my $marccontrolnumber   = GetMarcControlnumber ($record, $marcflavour);
329 my $marcissns = GetMarcISSN( $record, $marcflavour );
330 my $issn = $marcissns->[0] || '';
331
332 if (my $search_for_title = C4::Context->preference('OPACSearchForTitleIn')){
333     $dat->{title} =~ s/\/+$//; # remove trailing slash
334     $dat->{title} =~ s/\s+$//; # remove trailing space
335     $search_for_title = parametrized_url(
336         $search_for_title,
337         {
338             TITLE         => $dat->{title},
339             AUTHOR        => $dat->{author},
340             ISBN          => $isbn,
341             ISSN          => $issn,
342             CONTROLNUMBER => $marccontrolnumber,
343             BIBLIONUMBER  => $biblionumber,
344         }
345     );
346     $template->param('OPACSearchForTitleIn' => $search_for_title);
347 }
348
349 $template->param(
350     item_loop        => \@item_value_loop,
351     item_header_loop => \@header_value_loop,
352     biblionumber     => $biblionumber,
353 );
354
355 output_html_with_http_headers $query, $cookie, $template->output;