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