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