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