Bug 11366: make "no group" option in acq basket group drop-down translatable
[koha.git] / opac / opac-MARCdetail.pl
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 #       Copyright (C) 2000-2002 Katipo Communications
6 # Parts Copyright (C) 2010      BibLibre
7 # Parts Copyright (C) 2013      Mark Tompsett
8 #
9 # Koha is free software; you can redistribute it and/or modify it
10 # under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 3 of the License, or
12 # (at your option) any later version.
13 #
14 # Koha is distributed in the hope that it will be useful, but
15 # WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License
20 # along with Koha; if not, see <http://www.gnu.org/licenses>.
21
22
23 =head1 NAME
24
25 opac-MARCdetail.pl : script to show a biblio in MARC format
26
27 =head1 SYNOPSIS
28
29 =cut
30
31 =head1 DESCRIPTION
32
33 This script needs a biblionumber as  parameter
34
35 It shows the biblio in a (nice) MARC format depending on MARC
36 parameters tables.
37
38 The template is in <templates_dir>/catalogue/MARCdetail.tmpl.
39 this template must be divided into 11 "tabs".
40
41 The first 10 tabs present the biblio, the 11th one presents
42 the items attached to the biblio
43
44 =cut
45
46 use Modern::Perl;
47
48 use C4::Auth;
49 use C4::Context;
50 use C4::Output;
51 use CGI;
52 use MARC::Record;
53 use C4::Biblio;
54 use C4::Acquisition;
55 use C4::Koha;
56
57 my $query = new CGI;
58
59 my $dbh = C4::Context->dbh;
60
61 my $biblionumber = $query->param('biblionumber');
62 my $itemtype     = &GetFrameworkCode($biblionumber);
63 my $tagslib      = &GetMarcStructure( 0, $itemtype );
64 my $biblio = GetBiblioData($biblionumber);
65 $biblionumber = $biblio->{biblionumber};
66 my $record = GetMarcBiblio($biblionumber, 1);
67 if ( ! $record ) {
68     print $query->redirect("/cgi-bin/koha/errors/404.pl");
69     exit;
70 }
71 # open template
72 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
73     {
74         template_name   => "opac-MARCdetail.tmpl",
75         query           => $query,
76         type            => "opac",
77         authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ),
78         debug           => 1,
79     }
80 );
81
82 $template->param(
83     bibliotitle => $biblio->{title},
84 );
85
86 # get biblionumbers stored in the cart
87 my @cart_list;
88
89 if($query->cookie("bib_list")){
90     my $cart_list = $query->cookie("bib_list");
91     @cart_list = split(/\//, $cart_list);
92     if ( grep {$_ eq $biblionumber} @cart_list) {
93         $template->param( incart => 1 );
94     }
95 }
96
97 $template->param( 'AllowOnShelfHolds' => C4::Context->preference('AllowOnShelfHolds') );
98 $template->param( 'ItemsIssued' => CountItemsIssued( $biblionumber ) );
99
100 # adding the $RequestOnOpac param
101 my $RequestOnOpac;
102 if (C4::Context->preference("RequestOnOpac")) {
103         $RequestOnOpac = 1;
104 }
105
106 # fill arrays
107 my @loop_data = ();
108 my $tag;
109
110 # loop through each tab 0 through 9
111 for ( my $tabloop = 0 ; $tabloop <= 9 ; $tabloop++ ) {
112
113     # loop through each tag
114     my @loop_data = ();
115     my @subfields_data;
116
117     # deal with leader
118     unless ( $tagslib->{'000'}->{'@'}->{tab} ne $tabloop
119         or $tagslib->{'000'}->{'@'}->{hidden} > 0 )
120     {
121         my %subfield_data;
122         $subfield_data{marc_lib}      = $tagslib->{'000'}->{'@'}->{lib};
123         $subfield_data{marc_value}    = $record->leader();
124         $subfield_data{marc_subfield} = '@';
125         $subfield_data{marc_tag}      = '000';
126         push( @subfields_data, \%subfield_data );
127         my %tag_data;
128         $tag_data{tag} = '000 -' . $tagslib->{'000'}->{lib};
129         my @tmp = @subfields_data;
130         $tag_data{subfield} = \@tmp;
131         push( @loop_data, \%tag_data );
132         undef @subfields_data;
133     }
134     my @fields = $record->fields();
135     for ( my $x_i = 0 ; $x_i <= $#fields ; $x_i++ ) {
136
137         # if tag <10, there's no subfield, use the "@" trick
138         if ( $fields[$x_i]->tag() < 10 ) {
139             next
140               if (
141                 $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{tab} ne $tabloop );
142             next if ( $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{hidden} > 0 );
143             my %subfield_data;
144             $subfield_data{marc_lib} =
145               $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{lib};
146             $subfield_data{marc_value}    = $fields[$x_i]->data();
147             $subfield_data{marc_subfield} = '@';
148             $subfield_data{marc_tag}      = $fields[$x_i]->tag();
149             push( @subfields_data, \%subfield_data );
150         }
151         else {
152             my @subf = $fields[$x_i]->subfields;
153             my $previous = '';
154             # loop through each subfield
155             for my $i ( 0 .. $#subf ) {
156                 $subf[$i][0] = "@" unless defined($subf[$i][0]);
157                 my $sf_def = $tagslib->{ $fields[$x_i]->tag() };
158                 $sf_def = $sf_def->{ $subf[$i][0] } if defined($sf_def);
159                 my ($tab,$hidden,$lib);
160                 $tab = $sf_def->{tab} if defined($sf_def);
161                 $tab = $tab // int($fields[$x_i]->tag()/100);
162                 $hidden = $sf_def->{hidden} if defined($sf_def);
163                 $hidden = $hidden // 0;
164                 next if ( $tab != $tabloop );
165                 next if ( $hidden > 0 );
166                 my %subfield_data;
167                 $lib = $sf_def->{lib} if defined($sf_def);
168                 $lib = $lib // '--';
169                 $subfield_data{marc_lib} = ($lib eq $previous) ?  '--' : $lib;
170                 $previous = $lib;
171                 $subfield_data{link} = $sf_def->{link};
172                 $subf[$i][1] =~ s/\n/<br\/>/g;
173                 if ( $sf_def->{isurl} ) {
174                     $subfield_data{marc_value} = "<a href=\"$subf[$i][1]\">$subf[$i][1]</a>";
175                 }
176                 elsif ( defined($sf_def->{kohafield}) && $sf_def->{kohafield} eq "biblioitems.isbn" ) {
177                     $subfield_data{marc_value} = $subf[$i][1];
178                 }
179                 else {
180                     if ( $sf_def->{authtypecode} ) {
181                         $subfield_data{authority} = $fields[$x_i]->subfield(9);
182                     }
183                     $subfield_data{marc_value} = GetAuthorisedValueDesc( $fields[$x_i]->tag(),
184                         $subf[$i][0], $subf[$i][1], '', $tagslib, '', 'opac' );
185                 }
186                 $subfield_data{marc_subfield} = $subf[$i][0];
187                 $subfield_data{marc_tag}      = $fields[$x_i]->tag();
188                 push( @subfields_data, \%subfield_data );
189             }
190         }
191         if ( $#subfields_data >= 0 ) {
192             my %tag_data;
193             if (   ( $fields[$x_i]->tag() eq $fields[ $x_i - 1 ]->tag() )
194                 && ( C4::Context->preference('LabelMARCView') eq 'economical' )
195               )
196             {
197                 $tag_data{tag} = "";
198             }
199             else {
200                 if ( C4::Context->preference('hide_marc') ) {
201                     $tag_data{tag} = $tagslib->{ $fields[$x_i]->tag() }->{lib};
202                 }
203                 else {
204                     my $sf_def = $tagslib->{ $fields[$x_i]->tag() };
205                     my $lib;
206                     $lib = $sf_def->{lib} if defined($sf_def);
207                     $lib = $lib // '';
208                     $tag_data{tag} = $fields[$x_i]->tag() . ' '
209                       . C4::Koha::display_marc_indicators($fields[$x_i])
210                       . " - $lib";
211                 }
212             }
213             my @tmp = @subfields_data;
214             $tag_data{subfield} = \@tmp;
215             push( @loop_data, \%tag_data );
216             undef @subfields_data;
217         }
218     }
219     $template->param( "tab" . $tabloop . "XX" => \@loop_data );
220 }
221
222
223 # now, build item tab !
224 # the main difference is that datas are in lines and not in columns : thus, we build the <th> first, then the values...
225 # loop through each tag
226 # warning : we may have differents number of columns in each row. Thus, we first build a hash, complete it if necessary
227 # then construct template.
228 my @fields = $record->fields();
229 my %witness
230   ; #---- stores the list of subfields used at least once, with the "meaning" of the code
231 my @big_array;
232 foreach my $field (@fields) {
233     next if ( $field->tag() < 10 );
234     my @subf = $field->subfields;
235     my %this_row;
236
237     # loop through each subfield
238     for my $i ( 0 .. $#subf ) {
239         my $sf_def = $tagslib->{ $field->tag() }->{ $subf[$i][0] };
240         next if ( ($sf_def->{tab}||0) != 10 );
241         next if ( ($sf_def->{hidden}||0) > 0 );
242         $witness{ $subf[$i][0] } = $sf_def->{lib};
243
244         if ( $sf_def->{isurl} ) {
245             $this_row{ $subf[$i][0] } = "<a href=\"$subf[$i][1]\">$subf[$i][1]</a>";
246         }
247         elsif ( $sf_def->{kohafield} eq "biblioitems.isbn" ) {
248             $this_row{ $subf[$i][0] } = $subf[$i][1];
249         }
250         else {
251             $this_row{ $subf[$i][0] } = GetAuthorisedValueDesc( $field->tag(), $subf[$i][0],
252                 $subf[$i][1], '', $tagslib, '', 'opac' );
253         }
254     }
255     if (%this_row) {
256         push( @big_array, \%this_row );
257     }
258 }
259 my ( $holdingbrtagf, $holdingbrtagsubf ) =
260   &GetMarcFromKohaField( "items.holdingbranch", $itemtype );
261 @big_array =
262   sort { ($a->{$holdingbrtagsubf}||'') cmp ($b->{$holdingbrtagsubf}||'') } @big_array;
263
264 #fill big_row with missing datas
265 foreach my $subfield_code ( keys(%witness) ) {
266     for ( my $i = 0 ; $i <= $#big_array ; $i++ ) {
267         $big_array[$i]{$subfield_code} = "&nbsp;"
268           unless ( $big_array[$i]{$subfield_code} );
269     }
270 }
271
272 # now, construct template !
273 my @item_value_loop;
274 my @header_value_loop;
275 for ( my $i = 0 ; $i <= $#big_array ; $i++ ) {
276     my $items_data;
277     foreach my $subfield_code ( keys(%witness) ) {
278         $items_data .= "<td>" . $big_array[$i]{$subfield_code} . "</td>";
279     }
280     my %row_data;
281     $row_data{item_value} = $items_data;
282     push( @item_value_loop, \%row_data );
283 }
284
285 foreach my $subfield_code ( keys(%witness) ) {
286     my %header_value;
287     $header_value{header_value} = $witness{$subfield_code};
288     push( @header_value_loop, \%header_value );
289 }
290
291 if(C4::Context->preference("ISBD")) {
292         $template->param(ISBD => 1);
293 }
294
295 #Export options
296 my $OpacExportOptions=C4::Context->preference("OpacExportOptions");
297 my @export_options = split(/\|/,$OpacExportOptions);
298 $template->{VARS}->{'export_options'} = \@export_options;
299
300 #Search for title in links
301 my $marcflavour  = C4::Context->preference("marcflavour");
302 my $dat = TransformMarcToKoha( $dbh, $record );
303 my $isbn = GetNormalizedISBN(undef,$record,$marcflavour);
304 my $marccontrolnumber   = GetMarcControlnumber ($record, $marcflavour);
305 my $marcissns = GetMarcISSN( $record, $marcflavour );
306 my $issn = $marcissns->[0] || '';
307
308 if (my $search_for_title = C4::Context->preference('OPACSearchForTitleIn')){
309     $dat->{title} =~ s/\/+$//; # remove trailing slash
310     $dat->{title} =~ s/\s+$//; # remove trailing space
311     $search_for_title = parametrized_url(
312         $search_for_title,
313         {
314             TITLE         => $dat->{title},
315             AUTHOR        => $dat->{author},
316             ISBN          => $isbn,
317             ISSN          => $issn,
318             CONTROLNUMBER => $marccontrolnumber,
319             BIBLIONUMBER  => $biblionumber,
320         }
321     );
322     $template->param('OPACSearchForTitleIn' => $search_for_title);
323 }
324
325 $template->param(
326     item_loop        => \@item_value_loop,
327     item_header_loop => \@header_value_loop,
328     biblionumber     => $biblionumber,
329 );
330
331 output_html_with_http_headers $query, $cookie, $template->output;