Bug 29697: Deal with the degraded view in detail.pl
[srvgit] / catalogue / MARCdetail.pl
1 #!/usr/bin/perl
2
3 # Copyright 2000-2002 Katipo Communications
4 # Copyright 2010 BibLibre
5 #
6 # This file is part of Koha.
7 #
8 # Koha is free software; you can redistribute it and/or modify it
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20
21 =head1 NAME
22
23 MARCdetail.pl : script to show a biblio in MARC format
24
25 =head1 SYNOPSIS
26
27 =cut
28
29 =head1 DESCRIPTION
30
31 This script needs a biblionumber as parameter
32
33 It shows the biblio in a (nice) MARC format depending on MARC
34 parameters tables.
35
36 The template is in <templates_dir>/catalogue/MARCdetail.tt.
37 this template must be divided into 11 "tabs".
38
39 The first 10 tabs present the biblio, the 11th one presents
40 the items attached to the biblio
41
42 =head1 FUNCTIONS
43
44 =cut
45
46 use Modern::Perl;
47 use CGI qw ( -utf8 );
48 use HTML::Entities;
49
50 use C4::Auth qw( get_template_and_user );
51 use C4::Context;
52 use C4::Output qw( output_html_with_http_headers );
53 use C4::Koha;
54 use C4::Biblio qw(
55     GetAuthorisedValueDesc
56     GetBiblioData
57     GetFrameworkCode
58     GetMarcFromKohaField
59     GetMarcStructure
60 );
61 use C4::Serials qw( CountSubscriptionFromBiblionumber GetSubscription GetSubscriptionsFromBiblionumber );
62 use C4::Search qw( z3950_search_args enabled_staff_search_views );
63
64 use Koha::Biblios;
65 use Koha::BiblioFrameworks;
66 use Koha::Patrons;
67 use Koha::DateUtils qw( output_pref );
68 use Koha::Virtualshelves;
69
70 use List::MoreUtils qw( uniq );
71
72 my $query        = CGI->new;
73 my $dbh          = C4::Context->dbh;
74 my $biblionumber = $query->param('biblionumber');
75 $biblionumber = HTML::Entities::encode($biblionumber);
76 my $frameworkcode = $query->param('frameworkcode') // GetFrameworkCode( $biblionumber );
77 my $popup        =
78   $query->param('popup')
79   ;    # if set to 1, then don't insert links, it's just to show the biblio
80 my $subscriptionid = $query->param('subscriptionid');
81
82 # open template
83 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
84     {
85         template_name   => "catalogue/MARCdetail.tt",
86         query           => $query,
87         type            => "intranet",
88         flagsrequired   => { catalogue => 1 },
89     }
90 );
91
92 my $biblio_object = Koha::Biblios->find( $biblionumber ); # FIXME Should replace $biblio
93 my $record = $biblio_object->metadata->record({ embed_items => 1 });
94
95 if ( not defined $record ) {
96     # biblionumber invalid -> report and exit
97     $template->param( unknownbiblionumber => 1,
98                 biblionumber => $biblionumber
99     );
100     output_html_with_http_headers $query, $cookie, $template->output;
101     exit;
102 }
103
104 my $tagslib = &GetMarcStructure(1,$frameworkcode);
105 my $biblio = GetBiblioData($biblionumber);
106
107 if($query->cookie("holdfor")){ 
108     my $holdfor_patron = Koha::Patrons->find( $query->cookie("holdfor") );
109     $template->param(
110         holdfor        => $query->cookie("holdfor"),
111         holdfor_patron => $holdfor_patron,
112     );
113 }
114
115 if( $query->cookie("searchToOrder") ){
116     my ( $basketno, $vendorid ) = split( /\//, $query->cookie("searchToOrder") );
117     $template->param(
118         searchtoorder_basketno => $basketno,
119         searchtoorder_vendorid => $vendorid
120     );
121 }
122
123 $template->param( ocoins => $biblio_object->get_coins );
124
125 #count of item linked
126 my $itemcount = $biblio_object->items->count;
127 $template->param( count => $itemcount,
128                                         bibliotitle => $biblio->{title}, );
129
130 my $frameworks = Koha::BiblioFrameworks->search( {}, { order_by => ['frameworktext'] } );
131 $template->param(
132     frameworks    => $frameworks,
133     frameworkcode => $frameworkcode,
134 );
135 # fill arrays
136 my @loop_data = ();
137
138 # loop through each tab 0 through 9
139 for ( my $tabloop = 0 ; $tabloop <= 10 ; $tabloop++ ) {
140
141     # loop through each tag
142     my @fields    = $record->fields();
143     my @loop_data = ();
144     my @subfields_data;
145
146     # deal with leader
147     unless ( $tagslib->{'000'}->{'@'}->{tab} ne $tabloop )
148     {    #  or ($tagslib->{'000'}->{'@'}->{hidden} =~ /-7|-4|-3|-2|2|3|5|8/ )) {
149         my %subfield_data;
150         $subfield_data{marc_lib}      = $tagslib->{'000'}->{'@'}->{lib};
151         $subfield_data{marc_value}    = $record->leader();
152         $subfield_data{marc_subfield} = '@';
153         $subfield_data{marc_tag}      = '000';
154         push( @subfields_data, \%subfield_data );
155         my %tag_data;
156         $tag_data{tag} = '000';
157         $tag_data{tag_desc} = $tagslib->{'000'}->{lib};
158         my @tmp = @subfields_data;
159         $tag_data{subfield} = \@tmp;
160         push( @loop_data, \%tag_data );
161         undef @subfields_data;
162     }
163     @fields = $record->fields();
164     for ( my $x_i = 0 ; $x_i <= $#fields ; $x_i++ ) {
165
166         # if tag <10, there's no subfield, use the "@" trick
167         if ( $fields[$x_i]->tag() < 10 ) {
168             next
169               if (
170                 $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{tab} ne $tabloop );
171             next if ( $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{hidden} =~ /-7|-4|-3|-2|2|3|5|8/);
172             my %subfield_data;
173             $subfield_data{marc_lib} =
174               $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{lib};
175             $subfield_data{marc_value}    = $fields[$x_i]->data();
176             $subfield_data{marc_subfield} = '@';
177             $subfield_data{marc_tag}      = $fields[$x_i]->tag();
178             push( @subfields_data, \%subfield_data );
179         }
180         else {
181             my @subf = $fields[$x_i]->subfields;
182
183             # loop through each subfield
184             for my $i ( 0 .. $#subf ) {
185                 $subf[$i][0] = "@" unless defined $subf[$i][0];
186                 next
187                   if (
188                     $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }->{tab}
189                     ne $tabloop );
190                 next
191                   if ( $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }
192                     ->{hidden} =~ /-7|-4|-3|-2|2|3|5|8/);
193                 my %subfield_data;
194                 $subfield_data{short_desc} = $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }->{lib};
195                 $subfield_data{long_desc} =
196                   $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }->{lib};
197                 $subfield_data{link} =
198                   $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }->{link};
199
200 #                 warn "tag : ".$tagslib->{$fields[$x_i]->tag()}." subfield :".$tagslib->{$fields[$x_i]->tag()}->{$subf[$i][0]}."lien koha? : "$subfield_data{link};
201                 if ( $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }
202                     ->{isurl} )
203                 {
204                     $subfield_data{marc_value} = $subf[$i][1];
205                                         $subfield_data{is_url} = 1;
206                 }
207                 elsif ( $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }
208                     ->{kohafield} eq "biblioitems.isbn" )
209                 {
210
211 #                    warn " tag : ".$tagslib->{$fields[$x_i]->tag()}." subfield :".$tagslib->{$fields[$x_i]->tag()}->{$subf[$i][0]}. "ISBN : ".$subf[$i][1]."PosttraitementISBN :".DisplayISBN($subf[$i][1]);
212                     $subfield_data{marc_value} = $subf[$i][1];
213                 }
214                 else {
215                     if ( $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }
216                         ->{authtypecode} )
217                     {
218                         $subfield_data{authority} = $fields[$x_i]->subfield(9);
219                     }
220                     $subfield_data{marc_value} =
221                       GetAuthorisedValueDesc( $fields[$x_i]->tag(),
222                         $subf[$i][0], $subf[$i][1], '', $tagslib) || $subf[$i][1];
223
224                 }
225                 $subfield_data{marc_subfield} = $subf[$i][0];
226                 $subfield_data{marc_tag}      = $fields[$x_i]->tag();
227                 push( @subfields_data, \%subfield_data );
228             }
229         }
230         if ( $#subfields_data == 0 ) {
231             $subfields_data[0]->{marc_lib}      = '';
232 #            $subfields_data[0]->{marc_subfield} = '';
233         }
234         if ( $#subfields_data >= 0) {
235             my %tag_data;
236             if ( $fields[$x_i]->tag() eq $fields[ $x_i - 1 ]->tag() && (C4::Context->preference('LabelMARCView') eq 'economical')) {
237                 $tag_data{tag} = "";
238             }
239             else {
240                 if ( C4::Context->preference('hide_marc') ) {
241                     $tag_data{tag} = $tagslib->{ $fields[$x_i]->tag() }->{lib};
242                 }
243                 else {
244                     $tag_data{tag} = $fields[$x_i]->tag();
245             $tag_data{tag_ind} = C4::Koha::display_marc_indicators($fields[$x_i]);
246             $tag_data{tag_desc} = $tagslib->{ $fields[$x_i]->tag() }->{lib};
247                 }
248             }
249             my @tmp = @subfields_data;
250             $tag_data{subfield} = \@tmp;
251             push( @loop_data, \%tag_data );
252             undef @subfields_data;
253         }
254     }
255     $template->param( "tab" . $tabloop . "XX" => \@loop_data );
256 }
257
258 # now, build item tab !
259 # the main difference is that datas are in lines and not in columns : thus, we build the <th> first, then the values...
260 # loop through each tag
261 # warning : we may have differents number of columns in each row. Thus, we first build a hash, complete it if necessary
262 # then construct template.
263 my @fields = $record->fields();
264 my %witness
265   ; #---- stores the list of subfields used at least once, with the "meaning" of the code
266 my @item_subfield_codes;
267 my @item_loop;
268
269 foreach my $field (@fields) {
270     next if ( $field->tag() < 10 );
271     my @subf = $field->subfields;
272     my $item;
273
274     # loop through each subfield
275     for my $i ( 0 .. $#subf ) {
276         next if ( $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{tab} ne 10 );
277         next if ( $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{hidden} =~ /-7|-4|-3|-2|2|3|5|8/);
278
279         push @item_subfield_codes, $subf[$i][0];
280         $witness{ $subf[$i][0] } =
281         $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{lib};
282
283         # Allow repeatables (BZ 13574)
284         if( $item->{$subf[$i][0]}) {
285             $item->{$subf[$i][0]} .= ' | ';
286         } else {
287             $item->{$subf[$i][0]} = q{};
288         }
289         if( $tagslib->{$field->tag()}->{$subf[$i][0]}->{isurl} ) {
290             $item->{$subf[$i][0]} .= "<a href=\"$subf[$i][1]\">$subf[$i][1]</a>";
291         } else {
292             $item->{ $subf[$i][0] } .= GetAuthorisedValueDesc( $field->tag(), $subf[$i][0], $subf[$i][1], '', $tagslib) || $subf[$i][1];
293         }
294
295         my $kohafield = $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{kohafield};
296         $item->{ $subf[$i][0] } = output_pref( { str => $item->{ $subf[$i][0] }, dateonly => 1 } )
297           if grep { $kohafield eq $_ }
298               qw( items.dateaccessioned items.onloan items.datelastseen items.datelastborrowed items.replacementpricedate );
299     }
300     push @item_loop, $item if $item;
301 }
302
303 my ($holdingbrtagf,$holdingbrtagsubf) = &GetMarcFromKohaField( "items.holdingbranch" );
304 @item_loop = sort {$a->{$holdingbrtagsubf} cmp $b->{$holdingbrtagsubf}} @item_loop;
305
306 @item_subfield_codes = uniq @item_subfield_codes;
307 # fill item info
308 my @item_header_loop;
309 for my $subfield_code ( @item_subfield_codes ) {
310     push @item_header_loop, $witness{$subfield_code};
311     for my $item_data ( @item_loop ) {
312         $item_data->{$subfield_code} ||= "&nbsp;"
313     }
314 }
315
316 my $subscriptionscount = CountSubscriptionFromBiblionumber($biblionumber);
317
318 if ($subscriptionscount) {
319     my $subscriptions = GetSubscriptionsFromBiblionumber($biblionumber);
320     my $subscriptiontitle = $subscriptions->[0]{'bibliotitle'};
321     $template->param(
322         subscriptiontitle   => $subscriptiontitle,
323         subscriptionsnumber => $subscriptionscount,
324     );
325 }
326
327 # get biblionumbers stored in the cart
328 my @cart_list;
329
330 if($query->cookie("intranet_bib_list")){
331     my $cart_list = $query->cookie("intranet_bib_list");
332     @cart_list = split(/\//, $cart_list);
333     if ( grep {$_ eq $biblionumber} @cart_list) {
334         $template->param( incart => 1 );
335     }
336 }
337
338 my $some_private_shelves = Koha::Virtualshelves->get_some_shelves(
339     {
340         borrowernumber => $loggedinuser,
341         add_allowed    => 1,
342         public         => 0,
343     }
344 );
345 my $some_public_shelves = Koha::Virtualshelves->get_some_shelves(
346     {
347         borrowernumber => $loggedinuser,
348         add_allowed    => 1,
349         public         => 1,
350     }
351 );
352
353
354 $template->param(
355     add_to_some_private_shelves => $some_private_shelves,
356     add_to_some_public_shelves  => $some_public_shelves,
357 );
358
359 $template->param (
360     item_loop               => \@item_loop,
361     item_header_loop        => \@item_header_loop,
362     item_subfield_codes     => \@item_subfield_codes,
363     biblionumber            => $biblionumber,
364     popup                   => $popup,
365     hide_marc               => C4::Context->preference('hide_marc'),
366         marcview => 1,
367         z3950_search_params             => C4::Search::z3950_search_args($biblio),
368         C4::Search::enabled_staff_search_views,
369     searchid                => scalar $query->param('searchid'),
370     biblio                  => $biblio_object,
371     loggedinuser => $loggedinuser,
372 );
373
374 $template->param( holdcount => $biblio_object->holds->count );
375
376 output_html_with_http_headers $query, $cookie, $template->output;