Merge remote-tracking branch 'kc/new/enh/bug_5917' into kcmaster
[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 under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
11 # version.
12 #
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License along
18 # with Koha; if not, write to the Free Software Foundation, Inc.,
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
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.tmpl.
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
49 use C4::Auth;
50 use C4::Context;
51 use C4::Output;
52 use CGI;
53 use C4::Koha;
54 use MARC::Record;
55 use C4::Biblio;
56 use C4::Items;
57 use C4::Acquisition;
58 use C4::Members; # to use GetMember
59 use C4::Serials;    #uses getsubscriptionsfrombiblionumber GetSubscriptionsFromBiblionumber
60 use C4::Search;         # enabled_staff_search_views
61
62
63 my $query        = new CGI;
64 my $dbh          = C4::Context->dbh;
65 my $biblionumber = $query->param('biblionumber');
66 my $frameworkcode = $query->param('frameworkcode');
67 $frameworkcode = GetFrameworkCode( $biblionumber ) unless ($frameworkcode);
68 my $popup        =
69   $query->param('popup')
70   ;    # if set to 1, then don't insert links, it's just to show the biblio
71 my $subscriptionid = $query->param('subscriptionid');
72
73 # open template
74 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
75     {
76         template_name   => "catalogue/MARCdetail.tmpl",
77         query           => $query,
78         type            => "intranet",
79         authnotrequired => 0,
80         flagsrequired   => { catalogue => 1 },
81         debug           => 1,
82     }
83 );
84
85 my $record = GetMarcBiblio($biblionumber);
86
87 if ( not defined $record ) {
88     # biblionumber invalid -> report and exit
89     $template->param( unknownbiblionumber => 1,
90                 biblionumber => $biblionumber
91     );
92     output_html_with_http_headers $query, $cookie, $template->output;
93     exit;
94 }
95
96 my $tagslib = &GetMarcStructure(1,$frameworkcode);
97 my $biblio = GetBiblioData($biblionumber);
98
99 if($query->cookie("holdfor")){ 
100     my $holdfor_patron = GetMember('borrowernumber' => $query->cookie("holdfor"));
101     $template->param(
102         holdfor => $query->cookie("holdfor"),
103         holdfor_surname => $holdfor_patron->{'surname'},
104         holdfor_firstname => $holdfor_patron->{'firstname'},
105         holdfor_cardnumber => $holdfor_patron->{'cardnumber'},
106     );
107 }
108
109 #count of item linked
110 my $itemcount = GetItemsCount($biblionumber);
111 $template->param( count => $itemcount,
112                                         bibliotitle => $biblio->{title}, );
113
114 # Getting the list of all frameworks
115 # get framework list
116 my $frameworks = getframeworks;
117 my @frameworkcodeloop;
118 foreach my $thisframeworkcode ( keys %$frameworks ) {
119     my %row = (
120         value         => $thisframeworkcode,
121         frameworktext => $frameworks->{$thisframeworkcode}->{'frameworktext'},
122     );
123     if ($frameworkcode eq $thisframeworkcode){
124         $row{'selected'}= 1;
125         }
126     push @frameworkcodeloop, \%row;
127 }
128 $template->param( frameworkcodeloop => \@frameworkcodeloop, );
129 # fill arrays
130 my @loop_data = ();
131 my $tag;
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 -' . $tagslib->{'000'}->{lib};
152         my @tmp = @subfields_data;
153         $tag_data{subfield} = \@tmp;
154         push( @loop_data, \%tag_data );
155         undef @subfields_data;
156     }
157     @fields = $record->fields();
158     for ( my $x_i = 0 ; $x_i <= $#fields ; $x_i++ ) {
159
160         # if tag <10, there's no subfield, use the "@" trick
161         if ( $fields[$x_i]->tag() < 10 ) {
162             next
163               if (
164                 $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{tab} ne $tabloop );
165             next if ( $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{hidden} =~ /-7|-4|-3|-2|2|3|5|8/);
166             my %subfield_data;
167             $subfield_data{marc_lib} =
168               $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{lib};
169             $subfield_data{marc_value}    = $fields[$x_i]->data();
170             $subfield_data{marc_subfield} = '@';
171             $subfield_data{marc_tag}      = $fields[$x_i]->tag();
172             push( @subfields_data, \%subfield_data );
173         }
174         else {
175             my @subf = $fields[$x_i]->subfields;
176
177             # loop through each subfield
178             for my $i ( 0 .. $#subf ) {
179                 $subf[$i][0] = "@" unless $subf[$i][0];
180                 next
181                   if (
182                     $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }->{tab}
183                     ne $tabloop );
184                 next
185                   if ( $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }
186                     ->{hidden} =~ /-7|-4|-3|-2|2|3|5|8/);
187                 my %subfield_data;
188                 $subfield_data{short_desc} = $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }->{lib};
189                 $subfield_data{long_desc} =
190                   $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }->{lib};
191                 $subfield_data{link} =
192                   $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }->{link};
193
194 #                 warn "tag : ".$tagslib->{$fields[$x_i]->tag()}." subfield :".$tagslib->{$fields[$x_i]->tag()}->{$subf[$i][0]}."lien koha? : "$subfield_data{link};
195                 if ( $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }
196                     ->{isurl} )
197                 {
198                     $subfield_data{marc_value} = $subf[$i][1];
199                                         $subfield_data{is_url} = 1;
200                 }
201                 elsif ( $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }
202                     ->{kohafield} eq "biblioitems.isbn" )
203                 {
204
205 #                    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]);
206                     $subfield_data{marc_value} = $subf[$i][1];
207                 }
208                 else {
209                     if ( $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }
210                         ->{authtypecode} )
211                     {
212                         $subfield_data{authority} = $fields[$x_i]->subfield(9);
213                     }
214                     $subfield_data{marc_value} =
215                       GetAuthorisedValueDesc( $fields[$x_i]->tag(),
216                         $subf[$i][0], $subf[$i][1], '', $tagslib) || $subf[$i][1];
217
218                 }
219                 $subfield_data{marc_subfield} = $subf[$i][0];
220                 $subfield_data{marc_tag}      = $fields[$x_i]->tag();
221                 push( @subfields_data, \%subfield_data );
222             }
223         }
224         if ( $#subfields_data == 0 ) {
225             $subfields_data[0]->{marc_lib}      = '';
226 #            $subfields_data[0]->{marc_subfield} = '';
227         }
228         if ( $#subfields_data >= 0) {
229             my %tag_data;
230             if ( $fields[$x_i]->tag() eq $fields[ $x_i - 1 ]->tag() && (C4::Context->preference('LabelMARCView') eq 'economical')) {
231                 $tag_data{tag} = "";
232             }
233             else {
234                 if ( C4::Context->preference('hide_marc') ) {
235                     $tag_data{tag} = $tagslib->{ $fields[$x_i]->tag() }->{lib};
236                 }
237                 else {
238                     $tag_data{tag} =
239                         $fields[$x_i]->tag() 
240                       . ' '
241                       . C4::Koha::display_marc_indicators($fields[$x_i])
242                       . ' - '
243                       . $tagslib->{ $fields[$x_i]->tag() }->{lib};
244                 }
245             }
246             my @tmp = @subfields_data;
247             $tag_data{subfield} = \@tmp;
248             push( @loop_data, \%tag_data );
249             undef @subfields_data;
250         }
251     }
252     $template->param( "tab" . $tabloop . "XX" => \@loop_data );
253 }
254
255 # now, build item tab !
256 # the main difference is that datas are in lines and not in columns : thus, we build the <th> first, then the values...
257 # loop through each tag
258 # warning : we may have differents number of columns in each row. Thus, we first build a hash, complete it if necessary
259 # then construct template.
260 my @fields = $record->fields();
261 my %witness
262   ; #---- stores the list of subfields used at least once, with the "meaning" of the code
263 my @big_array;
264 my $norequests = 1;
265 foreach my $field (@fields) {
266     next if ( $field->tag() < 10 );
267     my @subf = $field->subfields;
268     my %this_row;
269
270     # loop through each subfield
271     for my $i ( 0 .. $#subf ) {
272         next if ( $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{tab} ne 10 );
273         next if ( $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{hidden} =~ /-7|-4|-3|-2|2|3|5|8/);
274         $witness{ $subf[$i][0] } =
275         $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{lib};
276         $this_row{ $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     if (%this_row) {
281         push( @big_array, \%this_row );
282     }
283 }
284
285 my ($holdingbrtagf,$holdingbrtagsubf) = &GetMarcFromKohaField("items.holdingbranch",$frameworkcode);
286 @big_array = sort {$a->{$holdingbrtagsubf} cmp $b->{$holdingbrtagsubf}} @big_array;
287
288 #fill big_row with missing datas
289 foreach my $subfield_code ( keys(%witness) ) {
290     for ( my $i = 0 ; $i <= $#big_array ; $i++ ) {
291         $big_array[$i]{$subfield_code} = "&nbsp;"
292           unless ( $big_array[$i]{$subfield_code} );
293     }
294 }
295
296 # now, construct template !
297 my @item_value_loop;
298 my @header_value_loop;
299 for ( my $i = 0 ; $i <= $#big_array ; $i++ ) {
300     my $items_data;
301     foreach my $subfield_code ( keys(%witness) ) {
302         $items_data .= "<td>" . $big_array[$i]{$subfield_code} . "</td>";
303     }
304     my %row_data;
305     $row_data{item_value} = $items_data;
306     push( @item_value_loop, \%row_data );
307 }
308 foreach my $subfield_code ( keys(%witness) ) {
309     my %header_value;
310     $header_value{header_value} = $witness{$subfield_code};
311     push( @header_value_loop, \%header_value );
312 }
313
314 my $subscriptionscount = CountSubscriptionFromBiblionumber($biblionumber);
315
316 if ($subscriptionscount) {
317     my $subscriptions = GetSubscriptionsFromBiblionumber($biblionumber);
318     my $subscriptiontitle = $subscriptions->[0]{'bibliotitle'};
319     $template->param(
320         subscriptiontitle   => $subscriptiontitle,
321         subscriptionsnumber => $subscriptionscount,
322     );
323 }
324
325 $template->param (
326     norequests              => $norequests, 
327     item_loop               => \@item_value_loop,
328     item_header_loop        => \@header_value_loop,
329     biblionumber            => $biblionumber,
330     popup                   => $popup,
331     hide_marc               => C4::Context->preference('hide_marc'),
332         marcview => 1,
333         z3950_search_params             => C4::Search::z3950_search_args($biblio),
334         C4::Search::enabled_staff_search_views,
335 );
336
337 output_html_with_http_headers $query, $cookie, $template->output;