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