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