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/;
63 my $dbh = C4::Context->dbh;
65 my $biblionumber = $query->param('biblionumber');
66 if ( ! $biblionumber ) {
67 print $query->redirect("/cgi-bin/koha/errors/404.pl");
71 my @all_items = GetItemsInfo($biblionumber);
73 if (scalar @all_items >= 1) {
74 push @items2hide, GetHiddenItemnumbers(@all_items);
76 if (scalar @items2hide == scalar @all_items ) {
77 print $query->redirect("/cgi-bin/koha/errors/404.pl");
82 my $itemtype = &GetFrameworkCode($biblionumber);
83 my $tagslib = &GetMarcStructure( 0, $itemtype );
84 my ($tag_itemnumber,$subtag_itemnumber) = &GetMarcFromKohaField('items.itemnumber',$itemtype);
85 my $biblio = GetBiblioData($biblionumber);
86 $biblionumber = $biblio->{biblionumber};
87 my $record = GetMarcBiblio($biblionumber, 1);
89 print $query->redirect("/cgi-bin/koha/errors/404.pl");
93 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
95 template_name => "opac-MARCdetail.tt",
98 authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ),
103 my ($bt_tag,$bt_subtag) = GetMarcFromKohaField('biblio.title',$itemtype);
105 bibliotitle => $biblio->{title},
106 ) if $tagslib->{$bt_tag}->{$bt_subtag}->{hidden} <= 0; #<=0 is OPAC visible.
108 # get biblionumbers stored in the cart
109 if(my $cart_list = $query->cookie("bib_list")){
110 my @cart_list = split(/\//, $cart_list);
111 if ( grep {$_ eq $biblionumber} @cart_list) {
112 $template->param( incart => 1 );
116 my $allow_onshelf_holds;
117 my $borrower = GetMember( 'borrowernumber' => $loggedinuser );
118 for my $itm (@all_items) {
119 $allow_onshelf_holds = C4::Reserves::OnShelfHoldsAllowed($itm, $borrower);
120 last if $allow_onshelf_holds;
123 $template->param( 'AllowOnShelfHolds' => $allow_onshelf_holds );
124 $template->param( 'ItemsIssued' => CountItemsIssued( $biblionumber ) );
126 # adding the $RequestOnOpac param
128 if (C4::Context->preference("RequestOnOpac")) {
136 # loop through each tab 0 through 9
137 for ( my $tabloop = 0 ; $tabloop <= 9 ; $tabloop++ ) {
139 # loop through each tag
144 unless ( $tagslib->{'000'}->{'@'}->{tab} ne $tabloop
145 or $tagslib->{'000'}->{'@'}->{hidden} > 0 )
148 $subfield_data{marc_lib} = $tagslib->{'000'}->{'@'}->{lib};
149 $subfield_data{marc_value} = $record->leader();
150 $subfield_data{marc_subfield} = '@';
151 $subfield_data{marc_tag} = '000';
152 push( @subfields_data, \%subfield_data );
154 $tag_data{tag} = '000 -' . $tagslib->{'000'}->{lib};
155 my @tmp = @subfields_data;
156 $tag_data{subfield} = \@tmp;
157 push( @loop_data, \%tag_data );
158 undef @subfields_data;
160 my @fields = $record->fields();
161 for ( my $x_i = 0 ; $x_i <= $#fields ; $x_i++ ) {
163 # if tag <10, there's no subfield, use the "@" trick
164 if ( $fields[$x_i]->tag() < 10 ) {
167 $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{tab} ne $tabloop );
168 next if ( $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{hidden} > 0 );
170 $subfield_data{marc_lib} =
171 $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{lib};
172 $subfield_data{marc_value} = $fields[$x_i]->data();
173 $subfield_data{marc_subfield} = '@';
174 $subfield_data{marc_tag} = $fields[$x_i]->tag();
175 push( @subfields_data, \%subfield_data );
178 my @subf = $fields[$x_i]->subfields;
180 # loop through each subfield
181 for my $i ( 0 .. $#subf ) {
182 $subf[$i][0] = "@" unless defined($subf[$i][0]);
183 my $sf_def = $tagslib->{ $fields[$x_i]->tag() };
184 $sf_def = $sf_def->{ $subf[$i][0] } if defined($sf_def);
185 my ($tab,$hidden,$lib);
186 $tab = $sf_def->{tab} if defined($sf_def);
187 $tab = $tab // int($fields[$x_i]->tag()/100);
188 $hidden = $sf_def->{hidden} if defined($sf_def);
189 $hidden = $hidden // 0;
190 next if ( $tab != $tabloop );
191 next if ( $hidden > 0 );
193 $lib = $sf_def->{lib} if defined($sf_def);
195 $subfield_data{marc_lib} = ($lib eq $previous) ? '--' : $lib;
197 $subfield_data{link} = $sf_def->{link};
198 $subf[$i][1] =~ s/\n/<br\/>/g;
199 if ( $sf_def->{isurl} ) {
200 $subfield_data{marc_value} = "<a href=\"$subf[$i][1]\">$subf[$i][1]</a>";
202 elsif ( defined($sf_def->{kohafield}) && $sf_def->{kohafield} eq "biblioitems.isbn" ) {
203 $subfield_data{marc_value} = $subf[$i][1];
206 if ( $sf_def->{authtypecode} ) {
207 $subfield_data{authority} = $fields[$x_i]->subfield(9);
209 $subfield_data{marc_value} = GetAuthorisedValueDesc( $fields[$x_i]->tag(),
210 $subf[$i][0], $subf[$i][1], '', $tagslib, '', 'opac' );
212 $subfield_data{marc_subfield} = $subf[$i][0];
213 $subfield_data{marc_tag} = $fields[$x_i]->tag();
214 push( @subfields_data, \%subfield_data );
217 if ( $#subfields_data >= 0 ) {
219 if ( ( $fields[$x_i]->tag() eq $fields[ $x_i - 1 ]->tag() )
220 && ( C4::Context->preference('LabelMARCView') eq 'economical' )
226 if ( C4::Context->preference('hide_marc') ) {
227 $tag_data{tag} = $tagslib->{ $fields[$x_i]->tag() }->{lib};
230 my $sf_def = $tagslib->{ $fields[$x_i]->tag() };
232 $lib = $sf_def->{lib} if defined($sf_def);
234 $tag_data{tag} = $fields[$x_i]->tag() . ' '
235 . C4::Koha::display_marc_indicators($fields[$x_i])
239 my @tmp = @subfields_data;
240 $tag_data{subfield} = \@tmp;
241 push( @loop_data, \%tag_data );
242 undef @subfields_data;
245 $template->param( "tab" . $tabloop . "XX" => \@loop_data );
249 # now, build item tab !
250 # the main difference is that datas are in lines and not in columns : thus, we build the <th> first, then the values...
251 # loop through each tag
252 # warning : we may have differents number of columns in each row. Thus, we first build a hash, complete it if necessary
253 # then construct template.
254 my @fields = $record->fields();
256 ; #---- stores the list of subfields used at least once, with the "meaning" of the code
258 foreach my $field (@fields) {
259 next if ( $field->tag() < 10 );
260 next if ( ( $field->tag() eq $tag_itemnumber ) &&
261 ( any { $field->subfield($subtag_itemnumber) eq $_ }
263 my @subf = $field->subfields;
266 # loop through each subfield
267 for my $i ( 0 .. $#subf ) {
268 my $sf_def = $tagslib->{ $field->tag() }->{ $subf[$i][0] };
269 next if ( ($sf_def->{tab}||0) != 10 );
270 next if ( ($sf_def->{hidden}||0) > 0 );
271 $witness{ $subf[$i][0] } = $sf_def->{lib};
273 if ( $sf_def->{isurl} ) {
274 $this_row{ $subf[$i][0] } = "<a href=\"$subf[$i][1]\">$subf[$i][1]</a>";
276 elsif ( $sf_def->{kohafield} eq "biblioitems.isbn" ) {
277 $this_row{ $subf[$i][0] } = $subf[$i][1];
280 $this_row{ $subf[$i][0] } = GetAuthorisedValueDesc( $field->tag(), $subf[$i][0],
281 $subf[$i][1], '', $tagslib, '', 'opac' );
285 push( @big_array, \%this_row );
288 my ( $holdingbrtagf, $holdingbrtagsubf ) =
289 &GetMarcFromKohaField( "items.holdingbranch", $itemtype );
291 sort { ($a->{$holdingbrtagsubf}||'') cmp ($b->{$holdingbrtagsubf}||'') } @big_array;
293 #fill big_row with missing datas
294 foreach my $subfield_code ( keys(%witness) ) {
295 for ( my $i = 0 ; $i <= $#big_array ; $i++ ) {
296 $big_array[$i]{$subfield_code} = " "
297 unless ( $big_array[$i]{$subfield_code} );
301 # now, construct template !
303 my @header_value_loop;
304 for ( my $i = 0 ; $i <= $#big_array ; $i++ ) {
306 foreach my $subfield_code ( keys(%witness) ) {
307 $items_data .= "<td>" . $big_array[$i]{$subfield_code} . "</td>";
310 $row_data{item_value} = $items_data;
311 push( @item_value_loop, \%row_data );
314 foreach my $subfield_code ( keys(%witness) ) {
316 $header_value{header_value} = $witness{$subfield_code};
317 push( @header_value_loop, \%header_value );
320 if(C4::Context->preference("ISBD")) {
321 $template->param(ISBD => 1);
324 #Search for title in links
325 my $marcflavour = C4::Context->preference("marcflavour");
326 my $dat = TransformMarcToKoha( $dbh, $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_value_loop,
351 item_header_loop => \@header_value_loop,
352 biblionumber => $biblionumber,
355 output_html_with_http_headers $query, $cookie, $template->output;