Merge commit 'kc/master'
[koha.git] / opac / opac-MARCdetail.pl
1 #!/usr/bin/perl
2
3 # Copyright 2000-2002 Katipo Communications
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20 =head1 NAME
21
22 MARCdetail.pl : script to show a biblio in MARC format
23
24 =head1 SYNOPSIS
25
26
27 =head1 DESCRIPTION
28
29 This script needs a biblionumber as  parameter
30
31 It shows the biblio in a (nice) MARC format depending on MARC
32 parameters tables.
33
34 The template is in <templates_dir>/catalogue/MARCdetail.tmpl.
35 this template must be divided into 11 "tabs".
36
37 The first 10 tabs present the biblio, the 11th one presents
38 the items attached to the biblio
39
40 =cut
41
42 use strict;
43 use warnings;
44
45 use C4::Auth;
46 use C4::Context;
47 use C4::Output;
48 use CGI;
49 use MARC::Record;
50 use C4::Biblio;
51 use C4::Acquisition;
52 use C4::Koha;
53
54 my $query = new CGI;
55
56 my $dbh = C4::Context->dbh;
57
58 my $biblionumber = $query->param('biblionumber');
59 my $itemtype     = &GetFrameworkCode($biblionumber);
60 my $tagslib      = &GetMarcStructure( 0, $itemtype );
61 my $biblio = GetBiblioData($biblionumber);
62 my $record = GetMarcBiblio($biblionumber);
63 if ( ! $record ) {
64     print $query->redirect("/cgi-bin/koha/errors/404.pl");
65     exit;
66 }
67 # open template
68 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
69     {
70         template_name   => "opac-MARCdetail.tmpl",
71         query           => $query,
72         type            => "opac",
73         authnotrequired => 1,
74         debug           => 1,
75     }
76 );
77
78 $template->param(
79     bibliotitle => $biblio->{title},
80 );
81
82 $template->param( 'AllowOnShelfHolds' => C4::Context->preference('AllowOnShelfHolds') );
83 $template->param( 'ItemsIssued' => CountItemsIssued( $biblionumber ) );
84
85 # adding the $RequestOnOpac param
86 my $RequestOnOpac;
87 if (C4::Context->preference("RequestOnOpac")) {
88         $RequestOnOpac = 1;
89 }
90
91 # fill arrays
92 my @loop_data = ();
93 my $tag;
94
95 # loop through each tab 0 through 9
96 for ( my $tabloop = 0 ; $tabloop <= 10 ; $tabloop++ ) {
97
98     # loop through each tag
99     my @loop_data = ();
100     my @subfields_data;
101
102     # deal with leader
103     unless ( $tagslib->{'000'}->{'@'}->{tab} ne $tabloop
104         or $tagslib->{'000'}->{'@'}->{hidden} > 0 )
105     {
106         my %subfield_data;
107         $subfield_data{marc_lib}      = $tagslib->{'000'}->{'@'}->{lib};
108         $subfield_data{marc_value}    = $record->leader();
109         $subfield_data{marc_subfield} = '@';
110         $subfield_data{marc_tag}      = '000';
111         push( @subfields_data, \%subfield_data );
112         my %tag_data;
113         $tag_data{tag} = '000 -' . $tagslib->{'000'}->{lib};
114         my @tmp = @subfields_data;
115         $tag_data{subfield} = \@tmp;
116         push( @loop_data, \%tag_data );
117         undef @subfields_data;
118     }
119     my @fields = $record->fields();
120     for ( my $x_i = 0 ; $x_i <= $#fields ; $x_i++ ) {
121
122         # if tag <10, there's no subfield, use the "@" trick
123         if ( $fields[$x_i]->tag() < 10 ) {
124             next
125               if (
126                 $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{tab} ne $tabloop );
127             next if ( $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{hidden} > 0 );
128             my %subfield_data;
129             $subfield_data{marc_lib} =
130               $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{lib};
131             $subfield_data{marc_value}    = $fields[$x_i]->data();
132             $subfield_data{marc_subfield} = '@';
133             $subfield_data{marc_tag}      = $fields[$x_i]->tag();
134             push( @subfields_data, \%subfield_data );
135         }
136         else {
137             my @subf = $fields[$x_i]->subfields;
138             my $previous = '';
139             # loop through each subfield
140             for my $i ( 0 .. $#subf ) {
141                 $subf[$i][0] = "@" unless $subf[$i][0];
142                 my $sf_def = $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] };
143                 next if ( $sf_def->{tab} ne $tabloop );
144                 next if ( $sf_def->{hidden} > 0 ); 
145                 my %subfield_data;
146                 $subfield_data{marc_lib} = ($sf_def->{lib} eq $previous) ?  '--' : $sf_def->{lib};
147                 $previous = $sf_def->{lib};
148                 $subfield_data{link} = $sf_def->{link};
149                 $subf[$i][1] =~ s/\n/<br\/>/g;
150                 if ( $sf_def->{isurl} ) {
151                     $subfield_data{marc_value} = "<a href=\"$subf[$i][1]\">$subf[$i][1]</a>";
152                 }
153                 elsif ( defined($sf_def->{kohafield}) && $sf_def->{kohafield} eq "biblioitems.isbn" ) {
154                     $subfield_data{marc_value} = $subf[$i][1];
155                 }
156                 else {
157                     if ( $sf_def->{authtypecode} ) {
158                         $subfield_data{authority} = $fields[$x_i]->subfield(9);
159                     }
160                     $subfield_data{marc_value} = GetAuthorisedValueDesc( $fields[$x_i]->tag(),
161                         $subf[$i][0], $subf[$i][1], '', $tagslib, '', 'opac' );
162                 }
163                 $subfield_data{marc_subfield} = $subf[$i][0];
164                 $subfield_data{marc_tag}      = $fields[$x_i]->tag();
165                 push( @subfields_data, \%subfield_data );
166             }
167         }
168         if ( $#subfields_data >= 0 ) {
169             my %tag_data;
170             if (   ( $fields[$x_i]->tag() eq $fields[ $x_i - 1 ]->tag() )
171                 && ( C4::Context->preference('LabelMARCView') eq 'economical' )
172               )
173             {
174                 $tag_data{tag} = "";
175             }
176             else {
177                 if ( C4::Context->preference('hide_marc') ) {
178                     $tag_data{tag} = $tagslib->{ $fields[$x_i]->tag() }->{lib};
179                 }
180                 else {
181                     $tag_data{tag} =
182                         $fields[$x_i]->tag() 
183                       . ' '
184                       . C4::Koha::display_marc_indicators($fields[$x_i])
185                       . ' - '
186                       . $tagslib->{ $fields[$x_i]->tag() }->{lib};
187                 }
188             }
189             my @tmp = @subfields_data;
190             $tag_data{subfield} = \@tmp;
191             push( @loop_data, \%tag_data );
192             undef @subfields_data;
193         }
194     }
195     $template->param( $tabloop . "XX" => \@loop_data );
196 }
197
198
199 # now, build item tab !
200 # the main difference is that datas are in lines and not in columns : thus, we build the <th> first, then the values...
201 # loop through each tag
202 # warning : we may have differents number of columns in each row. Thus, we first build a hash, complete it if necessary
203 # then construct template.
204 my @fields = $record->fields();
205 my %witness
206   ; #---- stores the list of subfields used at least once, with the "meaning" of the code
207 my @big_array;
208 foreach my $field (@fields) {
209     next if ( $field->tag() < 10 );
210     my @subf = $field->subfields;
211     my %this_row;
212
213     # loop through each subfield
214     for my $i ( 0 .. $#subf ) {
215         my $sf_def = $tagslib->{ $field->tag() }->{ $subf[$i][0] };
216         next if ( $sf_def->{tab} ne 10 );
217                 next if ( $sf_def->{hidden} > 0 );
218         $witness{ $subf[$i][0] } = $sf_def->{lib};
219
220         if ( $sf_def->{isurl} ) {
221             $this_row{ $subf[$i][0] } = "<a href=\"$subf[$i][1]\">$subf[$i][1]</a>";
222         }
223         elsif ( $sf_def->{kohafield} eq "biblioitems.isbn" ) {
224             $this_row{ $subf[$i][0] } = $subf[$i][1];
225         }
226         else {
227             $this_row{ $subf[$i][0] } = GetAuthorisedValueDesc( $field->tag(), $subf[$i][0],
228                 $subf[$i][1], '', $tagslib, '', 'opac' );
229         }
230     }
231     if (%this_row) {
232         push( @big_array, \%this_row );
233     }
234 }
235 my ( $holdingbrtagf, $holdingbrtagsubf ) =
236   &GetMarcFromKohaField( "items.holdingbranch", $itemtype );
237 @big_array =
238   sort { $a->{$holdingbrtagsubf} cmp $b->{$holdingbrtagsubf} } @big_array;
239
240 #fill big_row with missing datas
241 foreach my $subfield_code ( keys(%witness) ) {
242     for ( my $i = 0 ; $i <= $#big_array ; $i++ ) {
243         $big_array[$i]{$subfield_code} = "&nbsp;"
244           unless ( $big_array[$i]{$subfield_code} );
245     }
246 }
247
248 # now, construct template !
249 my @item_value_loop;
250 my @header_value_loop;
251 for ( my $i = 0 ; $i <= $#big_array ; $i++ ) {
252     my $items_data;
253     foreach my $subfield_code ( keys(%witness) ) {
254         $items_data .= "<td>" . $big_array[$i]{$subfield_code} . "</td>";
255     }
256     my %row_data;
257     $row_data{item_value} = $items_data;
258     push( @item_value_loop, \%row_data );
259 }
260
261 foreach my $subfield_code ( keys(%witness) ) {
262     my %header_value;
263     $header_value{header_value} = $witness{$subfield_code};
264     push( @header_value_loop, \%header_value );
265 }
266
267 if(C4::Context->preference("ISBD")) {
268         $template->param(ISBD => 1);
269 }
270
271 $template->param(
272     item_loop        => \@item_value_loop,
273     item_header_loop => \@header_value_loop,
274     biblionumber     => $biblionumber,
275 );
276
277 output_html_with_http_headers $query, $cookie, $template->output;