Bug 29244: Add dialog class where missing in print-notices.tt
[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::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 $biblio = Koha::Biblios->find( $biblionumber );
106 unless ( $patron and $patron->category->override_hidden_items ) {
107     # only skip this check if there's a logged in user
108     # and its category overrides OpacHiddenItems
109     if ( $biblio->hidden_in_opac({ rules => C4::Context->yaml_preference('OpacHiddenItems') }) ) {
110         print $query->redirect('/cgi-bin/koha/errors/404.pl'); # escape early
111         exit;
112     }
113 }
114
115 my $framework = $biblio ? $biblio->frameworkcode : q{};
116 my $tagslib   = &GetMarcStructure( 0, $framework );
117
118 my $record_processor = Koha::RecordProcessor->new({
119     filters => 'ViewPolicy',
120     options => {
121         interface => 'opac',
122         frameworkcode => $framework
123     }
124 });
125 $record_processor->process($record);
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 ($bt_tag,$bt_subtag) = GetMarcFromKohaField( 'biblio.title' );
136 $template->param(
137     bibliotitle => $biblio->title,
138 ) if $tagslib->{$bt_tag}->{$bt_subtag}->{hidden} <= 0 && # <=0 OPAC visible.
139      $tagslib->{$bt_tag}->{$bt_subtag}->{hidden} > -8;   # except -8;
140
141 my $norequests = 1;
142 my $allow_onshelf_holds;
143 my $items = $biblio->items;
144
145 while ( my $item = $items->next ) {
146     $norequests = 0
147       if $norequests
148         && !$item->withdrawn
149         && !$item->itemlost
150         && ($item->notforloan < 0 || not $item->notforloan )
151         && !Koha::ItemTypes->find($item->effective_itemtype)->notforloan
152         && $item->itemnumber;
153
154     $allow_onshelf_holds = Koha::CirculationRules->get_onshelfholds_policy( { item => $item, patron => $patron } )
155       unless $allow_onshelf_holds;
156 }
157
158 if( $allow_onshelf_holds || CountItemsIssued($biblionumber) || $biblio->has_items_waiting_or_intransit ) {
159     $template->param( ReservableItems => 1 );
160 }
161
162 # adding the $RequestOnOpac param
163 my $RequestOnOpac;
164 if (C4::Context->preference("RequestOnOpac")) {
165         $RequestOnOpac = 1;
166 }
167
168 # fill arrays
169 my @loop_data = ();
170
171 # loop through each tab 0 through 9
172 for ( my $tabloop = 0 ; $tabloop <= 9 ; $tabloop++ ) {
173
174     # loop through each tag
175     my @loop_data = ();
176     my @subfields_data;
177
178     # deal with leader
179     unless ( $tagslib->{'000'}->{'@'}->{tab} ne $tabloop
180         or $tagslib->{'000'}->{'@'}->{hidden} > 0 )
181     {
182         my %subfield_data;
183         $subfield_data{marc_lib}      = $tagslib->{'000'}->{'@'}->{lib};
184         $subfield_data{marc_value}    = $record->leader();
185         $subfield_data{marc_subfield} = '@';
186         $subfield_data{marc_tag}      = '000';
187         push( @subfields_data, \%subfield_data );
188         my %tag_data;
189         $tag_data{tag} = '000 -' . $tagslib->{'000'}->{lib};
190         my @tmp = @subfields_data;
191         $tag_data{subfield} = \@tmp;
192         push( @loop_data, \%tag_data );
193         undef @subfields_data;
194     }
195     my @fields = $record->fields();
196     for ( my $x_i = 0 ; $x_i <= $#fields ; $x_i++ ) {
197
198         # if tag <10, there's no subfield, use the "@" trick
199         if ( $fields[$x_i]->tag() < 10 ) {
200             next
201               if (
202                 $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{tab} ne $tabloop );
203             next if ( $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{hidden} > 0 );
204             my %subfield_data;
205             $subfield_data{marc_lib} =
206               $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{lib};
207             $subfield_data{marc_value}    = $fields[$x_i]->data();
208             $subfield_data{marc_subfield} = '@';
209             $subfield_data{marc_tag}      = $fields[$x_i]->tag();
210             push( @subfields_data, \%subfield_data );
211         }
212         else {
213             my @subf = $fields[$x_i]->subfields;
214             my $previous = '';
215             # loop through each subfield
216             for my $i ( 0 .. $#subf ) {
217                 $subf[$i][0] = "@" unless defined($subf[$i][0]);
218                 my $sf_def = $tagslib->{ $fields[$x_i]->tag() };
219                 $sf_def = $sf_def->{ $subf[$i][0] } if defined($sf_def);
220                 my ($tab,$hidden,$lib);
221                 $tab = $sf_def->{tab} if defined($sf_def);
222                 $tab = $tab // int($fields[$x_i]->tag()/100);
223                 $hidden = $sf_def->{hidden} if defined($sf_def);
224                 $hidden = $hidden // 0;
225                 next if ( $tab != $tabloop );
226                 next if ( $hidden > 0 );
227                 my %subfield_data;
228                 $lib = $sf_def->{lib} if defined($sf_def);
229                 $lib = $lib // '--';
230                 $subfield_data{marc_lib} = ($lib eq $previous) ?  '--' : $lib;
231                 $previous = $lib;
232                 $subfield_data{link} = $sf_def->{link};
233                 $subf[$i][1] =~ s/\n/<br\/>/g;
234                 if ( $sf_def->{isurl} ) {
235                     $subfield_data{marc_value} = "<a href=\"$subf[$i][1]\">$subf[$i][1]</a>";
236                 }
237                 elsif ( defined($sf_def->{kohafield}) && $sf_def->{kohafield} eq "biblioitems.isbn" ) {
238                     $subfield_data{marc_value} = $subf[$i][1];
239                 }
240                 else {
241                     if ( $sf_def->{authtypecode} ) {
242                         $subfield_data{authority} = $fields[$x_i]->subfield(9);
243                     }
244                     $subfield_data{marc_value} = GetAuthorisedValueDesc( $fields[$x_i]->tag(),
245                         $subf[$i][0], $subf[$i][1], '', $tagslib, '', 'opac' );
246                 }
247                 $subfield_data{marc_subfield} = $subf[$i][0];
248                 $subfield_data{marc_tag}      = $fields[$x_i]->tag();
249                 push( @subfields_data, \%subfield_data );
250             }
251         }
252         if ( $#subfields_data >= 0 ) {
253             my %tag_data;
254             if (   ( $fields[$x_i]->tag() eq $fields[ $x_i - 1 ]->tag() )
255                 && ( C4::Context->preference('LabelMARCView') eq 'economical' )
256               )
257             {
258                 $tag_data{tag} = "";
259             }
260             else {
261                 if ( C4::Context->preference('hide_marc') ) {
262                     $tag_data{tag} = $tagslib->{ $fields[$x_i]->tag() }->{lib};
263                 }
264                 else {
265                     my $sf_def = $tagslib->{ $fields[$x_i]->tag() };
266                     my $lib;
267                     $lib = $sf_def->{lib} if defined($sf_def);
268                     $lib = $lib // '';
269                     $tag_data{tag} = $fields[$x_i]->tag() . ' '
270                       . C4::Koha::display_marc_indicators($fields[$x_i])
271                       . " - $lib";
272                 }
273             }
274             my @tmp = @subfields_data;
275             $tag_data{subfield} = \@tmp;
276             push( @loop_data, \%tag_data );
277             undef @subfields_data;
278         }
279     }
280     $template->param( "tab" . $tabloop . "XX" => \@loop_data );
281 }
282
283
284 # now, build item tab !
285 # the main difference is that datas are in lines and not in columns : thus, we build the <th> first, then the values...
286 # loop through each tag
287 # warning : we may have differents number of columns in each row. Thus, we first build a hash, complete it if necessary
288 # then construct template.
289 # $record has already had all the item fields filtered above.
290 my @fields = $record->fields();
291 my %witness
292   ; #---- stores the list of subfields used at least once, with the "meaning" of the code
293 my @item_subfield_codes;
294 my @item_loop;
295 foreach my $field (@fields) {
296     next if ( $field->tag() < 10 );
297     my @subf = $field->subfields;
298     my $item;
299
300     # loop through each subfield
301     for my $i ( 0 .. $#subf ) {
302         my $sf_def = $tagslib->{ $field->tag() }->{ $subf[$i][0] };
303         next if ( ($sf_def->{tab}||0) != 10 );
304         next if ( ($sf_def->{hidden}||0) > 0 );
305
306         push @item_subfield_codes, $subf[$i][0];
307         $witness{ $subf[$i][0] } = $sf_def->{lib};
308
309         # Allow repeatables (BZ 13574)
310         if( $item->{$subf[$i][0]} ) {
311             $item->{$subf[$i][0]} .= ' | ';
312         } else {
313             $item->{$subf[$i][0]} = q{};
314         }
315
316         if ( $sf_def->{isurl} ) {
317             $item->{ $subf[$i][0] } .= "<a href=\"$subf[$i][1]\">$subf[$i][1]</a>";
318         }
319         elsif ( $sf_def->{kohafield} eq "biblioitems.isbn" ) {
320             $item->{ $subf[$i][0] } .= $subf[$i][1];
321         }
322         else {
323             $item->{ $subf[$i][0] } .= GetAuthorisedValueDesc( $field->tag(), $subf[$i][0],
324                 $subf[$i][1], '', $tagslib, '', 'opac' ) // q{};
325         }
326
327         my $kohafield = $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{kohafield};
328         $item->{ $subf[$i][0] } = output_pref( { str => $item->{ $subf[$i][0] }, dateonly => 1 } )
329           if grep { $kohafield eq $_ }
330               qw( items.dateaccessioned items.onloan items.datelastseen items.datelastborrowed items.replacementpricedate );
331
332     }
333     push @item_loop, $item if $item;
334 }
335 my ( $holdingbrtagf, $holdingbrtagsubf ) =
336   &GetMarcFromKohaField( "items.holdingbranch" );
337 @item_loop =
338   sort { ($a->{$holdingbrtagsubf}||'') cmp ($b->{$holdingbrtagsubf}||'') } @item_loop;
339
340 @item_subfield_codes = uniq @item_subfield_codes;
341 # fill item info
342 my @item_header_loop;
343 for my $subfield_code ( @item_subfield_codes ) {
344     push @item_header_loop, $witness{$subfield_code};
345     for my $item_data ( @item_loop ) {
346         $item_data->{$subfield_code} ||= "&nbsp;"
347      }
348 }
349
350 if ( C4::Context->preference("OPACISBD") ) {
351     $template->param( ISBD => 1 );
352 }
353
354 #Search for title in links
355 my $marcflavour  = C4::Context->preference("marcflavour");
356 my $dat = TransformMarcToKoha( $record );
357 my $isbn = GetNormalizedISBN(undef,$record,$marcflavour);
358 my $marccontrolnumber   = GetMarcControlnumber ($record, $marcflavour);
359 my $marcissns = GetMarcISSN( $record, $marcflavour );
360 my $issn = $marcissns->[0] || '';
361
362 if (my $search_for_title = C4::Context->preference('OPACSearchForTitleIn')){
363     $dat->{title} =~ s/\/+$//; # remove trailing slash
364     $dat->{title} =~ s/\s+$//; # remove trailing space
365     $search_for_title = parametrized_url(
366         $search_for_title,
367         {
368             TITLE         => $dat->{title},
369             AUTHOR        => $dat->{author},
370             ISBN          => $isbn,
371             ISSN          => $issn,
372             CONTROLNUMBER => $marccontrolnumber,
373             BIBLIONUMBER  => $biblionumber,
374         }
375     );
376     $template->param('OPACSearchForTitleIn' => $search_for_title);
377 }
378
379 if( C4::Context->preference('ArticleRequests') ) {
380     my $itemtype = Koha::ItemTypes->find($biblio->itemtype);
381     my $artreqpossible = $patron
382         ? $biblio->can_article_request( $patron )
383         : $itemtype
384         ? $itemtype->may_article_request
385         : q{};
386     $template->param( artreqpossible => $artreqpossible );
387 }
388
389 $template->param(
390     item_loop           => \@item_loop,
391     item_header_loop    => \@item_header_loop,
392     item_subfield_codes => \@item_subfield_codes,
393     biblio              => $biblio,
394     norequests          => $norequests,
395 );
396
397 output_html_with_http_headers $query, $cookie, $template->output;