Bug 20687: Check all share keys for a given list
[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 use Koha::Biblios;
61 use Koha::IssuingRules;
62 use Koha::Items;
63 use Koha::Patrons;
64 use Koha::RecordProcessor;
65
66 my $query = new CGI;
67
68 my $dbh = C4::Context->dbh;
69
70 my $biblionumber = $query->param('biblionumber');
71 if ( ! $biblionumber ) {
72     print $query->redirect("/cgi-bin/koha/errors/404.pl");
73     exit;
74 }
75
76 my $record = GetMarcBiblio({
77     biblionumber => $biblionumber,
78     embed_items  => 1 });
79 if ( ! $record ) {
80     print $query->redirect("/cgi-bin/koha/errors/404.pl");
81     exit;
82 }
83
84 my @all_items = GetItemsInfo($biblionumber);
85 my @items2hide;
86 if (scalar @all_items >= 1) {
87     push @items2hide, GetHiddenItemnumbers(@all_items);
88
89     if (scalar @items2hide == scalar @all_items ) {
90         print $query->redirect("/cgi-bin/koha/errors/404.pl");
91         exit;
92     }
93 }
94
95 my $framework = &GetFrameworkCode( $biblionumber );
96 my $tagslib = &GetMarcStructure( 0, $framework );
97 my ($tag_itemnumber,$subtag_itemnumber) = &GetMarcFromKohaField('items.itemnumber',$framework);
98 my $biblio = Koha::Biblios->find( $biblionumber );
99
100 my $record_processor = Koha::RecordProcessor->new({
101     filters => 'ViewPolicy',
102     options => {
103         interface => 'opac',
104         frameworkcode => $framework
105     }
106 });
107 $record_processor->process($record);
108
109 # open template
110 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
111     {
112         template_name   => "opac-MARCdetail.tt",
113         query           => $query,
114         type            => "opac",
115         authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ),
116         debug           => 1,
117     }
118 );
119
120 my ($bt_tag,$bt_subtag) = GetMarcFromKohaField('biblio.title',$framework);
121 $template->param(
122     bibliotitle => $biblio->title,
123 ) if $tagslib->{$bt_tag}->{$bt_subtag}->{hidden} <= 0 && # <=0 OPAC visible.
124      $tagslib->{$bt_tag}->{$bt_subtag}->{hidden} > -8;   # except -8;
125
126 # get biblionumbers stored in the cart
127 if(my $cart_list = $query->cookie("bib_list")){
128     my @cart_list = split(/\//, $cart_list);
129     if ( grep {$_ eq $biblionumber} @cart_list) {
130         $template->param( incart => 1 );
131     }
132 }
133
134 my $allow_onshelf_holds;
135 my $patron = Koha::Patrons->find( $loggedinuser );
136 for my $itm (@all_items) {
137     my $item = Koha::Items->find( $itm->{itemnumber} );
138     $allow_onshelf_holds = Koha::IssuingRules->get_onshelfholds_policy( { item => $item, patron => $patron } );
139     last if $allow_onshelf_holds;
140 }
141
142 if( $allow_onshelf_holds || CountItemsIssued($biblionumber) || $biblio->has_items_waiting_or_intransit ) {
143     $template->param( ReservableItems => 1 );
144 }
145
146 # adding the $RequestOnOpac param
147 my $RequestOnOpac;
148 if (C4::Context->preference("RequestOnOpac")) {
149         $RequestOnOpac = 1;
150 }
151
152 # fill arrays
153 my @loop_data = ();
154 my $tag;
155
156 # loop through each tab 0 through 9
157 for ( my $tabloop = 0 ; $tabloop <= 9 ; $tabloop++ ) {
158
159     # loop through each tag
160     my @loop_data = ();
161     my @subfields_data;
162
163     # deal with leader
164     unless ( $tagslib->{'000'}->{'@'}->{tab} ne $tabloop
165         or $tagslib->{'000'}->{'@'}->{hidden} > 0 )
166     {
167         my %subfield_data;
168         $subfield_data{marc_lib}      = $tagslib->{'000'}->{'@'}->{lib};
169         $subfield_data{marc_value}    = $record->leader();
170         $subfield_data{marc_subfield} = '@';
171         $subfield_data{marc_tag}      = '000';
172         push( @subfields_data, \%subfield_data );
173         my %tag_data;
174         $tag_data{tag} = '000 -' . $tagslib->{'000'}->{lib};
175         my @tmp = @subfields_data;
176         $tag_data{subfield} = \@tmp;
177         push( @loop_data, \%tag_data );
178         undef @subfields_data;
179     }
180     my @fields = $record->fields();
181     for ( my $x_i = 0 ; $x_i <= $#fields ; $x_i++ ) {
182
183         # if tag <10, there's no subfield, use the "@" trick
184         if ( $fields[$x_i]->tag() < 10 ) {
185             next
186               if (
187                 $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{tab} ne $tabloop );
188             next if ( $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{hidden} > 0 );
189             my %subfield_data;
190             $subfield_data{marc_lib} =
191               $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{lib};
192             $subfield_data{marc_value}    = $fields[$x_i]->data();
193             $subfield_data{marc_subfield} = '@';
194             $subfield_data{marc_tag}      = $fields[$x_i]->tag();
195             push( @subfields_data, \%subfield_data );
196         }
197         else {
198             my @subf = $fields[$x_i]->subfields;
199             my $previous = '';
200             # loop through each subfield
201             for my $i ( 0 .. $#subf ) {
202                 $subf[$i][0] = "@" unless defined($subf[$i][0]);
203                 my $sf_def = $tagslib->{ $fields[$x_i]->tag() };
204                 $sf_def = $sf_def->{ $subf[$i][0] } if defined($sf_def);
205                 my ($tab,$hidden,$lib);
206                 $tab = $sf_def->{tab} if defined($sf_def);
207                 $tab = $tab // int($fields[$x_i]->tag()/100);
208                 $hidden = $sf_def->{hidden} if defined($sf_def);
209                 $hidden = $hidden // 0;
210                 next if ( $tab != $tabloop );
211                 next if ( $hidden > 0 );
212                 my %subfield_data;
213                 $lib = $sf_def->{lib} if defined($sf_def);
214                 $lib = $lib // '--';
215                 $subfield_data{marc_lib} = ($lib eq $previous) ?  '--' : $lib;
216                 $previous = $lib;
217                 $subfield_data{link} = $sf_def->{link};
218                 $subf[$i][1] =~ s/\n/<br\/>/g;
219                 if ( $sf_def->{isurl} ) {
220                     $subfield_data{marc_value} = "<a href=\"$subf[$i][1]\">$subf[$i][1]</a>";
221                 }
222                 elsif ( defined($sf_def->{kohafield}) && $sf_def->{kohafield} eq "biblioitems.isbn" ) {
223                     $subfield_data{marc_value} = $subf[$i][1];
224                 }
225                 else {
226                     if ( $sf_def->{authtypecode} ) {
227                         $subfield_data{authority} = $fields[$x_i]->subfield(9);
228                     }
229                     $subfield_data{marc_value} = GetAuthorisedValueDesc( $fields[$x_i]->tag(),
230                         $subf[$i][0], $subf[$i][1], '', $tagslib, '', 'opac' );
231                 }
232                 $subfield_data{marc_subfield} = $subf[$i][0];
233                 $subfield_data{marc_tag}      = $fields[$x_i]->tag();
234                 push( @subfields_data, \%subfield_data );
235             }
236         }
237         if ( $#subfields_data >= 0 ) {
238             my %tag_data;
239             if (   ( $fields[$x_i]->tag() eq $fields[ $x_i - 1 ]->tag() )
240                 && ( C4::Context->preference('LabelMARCView') eq 'economical' )
241               )
242             {
243                 $tag_data{tag} = "";
244             }
245             else {
246                 if ( C4::Context->preference('hide_marc') ) {
247                     $tag_data{tag} = $tagslib->{ $fields[$x_i]->tag() }->{lib};
248                 }
249                 else {
250                     my $sf_def = $tagslib->{ $fields[$x_i]->tag() };
251                     my $lib;
252                     $lib = $sf_def->{lib} if defined($sf_def);
253                     $lib = $lib // '';
254                     $tag_data{tag} = $fields[$x_i]->tag() . ' '
255                       . C4::Koha::display_marc_indicators($fields[$x_i])
256                       . " - $lib";
257                 }
258             }
259             my @tmp = @subfields_data;
260             $tag_data{subfield} = \@tmp;
261             push( @loop_data, \%tag_data );
262             undef @subfields_data;
263         }
264     }
265     $template->param( "tab" . $tabloop . "XX" => \@loop_data );
266 }
267
268
269 # now, build item tab !
270 # the main difference is that datas are in lines and not in columns : thus, we build the <th> first, then the values...
271 # loop through each tag
272 # warning : we may have differents number of columns in each row. Thus, we first build a hash, complete it if necessary
273 # then construct template.
274 my @fields = $record->fields();
275 my %witness
276   ; #---- stores the list of subfields used at least once, with the "meaning" of the code
277 my @item_subfield_codes;
278 my @item_loop;
279 foreach my $field (@fields) {
280     next if ( $field->tag() < 10 );
281     next if ( ( $field->tag() eq $tag_itemnumber ) &&
282               ( any { $field->subfield($subtag_itemnumber) eq $_ }
283                    @items2hide) );
284     my @subf = $field->subfields;
285     my $item;
286
287     # loop through each subfield
288     for my $i ( 0 .. $#subf ) {
289         my $sf_def = $tagslib->{ $field->tag() }->{ $subf[$i][0] };
290         next if ( ($sf_def->{tab}||0) != 10 );
291         next if ( ($sf_def->{hidden}||0) > 0 );
292         push @item_subfield_codes, $subf[$i][0];
293         $witness{ $subf[$i][0] } = $sf_def->{lib};
294
295         if ( $sf_def->{isurl} ) {
296             $item->{ $subf[$i][0] } = "<a href=\"$subf[$i][1]\">$subf[$i][1]</a>";
297         }
298         elsif ( $sf_def->{kohafield} eq "biblioitems.isbn" ) {
299             $item->{ $subf[$i][0] } = $subf[$i][1];
300         }
301         else {
302             $item->{ $subf[$i][0] } = GetAuthorisedValueDesc( $field->tag(), $subf[$i][0],
303                 $subf[$i][1], '', $tagslib, '', 'opac' );
304         }
305     }
306     push @item_loop, $item if $item;
307 }
308 my ( $holdingbrtagf, $holdingbrtagsubf ) =
309   &GetMarcFromKohaField( "items.holdingbranch", $framework );
310 @item_loop =
311   sort { ($a->{$holdingbrtagsubf}||'') cmp ($b->{$holdingbrtagsubf}||'') } @item_loop;
312
313 @item_subfield_codes = uniq @item_subfield_codes;
314 # fill item info
315 my @item_header_loop;
316 for my $subfield_code ( @item_subfield_codes ) {
317     push @item_header_loop, $witness{$subfield_code};
318     for my $item_data ( @item_loop ) {
319         $item_data->{$subfield_code} ||= "&nbsp;"
320      }
321 }
322
323 if ( C4::Context->preference("OPACISBD") ) {
324     $template->param( ISBD => 1 );
325 }
326
327 #Search for title in links
328 my $marcflavour  = C4::Context->preference("marcflavour");
329 my $dat = TransformMarcToKoha( $record );
330 my $isbn = GetNormalizedISBN(undef,$record,$marcflavour);
331 my $marccontrolnumber   = GetMarcControlnumber ($record, $marcflavour);
332 my $marcissns = GetMarcISSN( $record, $marcflavour );
333 my $issn = $marcissns->[0] || '';
334
335 if (my $search_for_title = C4::Context->preference('OPACSearchForTitleIn')){
336     $dat->{title} =~ s/\/+$//; # remove trailing slash
337     $dat->{title} =~ s/\s+$//; # remove trailing space
338     $search_for_title = parametrized_url(
339         $search_for_title,
340         {
341             TITLE         => $dat->{title},
342             AUTHOR        => $dat->{author},
343             ISBN          => $isbn,
344             ISSN          => $issn,
345             CONTROLNUMBER => $marccontrolnumber,
346             BIBLIONUMBER  => $biblionumber,
347         }
348     );
349     $template->param('OPACSearchForTitleIn' => $search_for_title);
350 }
351
352 $template->param(
353     item_loop           => \@item_loop,
354     item_header_loop    => \@item_header_loop,
355     item_subfield_codes => \@item_subfield_codes,
356     biblio              => $biblio,
357 );
358
359 output_html_with_http_headers $query, $cookie, $template->output;