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