Bug 11592: MARCView and ISBD followup
[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.tt.
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 qw ( -utf8 );
52 use MARC::Record;
53 use C4::Biblio;
54 use C4::Items;
55 use C4::Reserves;
56 use C4::Members;
57 use C4::Acquisition;
58 use C4::Koha;
59 use List::MoreUtils qw( any uniq );
60 use Koha::RecordProcessor;
61
62 my $query = new CGI;
63
64 my $dbh = C4::Context->dbh;
65
66 my $biblionumber = $query->param('biblionumber');
67 if ( ! $biblionumber ) {
68     print $query->redirect("/cgi-bin/koha/errors/404.pl");
69     exit;
70 }
71
72 my @all_items = GetItemsInfo($biblionumber);
73 my @items2hide;
74 if (scalar @all_items >= 1) {
75     push @items2hide, GetHiddenItemnumbers(@all_items);
76
77     if (scalar @items2hide == scalar @all_items ) {
78         print $query->redirect("/cgi-bin/koha/errors/404.pl");
79         exit;
80     }
81 }
82
83 my $itemtype     = &GetFrameworkCode($biblionumber);
84 my $tagslib      = &GetMarcStructure( 0, $itemtype );
85 my ($tag_itemnumber,$subtag_itemnumber) = &GetMarcFromKohaField('items.itemnumber',$itemtype);
86 my $biblio = GetBiblioData($biblionumber);
87 $biblionumber = $biblio->{biblionumber};
88 my $record_unfiltered = GetMarcBiblio($biblionumber, 1);
89 if ( ! $record_unfiltered ) {
90     print $query->redirect("/cgi-bin/koha/errors/404.pl");
91     exit;
92 }
93 my $record_processor = Koha::RecordProcessor->new({ filters => 'ViewPolicy' });
94 my $record_filtered  = $record_unfiltered->clone();
95 my $record           = $record_processor->process($record_filtered);
96
97 # open template
98 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
99     {
100         template_name   => "opac-MARCdetail.tt",
101         query           => $query,
102         type            => "opac",
103         authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ),
104         debug           => 1,
105     }
106 );
107
108 my ($bt_tag,$bt_subtag) = GetMarcFromKohaField('biblio.title',$itemtype);
109 $template->param(
110     bibliotitle => $biblio->{title},
111 ) if $tagslib->{$bt_tag}->{$bt_subtag}->{hidden} <= 0 && # <=0 OPAC visible.
112      $tagslib->{$bt_tag}->{$bt_subtag}->{hidden} > -8;   # except -8;
113
114 # get biblionumbers stored in the cart
115 if(my $cart_list = $query->cookie("bib_list")){
116     my @cart_list = split(/\//, $cart_list);
117     if ( grep {$_ eq $biblionumber} @cart_list) {
118         $template->param( incart => 1 );
119     }
120 }
121
122 my $allow_onshelf_holds;
123 my $borrower = GetMember( 'borrowernumber' => $loggedinuser );
124 for my $itm (@all_items) {
125     $allow_onshelf_holds = C4::Reserves::OnShelfHoldsAllowed($itm, $borrower);
126     last if $allow_onshelf_holds;
127 }
128
129 $template->param( 'AllowOnShelfHolds' => $allow_onshelf_holds );
130 $template->param( 'ItemsIssued' => CountItemsIssued( $biblionumber ) );
131
132 # adding the $RequestOnOpac param
133 my $RequestOnOpac;
134 if (C4::Context->preference("RequestOnOpac")) {
135         $RequestOnOpac = 1;
136 }
137
138 # fill arrays
139 my @loop_data = ();
140 my $tag;
141
142 # loop through each tab 0 through 9
143 for ( my $tabloop = 0 ; $tabloop <= 9 ; $tabloop++ ) {
144
145     # loop through each tag
146     my @loop_data = ();
147     my @subfields_data;
148
149     # deal with leader
150     unless ( $tagslib->{'000'}->{'@'}->{tab} ne $tabloop
151         or $tagslib->{'000'}->{'@'}->{hidden} > 0 )
152     {
153         my %subfield_data;
154         $subfield_data{marc_lib}      = $tagslib->{'000'}->{'@'}->{lib};
155         $subfield_data{marc_value}    = $record->leader();
156         $subfield_data{marc_subfield} = '@';
157         $subfield_data{marc_tag}      = '000';
158         push( @subfields_data, \%subfield_data );
159         my %tag_data;
160         $tag_data{tag} = '000 -' . $tagslib->{'000'}->{lib};
161         my @tmp = @subfields_data;
162         $tag_data{subfield} = \@tmp;
163         push( @loop_data, \%tag_data );
164         undef @subfields_data;
165     }
166     my @fields = $record->fields();
167     for ( my $x_i = 0 ; $x_i <= $#fields ; $x_i++ ) {
168
169         # if tag <10, there's no subfield, use the "@" trick
170         if ( $fields[$x_i]->tag() < 10 ) {
171             next
172               if (
173                 $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{tab} ne $tabloop );
174             next if ( $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{hidden} > 0 );
175             my %subfield_data;
176             $subfield_data{marc_lib} =
177               $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{lib};
178             $subfield_data{marc_value}    = $fields[$x_i]->data();
179             $subfield_data{marc_subfield} = '@';
180             $subfield_data{marc_tag}      = $fields[$x_i]->tag();
181             push( @subfields_data, \%subfield_data );
182         }
183         else {
184             my @subf = $fields[$x_i]->subfields;
185             my $previous = '';
186             # loop through each subfield
187             for my $i ( 0 .. $#subf ) {
188                 $subf[$i][0] = "@" unless defined($subf[$i][0]);
189                 my $sf_def = $tagslib->{ $fields[$x_i]->tag() };
190                 $sf_def = $sf_def->{ $subf[$i][0] } if defined($sf_def);
191                 my ($tab,$hidden,$lib);
192                 $tab = $sf_def->{tab} if defined($sf_def);
193                 $tab = $tab // int($fields[$x_i]->tag()/100);
194                 $hidden = $sf_def->{hidden} if defined($sf_def);
195                 $hidden = $hidden // 0;
196                 next if ( $tab != $tabloop );
197                 next if ( $hidden > 0 );
198                 my %subfield_data;
199                 $lib = $sf_def->{lib} if defined($sf_def);
200                 $lib = $lib // '--';
201                 $subfield_data{marc_lib} = ($lib eq $previous) ?  '--' : $lib;
202                 $previous = $lib;
203                 $subfield_data{link} = $sf_def->{link};
204                 $subf[$i][1] =~ s/\n/<br\/>/g;
205                 if ( $sf_def->{isurl} ) {
206                     $subfield_data{marc_value} = "<a href=\"$subf[$i][1]\">$subf[$i][1]</a>";
207                 }
208                 elsif ( defined($sf_def->{kohafield}) && $sf_def->{kohafield} eq "biblioitems.isbn" ) {
209                     $subfield_data{marc_value} = $subf[$i][1];
210                 }
211                 else {
212                     if ( $sf_def->{authtypecode} ) {
213                         $subfield_data{authority} = $fields[$x_i]->subfield(9);
214                     }
215                     $subfield_data{marc_value} = GetAuthorisedValueDesc( $fields[$x_i]->tag(),
216                         $subf[$i][0], $subf[$i][1], '', $tagslib, '', 'opac' );
217                 }
218                 $subfield_data{marc_subfield} = $subf[$i][0];
219                 $subfield_data{marc_tag}      = $fields[$x_i]->tag();
220                 push( @subfields_data, \%subfield_data );
221             }
222         }
223         if ( $#subfields_data >= 0 ) {
224             my %tag_data;
225             if (   ( $fields[$x_i]->tag() eq $fields[ $x_i - 1 ]->tag() )
226                 && ( C4::Context->preference('LabelMARCView') eq 'economical' )
227               )
228             {
229                 $tag_data{tag} = "";
230             }
231             else {
232                 if ( C4::Context->preference('hide_marc') ) {
233                     $tag_data{tag} = $tagslib->{ $fields[$x_i]->tag() }->{lib};
234                 }
235                 else {
236                     my $sf_def = $tagslib->{ $fields[$x_i]->tag() };
237                     my $lib;
238                     $lib = $sf_def->{lib} if defined($sf_def);
239                     $lib = $lib // '';
240                     $tag_data{tag} = $fields[$x_i]->tag() . ' '
241                       . C4::Koha::display_marc_indicators($fields[$x_i])
242                       . " - $lib";
243                 }
244             }
245             my @tmp = @subfields_data;
246             $tag_data{subfield} = \@tmp;
247             push( @loop_data, \%tag_data );
248             undef @subfields_data;
249         }
250     }
251     $template->param( "tab" . $tabloop . "XX" => \@loop_data );
252 }
253
254
255 # now, build item tab !
256 # the main difference is that datas are in lines and not in columns : thus, we build the <th> first, then the values...
257 # loop through each tag
258 # warning : we may have differents number of columns in each row. Thus, we first build a hash, complete it if necessary
259 # then construct template.
260 my @fields = $record->fields();
261 my %witness
262   ; #---- stores the list of subfields used at least once, with the "meaning" of the code
263 my @item_subfield_codes;
264 my @item_loop;
265 foreach my $field (@fields) {
266     next if ( $field->tag() < 10 );
267     next if ( ( $field->tag() eq $tag_itemnumber ) &&
268               ( any { $field->subfield($subtag_itemnumber) eq $_ }
269                    @items2hide) );
270     my @subf = $field->subfields;
271     my $item;
272
273     # loop through each subfield
274     for my $i ( 0 .. $#subf ) {
275         my $sf_def = $tagslib->{ $field->tag() }->{ $subf[$i][0] };
276         next if ( ($sf_def->{tab}||0) != 10 );
277         next if ( ($sf_def->{hidden}||0) > 0 );
278         push @item_subfield_codes, $subf[$i][0];
279         $witness{ $subf[$i][0] } = $sf_def->{lib};
280
281         if ( $sf_def->{isurl} ) {
282             $item->{ $subf[$i][0] } = "<a href=\"$subf[$i][1]\">$subf[$i][1]</a>";
283         }
284         elsif ( $sf_def->{kohafield} eq "biblioitems.isbn" ) {
285             $item->{ $subf[$i][0] } = $subf[$i][1];
286         }
287         else {
288             $item->{ $subf[$i][0] } = GetAuthorisedValueDesc( $field->tag(), $subf[$i][0],
289                 $subf[$i][1], '', $tagslib, '', 'opac' );
290         }
291     }
292     push @item_loop, $item if $item;
293 }
294 my ( $holdingbrtagf, $holdingbrtagsubf ) =
295   &GetMarcFromKohaField( "items.holdingbranch", $itemtype );
296 @item_loop =
297   sort { ($a->{$holdingbrtagsubf}||'') cmp ($b->{$holdingbrtagsubf}||'') } @item_loop;
298
299 @item_subfield_codes = uniq @item_subfield_codes;
300 # fill item info
301 my @item_header_loop;
302 for my $subfield_code ( @item_subfield_codes ) {
303     push @item_header_loop, $witness{$subfield_code};
304     for my $item_data ( @item_loop ) {
305         $item_data->{$subfield_code} ||= "&nbsp;"
306      }
307 }
308
309 if ( C4::Context->preference("OPACISBD") ) {
310     $template->param( ISBD => 1 );
311 }
312
313 #Search for title in links
314 my $marcflavour  = C4::Context->preference("marcflavour");
315 my $dat = TransformMarcToKoha( $record );
316 my $isbn = GetNormalizedISBN(undef,$record,$marcflavour);
317 my $marccontrolnumber   = GetMarcControlnumber ($record, $marcflavour);
318 my $marcissns = GetMarcISSN( $record, $marcflavour );
319 my $issn = $marcissns->[0] || '';
320
321 if (my $search_for_title = C4::Context->preference('OPACSearchForTitleIn')){
322     $dat->{title} =~ s/\/+$//; # remove trailing slash
323     $dat->{title} =~ s/\s+$//; # remove trailing space
324     $search_for_title = parametrized_url(
325         $search_for_title,
326         {
327             TITLE         => $dat->{title},
328             AUTHOR        => $dat->{author},
329             ISBN          => $isbn,
330             ISSN          => $issn,
331             CONTROLNUMBER => $marccontrolnumber,
332             BIBLIONUMBER  => $biblionumber,
333         }
334     );
335     $template->param('OPACSearchForTitleIn' => $search_for_title);
336 }
337
338 $template->param(
339     item_loop           => \@item_loop,
340     item_header_loop    => \@item_header_loop,
341     item_subfield_codes => \@item_subfield_codes,
342     biblionumber        => $biblionumber,
343 );
344
345 output_html_with_http_headers $query, $cookie, $template->output;