Bug 2505: Enables warnings in opac-MARCdetail.pl and opac-authoritiesdetail.pl
[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 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 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
64 # open template
65 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
66     {
67         template_name   => "opac-MARCdetail.tmpl",
68         query           => $query,
69         type            => "opac",
70         authnotrequired => 1,
71         debug           => 1,
72     }
73 );
74
75 $template->param(
76     bibliotitle => $biblio->{title},
77 );
78
79 $template->param( 'AllowOnShelfHolds' => C4::Context->preference('AllowOnShelfHolds') );
80 $template->param( 'ItemsIssued' => CountItemsIssued( $biblionumber ) );
81
82 # adding the $RequestOnOpac param
83 my $RequestOnOpac;
84 if (C4::Context->preference("RequestOnOpac")) {
85         $RequestOnOpac = 1;
86 }
87
88 # fill arrays
89 my @loop_data = ();
90 my $tag;
91
92 # loop through each tab 0 through 9
93 for ( my $tabloop = 0 ; $tabloop <= 10 ; $tabloop++ ) {
94
95     # loop through each tag
96     my @loop_data = ();
97     my @subfields_data;
98
99     # deal with leader
100     unless ( $tagslib->{'000'}->{'@'}->{tab} ne $tabloop
101         or $tagslib->{'000'}->{'@'}->{hidden} > 0 )
102     {
103         my %subfield_data;
104         $subfield_data{marc_lib}      = $tagslib->{'000'}->{'@'}->{lib};
105         $subfield_data{marc_value}    = $record->leader();
106         $subfield_data{marc_subfield} = '@';
107         $subfield_data{marc_tag}      = '000';
108         push( @subfields_data, \%subfield_data );
109         my %tag_data;
110         $tag_data{tag} = '000 -' . $tagslib->{'000'}->{lib};
111         my @tmp = @subfields_data;
112         $tag_data{subfield} = \@tmp;
113         push( @loop_data, \%tag_data );
114         undef @subfields_data;
115     }
116     my @fields = $record->fields();
117     for ( my $x_i = 0 ; $x_i <= $#fields ; $x_i++ ) {
118
119         # if tag <10, there's no subfield, use the "@" trick
120         if ( $fields[$x_i]->tag() < 10 ) {
121             next
122               if (
123                 $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{tab} ne $tabloop );
124             next if ( $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{hidden} > 0 );
125             my %subfield_data;
126             $subfield_data{marc_lib} =
127               $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{lib};
128             $subfield_data{marc_value}    = $fields[$x_i]->data();
129             $subfield_data{marc_subfield} = '@';
130             $subfield_data{marc_tag}      = $fields[$x_i]->tag();
131             push( @subfields_data, \%subfield_data );
132         }
133         else {
134             my @subf = $fields[$x_i]->subfields;
135             my $previous;
136             # loop through each subfield
137             for my $i ( 0 .. $#subf ) {
138                 $subf[$i][0] = "@" unless $subf[$i][0];
139                 next
140                   if (
141                     $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }->{tab}
142                     ne $tabloop );
143                 next
144                   if ( $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }->{hidden} > 0 ); 
145                 my %subfield_data;
146                 $subfield_data{marc_lib} =
147                                 ($tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }->{lib} eq $previous) ?
148                                 '--' :
149                                 $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }->{lib};
150                 $previous = $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }->{lib};
151                 $subfield_data{link} =
152                   $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }->{link};
153                 $subf[$i][1] =~ s/\n/<br\/>/g;
154                 if ( $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }
155                     ->{isurl} )
156                 {
157                     $subfield_data{marc_value} =
158                       "<a href=\"$subf[$i][1]\">$subf[$i][1]</a>";
159                 }
160                 elsif ( $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }
161                     ->{kohafield} eq "biblioitems.isbn" )
162                 {
163
164 #                    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]);
165                     $subfield_data{marc_value} = $subf[$i][1];
166                 }
167                 else {
168                     if ( $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }
169                         ->{authtypecode} )
170                     {
171                         $subfield_data{authority} = $fields[$x_i]->subfield(9);
172                     }
173                     $subfield_data{marc_value} =
174                       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( $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         next if ( $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{tab} ne 10 );
230                 next if ( $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{hidden} > 0 );
231         $witness{ $subf[$i][0] } =
232           $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{lib};
233
234         if ( $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{isurl} ) {
235             $this_row{ $subf[$i][0] } =
236               "<a href=\"$subf[$i][1]\">$subf[$i][1]</a>";
237         }
238         elsif ( $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{kohafield} eq
239             "biblioitems.isbn" )
240         {
241             $this_row{ $subf[$i][0] } = $subf[$i][1];
242         }
243         else {
244             $this_row{ $subf[$i][0] } =
245               GetAuthorisedValueDesc( $field->tag(), $subf[$i][0],
246                 $subf[$i][1], '', $tagslib, '', 'opac' );
247         }
248     }
249     if (%this_row) {
250         push( @big_array, \%this_row );
251     }
252 }
253 my ( $holdingbrtagf, $holdingbrtagsubf ) =
254   &GetMarcFromKohaField( "items.holdingbranch", $itemtype );
255 @big_array =
256   sort { $a->{$holdingbrtagsubf} cmp $b->{$holdingbrtagsubf} } @big_array;
257
258 #fill big_row with missing datas
259 foreach my $subfield_code ( keys(%witness) ) {
260     for ( my $i = 0 ; $i <= $#big_array ; $i++ ) {
261         $big_array[$i]{$subfield_code} = "&nbsp;"
262           unless ( $big_array[$i]{$subfield_code} );
263     }
264 }
265
266 # now, construct template !
267 my @item_value_loop;
268 my @header_value_loop;
269 for ( my $i = 0 ; $i <= $#big_array ; $i++ ) {
270     my $items_data;
271     foreach my $subfield_code ( keys(%witness) ) {
272         $items_data .= "<td>" . $big_array[$i]{$subfield_code} . "</td>";
273     }
274     my %row_data;
275     $row_data{item_value} = $items_data;
276     push( @item_value_loop, \%row_data );
277 }
278
279 foreach my $subfield_code ( keys(%witness) ) {
280     my %header_value;
281     $header_value{header_value} = $witness{$subfield_code};
282     push( @header_value_loop, \%header_value );
283 }
284
285 if(C4::Context->preference("ISBD")) {
286         $template->param(ISBD => 1);
287 }
288
289 $template->param(
290     item_loop        => \@item_value_loop,
291     item_header_loop => \@header_value_loop,
292     biblionumber     => $biblionumber,
293 );
294
295 output_html_with_http_headers $query, $cookie, $template->output;