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