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