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