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