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