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 ),
87 my $patron = Koha::Patrons->find( $loggedinuser );
89 if ( C4::Context->preference('OpacHiddenItemsExceptions') ) {
90 # we need to fetch the borrower info here, so we can pass the category
91 $borcat = $patron ? $patron->categorycode : $borcat;
94 my $record = GetMarcBiblio({
95 biblionumber => $biblionumber,
100 print $query->redirect("/cgi-bin/koha/errors/404.pl");
104 my $biblio = Koha::Biblios->find( $biblionumber );
105 unless ( $patron and $patron->category->override_hidden_items ) {
106 # only skip this check if there's a logged in user
107 # and its category overrides OpacHiddenItems
108 if ( $biblio->hidden_in_opac({ rules => C4::Context->yaml_preference('OpacHiddenItems') }) ) {
109 print $query->redirect('/cgi-bin/koha/errors/404.pl'); # escape early
114 my $framework = $biblio ? $biblio->frameworkcode : q{};
115 my $tagslib = &GetMarcStructure( 0, $framework );
117 my $record_processor = Koha::RecordProcessor->new({
118 filters => 'ViewPolicy',
121 frameworkcode => $framework
124 $record_processor->process($record);
126 # get biblionumbers stored in the cart
127 if(my $cart_list = $query->cookie("bib_list")){
128 my @cart_list = split(/\//, $cart_list);
129 if ( grep {$_ eq $biblionumber} @cart_list) {
130 $template->param( incart => 1 );
134 my ($bt_tag,$bt_subtag) = GetMarcFromKohaField( 'biblio.title' );
136 bibliotitle => $biblio->title,
137 ) if $tagslib->{$bt_tag}->{$bt_subtag}->{hidden} <= 0 && # <=0 OPAC visible.
138 $tagslib->{$bt_tag}->{$bt_subtag}->{hidden} > -8; # except -8;
141 my $allow_onshelf_holds;
142 my $items = $biblio->items;
144 while ( my $item = $items->next ) {
149 && ($item->notforloan < 0 || not $item->notforloan )
150 && !Koha::ItemTypes->find($item->effective_itemtype)->notforloan
151 && $item->itemnumber;
153 $allow_onshelf_holds = Koha::CirculationRules->get_onshelfholds_policy( { item => $item, patron => $patron } )
154 unless $allow_onshelf_holds;
157 if( $allow_onshelf_holds || CountItemsIssued($biblionumber) || $biblio->has_items_waiting_or_intransit ) {
158 $template->param( ReservableItems => 1 );
161 # adding the $RequestOnOpac param
163 if (C4::Context->preference("RequestOnOpac")) {
170 # loop through each tab 0 through 9
171 for ( my $tabloop = 0 ; $tabloop <= 9 ; $tabloop++ ) {
173 # loop through each tag
178 unless ( $tagslib->{'000'}->{'@'}->{tab} ne $tabloop
179 or $tagslib->{'000'}->{'@'}->{hidden} > 0 )
182 $subfield_data{marc_lib} = $tagslib->{'000'}->{'@'}->{lib};
183 $subfield_data{marc_value} = $record->leader();
184 $subfield_data{marc_subfield} = '@';
185 $subfield_data{marc_tag} = '000';
186 push( @subfields_data, \%subfield_data );
188 $tag_data{tag} = '000 -' . $tagslib->{'000'}->{lib};
189 my @tmp = @subfields_data;
190 $tag_data{subfield} = \@tmp;
191 push( @loop_data, \%tag_data );
192 undef @subfields_data;
194 my @fields = $record->fields();
195 for ( my $x_i = 0 ; $x_i <= $#fields ; $x_i++ ) {
197 # if tag <10, there's no subfield, use the "@" trick
198 if ( $fields[$x_i]->tag() < 10 ) {
201 $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{tab} ne $tabloop );
202 next if ( $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{hidden} > 0 );
204 $subfield_data{marc_lib} =
205 $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{lib};
206 $subfield_data{marc_value} = $fields[$x_i]->data();
207 $subfield_data{marc_subfield} = '@';
208 $subfield_data{marc_tag} = $fields[$x_i]->tag();
209 push( @subfields_data, \%subfield_data );
212 my @subf = $fields[$x_i]->subfields;
214 # loop through each subfield
215 for my $i ( 0 .. $#subf ) {
216 $subf[$i][0] = "@" unless defined($subf[$i][0]);
217 my $sf_def = $tagslib->{ $fields[$x_i]->tag() };
218 $sf_def = $sf_def->{ $subf[$i][0] } if defined($sf_def);
219 my ($tab,$hidden,$lib);
220 $tab = $sf_def->{tab} if defined($sf_def);
221 $tab = $tab // int($fields[$x_i]->tag()/100);
222 $hidden = $sf_def->{hidden} if defined($sf_def);
223 $hidden = $hidden // 0;
224 next if ( $tab != $tabloop );
225 next if ( $hidden > 0 );
227 $lib = $sf_def->{lib} if defined($sf_def);
229 $subfield_data{marc_lib} = ($lib eq $previous) ? '--' : $lib;
231 $subfield_data{link} = $sf_def->{link};
232 $subf[$i][1] =~ s/\n/<br\/>/g;
233 if ( $sf_def->{isurl} ) {
234 $subfield_data{marc_value} = "<a href=\"$subf[$i][1]\">$subf[$i][1]</a>";
236 elsif ( defined($sf_def->{kohafield}) && $sf_def->{kohafield} eq "biblioitems.isbn" ) {
237 $subfield_data{marc_value} = $subf[$i][1];
240 if ( $sf_def->{authtypecode} ) {
241 $subfield_data{authority} = $fields[$x_i]->subfield(9);
243 $subfield_data{marc_value} = GetAuthorisedValueDesc( $fields[$x_i]->tag(),
244 $subf[$i][0], $subf[$i][1], '', $tagslib, '', 'opac' );
246 $subfield_data{marc_subfield} = $subf[$i][0];
247 $subfield_data{marc_tag} = $fields[$x_i]->tag();
248 push( @subfields_data, \%subfield_data );
251 if ( $#subfields_data >= 0 ) {
253 if ( ( $fields[$x_i]->tag() eq $fields[ $x_i - 1 ]->tag() )
254 && ( C4::Context->preference('LabelMARCView') eq 'economical' )
260 if ( C4::Context->preference('hide_marc') ) {
261 $tag_data{tag} = $tagslib->{ $fields[$x_i]->tag() }->{lib};
264 my $sf_def = $tagslib->{ $fields[$x_i]->tag() };
266 $lib = $sf_def->{lib} if defined($sf_def);
268 $tag_data{tag} = $fields[$x_i]->tag() . ' '
269 . C4::Koha::display_marc_indicators($fields[$x_i])
273 my @tmp = @subfields_data;
274 $tag_data{subfield} = \@tmp;
275 push( @loop_data, \%tag_data );
276 undef @subfields_data;
279 $template->param( "tab" . $tabloop . "XX" => \@loop_data );
283 # now, build item tab !
284 # the main difference is that datas are in lines and not in columns : thus, we build the <th> first, then the values...
285 # loop through each tag
286 # warning : we may have differents number of columns in each row. Thus, we first build a hash, complete it if necessary
287 # then construct template.
288 # $record has already had all the item fields filtered above.
289 my @fields = $record->fields();
291 ; #---- stores the list of subfields used at least once, with the "meaning" of the code
292 my @item_subfield_codes;
294 foreach my $field (@fields) {
295 next if ( $field->tag() < 10 );
296 my @subf = $field->subfields;
299 # loop through each subfield
300 for my $i ( 0 .. $#subf ) {
301 my $sf_def = $tagslib->{ $field->tag() }->{ $subf[$i][0] };
302 next if ( ($sf_def->{tab}||0) != 10 );
303 next if ( ($sf_def->{hidden}||0) > 0 );
305 push @item_subfield_codes, $subf[$i][0];
306 $witness{ $subf[$i][0] } = $sf_def->{lib};
308 # Allow repeatables (BZ 13574)
309 if( $item->{$subf[$i][0]} ) {
310 $item->{$subf[$i][0]} .= ' | ';
312 $item->{$subf[$i][0]} = q{};
315 if ( $sf_def->{isurl} ) {
316 $item->{ $subf[$i][0] } .= "<a href=\"$subf[$i][1]\">$subf[$i][1]</a>";
318 elsif ( $sf_def->{kohafield} eq "biblioitems.isbn" ) {
319 $item->{ $subf[$i][0] } .= $subf[$i][1];
322 $item->{ $subf[$i][0] } .= GetAuthorisedValueDesc( $field->tag(), $subf[$i][0],
323 $subf[$i][1], '', $tagslib, '', 'opac' ) // q{};
326 my $kohafield = $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{kohafield};
327 $item->{ $subf[$i][0] } = output_pref( { str => $item->{ $subf[$i][0] }, dateonly => 1 } )
328 if grep { $kohafield eq $_ }
329 qw( items.dateaccessioned items.onloan items.datelastseen items.datelastborrowed items.replacementpricedate );
332 push @item_loop, $item if $item;
334 my ( $holdingbrtagf, $holdingbrtagsubf ) =
335 &GetMarcFromKohaField( "items.holdingbranch" );
337 sort { ($a->{$holdingbrtagsubf}||'') cmp ($b->{$holdingbrtagsubf}||'') } @item_loop;
339 @item_subfield_codes = uniq @item_subfield_codes;
341 my @item_header_loop;
342 for my $subfield_code ( @item_subfield_codes ) {
343 push @item_header_loop, $witness{$subfield_code};
344 for my $item_data ( @item_loop ) {
345 $item_data->{$subfield_code} ||= " "
349 if ( C4::Context->preference("OPACISBD") ) {
350 $template->param( ISBD => 1 );
353 #Search for title in links
354 my $marcflavour = C4::Context->preference("marcflavour");
355 my $dat = TransformMarcToKoha( $record );
356 my $isbn = GetNormalizedISBN(undef,$record,$marcflavour);
357 my $marccontrolnumber = GetMarcControlnumber ($record, $marcflavour);
358 my $marcissns = GetMarcISSN( $record, $marcflavour );
359 my $issn = $marcissns->[0] || '';
361 if (my $search_for_title = C4::Context->preference('OPACSearchForTitleIn')){
362 $dat->{title} =~ s/\/+$//; # remove trailing slash
363 $dat->{title} =~ s/\s+$//; # remove trailing space
364 $search_for_title = parametrized_url(
367 TITLE => $dat->{title},
368 AUTHOR => $dat->{author},
371 CONTROLNUMBER => $marccontrolnumber,
372 BIBLIONUMBER => $biblionumber,
375 $template->param('OPACSearchForTitleIn' => $search_for_title);
378 if( C4::Context->preference('ArticleRequests') ) {
379 my $itemtype = Koha::ItemTypes->find($biblio->itemtype);
380 my $artreqpossible = $patron
381 ? $biblio->can_article_request( $patron )
383 ? $itemtype->may_article_request
385 $template->param( artreqpossible => $artreqpossible );
389 item_loop => \@item_loop,
390 item_header_loop => \@item_header_loop,
391 item_subfield_codes => \@item_subfield_codes,
393 norequests => $norequests,
396 output_html_with_http_headers $query, $cookie, $template->output;