Bug 28591: Don't pass debug to get_template_and_user
[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::Biblios;
61 use Koha::CirculationRules;
62 use Koha::Items;
63 use Koha::ItemTypes;
64 use Koha::Patrons;
65 use Koha::RecordProcessor;
66 use Koha::DateUtils;
67
68 my $query = CGI->new();
69
70 my $biblionumber = $query->param('biblionumber');
71 if ( ! $biblionumber ) {
72     print $query->redirect("/cgi-bin/koha/errors/404.pl");
73     exit;
74 }
75 $biblionumber = int($biblionumber);
76
77 # open template
78 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
79     {
80         template_name   => "opac-MARCdetail.tt",
81         query           => $query,
82         type            => "opac",
83         authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ),
84     }
85 );
86
87 my $patron = Koha::Patrons->find( $loggedinuser );
88 my $borcat = q{};
89 if ( C4::Context->preference('OpacHiddenItemsExceptions') ) {
90     # we need to fetch the borrower info here, so we can pass the category
91     $borcat = $patron ? $patron->categorycode : $borcat;
92 }
93
94 my $record = GetMarcBiblio({
95     biblionumber => $biblionumber,
96     embed_items  => 1,
97     opac         => 1,
98     borcat       => $borcat });
99 if ( ! $record ) {
100     print $query->redirect("/cgi-bin/koha/errors/404.pl");
101     exit;
102 }
103
104 my $biblio = Koha::Biblios->find( $biblionumber );
105 unless ( $patron and $patron->category->override_hidden_items ) {
106     # only skip this check if there's a logged in user
107     # and its category overrides OpacHiddenItems
108     if ( $biblio->hidden_in_opac({ rules => C4::Context->yaml_preference('OpacHiddenItems') }) ) {
109         print $query->redirect('/cgi-bin/koha/errors/404.pl'); # escape early
110         exit;
111     }
112 }
113
114 my $framework = $biblio ? $biblio->frameworkcode : q{};
115 my $tagslib   = &GetMarcStructure( 0, $framework );
116
117 my $record_processor = Koha::RecordProcessor->new({
118     filters => 'ViewPolicy',
119     options => {
120         interface => 'opac',
121         frameworkcode => $framework
122     }
123 });
124 $record_processor->process($record);
125
126 # get biblionumbers stored in the cart
127 if(my $cart_list = $query->cookie("bib_list")){
128     my @cart_list = split(/\//, $cart_list);
129     if ( grep {$_ eq $biblionumber} @cart_list) {
130         $template->param( incart => 1 );
131     }
132 }
133
134 my ($bt_tag,$bt_subtag) = GetMarcFromKohaField( 'biblio.title' );
135 $template->param(
136     bibliotitle => $biblio->title,
137 ) if $tagslib->{$bt_tag}->{$bt_subtag}->{hidden} <= 0 && # <=0 OPAC visible.
138      $tagslib->{$bt_tag}->{$bt_subtag}->{hidden} > -8;   # except -8;
139
140 my $norequests = 1;
141 my $allow_onshelf_holds;
142 my $items = $biblio->items;
143
144 while ( my $item = $items->next ) {
145     $norequests = 0
146       if $norequests
147         && !$item->withdrawn
148         && !$item->itemlost
149         && ($item->notforloan < 0 || not $item->notforloan )
150         && !Koha::ItemTypes->find($item->effective_itemtype)->notforloan
151         && $item->itemnumber;
152
153     $allow_onshelf_holds = Koha::CirculationRules->get_onshelfholds_policy( { item => $item, patron => $patron } )
154       unless $allow_onshelf_holds;
155 }
156
157 if( $allow_onshelf_holds || CountItemsIssued($biblionumber) || $biblio->has_items_waiting_or_intransit ) {
158     $template->param( ReservableItems => 1 );
159 }
160
161 # adding the $RequestOnOpac param
162 my $RequestOnOpac;
163 if (C4::Context->preference("RequestOnOpac")) {
164         $RequestOnOpac = 1;
165 }
166
167 # fill arrays
168 my @loop_data = ();
169
170 # loop through each tab 0 through 9
171 for ( my $tabloop = 0 ; $tabloop <= 9 ; $tabloop++ ) {
172
173     # loop through each tag
174     my @loop_data = ();
175     my @subfields_data;
176
177     # deal with leader
178     unless ( $tagslib->{'000'}->{'@'}->{tab} ne $tabloop
179         or $tagslib->{'000'}->{'@'}->{hidden} > 0 )
180     {
181         my %subfield_data;
182         $subfield_data{marc_lib}      = $tagslib->{'000'}->{'@'}->{lib};
183         $subfield_data{marc_value}    = $record->leader();
184         $subfield_data{marc_subfield} = '@';
185         $subfield_data{marc_tag}      = '000';
186         push( @subfields_data, \%subfield_data );
187         my %tag_data;
188         $tag_data{tag} = '000 -' . $tagslib->{'000'}->{lib};
189         my @tmp = @subfields_data;
190         $tag_data{subfield} = \@tmp;
191         push( @loop_data, \%tag_data );
192         undef @subfields_data;
193     }
194     my @fields = $record->fields();
195     for ( my $x_i = 0 ; $x_i <= $#fields ; $x_i++ ) {
196
197         # if tag <10, there's no subfield, use the "@" trick
198         if ( $fields[$x_i]->tag() < 10 ) {
199             next
200               if (
201                 $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{tab} ne $tabloop );
202             next if ( $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{hidden} > 0 );
203             my %subfield_data;
204             $subfield_data{marc_lib} =
205               $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{lib};
206             $subfield_data{marc_value}    = $fields[$x_i]->data();
207             $subfield_data{marc_subfield} = '@';
208             $subfield_data{marc_tag}      = $fields[$x_i]->tag();
209             push( @subfields_data, \%subfield_data );
210         }
211         else {
212             my @subf = $fields[$x_i]->subfields;
213             my $previous = '';
214             # loop through each subfield
215             for my $i ( 0 .. $#subf ) {
216                 $subf[$i][0] = "@" unless defined($subf[$i][0]);
217                 my $sf_def = $tagslib->{ $fields[$x_i]->tag() };
218                 $sf_def = $sf_def->{ $subf[$i][0] } if defined($sf_def);
219                 my ($tab,$hidden,$lib);
220                 $tab = $sf_def->{tab} if defined($sf_def);
221                 $tab = $tab // int($fields[$x_i]->tag()/100);
222                 $hidden = $sf_def->{hidden} if defined($sf_def);
223                 $hidden = $hidden // 0;
224                 next if ( $tab != $tabloop );
225                 next if ( $hidden > 0 );
226                 my %subfield_data;
227                 $lib = $sf_def->{lib} if defined($sf_def);
228                 $lib = $lib // '--';
229                 $subfield_data{marc_lib} = ($lib eq $previous) ?  '--' : $lib;
230                 $previous = $lib;
231                 $subfield_data{link} = $sf_def->{link};
232                 $subf[$i][1] =~ s/\n/<br\/>/g;
233                 if ( $sf_def->{isurl} ) {
234                     $subfield_data{marc_value} = "<a href=\"$subf[$i][1]\">$subf[$i][1]</a>";
235                 }
236                 elsif ( defined($sf_def->{kohafield}) && $sf_def->{kohafield} eq "biblioitems.isbn" ) {
237                     $subfield_data{marc_value} = $subf[$i][1];
238                 }
239                 else {
240                     if ( $sf_def->{authtypecode} ) {
241                         $subfield_data{authority} = $fields[$x_i]->subfield(9);
242                     }
243                     $subfield_data{marc_value} = GetAuthorisedValueDesc( $fields[$x_i]->tag(),
244                         $subf[$i][0], $subf[$i][1], '', $tagslib, '', 'opac' );
245                 }
246                 $subfield_data{marc_subfield} = $subf[$i][0];
247                 $subfield_data{marc_tag}      = $fields[$x_i]->tag();
248                 push( @subfields_data, \%subfield_data );
249             }
250         }
251         if ( $#subfields_data >= 0 ) {
252             my %tag_data;
253             if (   ( $fields[$x_i]->tag() eq $fields[ $x_i - 1 ]->tag() )
254                 && ( C4::Context->preference('LabelMARCView') eq 'economical' )
255               )
256             {
257                 $tag_data{tag} = "";
258             }
259             else {
260                 if ( C4::Context->preference('hide_marc') ) {
261                     $tag_data{tag} = $tagslib->{ $fields[$x_i]->tag() }->{lib};
262                 }
263                 else {
264                     my $sf_def = $tagslib->{ $fields[$x_i]->tag() };
265                     my $lib;
266                     $lib = $sf_def->{lib} if defined($sf_def);
267                     $lib = $lib // '';
268                     $tag_data{tag} = $fields[$x_i]->tag() . ' '
269                       . C4::Koha::display_marc_indicators($fields[$x_i])
270                       . " - $lib";
271                 }
272             }
273             my @tmp = @subfields_data;
274             $tag_data{subfield} = \@tmp;
275             push( @loop_data, \%tag_data );
276             undef @subfields_data;
277         }
278     }
279     $template->param( "tab" . $tabloop . "XX" => \@loop_data );
280 }
281
282
283 # now, build item tab !
284 # the main difference is that datas are in lines and not in columns : thus, we build the <th> first, then the values...
285 # loop through each tag
286 # warning : we may have differents number of columns in each row. Thus, we first build a hash, complete it if necessary
287 # then construct template.
288 # $record has already had all the item fields filtered above.
289 my @fields = $record->fields();
290 my %witness
291   ; #---- stores the list of subfields used at least once, with the "meaning" of the code
292 my @item_subfield_codes;
293 my @item_loop;
294 foreach my $field (@fields) {
295     next if ( $field->tag() < 10 );
296     my @subf = $field->subfields;
297     my $item;
298
299     # loop through each subfield
300     for my $i ( 0 .. $#subf ) {
301         my $sf_def = $tagslib->{ $field->tag() }->{ $subf[$i][0] };
302         next if ( ($sf_def->{tab}||0) != 10 );
303         next if ( ($sf_def->{hidden}||0) > 0 );
304
305         push @item_subfield_codes, $subf[$i][0];
306         $witness{ $subf[$i][0] } = $sf_def->{lib};
307
308         # Allow repeatables (BZ 13574)
309         if( $item->{$subf[$i][0]} ) {
310             $item->{$subf[$i][0]} .= ' | ';
311         } else {
312             $item->{$subf[$i][0]} = q{};
313         }
314
315         if ( $sf_def->{isurl} ) {
316             $item->{ $subf[$i][0] } .= "<a href=\"$subf[$i][1]\">$subf[$i][1]</a>";
317         }
318         elsif ( $sf_def->{kohafield} eq "biblioitems.isbn" ) {
319             $item->{ $subf[$i][0] } .= $subf[$i][1];
320         }
321         else {
322             $item->{ $subf[$i][0] } .= GetAuthorisedValueDesc( $field->tag(), $subf[$i][0],
323                 $subf[$i][1], '', $tagslib, '', 'opac' ) // q{};
324         }
325
326         my $kohafield = $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{kohafield};
327         $item->{ $subf[$i][0] } = output_pref( { str => $item->{ $subf[$i][0] }, dateonly => 1 } )
328           if grep { $kohafield eq $_ }
329               qw( items.dateaccessioned items.onloan items.datelastseen items.datelastborrowed items.replacementpricedate );
330
331     }
332     push @item_loop, $item if $item;
333 }
334 my ( $holdingbrtagf, $holdingbrtagsubf ) =
335   &GetMarcFromKohaField( "items.holdingbranch" );
336 @item_loop =
337   sort { ($a->{$holdingbrtagsubf}||'') cmp ($b->{$holdingbrtagsubf}||'') } @item_loop;
338
339 @item_subfield_codes = uniq @item_subfield_codes;
340 # fill item info
341 my @item_header_loop;
342 for my $subfield_code ( @item_subfield_codes ) {
343     push @item_header_loop, $witness{$subfield_code};
344     for my $item_data ( @item_loop ) {
345         $item_data->{$subfield_code} ||= "&nbsp;"
346      }
347 }
348
349 if ( C4::Context->preference("OPACISBD") ) {
350     $template->param( ISBD => 1 );
351 }
352
353 #Search for title in links
354 my $marcflavour  = C4::Context->preference("marcflavour");
355 my $dat = TransformMarcToKoha( $record );
356 my $isbn = GetNormalizedISBN(undef,$record,$marcflavour);
357 my $marccontrolnumber   = GetMarcControlnumber ($record, $marcflavour);
358 my $marcissns = GetMarcISSN( $record, $marcflavour );
359 my $issn = $marcissns->[0] || '';
360
361 if (my $search_for_title = C4::Context->preference('OPACSearchForTitleIn')){
362     $dat->{title} =~ s/\/+$//; # remove trailing slash
363     $dat->{title} =~ s/\s+$//; # remove trailing space
364     $search_for_title = parametrized_url(
365         $search_for_title,
366         {
367             TITLE         => $dat->{title},
368             AUTHOR        => $dat->{author},
369             ISBN          => $isbn,
370             ISSN          => $issn,
371             CONTROLNUMBER => $marccontrolnumber,
372             BIBLIONUMBER  => $biblionumber,
373         }
374     );
375     $template->param('OPACSearchForTitleIn' => $search_for_title);
376 }
377
378 if( C4::Context->preference('ArticleRequests') ) {
379     my $itemtype = Koha::ItemTypes->find($biblio->itemtype);
380     my $artreqpossible = $patron
381         ? $biblio->can_article_request( $patron )
382         : $itemtype
383         ? $itemtype->may_article_request
384         : q{};
385     $template->param( artreqpossible => $artreqpossible );
386 }
387
388 $template->param(
389     item_loop           => \@item_loop,
390     item_header_loop    => \@item_header_loop,
391     item_subfield_codes => \@item_subfield_codes,
392     biblio              => $biblio,
393     norequests          => $norequests,
394 );
395
396 output_html_with_http_headers $query, $cookie, $template->output;