Bug 16167: Remove Authorised value images prefs
[koha.git] / opac / opac-MARCdetail.pl
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 #       Copyright (C) 2000-2002 Katipo Communications
6 # Parts Copyright (C) 2010      BibLibre
7 # Parts Copyright (C) 2013      Mark Tompsett
8 #
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.
13 #
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.
18 #
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>.
21
22
23 =head1 NAME
24
25 opac-MARCdetail.pl : script to show a biblio in MARC format
26
27 =head1 SYNOPSIS
28
29 =cut
30
31 =head1 DESCRIPTION
32
33 This script needs a biblionumber as  parameter
34
35 It shows the biblio in a (nice) MARC format depending on MARC
36 parameters tables.
37
38 The template is in <templates_dir>/catalogue/MARCdetail.tt.
39 this template must be divided into 11 "tabs".
40
41 The first 10 tabs present the biblio, the 11th one presents
42 the items attached to the biblio
43
44 =cut
45
46 use Modern::Perl;
47
48 use C4::Auth;
49 use C4::Context;
50 use C4::Output;
51 use CGI qw ( -utf8 );
52 use MARC::Record;
53 use C4::Biblio;
54 use C4::Items;
55 use C4::Reserves;
56 use C4::Members;
57 use C4::Acquisition;
58 use C4::Koha;
59 use List::MoreUtils qw( any uniq );
60
61 my $query = new CGI;
62
63 my $dbh = C4::Context->dbh;
64
65 my $biblionumber = $query->param('biblionumber');
66 if ( ! $biblionumber ) {
67     print $query->redirect("/cgi-bin/koha/errors/404.pl");
68     exit;
69 }
70
71 my @all_items = GetItemsInfo($biblionumber);
72 my @items2hide;
73 if (scalar @all_items >= 1) {
74     push @items2hide, GetHiddenItemnumbers(@all_items);
75
76     if (scalar @items2hide == scalar @all_items ) {
77         print $query->redirect("/cgi-bin/koha/errors/404.pl");
78         exit;
79     }
80 }
81
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);
88 if ( ! $record ) {
89     print $query->redirect("/cgi-bin/koha/errors/404.pl");
90     exit;
91 }
92 # open template
93 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
94     {
95         template_name   => "opac-MARCdetail.tt",
96         query           => $query,
97         type            => "opac",
98         authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ),
99         debug           => 1,
100     }
101 );
102
103 my ($bt_tag,$bt_subtag) = GetMarcFromKohaField('biblio.title',$itemtype);
104 $template->param(
105     bibliotitle => $biblio->{title},
106 ) if $tagslib->{$bt_tag}->{$bt_subtag}->{hidden} <= 0; #<=0 is OPAC visible.
107
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 );
113     }
114 }
115
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;
121 }
122
123 $template->param( 'AllowOnShelfHolds' => $allow_onshelf_holds );
124 $template->param( 'ItemsIssued' => CountItemsIssued( $biblionumber ) );
125
126 # adding the $RequestOnOpac param
127 my $RequestOnOpac;
128 if (C4::Context->preference("RequestOnOpac")) {
129         $RequestOnOpac = 1;
130 }
131
132 # fill arrays
133 my @loop_data = ();
134 my $tag;
135
136 # loop through each tab 0 through 9
137 for ( my $tabloop = 0 ; $tabloop <= 9 ; $tabloop++ ) {
138
139     # loop through each tag
140     my @loop_data = ();
141     my @subfields_data;
142
143     # deal with leader
144     unless ( $tagslib->{'000'}->{'@'}->{tab} ne $tabloop
145         or $tagslib->{'000'}->{'@'}->{hidden} > 0 )
146     {
147         my %subfield_data;
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 );
153         my %tag_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;
159     }
160     my @fields = $record->fields();
161     for ( my $x_i = 0 ; $x_i <= $#fields ; $x_i++ ) {
162
163         # if tag <10, there's no subfield, use the "@" trick
164         if ( $fields[$x_i]->tag() < 10 ) {
165             next
166               if (
167                 $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{tab} ne $tabloop );
168             next if ( $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{hidden} > 0 );
169             my %subfield_data;
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 );
176         }
177         else {
178             my @subf = $fields[$x_i]->subfields;
179             my $previous = '';
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 );
192                 my %subfield_data;
193                 $lib = $sf_def->{lib} if defined($sf_def);
194                 $lib = $lib // '--';
195                 $subfield_data{marc_lib} = ($lib eq $previous) ?  '--' : $lib;
196                 $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>";
201                 }
202                 elsif ( defined($sf_def->{kohafield}) && $sf_def->{kohafield} eq "biblioitems.isbn" ) {
203                     $subfield_data{marc_value} = $subf[$i][1];
204                 }
205                 else {
206                     if ( $sf_def->{authtypecode} ) {
207                         $subfield_data{authority} = $fields[$x_i]->subfield(9);
208                     }
209                     $subfield_data{marc_value} = GetAuthorisedValueDesc( $fields[$x_i]->tag(),
210                         $subf[$i][0], $subf[$i][1], '', $tagslib, '', 'opac' );
211                 }
212                 $subfield_data{marc_subfield} = $subf[$i][0];
213                 $subfield_data{marc_tag}      = $fields[$x_i]->tag();
214                 push( @subfields_data, \%subfield_data );
215             }
216         }
217         if ( $#subfields_data >= 0 ) {
218             my %tag_data;
219             if (   ( $fields[$x_i]->tag() eq $fields[ $x_i - 1 ]->tag() )
220                 && ( C4::Context->preference('LabelMARCView') eq 'economical' )
221               )
222             {
223                 $tag_data{tag} = "";
224             }
225             else {
226                 if ( C4::Context->preference('hide_marc') ) {
227                     $tag_data{tag} = $tagslib->{ $fields[$x_i]->tag() }->{lib};
228                 }
229                 else {
230                     my $sf_def = $tagslib->{ $fields[$x_i]->tag() };
231                     my $lib;
232                     $lib = $sf_def->{lib} if defined($sf_def);
233                     $lib = $lib // '';
234                     $tag_data{tag} = $fields[$x_i]->tag() . ' '
235                       . C4::Koha::display_marc_indicators($fields[$x_i])
236                       . " - $lib";
237                 }
238             }
239             my @tmp = @subfields_data;
240             $tag_data{subfield} = \@tmp;
241             push( @loop_data, \%tag_data );
242             undef @subfields_data;
243         }
244     }
245     $template->param( "tab" . $tabloop . "XX" => \@loop_data );
246 }
247
248
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();
255 my %witness
256   ; #---- stores the list of subfields used at least once, with the "meaning" of the code
257 my @item_subfield_codes;
258 my @item_loop;
259 foreach my $field (@fields) {
260     next if ( $field->tag() < 10 );
261     next if ( ( $field->tag() eq $tag_itemnumber ) &&
262               ( any { $field->subfield($subtag_itemnumber) eq $_ }
263                    @items2hide) );
264     my @subf = $field->subfields;
265     my $item;
266
267     # loop through each subfield
268     for my $i ( 0 .. $#subf ) {
269         my $sf_def = $tagslib->{ $field->tag() }->{ $subf[$i][0] };
270         next if ( ($sf_def->{tab}||0) != 10 );
271         next if ( ($sf_def->{hidden}||0) > 0 );
272         push @item_subfield_codes, $subf[$i][0];
273         $witness{ $subf[$i][0] } = $sf_def->{lib};
274
275         if ( $sf_def->{isurl} ) {
276             $item->{ $subf[$i][0] } = "<a href=\"$subf[$i][1]\">$subf[$i][1]</a>";
277         }
278         elsif ( $sf_def->{kohafield} eq "biblioitems.isbn" ) {
279             $item->{ $subf[$i][0] } = $subf[$i][1];
280         }
281         else {
282             $item->{ $subf[$i][0] } = GetAuthorisedValueDesc( $field->tag(), $subf[$i][0],
283                 $subf[$i][1], '', $tagslib, '', 'opac' );
284         }
285     }
286     push @item_loop, $item if $item;
287 }
288 my ( $holdingbrtagf, $holdingbrtagsubf ) =
289   &GetMarcFromKohaField( "items.holdingbranch", $itemtype );
290 @item_loop =
291   sort { ($a->{$holdingbrtagsubf}||'') cmp ($b->{$holdingbrtagsubf}||'') } @item_loop;
292
293 @item_subfield_codes = uniq @item_subfield_codes;
294 # fill item info
295 my @item_header_loop;
296 for my $subfield_code ( @item_subfield_codes ) {
297     push @item_header_loop, $witness{$subfield_code};
298     for my $item_data ( @item_loop ) {
299         $item_data->{$subfield_code} ||= "&nbsp;"
300      }
301 }
302
303 if ( C4::Context->preference("OPACISBD") ) {
304     $template->param( ISBD => 1 );
305 }
306
307 #Search for title in links
308 my $marcflavour  = C4::Context->preference("marcflavour");
309 my $dat = TransformMarcToKoha( $record );
310 my $isbn = GetNormalizedISBN(undef,$record,$marcflavour);
311 my $marccontrolnumber   = GetMarcControlnumber ($record, $marcflavour);
312 my $marcissns = GetMarcISSN( $record, $marcflavour );
313 my $issn = $marcissns->[0] || '';
314
315 if (my $search_for_title = C4::Context->preference('OPACSearchForTitleIn')){
316     $dat->{title} =~ s/\/+$//; # remove trailing slash
317     $dat->{title} =~ s/\s+$//; # remove trailing space
318     $search_for_title = parametrized_url(
319         $search_for_title,
320         {
321             TITLE         => $dat->{title},
322             AUTHOR        => $dat->{author},
323             ISBN          => $isbn,
324             ISSN          => $issn,
325             CONTROLNUMBER => $marccontrolnumber,
326             BIBLIONUMBER  => $biblionumber,
327         }
328     );
329     $template->param('OPACSearchForTitleIn' => $search_for_title);
330 }
331
332 $template->param(
333     item_loop           => \@item_loop,
334     item_header_loop    => \@item_header_loop,
335     item_subfield_codes => \@item_subfield_codes,
336     biblionumber        => $biblionumber,
337 );
338
339 output_html_with_http_headers $query, $cookie, $template->output;