7439 Mailmap for master
[koha.git] / opac / opac-MARCdetail.pl
1 #!/usr/bin/perl
2
3 # Copyright 2000-2002 Katipo Communications
4 # Parts copyright 2010 BibLibre
5 #
6 # This file is part of Koha.
7 #
8 # Koha is free software; you can redistribute it and/or modify it under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
11 # version.
12 #
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License along
18 # with Koha; if not, write to the Free Software Foundation, Inc.,
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20
21 =head1 NAME
22
23 MARCdetail.pl : script to show a biblio in MARC format
24
25 =head1 SYNOPSIS
26
27 =cut
28
29 =head1 DESCRIPTION
30
31 This script needs a biblionumber as  parameter
32
33 It shows the biblio in a (nice) MARC format depending on MARC
34 parameters tables.
35
36 The template is in <templates_dir>/catalogue/MARCdetail.tmpl.
37 this template must be divided into 11 "tabs".
38
39 The first 10 tabs present the biblio, the 11th one presents
40 the items attached to the biblio
41
42 =cut
43
44 use strict;
45 use warnings;
46
47 use C4::Auth;
48 use C4::Context;
49 use C4::Output;
50 use CGI;
51 use MARC::Record;
52 use C4::Biblio;
53 use C4::Acquisition;
54 use C4::Koha;
55
56 my $query = new CGI;
57
58 my $dbh = C4::Context->dbh;
59
60 my $biblionumber = $query->param('biblionumber');
61 my $itemtype     = &GetFrameworkCode($biblionumber);
62 my $tagslib      = &GetMarcStructure( 0, $itemtype );
63 my $biblio = GetBiblioData($biblionumber);
64 my $record = GetMarcBiblio($biblionumber, 1);
65 if ( ! $record ) {
66     print $query->redirect("/cgi-bin/koha/errors/404.pl");
67     exit;
68 }
69 # open template
70 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
71     {
72         template_name   => "opac-MARCdetail.tmpl",
73         query           => $query,
74         type            => "opac",
75         authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ),
76         debug           => 1,
77     }
78 );
79
80 $template->param(
81     bibliotitle => $biblio->{title},
82 );
83
84 $template->param( 'AllowOnShelfHolds' => C4::Context->preference('AllowOnShelfHolds') );
85 $template->param( 'ItemsIssued' => CountItemsIssued( $biblionumber ) );
86
87 # adding the $RequestOnOpac param
88 my $RequestOnOpac;
89 if (C4::Context->preference("RequestOnOpac")) {
90         $RequestOnOpac = 1;
91 }
92
93 # fill arrays
94 my @loop_data = ();
95 my $tag;
96
97 # loop through each tab 0 through 9
98 for ( my $tabloop = 0 ; $tabloop <= 9 ; $tabloop++ ) {
99
100     # loop through each tag
101     my @loop_data = ();
102     my @subfields_data;
103
104     # deal with leader
105     unless ( $tagslib->{'000'}->{'@'}->{tab} ne $tabloop
106         or $tagslib->{'000'}->{'@'}->{hidden} > 0 )
107     {
108         my %subfield_data;
109         $subfield_data{marc_lib}      = $tagslib->{'000'}->{'@'}->{lib};
110         $subfield_data{marc_value}    = $record->leader();
111         $subfield_data{marc_subfield} = '@';
112         $subfield_data{marc_tag}      = '000';
113         push( @subfields_data, \%subfield_data );
114         my %tag_data;
115         $tag_data{tag} = '000 -' . $tagslib->{'000'}->{lib};
116         my @tmp = @subfields_data;
117         $tag_data{subfield} = \@tmp;
118         push( @loop_data, \%tag_data );
119         undef @subfields_data;
120     }
121     my @fields = $record->fields();
122     for ( my $x_i = 0 ; $x_i <= $#fields ; $x_i++ ) {
123
124         # if tag <10, there's no subfield, use the "@" trick
125         if ( $fields[$x_i]->tag() < 10 ) {
126             next
127               if (
128                 $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{tab} ne $tabloop );
129             next if ( $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{hidden} > 0 );
130             my %subfield_data;
131             $subfield_data{marc_lib} =
132               $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{lib};
133             $subfield_data{marc_value}    = $fields[$x_i]->data();
134             $subfield_data{marc_subfield} = '@';
135             $subfield_data{marc_tag}      = $fields[$x_i]->tag();
136             push( @subfields_data, \%subfield_data );
137         }
138         else {
139             my @subf = $fields[$x_i]->subfields;
140             my $previous = '';
141             # loop through each subfield
142             for my $i ( 0 .. $#subf ) {
143                 $subf[$i][0] = "@" unless defined($subf[$i][0]);
144                 my $sf_def = $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] };
145                 next if ( ($sf_def->{tab}||0) != $tabloop );
146                 next if ( ($sf_def->{hidden}||0) > 0 );
147                 my %subfield_data;
148                 $subfield_data{marc_lib} = ($sf_def->{lib} eq $previous) ?  '--' : $sf_def->{lib};
149                 $previous = $sf_def->{lib};
150                 $subfield_data{link} = $sf_def->{link};
151                 $subf[$i][1] =~ s/\n/<br\/>/g;
152                 if ( $sf_def->{isurl} ) {
153                     $subfield_data{marc_value} = "<a href=\"$subf[$i][1]\">$subf[$i][1]</a>";
154                 }
155                 elsif ( defined($sf_def->{kohafield}) && $sf_def->{kohafield} eq "biblioitems.isbn" ) {
156                     $subfield_data{marc_value} = $subf[$i][1];
157                 }
158                 else {
159                     if ( $sf_def->{authtypecode} ) {
160                         $subfield_data{authority} = $fields[$x_i]->subfield(9);
161                     }
162                     $subfield_data{marc_value} = GetAuthorisedValueDesc( $fields[$x_i]->tag(),
163                         $subf[$i][0], $subf[$i][1], '', $tagslib, '', 'opac' );
164                 }
165                 $subfield_data{marc_subfield} = $subf[$i][0];
166                 $subfield_data{marc_tag}      = $fields[$x_i]->tag();
167                 push( @subfields_data, \%subfield_data );
168             }
169         }
170         if ( $#subfields_data >= 0 ) {
171             my %tag_data;
172             if (   ( $fields[$x_i]->tag() eq $fields[ $x_i - 1 ]->tag() )
173                 && ( C4::Context->preference('LabelMARCView') eq 'economical' )
174               )
175             {
176                 $tag_data{tag} = "";
177             }
178             else {
179                 if ( C4::Context->preference('hide_marc') ) {
180                     $tag_data{tag} = $tagslib->{ $fields[$x_i]->tag() }->{lib};
181                 }
182                 else {
183                     $tag_data{tag} =
184                         $fields[$x_i]->tag() 
185                       . ' '
186                       . C4::Koha::display_marc_indicators($fields[$x_i])
187                       . ' - '
188                       . $tagslib->{ $fields[$x_i]->tag() }->{lib};
189                 }
190             }
191             my @tmp = @subfields_data;
192             $tag_data{subfield} = \@tmp;
193             push( @loop_data, \%tag_data );
194             undef @subfields_data;
195         }
196     }
197     $template->param( "tab" . $tabloop . "XX" => \@loop_data );
198 }
199
200
201 # now, build item tab !
202 # the main difference is that datas are in lines and not in columns : thus, we build the <th> first, then the values...
203 # loop through each tag
204 # warning : we may have differents number of columns in each row. Thus, we first build a hash, complete it if necessary
205 # then construct template.
206 my @fields = $record->fields();
207 my %witness
208   ; #---- stores the list of subfields used at least once, with the "meaning" of the code
209 my @big_array;
210 foreach my $field (@fields) {
211     next if ( $field->tag() < 10 );
212     my @subf = $field->subfields;
213     my %this_row;
214
215     # loop through each subfield
216     for my $i ( 0 .. $#subf ) {
217         my $sf_def = $tagslib->{ $field->tag() }->{ $subf[$i][0] };
218         next if ( ($sf_def->{tab}||0) != 10 );
219         next if ( ($sf_def->{hidden}||0) > 0 );
220         $witness{ $subf[$i][0] } = $sf_def->{lib};
221
222         if ( $sf_def->{isurl} ) {
223             $this_row{ $subf[$i][0] } = "<a href=\"$subf[$i][1]\">$subf[$i][1]</a>";
224         }
225         elsif ( $sf_def->{kohafield} eq "biblioitems.isbn" ) {
226             $this_row{ $subf[$i][0] } = $subf[$i][1];
227         }
228         else {
229             $this_row{ $subf[$i][0] } = GetAuthorisedValueDesc( $field->tag(), $subf[$i][0],
230                 $subf[$i][1], '', $tagslib, '', 'opac' );
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 #Search for title in links
274 if (my $search_for_title = C4::Context->preference('OPACSearchForTitleIn')){
275     $biblio->{author} ? $search_for_title =~ s/{AUTHOR}/$biblio->{author}/g : $search_for_title =~ s/{AUTHOR}//g;
276     $biblio->{title} =~ s/\/+$//; # remove trailing slash
277     $biblio->{title} =~ s/\s+$//; # remove trailing space
278     $biblio->{title} ? $search_for_title =~ s/{TITLE}/$biblio->{title}/g : $search_for_title =~ s/{TITLE}//g;
279     $biblio->{isbn} ? $search_for_title =~ s/{ISBN}/$biblio->{isbn}/g : $search_for_title =~ s/{ISBN}//g;
280  $template->param('OPACSearchForTitleIn' => $search_for_title);
281 }
282
283 $template->param(
284     item_loop        => \@item_value_loop,
285     item_header_loop => \@header_value_loop,
286     biblionumber     => $biblionumber,
287 );
288
289 output_html_with_http_headers $query, $cookie, $template->output;