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
48 use C4::Auth qw( get_template_and_user );
50 use C4::Output qw( parametrized_url output_html_with_http_headers );
54 GetAuthorisedValueDesc
64 use C4::Koha qw( GetNormalizedISBN );
65 use List::MoreUtils qw( uniq );
67 use Koha::CirculationRules;
71 use Koha::RecordProcessor;
72 use Koha::DateUtils qw( output_pref );
74 my $query = CGI->new();
76 my $biblionumber = $query->param('biblionumber');
77 if ( ! $biblionumber ) {
78 print $query->redirect("/cgi-bin/koha/errors/404.pl");
81 $biblionumber = int($biblionumber);
84 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
86 template_name => "opac-MARCdetail.tt",
89 authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ),
93 my $patron = Koha::Patrons->find( $loggedinuser );
95 if ( C4::Context->preference('OpacHiddenItemsExceptions') ) {
96 # we need to fetch the borrower info here, so we can pass the category
97 $borcat = $patron ? $patron->categorycode : $borcat;
100 my $record = GetMarcBiblio({
101 biblionumber => $biblionumber,
104 borcat => $borcat });
106 print $query->redirect("/cgi-bin/koha/errors/404.pl");
110 my $biblio = Koha::Biblios->find( $biblionumber );
111 unless ( $patron and $patron->category->override_hidden_items ) {
112 # only skip this check if there's a logged in user
113 # and its category overrides OpacHiddenItems
114 if ( $biblio->hidden_in_opac({ rules => C4::Context->yaml_preference('OpacHiddenItems') }) ) {
115 print $query->redirect('/cgi-bin/koha/errors/404.pl'); # escape early
120 my $framework = $biblio ? $biblio->frameworkcode : q{};
121 my $tagslib = &GetMarcStructure( 0, $framework );
123 my $record_processor = Koha::RecordProcessor->new({
124 filters => 'ViewPolicy',
127 frameworkcode => $framework
130 $record_processor->process($record);
132 # get biblionumbers stored in the cart
133 if(my $cart_list = $query->cookie("bib_list")){
134 my @cart_list = split(/\//, $cart_list);
135 if ( grep {$_ eq $biblionumber} @cart_list) {
136 $template->param( incart => 1 );
140 my ($bt_tag,$bt_subtag) = GetMarcFromKohaField( 'biblio.title' );
142 bibliotitle => $biblio->title,
143 ) if $tagslib->{$bt_tag}->{$bt_subtag}->{hidden} <= 0 && # <=0 OPAC visible.
144 $tagslib->{$bt_tag}->{$bt_subtag}->{hidden} > -8; # except -8;
146 my $allow_onshelf_holds;
147 my $items = $biblio->items;
149 while ( my $item = $items->next ) {
150 $allow_onshelf_holds = Koha::CirculationRules->get_onshelfholds_policy( { item => $item, patron => $patron } )
151 unless $allow_onshelf_holds;
154 if( $allow_onshelf_holds || CountItemsIssued($biblionumber) || $biblio->has_items_waiting_or_intransit ) {
155 $template->param( ReservableItems => 1 );
158 # adding the $RequestOnOpac param
160 if (C4::Context->preference("RequestOnOpac")) {
167 # loop through each tab 0 through 9
168 for ( my $tabloop = 0 ; $tabloop <= 9 ; $tabloop++ ) {
170 # loop through each tag
175 unless ( $tagslib->{'000'}->{'@'}->{tab} ne $tabloop
176 or $tagslib->{'000'}->{'@'}->{hidden} > 0 )
179 $subfield_data{marc_lib} = $tagslib->{'000'}->{'@'}->{lib};
180 $subfield_data{marc_value} = $record->leader();
181 $subfield_data{marc_subfield} = '@';
182 $subfield_data{marc_tag} = '000';
183 push( @subfields_data, \%subfield_data );
185 $tag_data{tag} = '000 -' . $tagslib->{'000'}->{lib};
186 my @tmp = @subfields_data;
187 $tag_data{subfield} = \@tmp;
188 push( @loop_data, \%tag_data );
189 undef @subfields_data;
191 my @fields = $record->fields();
192 for ( my $x_i = 0 ; $x_i <= $#fields ; $x_i++ ) {
194 # if tag <10, there's no subfield, use the "@" trick
195 if ( $fields[$x_i]->tag() < 10 ) {
198 $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{tab} ne $tabloop );
199 next if ( $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{hidden} > 0 );
201 $subfield_data{marc_lib} =
202 $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{lib};
203 $subfield_data{marc_value} = $fields[$x_i]->data();
204 $subfield_data{marc_subfield} = '@';
205 $subfield_data{marc_tag} = $fields[$x_i]->tag();
206 push( @subfields_data, \%subfield_data );
209 my @subf = $fields[$x_i]->subfields;
211 # loop through each subfield
212 for my $i ( 0 .. $#subf ) {
213 $subf[$i][0] = "@" unless defined($subf[$i][0]);
214 my $sf_def = $tagslib->{ $fields[$x_i]->tag() };
215 $sf_def = $sf_def->{ $subf[$i][0] } if defined($sf_def);
216 my ($tab,$hidden,$lib);
217 $tab = $sf_def->{tab} if defined($sf_def);
218 $tab = $tab // int($fields[$x_i]->tag()/100);
219 $hidden = $sf_def->{hidden} if defined($sf_def);
220 $hidden = $hidden // 0;
221 next if ( $tab != $tabloop );
222 next if ( $hidden > 0 );
224 $lib = $sf_def->{lib} if defined($sf_def);
226 $subfield_data{marc_lib} = ($lib eq $previous) ? '--' : $lib;
228 $subfield_data{link} = $sf_def->{link};
229 $subf[$i][1] =~ s/\n/<br\/>/g;
230 if ( $sf_def->{isurl} ) {
231 $subfield_data{marc_value} = "<a href=\"$subf[$i][1]\">$subf[$i][1]</a>";
233 elsif ( defined($sf_def->{kohafield}) && $sf_def->{kohafield} eq "biblioitems.isbn" ) {
234 $subfield_data{marc_value} = $subf[$i][1];
237 if ( $sf_def->{authtypecode} ) {
238 $subfield_data{authority} = $fields[$x_i]->subfield(9);
240 $subfield_data{marc_value} = GetAuthorisedValueDesc( $fields[$x_i]->tag(),
241 $subf[$i][0], $subf[$i][1], '', $tagslib, '', 'opac' );
243 $subfield_data{marc_subfield} = $subf[$i][0];
244 $subfield_data{marc_tag} = $fields[$x_i]->tag();
245 push( @subfields_data, \%subfield_data );
248 if ( $#subfields_data >= 0 ) {
250 if ( ( $fields[$x_i]->tag() eq $fields[ $x_i - 1 ]->tag() )
251 && ( C4::Context->preference('LabelMARCView') eq 'economical' )
257 if ( C4::Context->preference('hide_marc') ) {
258 $tag_data{tag} = $tagslib->{ $fields[$x_i]->tag() }->{lib};
261 my $sf_def = $tagslib->{ $fields[$x_i]->tag() };
263 $lib = $sf_def->{lib} if defined($sf_def);
265 $tag_data{tag} = $fields[$x_i]->tag() . ' '
266 . C4::Koha::display_marc_indicators($fields[$x_i])
270 my @tmp = @subfields_data;
271 $tag_data{subfield} = \@tmp;
272 push( @loop_data, \%tag_data );
273 undef @subfields_data;
276 $template->param( "tab" . $tabloop . "XX" => \@loop_data );
280 # now, build item tab !
281 # the main difference is that datas are in lines and not in columns : thus, we build the <th> first, then the values...
282 # loop through each tag
283 # warning : we may have differents number of columns in each row. Thus, we first build a hash, complete it if necessary
284 # then construct template.
285 # $record has already had all the item fields filtered above.
286 my @fields = $record->fields();
288 ; #---- stores the list of subfields used at least once, with the "meaning" of the code
289 my @item_subfield_codes;
291 foreach my $field (@fields) {
292 next if ( $field->tag() < 10 );
293 my @subf = $field->subfields;
296 # loop through each subfield
297 for my $i ( 0 .. $#subf ) {
298 my $sf_def = $tagslib->{ $field->tag() }->{ $subf[$i][0] };
299 next if ( ($sf_def->{tab}||0) != 10 );
300 next if ( ($sf_def->{hidden}||0) > 0 );
302 push @item_subfield_codes, $subf[$i][0];
303 $witness{ $subf[$i][0] } = $sf_def->{lib};
305 # Allow repeatables (BZ 13574)
306 if( $item->{$subf[$i][0]} ) {
307 $item->{$subf[$i][0]} .= ' | ';
309 $item->{$subf[$i][0]} = q{};
312 if ( $sf_def->{isurl} ) {
313 $item->{ $subf[$i][0] } .= "<a href=\"$subf[$i][1]\">$subf[$i][1]</a>";
315 elsif ( $sf_def->{kohafield} eq "biblioitems.isbn" ) {
316 $item->{ $subf[$i][0] } .= $subf[$i][1];
319 $item->{ $subf[$i][0] } .= GetAuthorisedValueDesc( $field->tag(), $subf[$i][0],
320 $subf[$i][1], '', $tagslib, '', 'opac' ) // q{};
323 my $kohafield = $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{kohafield};
324 $item->{ $subf[$i][0] } = output_pref( { str => $item->{ $subf[$i][0] }, dateonly => 1 } )
325 if grep { $kohafield eq $_ }
326 qw( items.dateaccessioned items.onloan items.datelastseen items.datelastborrowed items.replacementpricedate );
329 push @item_loop, $item if $item;
331 my ( $holdingbrtagf, $holdingbrtagsubf ) =
332 &GetMarcFromKohaField( "items.holdingbranch" );
334 sort { ($a->{$holdingbrtagsubf}||'') cmp ($b->{$holdingbrtagsubf}||'') } @item_loop;
336 @item_subfield_codes = uniq @item_subfield_codes;
338 my @item_header_loop;
339 for my $subfield_code ( @item_subfield_codes ) {
340 push @item_header_loop, $witness{$subfield_code};
341 for my $item_data ( @item_loop ) {
342 $item_data->{$subfield_code} ||= " "
346 if ( C4::Context->preference("OPACISBD") ) {
347 $template->param( ISBD => 1 );
350 #Search for title in links
351 my $marcflavour = C4::Context->preference("marcflavour");
352 my $dat = TransformMarcToKoha( $record );
353 my $isbn = GetNormalizedISBN(undef,$record,$marcflavour);
354 my $marccontrolnumber = GetMarcControlnumber ($record, $marcflavour);
355 my $marcissns = GetMarcISSN( $record, $marcflavour );
356 my $issn = $marcissns->[0] || '';
358 if (my $search_for_title = C4::Context->preference('OPACSearchForTitleIn')){
359 $dat->{title} =~ s/\/+$//; # remove trailing slash
360 $dat->{title} =~ s/\s+$//; # remove trailing space
361 $search_for_title = parametrized_url(
364 TITLE => $dat->{title},
365 AUTHOR => $dat->{author},
368 CONTROLNUMBER => $marccontrolnumber,
369 BIBLIONUMBER => $biblionumber,
372 $template->param('OPACSearchForTitleIn' => $search_for_title);
375 if( C4::Context->preference('ArticleRequests') ) {
376 my $itemtype = Koha::ItemTypes->find($biblio->itemtype);
377 my $artreqpossible = $patron
378 ? $biblio->can_article_request( $patron )
380 ? $itemtype->may_article_request
382 $template->param( artreqpossible => $artreqpossible );
385 my $norequests = ! $biblio->items->filter_by_for_hold->count;
387 item_loop => \@item_loop,
388 item_header_loop => \@item_header_loop,
389 item_subfield_codes => \@item_subfield_codes,
391 norequests => $norequests,
394 output_html_with_http_headers $query, $cookie, $template->output;