Using my precrash CVS copy I did the following:
[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 C4::Interface::CGI::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     = &MARCfind_frameworkcode($biblionumber);
60 my $tagslib      = &MARCgettagslib( $dbh, 0, $itemtype );
61
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 # adding the $RequestOnOpac param
75 my $RequestOnOpac;
76 if (C4::Context->preference("RequestOnOpac")) {
77         $RequestOnOpac = 1;
78 }
79
80 # fill arrays
81 my @loop_data = ();
82 my $tag;
83
84 # loop through each tab 0 through 9
85 for ( my $tabloop = 0 ; $tabloop <= 10 ; $tabloop++ ) {
86
87     # loop through each tag
88     my @loop_data = ();
89     my @subfields_data;
90
91     # deal with leader
92     unless ( $tagslib->{'000'}->{'@'}->{tab} ne $tabloop
93         or $tagslib->{'000'}->{'@'}->{hidden} > 0 )
94     {
95         my %subfield_data;
96         $subfield_data{marc_lib}      = $tagslib->{'000'}->{'@'}->{lib};
97         $subfield_data{marc_value}    = $record->leader();
98         $subfield_data{marc_subfield} = '@';
99         $subfield_data{marc_tag}      = '000';
100         push( @subfields_data, \%subfield_data );
101         my %tag_data;
102         $tag_data{tag} = '000 -' . $tagslib->{'000'}->{lib};
103         my @tmp = @subfields_data;
104         $tag_data{subfield} = \@tmp;
105         push( @loop_data, \%tag_data );
106         undef @subfields_data;
107     }
108     my @fields = $record->fields();
109     for ( my $x_i = 0 ; $x_i <= $#fields ; $x_i++ ) {
110
111         # if tag <10, there's no subfield, use the "@" trick
112         if ( $fields[$x_i]->tag() < 10 ) {
113             next
114               if (
115                 $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{tab} ne $tabloop );
116             next if ( $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{hidden} > 0 );
117             my %subfield_data;
118             $subfield_data{marc_lib} =
119               $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{lib};
120             $subfield_data{marc_value}    = $fields[$x_i]->data();
121             $subfield_data{marc_subfield} = '@';
122             $subfield_data{marc_tag}      = $fields[$x_i]->tag();
123             push( @subfields_data, \%subfield_data );
124         }
125         else {
126             my @subf = $fields[$x_i]->subfields;
127
128             # loop through each subfield
129             for my $i ( 0 .. $#subf ) {
130                 $subf[$i][0] = "@" unless $subf[$i][0];
131                 next
132                   if (
133                     $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }->{tab}
134                     ne $tabloop );
135                 next
136                   if ( $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }
137                     ->{hidden} > 0 );
138                 my %subfield_data;
139                 $subfield_data{marc_lib} =
140                   $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }->{lib};
141                 $subfield_data{link} =
142                   $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }->{link};
143                 $subf[$i][1] =~ s/\n/<br\/>/g;
144                 if ( $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }
145                     ->{isurl} )
146                 {
147                     $subfield_data{marc_value} =
148                       "<a href=\"$subf[$i][1]\">$subf[$i][1]</a>";
149                 }
150                 elsif ( $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }
151                     ->{kohafield} eq "biblioitems.isbn" )
152                 {
153
154 #                    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]);
155                     $subfield_data{marc_value} = DisplayISBN( $subf[$i][1] );
156                 }
157                 else {
158                     if ( $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }
159                         ->{authtypecode} )
160                     {
161                         $subfield_data{authority} = $fields[$x_i]->subfield(9);
162                     }
163                     $subfield_data{marc_value} =
164                       GetAuthorisedValueDesc( $fields[$x_i]->tag(),
165                         $subf[$i][0], $subf[$i][1], '', $tagslib );
166                 }
167                 $subfield_data{marc_subfield} = $subf[$i][0];
168                 $subfield_data{marc_tag}      = $fields[$x_i]->tag();
169                 push( @subfields_data, \%subfield_data );
170             }
171         }
172         if ( $#subfields_data >= 0 ) {
173             my %tag_data;
174             if (   ( $fields[$x_i]->tag() eq $fields[ $x_i - 1 ]->tag() )
175                 && ( C4::Context->preference('LabelMARCView') eq 'economical' )
176               )
177             {
178                 $tag_data{tag} = "";
179             }
180             else {
181                 if ( C4::Context->preference('hide_marc') ) {
182                     $tag_data{tag} = $tagslib->{ $fields[$x_i]->tag() }->{lib};
183                 }
184                 else {
185                     $tag_data{tag} =
186                         $fields[$x_i]->tag() . ' -'
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         next if ( $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{tab} ne 10 );
217         $witness{ $subf[$i][0] } =
218           $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{lib};
219         if ( $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{isurl} ) {
220             $this_row{ $subf[$i][0] } =
221               "<a href=\"$subf[$i][1]\">$subf[$i][1]</a>";
222         }
223         elsif ( $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{kohafield} eq
224             "biblioitems.isbn" )
225         {
226             $this_row{ $subf[$i][0] } = DisplayISBN( $subf[$i][1] );
227         }
228         else {
229             $this_row{ $subf[$i][0] } =
230               GetAuthorisedValueDesc( $field->tag(), $subf[$i][0],
231                 $subf[$i][1], '', $tagslib );
232         }
233     }
234     if (%this_row) {
235         push( @big_array, \%this_row );
236     }
237 }
238 my ( $holdingbrtagf, $holdingbrtagsubf ) =
239   &MARCfind_marc_from_kohafield( $dbh, "items.holdingbranch", $itemtype );
240 @big_array =
241   sort { $a->{$holdingbrtagsubf} cmp $b->{$holdingbrtagsubf} } @big_array;
242
243 #fill big_row with missing datas
244 foreach my $subfield_code ( keys(%witness) ) {
245     for ( my $i = 0 ; $i <= $#big_array ; $i++ ) {
246         $big_array[$i]{$subfield_code} = "&nbsp;"
247           unless ( $big_array[$i]{$subfield_code} );
248     }
249 }
250
251 # now, construct template !
252 my @item_value_loop;
253 my @header_value_loop;
254 for ( my $i = 0 ; $i <= $#big_array ; $i++ ) {
255     my $items_data;
256     foreach my $subfield_code ( keys(%witness) ) {
257         $items_data .= "<td>" . $big_array[$i]{$subfield_code} . "</td>";
258     }
259     my %row_data;
260     $row_data{item_value} = $items_data;
261     push( @item_value_loop, \%row_data );
262 }
263
264 foreach my $subfield_code ( keys(%witness) ) {
265     my %header_value;
266     $header_value{header_value} = $witness{$subfield_code};
267     push( @header_value_loop, \%header_value );
268 }
269
270 if(C4::Context->preference("ISBD")) {
271         $template->param(ISBD => 1);
272 }
273
274 $template->param(
275     item_loop        => \@item_value_loop,
276     item_header_loop => \@header_value_loop,
277     biblionumber     => $biblionumber,
278 );
279
280 output_html_with_http_headers $query, $cookie, $template->output;