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