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