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;
141 my $allow_onshelf_holds;
142 my $items = $biblio->items;
143 while ( my $item = $items->next ) {
144 $allow_onshelf_holds = Koha::CirculationRules->get_onshelfholds_policy( { item => $item, patron => $patron } );
145 last if $allow_onshelf_holds;
148 if( $allow_onshelf_holds || CountItemsIssued($biblionumber) || $biblio->has_items_waiting_or_intransit ) {
149 $template->param( ReservableItems => 1 );
152 # adding the $RequestOnOpac param
154 if (C4::Context->preference("RequestOnOpac")) {
161 # loop through each tab 0 through 9
162 for ( my $tabloop = 0 ; $tabloop <= 9 ; $tabloop++ ) {
164 # loop through each tag
169 unless ( $tagslib->{'000'}->{'@'}->{tab} ne $tabloop
170 or $tagslib->{'000'}->{'@'}->{hidden} > 0 )
173 $subfield_data{marc_lib} = $tagslib->{'000'}->{'@'}->{lib};
174 $subfield_data{marc_value} = $record->leader();
175 $subfield_data{marc_subfield} = '@';
176 $subfield_data{marc_tag} = '000';
177 push( @subfields_data, \%subfield_data );
179 $tag_data{tag} = '000 -' . $tagslib->{'000'}->{lib};
180 my @tmp = @subfields_data;
181 $tag_data{subfield} = \@tmp;
182 push( @loop_data, \%tag_data );
183 undef @subfields_data;
185 my @fields = $record->fields();
186 for ( my $x_i = 0 ; $x_i <= $#fields ; $x_i++ ) {
188 # if tag <10, there's no subfield, use the "@" trick
189 if ( $fields[$x_i]->tag() < 10 ) {
192 $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{tab} ne $tabloop );
193 next if ( $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{hidden} > 0 );
195 $subfield_data{marc_lib} =
196 $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{lib};
197 $subfield_data{marc_value} = $fields[$x_i]->data();
198 $subfield_data{marc_subfield} = '@';
199 $subfield_data{marc_tag} = $fields[$x_i]->tag();
200 push( @subfields_data, \%subfield_data );
203 my @subf = $fields[$x_i]->subfields;
205 # loop through each subfield
206 for my $i ( 0 .. $#subf ) {
207 $subf[$i][0] = "@" unless defined($subf[$i][0]);
208 my $sf_def = $tagslib->{ $fields[$x_i]->tag() };
209 $sf_def = $sf_def->{ $subf[$i][0] } if defined($sf_def);
210 my ($tab,$hidden,$lib);
211 $tab = $sf_def->{tab} if defined($sf_def);
212 $tab = $tab // int($fields[$x_i]->tag()/100);
213 $hidden = $sf_def->{hidden} if defined($sf_def);
214 $hidden = $hidden // 0;
215 next if ( $tab != $tabloop );
216 next if ( $hidden > 0 );
218 $lib = $sf_def->{lib} if defined($sf_def);
220 $subfield_data{marc_lib} = ($lib eq $previous) ? '--' : $lib;
222 $subfield_data{link} = $sf_def->{link};
223 $subf[$i][1] =~ s/\n/<br\/>/g;
224 if ( $sf_def->{isurl} ) {
225 $subfield_data{marc_value} = "<a href=\"$subf[$i][1]\">$subf[$i][1]</a>";
227 elsif ( defined($sf_def->{kohafield}) && $sf_def->{kohafield} eq "biblioitems.isbn" ) {
228 $subfield_data{marc_value} = $subf[$i][1];
231 if ( $sf_def->{authtypecode} ) {
232 $subfield_data{authority} = $fields[$x_i]->subfield(9);
234 $subfield_data{marc_value} = GetAuthorisedValueDesc( $fields[$x_i]->tag(),
235 $subf[$i][0], $subf[$i][1], '', $tagslib, '', 'opac' );
237 $subfield_data{marc_subfield} = $subf[$i][0];
238 $subfield_data{marc_tag} = $fields[$x_i]->tag();
239 push( @subfields_data, \%subfield_data );
242 if ( $#subfields_data >= 0 ) {
244 if ( ( $fields[$x_i]->tag() eq $fields[ $x_i - 1 ]->tag() )
245 && ( C4::Context->preference('LabelMARCView') eq 'economical' )
251 if ( C4::Context->preference('hide_marc') ) {
252 $tag_data{tag} = $tagslib->{ $fields[$x_i]->tag() }->{lib};
255 my $sf_def = $tagslib->{ $fields[$x_i]->tag() };
257 $lib = $sf_def->{lib} if defined($sf_def);
259 $tag_data{tag} = $fields[$x_i]->tag() . ' '
260 . C4::Koha::display_marc_indicators($fields[$x_i])
264 my @tmp = @subfields_data;
265 $tag_data{subfield} = \@tmp;
266 push( @loop_data, \%tag_data );
267 undef @subfields_data;
270 $template->param( "tab" . $tabloop . "XX" => \@loop_data );
274 # now, build item tab !
275 # the main difference is that datas are in lines and not in columns : thus, we build the <th> first, then the values...
276 # loop through each tag
277 # warning : we may have differents number of columns in each row. Thus, we first build a hash, complete it if necessary
278 # then construct template.
279 # $record has already had all the item fields filtered above.
280 my @fields = $record->fields();
282 ; #---- stores the list of subfields used at least once, with the "meaning" of the code
283 my @item_subfield_codes;
285 foreach my $field (@fields) {
286 next if ( $field->tag() < 10 );
287 my @subf = $field->subfields;
290 # loop through each subfield
291 for my $i ( 0 .. $#subf ) {
292 my $sf_def = $tagslib->{ $field->tag() }->{ $subf[$i][0] };
293 next if ( ($sf_def->{tab}||0) != 10 );
294 next if ( ($sf_def->{hidden}||0) > 0 );
296 push @item_subfield_codes, $subf[$i][0];
297 $witness{ $subf[$i][0] } = $sf_def->{lib};
299 # Allow repeatables (BZ 13574)
300 if( $item->{$subf[$i][0]} ) {
301 $item->{$subf[$i][0]} .= ' | ';
303 $item->{$subf[$i][0]} = q{};
306 if ( $sf_def->{isurl} ) {
307 $item->{ $subf[$i][0] } .= "<a href=\"$subf[$i][1]\">$subf[$i][1]</a>";
309 elsif ( $sf_def->{kohafield} eq "biblioitems.isbn" ) {
310 $item->{ $subf[$i][0] } .= $subf[$i][1];
313 $item->{ $subf[$i][0] } .= GetAuthorisedValueDesc( $field->tag(), $subf[$i][0],
314 $subf[$i][1], '', $tagslib, '', 'opac' );
317 my $kohafield = $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{kohafield};
318 $item->{ $subf[$i][0] } = output_pref( { str => $item->{ $subf[$i][0] }, dateonly => 1 } )
319 if grep { $kohafield eq $_ }
320 qw( items.dateaccessioned items.onloan items.datelastseen items.datelastborrowed items.replacementpricedate );
323 push @item_loop, $item if $item;
325 my ( $holdingbrtagf, $holdingbrtagsubf ) =
326 &GetMarcFromKohaField( "items.holdingbranch" );
328 sort { ($a->{$holdingbrtagsubf}||'') cmp ($b->{$holdingbrtagsubf}||'') } @item_loop;
330 @item_subfield_codes = uniq @item_subfield_codes;
332 my @item_header_loop;
333 for my $subfield_code ( @item_subfield_codes ) {
334 push @item_header_loop, $witness{$subfield_code};
335 for my $item_data ( @item_loop ) {
336 $item_data->{$subfield_code} ||= " "
340 if ( C4::Context->preference("OPACISBD") ) {
341 $template->param( ISBD => 1 );
344 #Search for title in links
345 my $marcflavour = C4::Context->preference("marcflavour");
346 my $dat = TransformMarcToKoha( $record );
347 my $isbn = GetNormalizedISBN(undef,$record,$marcflavour);
348 my $marccontrolnumber = GetMarcControlnumber ($record, $marcflavour);
349 my $marcissns = GetMarcISSN( $record, $marcflavour );
350 my $issn = $marcissns->[0] || '';
352 if (my $search_for_title = C4::Context->preference('OPACSearchForTitleIn')){
353 $dat->{title} =~ s/\/+$//; # remove trailing slash
354 $dat->{title} =~ s/\s+$//; # remove trailing space
355 $search_for_title = parametrized_url(
358 TITLE => $dat->{title},
359 AUTHOR => $dat->{author},
362 CONTROLNUMBER => $marccontrolnumber,
363 BIBLIONUMBER => $biblionumber,
366 $template->param('OPACSearchForTitleIn' => $search_for_title);
369 if( C4::Context->preference('ArticleRequests') ) {
370 my $itemtype = Koha::ItemTypes->find($biblio->itemtype);
371 my $artreqpossible = $patron
372 ? $biblio->can_article_request( $patron )
374 ? $itemtype->may_article_request
376 $template->param( artreqpossible => $artreqpossible );
380 item_loop => \@item_loop,
381 item_header_loop => \@item_header_loop,
382 item_subfield_codes => \@item_subfield_codes,
386 output_html_with_http_headers $query, $cookie, $template->output;