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 );
62 use Koha::RecordProcessor;
66 my $dbh = C4::Context->dbh;
68 my $biblionumber = $query->param('biblionumber');
69 if ( ! $biblionumber ) {
70 print $query->redirect("/cgi-bin/koha/errors/404.pl");
74 my $record = GetMarcBiblio({
75 biblionumber => $biblionumber,
78 print $query->redirect("/cgi-bin/koha/errors/404.pl");
82 my @all_items = GetItemsInfo($biblionumber);
84 if (scalar @all_items >= 1) {
85 push @items2hide, GetHiddenItemnumbers(@all_items);
87 if (scalar @items2hide == scalar @all_items ) {
88 print $query->redirect("/cgi-bin/koha/errors/404.pl");
93 my $framework = &GetFrameworkCode( $biblionumber );
94 my $tagslib = &GetMarcStructure( 0, $framework );
95 my ($tag_itemnumber,$subtag_itemnumber) = &GetMarcFromKohaField('items.itemnumber',$framework);
96 my $biblio = Koha::Biblios->find( $biblionumber );
98 my $record_processor = Koha::RecordProcessor->new({
99 filters => 'ViewPolicy',
102 frameworkcode => $framework
105 $record_processor->process($record);
108 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
110 template_name => "opac-MARCdetail.tt",
113 authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ),
118 my ($bt_tag,$bt_subtag) = GetMarcFromKohaField('biblio.title',$framework);
120 bibliotitle => $biblio->title,
121 ) if $tagslib->{$bt_tag}->{$bt_subtag}->{hidden} <= 0 && # <=0 OPAC visible.
122 $tagslib->{$bt_tag}->{$bt_subtag}->{hidden} > -8; # except -8;
124 # get biblionumbers stored in the cart
125 if(my $cart_list = $query->cookie("bib_list")){
126 my @cart_list = split(/\//, $cart_list);
127 if ( grep {$_ eq $biblionumber} @cart_list) {
128 $template->param( incart => 1 );
132 my $allow_onshelf_holds;
133 my $patron = Koha::Patrons->find( $loggedinuser );
134 for my $itm (@all_items) {
135 $allow_onshelf_holds = C4::Reserves::OnShelfHoldsAllowed( $itm, ( $patron ? $patron->unblessed : {} ) );
136 last if $allow_onshelf_holds;
139 if( $allow_onshelf_holds || CountItemsIssued($biblionumber) || $biblio->has_items_waiting_or_intransit ) {
140 $template->param( ReservableItems => 1 );
143 # adding the $RequestOnOpac param
145 if (C4::Context->preference("RequestOnOpac")) {
153 # loop through each tab 0 through 9
154 for ( my $tabloop = 0 ; $tabloop <= 9 ; $tabloop++ ) {
156 # loop through each tag
161 unless ( $tagslib->{'000'}->{'@'}->{tab} ne $tabloop
162 or $tagslib->{'000'}->{'@'}->{hidden} > 0 )
165 $subfield_data{marc_lib} = $tagslib->{'000'}->{'@'}->{lib};
166 $subfield_data{marc_value} = $record->leader();
167 $subfield_data{marc_subfield} = '@';
168 $subfield_data{marc_tag} = '000';
169 push( @subfields_data, \%subfield_data );
171 $tag_data{tag} = '000 -' . $tagslib->{'000'}->{lib};
172 my @tmp = @subfields_data;
173 $tag_data{subfield} = \@tmp;
174 push( @loop_data, \%tag_data );
175 undef @subfields_data;
177 my @fields = $record->fields();
178 for ( my $x_i = 0 ; $x_i <= $#fields ; $x_i++ ) {
180 # if tag <10, there's no subfield, use the "@" trick
181 if ( $fields[$x_i]->tag() < 10 ) {
184 $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{tab} ne $tabloop );
185 next if ( $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{hidden} > 0 );
187 $subfield_data{marc_lib} =
188 $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{lib};
189 $subfield_data{marc_value} = $fields[$x_i]->data();
190 $subfield_data{marc_subfield} = '@';
191 $subfield_data{marc_tag} = $fields[$x_i]->tag();
192 push( @subfields_data, \%subfield_data );
195 my @subf = $fields[$x_i]->subfields;
197 # loop through each subfield
198 for my $i ( 0 .. $#subf ) {
199 $subf[$i][0] = "@" unless defined($subf[$i][0]);
200 my $sf_def = $tagslib->{ $fields[$x_i]->tag() };
201 $sf_def = $sf_def->{ $subf[$i][0] } if defined($sf_def);
202 my ($tab,$hidden,$lib);
203 $tab = $sf_def->{tab} if defined($sf_def);
204 $tab = $tab // int($fields[$x_i]->tag()/100);
205 $hidden = $sf_def->{hidden} if defined($sf_def);
206 $hidden = $hidden // 0;
207 next if ( $tab != $tabloop );
208 next if ( $hidden > 0 );
210 $lib = $sf_def->{lib} if defined($sf_def);
212 $subfield_data{marc_lib} = ($lib eq $previous) ? '--' : $lib;
214 $subfield_data{link} = $sf_def->{link};
215 $subf[$i][1] =~ s/\n/<br\/>/g;
216 if ( $sf_def->{isurl} ) {
217 $subfield_data{marc_value} = "<a href=\"$subf[$i][1]\">$subf[$i][1]</a>";
219 elsif ( defined($sf_def->{kohafield}) && $sf_def->{kohafield} eq "biblioitems.isbn" ) {
220 $subfield_data{marc_value} = $subf[$i][1];
223 if ( $sf_def->{authtypecode} ) {
224 $subfield_data{authority} = $fields[$x_i]->subfield(9);
226 $subfield_data{marc_value} = GetAuthorisedValueDesc( $fields[$x_i]->tag(),
227 $subf[$i][0], $subf[$i][1], '', $tagslib, '', 'opac' );
229 $subfield_data{marc_subfield} = $subf[$i][0];
230 $subfield_data{marc_tag} = $fields[$x_i]->tag();
231 push( @subfields_data, \%subfield_data );
234 if ( $#subfields_data >= 0 ) {
236 if ( ( $fields[$x_i]->tag() eq $fields[ $x_i - 1 ]->tag() )
237 && ( C4::Context->preference('LabelMARCView') eq 'economical' )
243 if ( C4::Context->preference('hide_marc') ) {
244 $tag_data{tag} = $tagslib->{ $fields[$x_i]->tag() }->{lib};
247 my $sf_def = $tagslib->{ $fields[$x_i]->tag() };
249 $lib = $sf_def->{lib} if defined($sf_def);
251 $tag_data{tag} = $fields[$x_i]->tag() . ' '
252 . C4::Koha::display_marc_indicators($fields[$x_i])
256 my @tmp = @subfields_data;
257 $tag_data{subfield} = \@tmp;
258 push( @loop_data, \%tag_data );
259 undef @subfields_data;
262 $template->param( "tab" . $tabloop . "XX" => \@loop_data );
266 # now, build item tab !
267 # the main difference is that datas are in lines and not in columns : thus, we build the <th> first, then the values...
268 # loop through each tag
269 # warning : we may have differents number of columns in each row. Thus, we first build a hash, complete it if necessary
270 # then construct template.
271 my @fields = $record->fields();
273 ; #---- stores the list of subfields used at least once, with the "meaning" of the code
274 my @item_subfield_codes;
276 foreach my $field (@fields) {
277 next if ( $field->tag() < 10 );
278 next if ( ( $field->tag() eq $tag_itemnumber ) &&
279 ( any { $field->subfield($subtag_itemnumber) eq $_ }
281 my @subf = $field->subfields;
284 # loop through each subfield
285 for my $i ( 0 .. $#subf ) {
286 my $sf_def = $tagslib->{ $field->tag() }->{ $subf[$i][0] };
287 next if ( ($sf_def->{tab}||0) != 10 );
288 next if ( ($sf_def->{hidden}||0) > 0 );
289 push @item_subfield_codes, $subf[$i][0];
290 $witness{ $subf[$i][0] } = $sf_def->{lib};
292 if ( $sf_def->{isurl} ) {
293 $item->{ $subf[$i][0] } = "<a href=\"$subf[$i][1]\">$subf[$i][1]</a>";
295 elsif ( $sf_def->{kohafield} eq "biblioitems.isbn" ) {
296 $item->{ $subf[$i][0] } = $subf[$i][1];
299 $item->{ $subf[$i][0] } = GetAuthorisedValueDesc( $field->tag(), $subf[$i][0],
300 $subf[$i][1], '', $tagslib, '', 'opac' );
303 push @item_loop, $item if $item;
305 my ( $holdingbrtagf, $holdingbrtagsubf ) =
306 &GetMarcFromKohaField( "items.holdingbranch", $framework );
308 sort { ($a->{$holdingbrtagsubf}||'') cmp ($b->{$holdingbrtagsubf}||'') } @item_loop;
310 @item_subfield_codes = uniq @item_subfield_codes;
312 my @item_header_loop;
313 for my $subfield_code ( @item_subfield_codes ) {
314 push @item_header_loop, $witness{$subfield_code};
315 for my $item_data ( @item_loop ) {
316 $item_data->{$subfield_code} ||= " "
320 if ( C4::Context->preference("OPACISBD") ) {
321 $template->param( ISBD => 1 );
324 #Search for title in links
325 my $marcflavour = C4::Context->preference("marcflavour");
326 my $dat = TransformMarcToKoha( $record );
327 my $isbn = GetNormalizedISBN(undef,$record,$marcflavour);
328 my $marccontrolnumber = GetMarcControlnumber ($record, $marcflavour);
329 my $marcissns = GetMarcISSN( $record, $marcflavour );
330 my $issn = $marcissns->[0] || '';
332 if (my $search_for_title = C4::Context->preference('OPACSearchForTitleIn')){
333 $dat->{title} =~ s/\/+$//; # remove trailing slash
334 $dat->{title} =~ s/\s+$//; # remove trailing space
335 $search_for_title = parametrized_url(
338 TITLE => $dat->{title},
339 AUTHOR => $dat->{author},
342 CONTROLNUMBER => $marccontrolnumber,
343 BIBLIONUMBER => $biblionumber,
346 $template->param('OPACSearchForTitleIn' => $search_for_title);
350 item_loop => \@item_loop,
351 item_header_loop => \@item_header_loop,
352 item_subfield_codes => \@item_subfield_codes,
356 output_html_with_http_headers $query, $cookie, $template->output;