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