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>.
25 opac-MARCdetail.pl : script to show a biblio in MARC format
33 This script needs a biblionumber as parameter
35 It shows the biblio in a (nice) MARC format depending on MARC
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
59 use List::MoreUtils qw( any uniq );
61 use Koha::CirculationRules;
65 use Koha::RecordProcessor;
68 my $query = CGI->new();
70 my $biblionumber = $query->param('biblionumber');
71 if ( ! $biblionumber ) {
72 print $query->redirect("/cgi-bin/koha/errors/404.pl");
75 $biblionumber = int($biblionumber);
78 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
80 template_name => "opac-MARCdetail.tt",
83 authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ),
88 my $patron = Koha::Patrons->find( $loggedinuser );
90 if ( C4::Context->preference('OpacHiddenItemsExceptions') ) {
91 # we need to fetch the borrower info here, so we can pass the category
92 $borcat = $patron ? $patron->categorycode : $borcat;
95 my $record = GetMarcBiblio({
96 biblionumber => $biblionumber,
101 print $query->redirect("/cgi-bin/koha/errors/404.pl");
105 my $biblio = Koha::Biblios->find( $biblionumber );
106 unless ( $patron and $patron->category->override_hidden_items ) {
107 # only skip this check if there's a logged in user
108 # and its category overrides OpacHiddenItems
109 if ( $biblio->hidden_in_opac({ rules => C4::Context->yaml_preference('OpacHiddenItems') }) ) {
110 print $query->redirect('/cgi-bin/koha/errors/404.pl'); # escape early
115 my $framework = $biblio ? $biblio->frameworkcode : q{};
116 my $tagslib = &GetMarcStructure( 0, $framework );
118 my $record_processor = Koha::RecordProcessor->new({
119 filters => 'ViewPolicy',
122 frameworkcode => $framework
125 $record_processor->process($record);
127 # get biblionumbers stored in the cart
128 if(my $cart_list = $query->cookie("bib_list")){
129 my @cart_list = split(/\//, $cart_list);
130 if ( grep {$_ eq $biblionumber} @cart_list) {
131 $template->param( incart => 1 );
135 my ($bt_tag,$bt_subtag) = GetMarcFromKohaField( 'biblio.title' );
137 bibliotitle => $biblio->title,
138 ) if $tagslib->{$bt_tag}->{$bt_subtag}->{hidden} <= 0 && # <=0 OPAC visible.
139 $tagslib->{$bt_tag}->{$bt_subtag}->{hidden} > -8; # except -8;
142 my $allow_onshelf_holds;
143 my $items = $biblio->items;
145 while ( my $item = $items->next ) {
150 && ($item->notforloan < 0 || not $item->notforloan )
151 && !Koha::ItemTypes->find($item->effective_itemtype)->notforloan
152 && $item->itemnumber;
154 $allow_onshelf_holds = Koha::CirculationRules->get_onshelfholds_policy( { item => $item, patron => $patron } )
155 unless $allow_onshelf_holds;
158 if( $allow_onshelf_holds || CountItemsIssued($biblionumber) || $biblio->has_items_waiting_or_intransit ) {
159 $template->param( ReservableItems => 1 );
162 # adding the $RequestOnOpac param
164 if (C4::Context->preference("RequestOnOpac")) {
171 # loop through each tab 0 through 9
172 for ( my $tabloop = 0 ; $tabloop <= 9 ; $tabloop++ ) {
174 # loop through each tag
179 unless ( $tagslib->{'000'}->{'@'}->{tab} ne $tabloop
180 or $tagslib->{'000'}->{'@'}->{hidden} > 0 )
183 $subfield_data{marc_lib} = $tagslib->{'000'}->{'@'}->{lib};
184 $subfield_data{marc_value} = $record->leader();
185 $subfield_data{marc_subfield} = '@';
186 $subfield_data{marc_tag} = '000';
187 push( @subfields_data, \%subfield_data );
189 $tag_data{tag} = '000 -' . $tagslib->{'000'}->{lib};
190 my @tmp = @subfields_data;
191 $tag_data{subfield} = \@tmp;
192 push( @loop_data, \%tag_data );
193 undef @subfields_data;
195 my @fields = $record->fields();
196 for ( my $x_i = 0 ; $x_i <= $#fields ; $x_i++ ) {
198 # if tag <10, there's no subfield, use the "@" trick
199 if ( $fields[$x_i]->tag() < 10 ) {
202 $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{tab} ne $tabloop );
203 next if ( $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{hidden} > 0 );
205 $subfield_data{marc_lib} =
206 $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{lib};
207 $subfield_data{marc_value} = $fields[$x_i]->data();
208 $subfield_data{marc_subfield} = '@';
209 $subfield_data{marc_tag} = $fields[$x_i]->tag();
210 push( @subfields_data, \%subfield_data );
213 my @subf = $fields[$x_i]->subfields;
215 # loop through each subfield
216 for my $i ( 0 .. $#subf ) {
217 $subf[$i][0] = "@" unless defined($subf[$i][0]);
218 my $sf_def = $tagslib->{ $fields[$x_i]->tag() };
219 $sf_def = $sf_def->{ $subf[$i][0] } if defined($sf_def);
220 my ($tab,$hidden,$lib);
221 $tab = $sf_def->{tab} if defined($sf_def);
222 $tab = $tab // int($fields[$x_i]->tag()/100);
223 $hidden = $sf_def->{hidden} if defined($sf_def);
224 $hidden = $hidden // 0;
225 next if ( $tab != $tabloop );
226 next if ( $hidden > 0 );
228 $lib = $sf_def->{lib} if defined($sf_def);
230 $subfield_data{marc_lib} = ($lib eq $previous) ? '--' : $lib;
232 $subfield_data{link} = $sf_def->{link};
233 $subf[$i][1] =~ s/\n/<br\/>/g;
234 if ( $sf_def->{isurl} ) {
235 $subfield_data{marc_value} = "<a href=\"$subf[$i][1]\">$subf[$i][1]</a>";
237 elsif ( defined($sf_def->{kohafield}) && $sf_def->{kohafield} eq "biblioitems.isbn" ) {
238 $subfield_data{marc_value} = $subf[$i][1];
241 if ( $sf_def->{authtypecode} ) {
242 $subfield_data{authority} = $fields[$x_i]->subfield(9);
244 $subfield_data{marc_value} = GetAuthorisedValueDesc( $fields[$x_i]->tag(),
245 $subf[$i][0], $subf[$i][1], '', $tagslib, '', 'opac' );
247 $subfield_data{marc_subfield} = $subf[$i][0];
248 $subfield_data{marc_tag} = $fields[$x_i]->tag();
249 push( @subfields_data, \%subfield_data );
252 if ( $#subfields_data >= 0 ) {
254 if ( ( $fields[$x_i]->tag() eq $fields[ $x_i - 1 ]->tag() )
255 && ( C4::Context->preference('LabelMARCView') eq 'economical' )
261 if ( C4::Context->preference('hide_marc') ) {
262 $tag_data{tag} = $tagslib->{ $fields[$x_i]->tag() }->{lib};
265 my $sf_def = $tagslib->{ $fields[$x_i]->tag() };
267 $lib = $sf_def->{lib} if defined($sf_def);
269 $tag_data{tag} = $fields[$x_i]->tag() . ' '
270 . C4::Koha::display_marc_indicators($fields[$x_i])
274 my @tmp = @subfields_data;
275 $tag_data{subfield} = \@tmp;
276 push( @loop_data, \%tag_data );
277 undef @subfields_data;
280 $template->param( "tab" . $tabloop . "XX" => \@loop_data );
284 # now, build item tab !
285 # the main difference is that datas are in lines and not in columns : thus, we build the <th> first, then the values...
286 # loop through each tag
287 # warning : we may have differents number of columns in each row. Thus, we first build a hash, complete it if necessary
288 # then construct template.
289 # $record has already had all the item fields filtered above.
290 my @fields = $record->fields();
292 ; #---- stores the list of subfields used at least once, with the "meaning" of the code
293 my @item_subfield_codes;
295 foreach my $field (@fields) {
296 next if ( $field->tag() < 10 );
297 my @subf = $field->subfields;
300 # loop through each subfield
301 for my $i ( 0 .. $#subf ) {
302 my $sf_def = $tagslib->{ $field->tag() }->{ $subf[$i][0] };
303 next if ( ($sf_def->{tab}||0) != 10 );
304 next if ( ($sf_def->{hidden}||0) > 0 );
306 push @item_subfield_codes, $subf[$i][0];
307 $witness{ $subf[$i][0] } = $sf_def->{lib};
309 # Allow repeatables (BZ 13574)
310 if( $item->{$subf[$i][0]} ) {
311 $item->{$subf[$i][0]} .= ' | ';
313 $item->{$subf[$i][0]} = q{};
316 if ( $sf_def->{isurl} ) {
317 $item->{ $subf[$i][0] } .= "<a href=\"$subf[$i][1]\">$subf[$i][1]</a>";
319 elsif ( $sf_def->{kohafield} eq "biblioitems.isbn" ) {
320 $item->{ $subf[$i][0] } .= $subf[$i][1];
323 $item->{ $subf[$i][0] } .= GetAuthorisedValueDesc( $field->tag(), $subf[$i][0],
324 $subf[$i][1], '', $tagslib, '', 'opac' );
327 my $kohafield = $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{kohafield};
328 $item->{ $subf[$i][0] } = output_pref( { str => $item->{ $subf[$i][0] }, dateonly => 1 } )
329 if grep { $kohafield eq $_ }
330 qw( items.dateaccessioned items.onloan items.datelastseen items.datelastborrowed items.replacementpricedate );
333 push @item_loop, $item if $item;
335 my ( $holdingbrtagf, $holdingbrtagsubf ) =
336 &GetMarcFromKohaField( "items.holdingbranch" );
338 sort { ($a->{$holdingbrtagsubf}||'') cmp ($b->{$holdingbrtagsubf}||'') } @item_loop;
340 @item_subfield_codes = uniq @item_subfield_codes;
342 my @item_header_loop;
343 for my $subfield_code ( @item_subfield_codes ) {
344 push @item_header_loop, $witness{$subfield_code};
345 for my $item_data ( @item_loop ) {
346 $item_data->{$subfield_code} ||= " "
350 if ( C4::Context->preference("OPACISBD") ) {
351 $template->param( ISBD => 1 );
354 #Search for title in links
355 my $marcflavour = C4::Context->preference("marcflavour");
356 my $dat = TransformMarcToKoha( $record );
357 my $isbn = GetNormalizedISBN(undef,$record,$marcflavour);
358 my $marccontrolnumber = GetMarcControlnumber ($record, $marcflavour);
359 my $marcissns = GetMarcISSN( $record, $marcflavour );
360 my $issn = $marcissns->[0] || '';
362 if (my $search_for_title = C4::Context->preference('OPACSearchForTitleIn')){
363 $dat->{title} =~ s/\/+$//; # remove trailing slash
364 $dat->{title} =~ s/\s+$//; # remove trailing space
365 $search_for_title = parametrized_url(
368 TITLE => $dat->{title},
369 AUTHOR => $dat->{author},
372 CONTROLNUMBER => $marccontrolnumber,
373 BIBLIONUMBER => $biblionumber,
376 $template->param('OPACSearchForTitleIn' => $search_for_title);
379 if( C4::Context->preference('ArticleRequests') ) {
380 my $itemtype = Koha::ItemTypes->find($biblio->itemtype);
381 my $artreqpossible = $patron
382 ? $biblio->can_article_request( $patron )
384 ? $itemtype->may_article_request
386 $template->param( artreqpossible => $artreqpossible );
390 item_loop => \@item_loop,
391 item_header_loop => \@item_header_loop,
392 item_subfield_codes => \@item_subfield_codes,
394 norequests => $norequests,
397 output_html_with_http_headers $query, $cookie, $template->output;