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