Bug 23248: Avoid opac-ISBDdetail.pl breaking on invalid biblionumber
[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 uniq );
60 use Koha::Biblios;
61 use Koha::IssuingRules;
62 use Koha::Items;
63 use Koha::ItemTypes;
64 use Koha::Patrons;
65 use Koha::RecordProcessor;
66
67 my $query = CGI->new();
68
69 my $biblionumber = $query->param('biblionumber');
70 if ( ! $biblionumber ) {
71     print $query->redirect("/cgi-bin/koha/errors/404.pl");
72     exit;
73 }
74 $biblionumber = int($biblionumber);
75
76 # open template
77 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
78     {
79         template_name   => "opac-MARCdetail.tt",
80         query           => $query,
81         type            => "opac",
82         authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ),
83         debug           => 1,
84     }
85 );
86
87 my $patron = Koha::Patrons->find( $loggedinuser );
88 my $borcat = q{};
89 if ( C4::Context->preference('OpacHiddenItemsExceptions') ) {
90     # we need to fetch the borrower info here, so we can pass the category
91     $borcat = $patron ? $patron->categorycode : $borcat;
92 }
93
94 my $record = GetMarcBiblio({
95     biblionumber => $biblionumber,
96     embed_items  => 1,
97     opac         => 1,
98     borcat       => $borcat });
99 if ( ! $record ) {
100     print $query->redirect("/cgi-bin/koha/errors/404.pl");
101     exit;
102 }
103
104 my @all_items = GetItemsInfo($biblionumber);
105 my $biblio = Koha::Biblios->find( $biblionumber );
106 my $framework = $biblio ? $biblio->frameworkcode : q{};
107 my $tagslib = &GetMarcStructure( 0, $framework );
108 my ($tag_itemnumber,$subtag_itemnumber) = &GetMarcFromKohaField('items.itemnumber',$framework);
109 my @nonhiddenitems = $record->field($tag_itemnumber);
110 if (scalar @all_items >= 1 && scalar @nonhiddenitems == 0) {
111     print $query->redirect("/cgi-bin/koha/errors/404.pl");
112     exit;
113 }
114
115 my $record_processor = Koha::RecordProcessor->new({
116     filters => 'ViewPolicy',
117     options => {
118         interface => 'opac',
119         frameworkcode => $framework
120     }
121 });
122 $record_processor->process($record);
123
124 # get biblionumbers stored in the cart
125 if(my $cart_list = $query->cookie("bib_list")){
126     my @cart_list = split(/\//, $cart_list);
127     if ( grep {$_ eq $biblionumber} @cart_list) {
128         $template->param( incart => 1 );
129     }
130 }
131
132 my ($bt_tag,$bt_subtag) = GetMarcFromKohaField('biblio.title',$framework);
133 $template->param(
134     bibliotitle => $biblio->title,
135 ) if $tagslib->{$bt_tag}->{$bt_subtag}->{hidden} <= 0 && # <=0 OPAC visible.
136      $tagslib->{$bt_tag}->{$bt_subtag}->{hidden} > -8;   # except -8;
137
138 my $allow_onshelf_holds;
139 for my $itm (@all_items) {
140     my $item = Koha::Items->find( $itm->{itemnumber} );
141     $allow_onshelf_holds = Koha::IssuingRules->get_onshelfholds_policy( { item => $item, patron => $patron } );
142     last if $allow_onshelf_holds;
143 }
144
145 if( $allow_onshelf_holds || CountItemsIssued($biblionumber) || $biblio->has_items_waiting_or_intransit ) {
146     $template->param( ReservableItems => 1 );
147 }
148
149 # adding the $RequestOnOpac param
150 my $RequestOnOpac;
151 if (C4::Context->preference("RequestOnOpac")) {
152         $RequestOnOpac = 1;
153 }
154
155 # fill arrays
156 my @loop_data = ();
157 my $tag;
158
159 # loop through each tab 0 through 9
160 for ( my $tabloop = 0 ; $tabloop <= 9 ; $tabloop++ ) {
161
162     # loop through each tag
163     my @loop_data = ();
164     my @subfields_data;
165
166     # deal with leader
167     unless ( $tagslib->{'000'}->{'@'}->{tab} ne $tabloop
168         or $tagslib->{'000'}->{'@'}->{hidden} > 0 )
169     {
170         my %subfield_data;
171         $subfield_data{marc_lib}      = $tagslib->{'000'}->{'@'}->{lib};
172         $subfield_data{marc_value}    = $record->leader();
173         $subfield_data{marc_subfield} = '@';
174         $subfield_data{marc_tag}      = '000';
175         push( @subfields_data, \%subfield_data );
176         my %tag_data;
177         $tag_data{tag} = '000 -' . $tagslib->{'000'}->{lib};
178         my @tmp = @subfields_data;
179         $tag_data{subfield} = \@tmp;
180         push( @loop_data, \%tag_data );
181         undef @subfields_data;
182     }
183     my @fields = $record->fields();
184     for ( my $x_i = 0 ; $x_i <= $#fields ; $x_i++ ) {
185
186         # if tag <10, there's no subfield, use the "@" trick
187         if ( $fields[$x_i]->tag() < 10 ) {
188             next
189               if (
190                 $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{tab} ne $tabloop );
191             next if ( $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{hidden} > 0 );
192             my %subfield_data;
193             $subfield_data{marc_lib} =
194               $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{lib};
195             $subfield_data{marc_value}    = $fields[$x_i]->data();
196             $subfield_data{marc_subfield} = '@';
197             $subfield_data{marc_tag}      = $fields[$x_i]->tag();
198             push( @subfields_data, \%subfield_data );
199         }
200         else {
201             my @subf = $fields[$x_i]->subfields;
202             my $previous = '';
203             # loop through each subfield
204             for my $i ( 0 .. $#subf ) {
205                 $subf[$i][0] = "@" unless defined($subf[$i][0]);
206                 my $sf_def = $tagslib->{ $fields[$x_i]->tag() };
207                 $sf_def = $sf_def->{ $subf[$i][0] } if defined($sf_def);
208                 my ($tab,$hidden,$lib);
209                 $tab = $sf_def->{tab} if defined($sf_def);
210                 $tab = $tab // int($fields[$x_i]->tag()/100);
211                 $hidden = $sf_def->{hidden} if defined($sf_def);
212                 $hidden = $hidden // 0;
213                 next if ( $tab != $tabloop );
214                 next if ( $hidden > 0 );
215                 my %subfield_data;
216                 $lib = $sf_def->{lib} if defined($sf_def);
217                 $lib = $lib // '--';
218                 $subfield_data{marc_lib} = ($lib eq $previous) ?  '--' : $lib;
219                 $previous = $lib;
220                 $subfield_data{link} = $sf_def->{link};
221                 $subf[$i][1] =~ s/\n/<br\/>/g;
222                 if ( $sf_def->{isurl} ) {
223                     $subfield_data{marc_value} = "<a href=\"$subf[$i][1]\">$subf[$i][1]</a>";
224                 }
225                 elsif ( defined($sf_def->{kohafield}) && $sf_def->{kohafield} eq "biblioitems.isbn" ) {
226                     $subfield_data{marc_value} = $subf[$i][1];
227                 }
228                 else {
229                     if ( $sf_def->{authtypecode} ) {
230                         $subfield_data{authority} = $fields[$x_i]->subfield(9);
231                     }
232                     $subfield_data{marc_value} = GetAuthorisedValueDesc( $fields[$x_i]->tag(),
233                         $subf[$i][0], $subf[$i][1], '', $tagslib, '', 'opac' );
234                 }
235                 $subfield_data{marc_subfield} = $subf[$i][0];
236                 $subfield_data{marc_tag}      = $fields[$x_i]->tag();
237                 push( @subfields_data, \%subfield_data );
238             }
239         }
240         if ( $#subfields_data >= 0 ) {
241             my %tag_data;
242             if (   ( $fields[$x_i]->tag() eq $fields[ $x_i - 1 ]->tag() )
243                 && ( C4::Context->preference('LabelMARCView') eq 'economical' )
244               )
245             {
246                 $tag_data{tag} = "";
247             }
248             else {
249                 if ( C4::Context->preference('hide_marc') ) {
250                     $tag_data{tag} = $tagslib->{ $fields[$x_i]->tag() }->{lib};
251                 }
252                 else {
253                     my $sf_def = $tagslib->{ $fields[$x_i]->tag() };
254                     my $lib;
255                     $lib = $sf_def->{lib} if defined($sf_def);
256                     $lib = $lib // '';
257                     $tag_data{tag} = $fields[$x_i]->tag() . ' '
258                       . C4::Koha::display_marc_indicators($fields[$x_i])
259                       . " - $lib";
260                 }
261             }
262             my @tmp = @subfields_data;
263             $tag_data{subfield} = \@tmp;
264             push( @loop_data, \%tag_data );
265             undef @subfields_data;
266         }
267     }
268     $template->param( "tab" . $tabloop . "XX" => \@loop_data );
269 }
270
271
272 # now, build item tab !
273 # the main difference is that datas are in lines and not in columns : thus, we build the <th> first, then the values...
274 # loop through each tag
275 # warning : we may have differents number of columns in each row. Thus, we first build a hash, complete it if necessary
276 # then construct template.
277 # $record has already had all the item fields filtered above.
278 my @fields = $record->fields();
279 my %witness
280   ; #---- stores the list of subfields used at least once, with the "meaning" of the code
281 my @item_subfield_codes;
282 my @item_loop;
283 foreach my $field (@fields) {
284     next if ( $field->tag() < 10 );
285     my @subf = $field->subfields;
286     my $item;
287
288     # loop through each subfield
289     for my $i ( 0 .. $#subf ) {
290         my $sf_def = $tagslib->{ $field->tag() }->{ $subf[$i][0] };
291         next if ( ($sf_def->{tab}||0) != 10 );
292         next if ( ($sf_def->{hidden}||0) > 0 );
293         push @item_subfield_codes, $subf[$i][0];
294         $witness{ $subf[$i][0] } = $sf_def->{lib};
295
296         if ( $sf_def->{isurl} ) {
297             $item->{ $subf[$i][0] } = "<a href=\"$subf[$i][1]\">$subf[$i][1]</a>";
298         }
299         elsif ( $sf_def->{kohafield} eq "biblioitems.isbn" ) {
300             $item->{ $subf[$i][0] } = $subf[$i][1];
301         }
302         else {
303             $item->{ $subf[$i][0] } = GetAuthorisedValueDesc( $field->tag(), $subf[$i][0],
304                 $subf[$i][1], '', $tagslib, '', 'opac' );
305         }
306     }
307     push @item_loop, $item if $item;
308 }
309 my ( $holdingbrtagf, $holdingbrtagsubf ) =
310   &GetMarcFromKohaField( "items.holdingbranch", $framework );
311 @item_loop =
312   sort { ($a->{$holdingbrtagsubf}||'') cmp ($b->{$holdingbrtagsubf}||'') } @item_loop;
313
314 @item_subfield_codes = uniq @item_subfield_codes;
315 # fill item info
316 my @item_header_loop;
317 for my $subfield_code ( @item_subfield_codes ) {
318     push @item_header_loop, $witness{$subfield_code};
319     for my $item_data ( @item_loop ) {
320         $item_data->{$subfield_code} ||= "&nbsp;"
321      }
322 }
323
324 if ( C4::Context->preference("OPACISBD") ) {
325     $template->param( ISBD => 1 );
326 }
327
328 #Search for title in links
329 my $marcflavour  = C4::Context->preference("marcflavour");
330 my $dat = TransformMarcToKoha( $record );
331 my $isbn = GetNormalizedISBN(undef,$record,$marcflavour);
332 my $marccontrolnumber   = GetMarcControlnumber ($record, $marcflavour);
333 my $marcissns = GetMarcISSN( $record, $marcflavour );
334 my $issn = $marcissns->[0] || '';
335
336 if (my $search_for_title = C4::Context->preference('OPACSearchForTitleIn')){
337     $dat->{title} =~ s/\/+$//; # remove trailing slash
338     $dat->{title} =~ s/\s+$//; # remove trailing space
339     $search_for_title = parametrized_url(
340         $search_for_title,
341         {
342             TITLE         => $dat->{title},
343             AUTHOR        => $dat->{author},
344             ISBN          => $isbn,
345             ISSN          => $issn,
346             CONTROLNUMBER => $marccontrolnumber,
347             BIBLIONUMBER  => $biblionumber,
348         }
349     );
350     $template->param('OPACSearchForTitleIn' => $search_for_title);
351 }
352
353 if( C4::Context->preference('ArticleRequests') ) {
354     my $itemtype = Koha::ItemTypes->find($biblio->itemtype);
355     my $artreqpossible = $patron
356         ? $biblio->can_article_request( $patron )
357         : $itemtype
358         ? $itemtype->may_article_request
359         : q{};
360     $template->param( artreqpossible => $artreqpossible );
361 }
362
363 $template->param(
364     item_loop           => \@item_loop,
365     item_header_loop    => \@item_header_loop,
366     item_subfield_codes => \@item_subfield_codes,
367     biblio              => $biblio,
368 );
369
370 output_html_with_http_headers $query, $cookie, $template->output;