Merge remote branch 'kc/new/bug_4218' into kcmaster
[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 =cut
27
28 =head1 DESCRIPTION
29
30 This script needs a biblionumber as  parameter
31
32 It shows the biblio in a (nice) MARC format depending on MARC
33 parameters tables.
34
35 The template is in <templates_dir>/catalogue/MARCdetail.tmpl.
36 this template must be divided into 11 "tabs".
37
38 The first 10 tabs present the biblio, the 11th one presents
39 the items attached to the biblio
40
41 =cut
42
43 use strict;
44 use warnings;
45
46 use C4::Auth;
47 use C4::Context;
48 use C4::Output;
49 use CGI;
50 use MARC::Record;
51 use C4::Biblio;
52 use C4::Acquisition;
53 use C4::Koha;
54
55 my $query = new CGI;
56
57 my $dbh = C4::Context->dbh;
58
59 my $biblionumber = $query->param('biblionumber');
60 my $itemtype     = &GetFrameworkCode($biblionumber);
61 my $tagslib      = &GetMarcStructure( 0, $itemtype );
62 my $biblio = GetBiblioData($biblionumber);
63 my $record = GetMarcBiblio($biblionumber);
64 if ( ! $record ) {
65     print $query->redirect("/cgi-bin/koha/errors/404.pl");
66     exit;
67 }
68 # open template
69 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
70     {
71         template_name   => "opac-MARCdetail.tmpl",
72         query           => $query,
73         type            => "opac",
74         authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ),
75         debug           => 1,
76     }
77 );
78
79 $template->param(
80     bibliotitle => $biblio->{title},
81 );
82
83 $template->param( 'AllowOnShelfHolds' => C4::Context->preference('AllowOnShelfHolds') );
84 $template->param( 'ItemsIssued' => CountItemsIssued( $biblionumber ) );
85
86 # adding the $RequestOnOpac param
87 my $RequestOnOpac;
88 if (C4::Context->preference("RequestOnOpac")) {
89         $RequestOnOpac = 1;
90 }
91
92 # fill arrays
93 my @loop_data = ();
94 my $tag;
95
96 # loop through each tab 0 through 9
97 for ( my $tabloop = 0 ; $tabloop <= 10 ; $tabloop++ ) {
98
99     # loop through each tag
100     my @loop_data = ();
101     my @subfields_data;
102
103     # deal with leader
104     unless ( $tagslib->{'000'}->{'@'}->{tab} ne $tabloop
105         or $tagslib->{'000'}->{'@'}->{hidden} > 0 )
106     {
107         my %subfield_data;
108         $subfield_data{marc_lib}      = $tagslib->{'000'}->{'@'}->{lib};
109         $subfield_data{marc_value}    = $record->leader();
110         $subfield_data{marc_subfield} = '@';
111         $subfield_data{marc_tag}      = '000';
112         push( @subfields_data, \%subfield_data );
113         my %tag_data;
114         $tag_data{tag} = '000 -' . $tagslib->{'000'}->{lib};
115         my @tmp = @subfields_data;
116         $tag_data{subfield} = \@tmp;
117         push( @loop_data, \%tag_data );
118         undef @subfields_data;
119     }
120     my @fields = $record->fields();
121     for ( my $x_i = 0 ; $x_i <= $#fields ; $x_i++ ) {
122
123         # if tag <10, there's no subfield, use the "@" trick
124         if ( $fields[$x_i]->tag() < 10 ) {
125             next
126               if (
127                 $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{tab} ne $tabloop );
128             next if ( $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{hidden} > 0 );
129             my %subfield_data;
130             $subfield_data{marc_lib} =
131               $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{lib};
132             $subfield_data{marc_value}    = $fields[$x_i]->data();
133             $subfield_data{marc_subfield} = '@';
134             $subfield_data{marc_tag}      = $fields[$x_i]->tag();
135             push( @subfields_data, \%subfield_data );
136         }
137         else {
138             my @subf = $fields[$x_i]->subfields;
139             my $previous = '';
140             # loop through each subfield
141             for my $i ( 0 .. $#subf ) {
142                 $subf[$i][0] = "@" unless $subf[$i][0];
143                 my $sf_def = $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] };
144                 next if ( $sf_def->{tab} ne $tabloop );
145                 next if ( $sf_def->{hidden} > 0 ); 
146                 my %subfield_data;
147                 $subfield_data{marc_lib} = ($sf_def->{lib} eq $previous) ?  '--' : $sf_def->{lib};
148                 $previous = $sf_def->{lib};
149                 $subfield_data{link} = $sf_def->{link};
150                 $subf[$i][1] =~ s/\n/<br\/>/g;
151                 if ( $sf_def->{isurl} ) {
152                     $subfield_data{marc_value} = "<a href=\"$subf[$i][1]\">$subf[$i][1]</a>";
153                 }
154                 elsif ( defined($sf_def->{kohafield}) && $sf_def->{kohafield} eq "biblioitems.isbn" ) {
155                     $subfield_data{marc_value} = $subf[$i][1];
156                 }
157                 else {
158                     if ( $sf_def->{authtypecode} ) {
159                         $subfield_data{authority} = $fields[$x_i]->subfield(9);
160                     }
161                     $subfield_data{marc_value} = GetAuthorisedValueDesc( $fields[$x_i]->tag(),
162                         $subf[$i][0], $subf[$i][1], '', $tagslib, '', 'opac' );
163                 }
164                 $subfield_data{marc_subfield} = $subf[$i][0];
165                 $subfield_data{marc_tag}      = $fields[$x_i]->tag();
166                 push( @subfields_data, \%subfield_data );
167             }
168         }
169         if ( $#subfields_data >= 0 ) {
170             my %tag_data;
171             if (   ( $fields[$x_i]->tag() eq $fields[ $x_i - 1 ]->tag() )
172                 && ( C4::Context->preference('LabelMARCView') eq 'economical' )
173               )
174             {
175                 $tag_data{tag} = "";
176             }
177             else {
178                 if ( C4::Context->preference('hide_marc') ) {
179                     $tag_data{tag} = $tagslib->{ $fields[$x_i]->tag() }->{lib};
180                 }
181                 else {
182                     $tag_data{tag} =
183                         $fields[$x_i]->tag() 
184                       . ' '
185                       . C4::Koha::display_marc_indicators($fields[$x_i])
186                       . ' - '
187                       . $tagslib->{ $fields[$x_i]->tag() }->{lib};
188                 }
189             }
190             my @tmp = @subfields_data;
191             $tag_data{subfield} = \@tmp;
192             push( @loop_data, \%tag_data );
193             undef @subfields_data;
194         }
195     }
196     $template->param( $tabloop . "XX" => \@loop_data );
197 }
198
199
200 # now, build item tab !
201 # the main difference is that datas are in lines and not in columns : thus, we build the <th> first, then the values...
202 # loop through each tag
203 # warning : we may have differents number of columns in each row. Thus, we first build a hash, complete it if necessary
204 # then construct template.
205 my @fields = $record->fields();
206 my %witness
207   ; #---- stores the list of subfields used at least once, with the "meaning" of the code
208 my @big_array;
209 foreach my $field (@fields) {
210     next if ( $field->tag() < 10 );
211     my @subf = $field->subfields;
212     my %this_row;
213
214     # loop through each subfield
215     for my $i ( 0 .. $#subf ) {
216         my $sf_def = $tagslib->{ $field->tag() }->{ $subf[$i][0] };
217         next if ( $sf_def->{tab} ne 10 );
218                 next if ( $sf_def->{hidden} > 0 );
219         $witness{ $subf[$i][0] } = $sf_def->{lib};
220
221         if ( $sf_def->{isurl} ) {
222             $this_row{ $subf[$i][0] } = "<a href=\"$subf[$i][1]\">$subf[$i][1]</a>";
223         }
224         elsif ( $sf_def->{kohafield} eq "biblioitems.isbn" ) {
225             $this_row{ $subf[$i][0] } = $subf[$i][1];
226         }
227         else {
228             $this_row{ $subf[$i][0] } = GetAuthorisedValueDesc( $field->tag(), $subf[$i][0],
229                 $subf[$i][1], '', $tagslib, '', 'opac' );
230         }
231     }
232     if (%this_row) {
233         push( @big_array, \%this_row );
234     }
235 }
236 my ( $holdingbrtagf, $holdingbrtagsubf ) =
237   &GetMarcFromKohaField( "items.holdingbranch", $itemtype );
238 @big_array =
239   sort { $a->{$holdingbrtagsubf} cmp $b->{$holdingbrtagsubf} } @big_array;
240
241 #fill big_row with missing datas
242 foreach my $subfield_code ( keys(%witness) ) {
243     for ( my $i = 0 ; $i <= $#big_array ; $i++ ) {
244         $big_array[$i]{$subfield_code} = "&nbsp;"
245           unless ( $big_array[$i]{$subfield_code} );
246     }
247 }
248
249 # now, construct template !
250 my @item_value_loop;
251 my @header_value_loop;
252 for ( my $i = 0 ; $i <= $#big_array ; $i++ ) {
253     my $items_data;
254     foreach my $subfield_code ( keys(%witness) ) {
255         $items_data .= "<td>" . $big_array[$i]{$subfield_code} . "</td>";
256     }
257     my %row_data;
258     $row_data{item_value} = $items_data;
259     push( @item_value_loop, \%row_data );
260 }
261
262 foreach my $subfield_code ( keys(%witness) ) {
263     my %header_value;
264     $header_value{header_value} = $witness{$subfield_code};
265     push( @header_value_loop, \%header_value );
266 }
267
268 if(C4::Context->preference("ISBD")) {
269         $template->param(ISBD => 1);
270 }
271
272 #Search for title in links
273 if (my $search_for_title = C4::Context->preference('OPACSearchForTitleIn')){
274     $biblio->{author} ? $search_for_title =~ s/{AUTHOR}/$biblio->{author}/g : $search_for_title =~ s/{AUTHOR}//g;
275     $biblio->{title} =~ s/\/+$//; # remove trailing slash
276     $biblio->{title} =~ s/\s+$//; # remove trailing space
277     $biblio->{title} ? $search_for_title =~ s/{TITLE}/$biblio->{title}/g : $search_for_title =~ s/{TITLE}//g;
278     $biblio->{isbn} ? $search_for_title =~ s/{ISBN}/$biblio->{isbn}/g : $search_for_title =~ s/{ISBN}//g;
279  $template->param('OPACSearchForTitleIn' => $search_for_title);
280 }
281
282 $template->param(
283     item_loop        => \@item_value_loop,
284     item_header_loop => \@header_value_loop,
285     biblionumber     => $biblionumber,
286 );
287
288 output_html_with_http_headers $query, $cookie, $template->output;