Bug 20968: (follow-up) Respond to feedback
[koha.git] / 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 strict;
47 #use warnings; FIXME - Bug 2505
48 use CGI qw ( -utf8 );
49 use HTML::Entities;
50
51 use C4::Auth;
52 use C4::Context;
53 use C4::Output;
54 use C4::Koha;
55 use MARC::Record;
56 use C4::Biblio;
57 use C4::Items;
58 use C4::Acquisition;
59 use C4::Serials;    #uses getsubscriptionsfrombiblionumber GetSubscriptionsFromBiblionumber
60 use C4::Search;         # enabled_staff_search_views
61
62 use Koha::Biblios;
63 use Koha::BiblioFrameworks;
64 use Koha::Patrons;
65
66 use List::MoreUtils qw( uniq );
67
68 my $query        = new CGI;
69 my $dbh          = C4::Context->dbh;
70 my $biblionumber = $query->param('biblionumber');
71 $biblionumber = HTML::Entities::encode($biblionumber);
72 my $frameworkcode = $query->param('frameworkcode');
73 $frameworkcode = GetFrameworkCode( $biblionumber ) unless ($frameworkcode);
74 my $popup        =
75   $query->param('popup')
76   ;    # if set to 1, then don't insert links, it's just to show the biblio
77 my $subscriptionid = $query->param('subscriptionid');
78
79 # open template
80 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
81     {
82         template_name   => "catalogue/MARCdetail.tt",
83         query           => $query,
84         type            => "intranet",
85         authnotrequired => 0,
86         flagsrequired   => { catalogue => 1 },
87         debug           => 1,
88     }
89 );
90
91 my $record = GetMarcBiblio({
92     biblionumber => $biblionumber,
93     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 $template->param( ocoins => GetCOinSBiblio($record) );
105
106 my $biblio_object = Koha::Biblios->find( $biblionumber ); # FIXME Should replace $biblio
107 my $tagslib = &GetMarcStructure(1,$frameworkcode);
108 my $biblio = GetBiblioData($biblionumber);
109
110 if($query->cookie("holdfor")){ 
111     my $holdfor_patron = Koha::Patrons->find( $query->cookie("holdfor") );
112     $template->param(
113         holdfor => $query->cookie("holdfor"),
114         holdfor_surname => $holdfor_patron->surname,
115         holdfor_firstname => $holdfor_patron->firstname,
116         holdfor_cardnumber => $holdfor_patron->cardnumber,
117     );
118 }
119
120 #count of item linked
121 my $itemcount = $biblio_object->items->count;
122 $template->param( count => $itemcount,
123                                         bibliotitle => $biblio->{title}, );
124
125 my $frameworks = Koha::BiblioFrameworks->search( {}, { order_by => ['frameworktext'] } );
126 $template->param(
127     frameworks    => $frameworks,
128     frameworkcode => $frameworkcode,
129 );
130 # fill arrays
131 my @loop_data = ();
132
133 # loop through each tab 0 through 9
134 for ( my $tabloop = 0 ; $tabloop <= 10 ; $tabloop++ ) {
135
136     # loop through each tag
137     my @fields    = $record->fields();
138     my @loop_data = ();
139     my @subfields_data;
140
141     # deal with leader
142     unless ( $tagslib->{'000'}->{'@'}->{tab} ne $tabloop )
143     {    #  or ($tagslib->{'000'}->{'@'}->{hidden} =~ /-7|-4|-3|-2|2|3|5|8/ )) {
144         my %subfield_data;
145         $subfield_data{marc_lib}      = $tagslib->{'000'}->{'@'}->{lib};
146         $subfield_data{marc_value}    = $record->leader();
147         $subfield_data{marc_subfield} = '@';
148         $subfield_data{marc_tag}      = '000';
149         push( @subfields_data, \%subfield_data );
150         my %tag_data;
151         $tag_data{tag} = '000';
152         $tag_data{tag_desc} = $tagslib->{'000'}->{lib};
153         my @tmp = @subfields_data;
154         $tag_data{subfield} = \@tmp;
155         push( @loop_data, \%tag_data );
156         undef @subfields_data;
157     }
158     @fields = $record->fields();
159     for ( my $x_i = 0 ; $x_i <= $#fields ; $x_i++ ) {
160
161         # if tag <10, there's no subfield, use the "@" trick
162         if ( $fields[$x_i]->tag() < 10 ) {
163             next
164               if (
165                 $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{tab} ne $tabloop );
166             next if ( $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{hidden} =~ /-7|-4|-3|-2|2|3|5|8/);
167             my %subfield_data;
168             $subfield_data{marc_lib} =
169               $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{lib};
170             $subfield_data{marc_value}    = $fields[$x_i]->data();
171             $subfield_data{marc_subfield} = '@';
172             $subfield_data{marc_tag}      = $fields[$x_i]->tag();
173             push( @subfields_data, \%subfield_data );
174         }
175         else {
176             my @subf = $fields[$x_i]->subfields;
177
178             # loop through each subfield
179             for my $i ( 0 .. $#subf ) {
180                 $subf[$i][0] = "@" unless defined $subf[$i][0];
181                 next
182                   if (
183                     $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }->{tab}
184                     ne $tabloop );
185                 next
186                   if ( $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }
187                     ->{hidden} =~ /-7|-4|-3|-2|2|3|5|8/);
188                 my %subfield_data;
189                 $subfield_data{short_desc} = $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }->{lib};
190                 $subfield_data{long_desc} =
191                   $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }->{lib};
192                 $subfield_data{link} =
193                   $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }->{link};
194
195 #                 warn "tag : ".$tagslib->{$fields[$x_i]->tag()}." subfield :".$tagslib->{$fields[$x_i]->tag()}->{$subf[$i][0]}."lien koha? : "$subfield_data{link};
196                 if ( $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }
197                     ->{isurl} )
198                 {
199                     $subfield_data{marc_value} = $subf[$i][1];
200                                         $subfield_data{is_url} = 1;
201                 }
202                 elsif ( $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }
203                     ->{kohafield} eq "biblioitems.isbn" )
204                 {
205
206 #                    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]);
207                     $subfield_data{marc_value} = $subf[$i][1];
208                 }
209                 else {
210                     if ( $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }
211                         ->{authtypecode} )
212                     {
213                         $subfield_data{authority} = $fields[$x_i]->subfield(9);
214                     }
215                     $subfield_data{marc_value} =
216                       GetAuthorisedValueDesc( $fields[$x_i]->tag(),
217                         $subf[$i][0], $subf[$i][1], '', $tagslib) || $subf[$i][1];
218
219                 }
220                 $subfield_data{marc_subfield} = $subf[$i][0];
221                 $subfield_data{marc_tag}      = $fields[$x_i]->tag();
222                 push( @subfields_data, \%subfield_data );
223             }
224         }
225         if ( $#subfields_data == 0 ) {
226             $subfields_data[0]->{marc_lib}      = '';
227 #            $subfields_data[0]->{marc_subfield} = '';
228         }
229         if ( $#subfields_data >= 0) {
230             my %tag_data;
231             if ( $fields[$x_i]->tag() eq $fields[ $x_i - 1 ]->tag() && (C4::Context->preference('LabelMARCView') eq 'economical')) {
232                 $tag_data{tag} = "";
233             }
234             else {
235                 if ( C4::Context->preference('hide_marc') ) {
236                     $tag_data{tag} = $tagslib->{ $fields[$x_i]->tag() }->{lib};
237                 }
238                 else {
239                     $tag_data{tag} = $fields[$x_i]->tag();
240             $tag_data{tag_ind} = C4::Koha::display_marc_indicators($fields[$x_i]);
241             $tag_data{tag_desc} = $tagslib->{ $fields[$x_i]->tag() }->{lib};
242                 }
243             }
244             my @tmp = @subfields_data;
245             $tag_data{subfield} = \@tmp;
246             push( @loop_data, \%tag_data );
247             undef @subfields_data;
248         }
249     }
250     $template->param( "tab" . $tabloop . "XX" => \@loop_data );
251 }
252
253 # now, build item tab !
254 # the main difference is that datas are in lines and not in columns : thus, we build the <th> first, then the values...
255 # loop through each tag
256 # warning : we may have differents number of columns in each row. Thus, we first build a hash, complete it if necessary
257 # then construct template.
258 my @fields = $record->fields();
259 my %witness
260   ; #---- stores the list of subfields used at least once, with the "meaning" of the code
261 my @item_subfield_codes;
262 my @item_loop;
263 my $norequests = 1;
264 foreach my $field (@fields) {
265     next if ( $field->tag() < 10 );
266     my @subf = $field->subfields;
267     my $item;
268
269     # loop through each subfield
270     for my $i ( 0 .. $#subf ) {
271         next if ( $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{tab} ne 10 );
272         next if ( $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{hidden} =~ /-7|-4|-3|-2|2|3|5|8/);
273         push @item_subfield_codes, $subf[$i][0];
274         $witness{ $subf[$i][0] } =
275         $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{lib};
276         $item->{ $subf[$i][0] } = GetAuthorisedValueDesc( $field->tag(),
277                         $subf[$i][0], $subf[$i][1], '', $tagslib) || $subf[$i][1];
278         $norequests = 0 if $subf[$i][1] ==0 and $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{kohafield} eq 'items.notforloan';
279     }
280     push @item_loop, $item if $item;
281 }
282
283 my ($holdingbrtagf,$holdingbrtagsubf) = &GetMarcFromKohaField("items.holdingbranch",$frameworkcode);
284 @item_loop = sort {$a->{$holdingbrtagsubf} cmp $b->{$holdingbrtagsubf}} @item_loop;
285
286 @item_subfield_codes = uniq @item_subfield_codes;
287 # fill item info
288 my @item_header_loop;
289 for my $subfield_code ( @item_subfield_codes ) {
290     push @item_header_loop, $witness{$subfield_code};
291     for my $item_data ( @item_loop ) {
292         $item_data->{$subfield_code} ||= "&nbsp;"
293     }
294 }
295
296 my $subscriptionscount = CountSubscriptionFromBiblionumber($biblionumber);
297
298 if ($subscriptionscount) {
299     my $subscriptions = GetSubscriptionsFromBiblionumber($biblionumber);
300     my $subscriptiontitle = $subscriptions->[0]{'bibliotitle'};
301     $template->param(
302         subscriptiontitle   => $subscriptiontitle,
303         subscriptionsnumber => $subscriptionscount,
304     );
305 }
306
307 $template->param (
308     norequests              => $norequests,
309     item_loop               => \@item_loop,
310     item_header_loop        => \@item_header_loop,
311     item_subfield_codes     => \@item_subfield_codes,
312     biblionumber            => $biblionumber,
313     popup                   => $popup,
314     hide_marc               => C4::Context->preference('hide_marc'),
315         marcview => 1,
316         z3950_search_params             => C4::Search::z3950_search_args($biblio),
317         C4::Search::enabled_staff_search_views,
318     searchid            => scalar $query->param('searchid'),
319 );
320
321 my @allorders_using_biblio = GetOrdersByBiblionumber ($biblionumber);
322 my @deletedorders_using_biblio;
323 my @orders_using_biblio;
324 my @baskets_orders;
325 my @baskets_deletedorders;
326
327 foreach my $myorder (@allorders_using_biblio) {
328     my $basket = $myorder->{'basketno'};
329     if ((defined $myorder->{'datecancellationprinted'}) and  ($myorder->{'datecancellationprinted'} ne '0000-00-00') ){
330         push @deletedorders_using_biblio, $myorder;
331         unless (grep(/^$basket$/, @baskets_deletedorders)){
332             push @baskets_deletedorders,$myorder->{'basketno'};
333         }
334     }
335     else {
336         push @orders_using_biblio, $myorder;
337         unless (grep(/^$basket$/, @baskets_orders)){
338             push @baskets_orders,$myorder->{'basketno'};
339             }
340     }
341 }
342
343 my $count_orders_using_biblio = scalar @orders_using_biblio ;
344 $template->param (countorders => $count_orders_using_biblio);
345
346 my $count_deletedorders_using_biblio = scalar @deletedorders_using_biblio ;
347 $template->param (countdeletedorders => $count_deletedorders_using_biblio);
348
349 $biblio = Koha::Biblios->find( $biblionumber );
350 my $holds = $biblio->holds;
351 $template->param( holdcount => $holds->count );
352
353 output_html_with_http_headers $query, $cookie, $template->output;