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