Bug 18624: (followup) opac-authorities-home.tt uses 'any'
[koha.git] / opac / opac-MARCdetail.pl
bloba899e92e82e0d2c5552b797c0bcaef4ad1c509cf
1 #!/usr/bin/perl
3 # This file is part of Koha.
5 # Copyright (C) 2000-2002 Katipo Communications
6 # Parts Copyright (C) 2010 BibLibre
7 # Parts Copyright (C) 2013 Mark Tompsett
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.
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.
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>.
23 =head1 NAME
25 opac-MARCdetail.pl : script to show a biblio in MARC format
27 =head1 SYNOPSIS
29 =cut
31 =head1 DESCRIPTION
33 This script needs a biblionumber as parameter
35 It shows the biblio in a (nice) MARC format depending on MARC
36 parameters tables.
38 The template is in <templates_dir>/catalogue/MARCdetail.tt.
39 this template must be divided into 11 "tabs".
41 The first 10 tabs present the biblio, the 11th one presents
42 the items attached to the biblio
44 =cut
46 use Modern::Perl;
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::Patrons;
62 use Koha::RecordProcessor;
64 my $query = new CGI;
66 my $dbh = C4::Context->dbh;
68 my $biblionumber = $query->param('biblionumber');
69 if ( ! $biblionumber ) {
70 print $query->redirect("/cgi-bin/koha/errors/404.pl");
71 exit;
74 my $record = GetMarcBiblio($biblionumber, 1);
75 if ( ! $record ) {
76 print $query->redirect("/cgi-bin/koha/errors/404.pl");
77 exit;
80 my @all_items = GetItemsInfo($biblionumber);
81 my @items2hide;
82 if (scalar @all_items >= 1) {
83 push @items2hide, GetHiddenItemnumbers(@all_items);
85 if (scalar @items2hide == scalar @all_items ) {
86 print $query->redirect("/cgi-bin/koha/errors/404.pl");
87 exit;
91 my $framework = &GetFrameworkCode( $biblionumber );
92 my $tagslib = &GetMarcStructure( 0, $framework );
93 my ($tag_itemnumber,$subtag_itemnumber) = &GetMarcFromKohaField('items.itemnumber',$framework);
94 my $biblio = Koha::Biblios->find( $biblionumber );
96 my $record_processor = Koha::RecordProcessor->new({
97 filters => 'ViewPolicy',
98 options => {
99 interface => 'opac',
100 frameworkcode => $framework
103 $record_processor->process($record);
105 # open template
106 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
108 template_name => "opac-MARCdetail.tt",
109 query => $query,
110 type => "opac",
111 authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ),
112 debug => 1,
116 my ($bt_tag,$bt_subtag) = GetMarcFromKohaField('biblio.title',$framework);
117 $template->param(
118 bibliotitle => $biblio->title,
119 ) if $tagslib->{$bt_tag}->{$bt_subtag}->{hidden} <= 0 && # <=0 OPAC visible.
120 $tagslib->{$bt_tag}->{$bt_subtag}->{hidden} > -8; # except -8;
122 # get biblionumbers stored in the cart
123 if(my $cart_list = $query->cookie("bib_list")){
124 my @cart_list = split(/\//, $cart_list);
125 if ( grep {$_ eq $biblionumber} @cart_list) {
126 $template->param( incart => 1 );
130 my $allow_onshelf_holds;
131 my $patron = Koha::Patrons->find( $loggedinuser );
132 for my $itm (@all_items) {
133 $allow_onshelf_holds = C4::Reserves::OnShelfHoldsAllowed( $itm, ( $patron ? $patron->unblessed : {} ) );
134 last if $allow_onshelf_holds;
137 $template->param( 'AllowOnShelfHolds' => $allow_onshelf_holds );
138 $template->param( 'ItemsIssued' => CountItemsIssued( $biblionumber ) );
140 # adding the $RequestOnOpac param
141 my $RequestOnOpac;
142 if (C4::Context->preference("RequestOnOpac")) {
143 $RequestOnOpac = 1;
146 # fill arrays
147 my @loop_data = ();
148 my $tag;
150 # loop through each tab 0 through 9
151 for ( my $tabloop = 0 ; $tabloop <= 9 ; $tabloop++ ) {
153 # loop through each tag
154 my @loop_data = ();
155 my @subfields_data;
157 # deal with leader
158 unless ( $tagslib->{'000'}->{'@'}->{tab} ne $tabloop
159 or $tagslib->{'000'}->{'@'}->{hidden} > 0 )
161 my %subfield_data;
162 $subfield_data{marc_lib} = $tagslib->{'000'}->{'@'}->{lib};
163 $subfield_data{marc_value} = $record->leader();
164 $subfield_data{marc_subfield} = '@';
165 $subfield_data{marc_tag} = '000';
166 push( @subfields_data, \%subfield_data );
167 my %tag_data;
168 $tag_data{tag} = '000 -' . $tagslib->{'000'}->{lib};
169 my @tmp = @subfields_data;
170 $tag_data{subfield} = \@tmp;
171 push( @loop_data, \%tag_data );
172 undef @subfields_data;
174 my @fields = $record->fields();
175 for ( my $x_i = 0 ; $x_i <= $#fields ; $x_i++ ) {
177 # if tag <10, there's no subfield, use the "@" trick
178 if ( $fields[$x_i]->tag() < 10 ) {
179 next
180 if (
181 $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{tab} ne $tabloop );
182 next if ( $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{hidden} > 0 );
183 my %subfield_data;
184 $subfield_data{marc_lib} =
185 $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{lib};
186 $subfield_data{marc_value} = $fields[$x_i]->data();
187 $subfield_data{marc_subfield} = '@';
188 $subfield_data{marc_tag} = $fields[$x_i]->tag();
189 push( @subfields_data, \%subfield_data );
191 else {
192 my @subf = $fields[$x_i]->subfields;
193 my $previous = '';
194 # loop through each subfield
195 for my $i ( 0 .. $#subf ) {
196 $subf[$i][0] = "@" unless defined($subf[$i][0]);
197 my $sf_def = $tagslib->{ $fields[$x_i]->tag() };
198 $sf_def = $sf_def->{ $subf[$i][0] } if defined($sf_def);
199 my ($tab,$hidden,$lib);
200 $tab = $sf_def->{tab} if defined($sf_def);
201 $tab = $tab // int($fields[$x_i]->tag()/100);
202 $hidden = $sf_def->{hidden} if defined($sf_def);
203 $hidden = $hidden // 0;
204 next if ( $tab != $tabloop );
205 next if ( $hidden > 0 );
206 my %subfield_data;
207 $lib = $sf_def->{lib} if defined($sf_def);
208 $lib = $lib // '--';
209 $subfield_data{marc_lib} = ($lib eq $previous) ? '--' : $lib;
210 $previous = $lib;
211 $subfield_data{link} = $sf_def->{link};
212 $subf[$i][1] =~ s/\n/<br\/>/g;
213 if ( $sf_def->{isurl} ) {
214 $subfield_data{marc_value} = "<a href=\"$subf[$i][1]\">$subf[$i][1]</a>";
216 elsif ( defined($sf_def->{kohafield}) && $sf_def->{kohafield} eq "biblioitems.isbn" ) {
217 $subfield_data{marc_value} = $subf[$i][1];
219 else {
220 if ( $sf_def->{authtypecode} ) {
221 $subfield_data{authority} = $fields[$x_i]->subfield(9);
223 $subfield_data{marc_value} = GetAuthorisedValueDesc( $fields[$x_i]->tag(),
224 $subf[$i][0], $subf[$i][1], '', $tagslib, '', 'opac' );
226 $subfield_data{marc_subfield} = $subf[$i][0];
227 $subfield_data{marc_tag} = $fields[$x_i]->tag();
228 push( @subfields_data, \%subfield_data );
231 if ( $#subfields_data >= 0 ) {
232 my %tag_data;
233 if ( ( $fields[$x_i]->tag() eq $fields[ $x_i - 1 ]->tag() )
234 && ( C4::Context->preference('LabelMARCView') eq 'economical' )
237 $tag_data{tag} = "";
239 else {
240 if ( C4::Context->preference('hide_marc') ) {
241 $tag_data{tag} = $tagslib->{ $fields[$x_i]->tag() }->{lib};
243 else {
244 my $sf_def = $tagslib->{ $fields[$x_i]->tag() };
245 my $lib;
246 $lib = $sf_def->{lib} if defined($sf_def);
247 $lib = $lib // '';
248 $tag_data{tag} = $fields[$x_i]->tag() . ' '
249 . C4::Koha::display_marc_indicators($fields[$x_i])
250 . " - $lib";
253 my @tmp = @subfields_data;
254 $tag_data{subfield} = \@tmp;
255 push( @loop_data, \%tag_data );
256 undef @subfields_data;
259 $template->param( "tab" . $tabloop . "XX" => \@loop_data );
263 # now, build item tab !
264 # the main difference is that datas are in lines and not in columns : thus, we build the <th> first, then the values...
265 # loop through each tag
266 # warning : we may have differents number of columns in each row. Thus, we first build a hash, complete it if necessary
267 # then construct template.
268 my @fields = $record->fields();
269 my %witness
270 ; #---- stores the list of subfields used at least once, with the "meaning" of the code
271 my @item_subfield_codes;
272 my @item_loop;
273 foreach my $field (@fields) {
274 next if ( $field->tag() < 10 );
275 next if ( ( $field->tag() eq $tag_itemnumber ) &&
276 ( any { $field->subfield($subtag_itemnumber) eq $_ }
277 @items2hide) );
278 my @subf = $field->subfields;
279 my $item;
281 # loop through each subfield
282 for my $i ( 0 .. $#subf ) {
283 my $sf_def = $tagslib->{ $field->tag() }->{ $subf[$i][0] };
284 next if ( ($sf_def->{tab}||0) != 10 );
285 next if ( ($sf_def->{hidden}||0) > 0 );
286 push @item_subfield_codes, $subf[$i][0];
287 $witness{ $subf[$i][0] } = $sf_def->{lib};
289 if ( $sf_def->{isurl} ) {
290 $item->{ $subf[$i][0] } = "<a href=\"$subf[$i][1]\">$subf[$i][1]</a>";
292 elsif ( $sf_def->{kohafield} eq "biblioitems.isbn" ) {
293 $item->{ $subf[$i][0] } = $subf[$i][1];
295 else {
296 $item->{ $subf[$i][0] } = GetAuthorisedValueDesc( $field->tag(), $subf[$i][0],
297 $subf[$i][1], '', $tagslib, '', 'opac' );
300 push @item_loop, $item if $item;
302 my ( $holdingbrtagf, $holdingbrtagsubf ) =
303 &GetMarcFromKohaField( "items.holdingbranch", $framework );
304 @item_loop =
305 sort { ($a->{$holdingbrtagsubf}||'') cmp ($b->{$holdingbrtagsubf}||'') } @item_loop;
307 @item_subfield_codes = uniq @item_subfield_codes;
308 # fill item info
309 my @item_header_loop;
310 for my $subfield_code ( @item_subfield_codes ) {
311 push @item_header_loop, $witness{$subfield_code};
312 for my $item_data ( @item_loop ) {
313 $item_data->{$subfield_code} ||= "&nbsp;"
317 if ( C4::Context->preference("OPACISBD") ) {
318 $template->param( ISBD => 1 );
321 #Search for title in links
322 my $marcflavour = C4::Context->preference("marcflavour");
323 my $dat = TransformMarcToKoha( $record );
324 my $isbn = GetNormalizedISBN(undef,$record,$marcflavour);
325 my $marccontrolnumber = GetMarcControlnumber ($record, $marcflavour);
326 my $marcissns = GetMarcISSN( $record, $marcflavour );
327 my $issn = $marcissns->[0] || '';
329 if (my $search_for_title = C4::Context->preference('OPACSearchForTitleIn')){
330 $dat->{title} =~ s/\/+$//; # remove trailing slash
331 $dat->{title} =~ s/\s+$//; # remove trailing space
332 $search_for_title = parametrized_url(
333 $search_for_title,
335 TITLE => $dat->{title},
336 AUTHOR => $dat->{author},
337 ISBN => $isbn,
338 ISSN => $issn,
339 CONTROLNUMBER => $marccontrolnumber,
340 BIBLIONUMBER => $biblionumber,
343 $template->param('OPACSearchForTitleIn' => $search_for_title);
346 $template->param(
347 item_loop => \@item_loop,
348 item_header_loop => \@item_header_loop,
349 item_subfield_codes => \@item_subfield_codes,
350 biblionumber => $biblionumber,
353 output_html_with_http_headers $query, $cookie, $template->output;