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
63 use C4::Koha qw( GetNormalizedISBN );
64 use List::MoreUtils qw( uniq );
66 use Koha::CirculationRules;
70 use Koha::RecordProcessor;
71 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);
94 my $biblio = Koha::Biblios->find($biblionumber);
95 my $record = $biblio->metadata->record;
98 print $query->redirect("/cgi-bin/koha/errors/404.pl");
102 unless ( $patron and $patron->category->override_hidden_items ) {
103 # only skip this check if there's a logged in user
104 # and its category overrides OpacHiddenItems
105 if ( $biblio->hidden_in_opac({ rules => C4::Context->yaml_preference('OpacHiddenItems') }) ) {
106 print $query->redirect('/cgi-bin/koha/errors/404.pl'); # escape early
111 my $items = $biblio->items->filter_by_visible_in_opac({ patron => $patron });
112 my $framework = $biblio ? $biblio->frameworkcode : q{};
113 my $tagslib = &GetMarcStructure( 0, $framework );
115 my $record_processor = Koha::RecordProcessor->new({
116 filters => [ 'EmbedItems', 'ViewPolicy' ],
119 frameworkcode => $framework,
120 items => [ $items->as_list ],
123 $record_processor->process($record);
125 # get biblionumbers stored in the cart
126 if(my $cart_list = $query->cookie("bib_list")){
127 my @cart_list = split(/\//, $cart_list);
128 if ( grep {$_ eq $biblionumber} @cart_list) {
129 $template->param( incart => 1 );
133 my ($bt_tag,$bt_subtag) = GetMarcFromKohaField( 'biblio.title' );
135 bibliotitle => $biblio->title,
136 ) if $tagslib->{$bt_tag}->{$bt_subtag}->{hidden} <= 0 && # <=0 OPAC visible.
137 $tagslib->{$bt_tag}->{$bt_subtag}->{hidden} > -8; # except -8;
139 my $allow_onshelf_holds;
142 while ( my $item = $items->next ) {
143 $allow_onshelf_holds = Koha::CirculationRules->get_onshelfholds_policy( { item => $item, patron => $patron } )
144 unless $allow_onshelf_holds;
147 if( $allow_onshelf_holds || CountItemsIssued($biblionumber) || $biblio->has_items_waiting_or_intransit ) {
148 $template->param( ReservableItems => 1 );
154 # loop through each tab 0 through 9
155 for ( my $tabloop = 0 ; $tabloop <= 9 ; $tabloop++ ) {
157 # loop through each tag
162 unless ( $tagslib->{'000'}->{'@'}->{tab} ne $tabloop
163 or $tagslib->{'000'}->{'@'}->{hidden} > 0 )
166 $subfield_data{marc_lib} = $tagslib->{'000'}->{'@'}->{lib};
167 $subfield_data{marc_value} = $record->leader();
168 $subfield_data{marc_subfield} = '@';
169 $subfield_data{marc_tag} = '000';
170 push( @subfields_data, \%subfield_data );
172 $tag_data{tag} = '000 -' . $tagslib->{'000'}->{lib};
173 my @tmp = @subfields_data;
174 $tag_data{subfield} = \@tmp;
175 push( @loop_data, \%tag_data );
176 undef @subfields_data;
178 my @fields = $record->fields();
179 for ( my $x_i = 0 ; $x_i <= $#fields ; $x_i++ ) {
181 # if tag <10, there's no subfield, use the "@" trick
182 if ( $fields[$x_i]->tag() < 10 ) {
185 $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{tab} ne $tabloop );
186 next if ( $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{hidden} > 0 );
188 $subfield_data{marc_lib} =
189 $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{lib};
190 $subfield_data{marc_value} = $fields[$x_i]->data();
191 $subfield_data{marc_subfield} = '@';
192 $subfield_data{marc_tag} = $fields[$x_i]->tag();
193 push( @subfields_data, \%subfield_data );
196 my @subf = $fields[$x_i]->subfields;
198 # loop through each subfield
199 for my $i ( 0 .. $#subf ) {
200 $subf[$i][0] = "@" unless defined($subf[$i][0]);
201 my $sf_def = $tagslib->{ $fields[$x_i]->tag() };
202 $sf_def = $sf_def->{ $subf[$i][0] } if defined($sf_def);
203 my ($tab,$hidden,$lib);
204 $tab = $sf_def->{tab} if defined($sf_def);
205 $tab = $tab // int($fields[$x_i]->tag()/100);
206 $hidden = $sf_def->{hidden} if defined($sf_def);
207 $hidden = $hidden // 0;
208 next if ( $tab != $tabloop );
209 next if ( $hidden > 0 );
211 $lib = $sf_def->{lib} if defined($sf_def);
213 $subfield_data{marc_lib} = ($lib eq $previous) ? '--' : $lib;
215 $subfield_data{link} = $sf_def->{link};
216 $subf[$i][1] =~ s/\n/<br\/>/g;
217 if ( $sf_def->{isurl} ) {
218 $subfield_data{marc_value} = "<a href=\"$subf[$i][1]\">$subf[$i][1]</a>";
220 elsif ( defined($sf_def->{kohafield}) && $sf_def->{kohafield} eq "biblioitems.isbn" ) {
221 $subfield_data{marc_value} = $subf[$i][1];
224 if ( $sf_def->{authtypecode} ) {
225 $subfield_data{authority} = $fields[$x_i]->subfield(9);
227 $subfield_data{marc_value} = GetAuthorisedValueDesc( $fields[$x_i]->tag(),
228 $subf[$i][0], $subf[$i][1], '', $tagslib, '', 'opac' );
230 $subfield_data{marc_subfield} = $subf[$i][0];
231 $subfield_data{marc_tag} = $fields[$x_i]->tag();
232 push( @subfields_data, \%subfield_data );
235 if ( $#subfields_data >= 0 ) {
237 if ( ( $fields[$x_i]->tag() eq $fields[ $x_i - 1 ]->tag() )
238 && ( C4::Context->preference('LabelMARCView') eq 'economical' )
244 if ( C4::Context->preference('hide_marc') ) {
245 $tag_data{tag} = $tagslib->{ $fields[$x_i]->tag() }->{lib};
248 my $sf_def = $tagslib->{ $fields[$x_i]->tag() };
250 $lib = $sf_def->{lib} if defined($sf_def);
252 $tag_data{tag} = $fields[$x_i]->tag() . ' '
253 . C4::Koha::display_marc_indicators($fields[$x_i])
257 my @tmp = @subfields_data;
258 $tag_data{subfield} = \@tmp;
259 push( @loop_data, \%tag_data );
260 undef @subfields_data;
263 $template->param( "tab" . $tabloop . "XX" => \@loop_data );
267 # now, build item tab !
268 # the main difference is that datas are in lines and not in columns : thus, we build the <th> first, then the values...
269 # loop through each tag
270 # warning : we may have differents number of columns in each row. Thus, we first build a hash, complete it if necessary
271 # then construct template.
272 # $record has already had all the item fields filtered above.
273 my @fields = $record->fields();
275 ; #---- stores the list of subfields used at least once, with the "meaning" of the code
276 my @item_subfield_codes;
278 foreach my $field (@fields) {
279 next if ( $field->tag() < 10 );
280 my @subf = $field->subfields;
283 # loop through each subfield
284 for my $i ( 0 .. $#subf ) {
285 my $sf_def = $tagslib->{ $field->tag() }->{ $subf[$i][0] };
286 next if ( ($sf_def->{tab}||0) != 10 );
287 next if ( ($sf_def->{hidden}||0) > 0 );
289 push @item_subfield_codes, $subf[$i][0];
290 $witness{ $subf[$i][0] } = $sf_def->{lib};
292 # Allow repeatables (BZ 13574)
293 if( $item->{$subf[$i][0]} ) {
294 $item->{$subf[$i][0]} .= ' | ';
296 $item->{$subf[$i][0]} = q{};
299 if ( $sf_def->{isurl} ) {
300 $item->{ $subf[$i][0] } .= "<a href=\"$subf[$i][1]\">$subf[$i][1]</a>";
302 elsif ( $sf_def->{kohafield} eq "biblioitems.isbn" ) {
303 $item->{ $subf[$i][0] } .= $subf[$i][1];
306 $item->{ $subf[$i][0] } .= GetAuthorisedValueDesc( $field->tag(), $subf[$i][0],
307 $subf[$i][1], '', $tagslib, '', 'opac' ) // q{};
310 my $kohafield = $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{kohafield};
311 $item->{ $subf[$i][0] } = output_pref( { str => $item->{ $subf[$i][0] }, dateonly => 1 } )
312 if grep { $kohafield eq $_ }
313 qw( items.dateaccessioned items.onloan items.datelastseen items.datelastborrowed items.replacementpricedate );
316 push @item_loop, $item if $item;
318 my ( $holdingbrtagf, $holdingbrtagsubf ) =
319 &GetMarcFromKohaField( "items.holdingbranch" );
321 sort { ($a->{$holdingbrtagsubf}||'') cmp ($b->{$holdingbrtagsubf}||'') } @item_loop;
323 @item_subfield_codes = uniq @item_subfield_codes;
325 my @item_header_loop;
326 for my $subfield_code ( @item_subfield_codes ) {
327 push @item_header_loop, $witness{$subfield_code};
328 for my $item_data ( @item_loop ) {
329 $item_data->{$subfield_code} ||= " "
333 if ( C4::Context->preference("OPACISBD") ) {
334 $template->param( ISBD => 1 );
337 #Search for title in links
338 my $marcflavour = C4::Context->preference("marcflavour");
339 my $dat = TransformMarcToKoha({ record => $record });
340 my $isbn = GetNormalizedISBN(undef,$record,$marcflavour);
341 my $marccontrolnumber = GetMarcControlnumber ($record, $marcflavour);
342 my $marcissns = GetMarcISSN( $record, $marcflavour );
343 my $issn = $marcissns->[0] || '';
345 if (my $search_for_title = C4::Context->preference('OPACSearchForTitleIn')){
346 $dat->{title} =~ s/\/+$//; # remove trailing slash
347 $dat->{title} =~ s/\s+$//; # remove trailing space
348 my $oclc_no = Koha::Util::MARC::oclc_number( $record );
349 $search_for_title = parametrized_url(
352 TITLE => $dat->{title},
353 AUTHOR => $dat->{author},
356 CONTROLNUMBER => $marccontrolnumber,
357 BIBLIONUMBER => $biblionumber,
361 $template->param('OPACSearchForTitleIn' => $search_for_title);
364 if( C4::Context->preference('ArticleRequests') ) {
365 my $itemtype = Koha::ItemTypes->find($biblio->itemtype);
366 my $artreqpossible = $patron
367 ? $biblio->can_article_request( $patron )
369 ? $itemtype->may_article_request
371 $template->param( artreqpossible => $artreqpossible );
374 my $norequests = ! $biblio->items->filter_by_for_hold->count;
376 item_loop => \@item_loop,
377 item_header_loop => \@item_header_loop,
378 item_subfield_codes => \@item_subfield_codes,
380 norequests => $norequests,
383 output_html_with_http_headers $query, $cookie, $template->output;