Merge remote branch 'kc/new/enh/bug_4449' 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 my $tagslib = &GetMarcStructure(1,$frameworkcode);
74
75 my $record = GetMarcBiblio($biblionumber);
76 my $biblio = GetBiblioData($biblionumber);
77 # open template
78 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
79     {
80         template_name   => "catalogue/MARCdetail.tmpl",
81         query           => $query,
82         type            => "intranet",
83         authnotrequired => 0,
84         flagsrequired   => { catalogue => 1 },
85         debug           => 1,
86     }
87 );
88
89 if($query->cookie("holdfor")){ 
90     my $holdfor_patron = GetMember('borrowernumber' => $query->cookie("holdfor"));
91     $template->param(
92         holdfor => $query->cookie("holdfor"),
93         holdfor_surname => $holdfor_patron->{'surname'},
94         holdfor_firstname => $holdfor_patron->{'firstname'},
95         holdfor_cardnumber => $holdfor_patron->{'cardnumber'},
96     );
97 }
98
99 #count of item linked
100 my $itemcount = GetItemsCount($biblionumber);
101 $template->param( count => $itemcount,
102                                         bibliotitle => $biblio->{title}, );
103
104 # Getting the list of all frameworks
105 # get framework list
106 my $frameworks = getframeworks;
107 my @frameworkcodeloop;
108 foreach my $thisframeworkcode ( keys %$frameworks ) {
109     my %row = (
110         value         => $thisframeworkcode,
111         frameworktext => $frameworks->{$thisframeworkcode}->{'frameworktext'},
112     );
113     if ($frameworkcode eq $thisframeworkcode){
114         $row{'selected'}= 1;
115         }
116     push @frameworkcodeloop, \%row;
117 }
118 $template->param( frameworkcodeloop => \@frameworkcodeloop, );
119 # fill arrays
120 my @loop_data = ();
121 my $tag;
122
123 # loop through each tab 0 through 9
124 for ( my $tabloop = 0 ; $tabloop <= 10 ; $tabloop++ ) {
125
126     # loop through each tag
127     my @fields    = $record->fields();
128     my @loop_data = ();
129     my @subfields_data;
130
131     # deal with leader
132     unless ( $tagslib->{'000'}->{'@'}->{tab} ne $tabloop )
133     {    #  or ($tagslib->{'000'}->{'@'}->{hidden} =~ /-7|-4|-3|-2|2|3|5|8/ )) {
134         my %subfield_data;
135         $subfield_data{marc_lib}      = $tagslib->{'000'}->{'@'}->{lib};
136         $subfield_data{marc_value}    = $record->leader();
137         $subfield_data{marc_subfield} = '@';
138         $subfield_data{marc_tag}      = '000';
139         push( @subfields_data, \%subfield_data );
140         my %tag_data;
141         $tag_data{tag} = '000 -' . $tagslib->{'000'}->{lib};
142         my @tmp = @subfields_data;
143         $tag_data{subfield} = \@tmp;
144         push( @loop_data, \%tag_data );
145         undef @subfields_data;
146     }
147     @fields = $record->fields();
148     for ( my $x_i = 0 ; $x_i <= $#fields ; $x_i++ ) {
149
150         # if tag <10, there's no subfield, use the "@" trick
151         if ( $fields[$x_i]->tag() < 10 ) {
152             next
153               if (
154                 $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{tab} ne $tabloop );
155             next if ( $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{hidden} =~ /-7|-4|-3|-2|2|3|5|8/);
156             my %subfield_data;
157             $subfield_data{marc_lib} =
158               $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{lib};
159             $subfield_data{marc_value}    = $fields[$x_i]->data();
160             $subfield_data{marc_subfield} = '@';
161             $subfield_data{marc_tag}      = $fields[$x_i]->tag();
162             push( @subfields_data, \%subfield_data );
163         }
164         else {
165             my @subf = $fields[$x_i]->subfields;
166
167             # loop through each subfield
168             for my $i ( 0 .. $#subf ) {
169                 $subf[$i][0] = "@" unless $subf[$i][0];
170                 next
171                   if (
172                     $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }->{tab}
173                     ne $tabloop );
174                 next
175                   if ( $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }
176                     ->{hidden} =~ /-7|-4|-3|-2|2|3|5|8/);
177                 my %subfield_data;
178                 $subfield_data{short_desc} = $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }->{lib};
179                 $subfield_data{long_desc} =
180                   $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }->{lib};
181                 $subfield_data{link} =
182                   $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }->{link};
183
184 #                 warn "tag : ".$tagslib->{$fields[$x_i]->tag()}." subfield :".$tagslib->{$fields[$x_i]->tag()}->{$subf[$i][0]}."lien koha? : "$subfield_data{link};
185                 if ( $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }
186                     ->{isurl} )
187                 {
188                     $subfield_data{marc_value} = $subf[$i][1];
189                                         $subfield_data{is_url} = 1;
190                 }
191                 elsif ( $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }
192                     ->{kohafield} eq "biblioitems.isbn" )
193                 {
194
195 #                    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]);
196                     $subfield_data{marc_value} = $subf[$i][1];
197                 }
198                 else {
199                     if ( $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }
200                         ->{authtypecode} )
201                     {
202                         $subfield_data{authority} = $fields[$x_i]->subfield(9);
203                     }
204                     $subfield_data{marc_value} =
205                       GetAuthorisedValueDesc( $fields[$x_i]->tag(),
206                         $subf[$i][0], $subf[$i][1], '', $tagslib) || $subf[$i][1];
207
208                 }
209                 $subfield_data{marc_subfield} = $subf[$i][0];
210                 $subfield_data{marc_tag}      = $fields[$x_i]->tag();
211                 push( @subfields_data, \%subfield_data );
212             }
213         }
214         if ( $#subfields_data == 0 ) {
215             $subfields_data[0]->{marc_lib}      = '';
216 #            $subfields_data[0]->{marc_subfield} = '';
217         }
218         if ( $#subfields_data >= 0) {
219             my %tag_data;
220             if ( $fields[$x_i]->tag() eq $fields[ $x_i - 1 ]->tag() && (C4::Context->preference('LabelMARCView') eq 'economical')) {
221                 $tag_data{tag} = "";
222             }
223             else {
224                 if ( C4::Context->preference('hide_marc') ) {
225                     $tag_data{tag} = $tagslib->{ $fields[$x_i]->tag() }->{lib};
226                 }
227                 else {
228                     $tag_data{tag} =
229                         $fields[$x_i]->tag() 
230                       . ' '
231                       . C4::Koha::display_marc_indicators($fields[$x_i])
232                       . ' - '
233                       . $tagslib->{ $fields[$x_i]->tag() }->{lib};
234                 }
235             }
236             my @tmp = @subfields_data;
237             $tag_data{subfield} = \@tmp;
238             push( @loop_data, \%tag_data );
239             undef @subfields_data;
240         }
241     }
242     $template->param( "tab" . $tabloop . "XX" => \@loop_data );
243 }
244
245 # now, build item tab !
246 # the main difference is that datas are in lines and not in columns : thus, we build the <th> first, then the values...
247 # loop through each tag
248 # warning : we may have differents number of columns in each row. Thus, we first build a hash, complete it if necessary
249 # then construct template.
250 my @fields = $record->fields();
251 my %witness
252   ; #---- stores the list of subfields used at least once, with the "meaning" of the code
253 my @big_array;
254 my $norequests = 1;
255 foreach my $field (@fields) {
256     next if ( $field->tag() < 10 );
257     my @subf = $field->subfields;
258     my %this_row;
259
260     # loop through each subfield
261     for my $i ( 0 .. $#subf ) {
262         next if ( $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{tab} ne 10 );
263         next if ( $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{hidden} =~ /-7|-4|-3|-2|2|3|5|8/);
264         $witness{ $subf[$i][0] } =
265         $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{lib};
266         $this_row{ $subf[$i][0] } = GetAuthorisedValueDesc( $field->tag(),
267                         $subf[$i][0], $subf[$i][1], '', $tagslib) || $subf[$i][1];
268         $norequests = 0 if $subf[$i][1] ==0 and $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{kohafield} eq 'items.notforloan';
269     }
270     if (%this_row) {
271         push( @big_array, \%this_row );
272     }
273 }
274
275 my ($holdingbrtagf,$holdingbrtagsubf) = &GetMarcFromKohaField("items.holdingbranch",$frameworkcode);
276 @big_array = sort {$a->{$holdingbrtagsubf} cmp $b->{$holdingbrtagsubf}} @big_array;
277
278 #fill big_row with missing datas
279 foreach my $subfield_code ( keys(%witness) ) {
280     for ( my $i = 0 ; $i <= $#big_array ; $i++ ) {
281         $big_array[$i]{$subfield_code} = "&nbsp;"
282           unless ( $big_array[$i]{$subfield_code} );
283     }
284 }
285
286 # now, construct template !
287 my @item_value_loop;
288 my @header_value_loop;
289 for ( my $i = 0 ; $i <= $#big_array ; $i++ ) {
290     my $items_data;
291     foreach my $subfield_code ( keys(%witness) ) {
292         $items_data .= "<td>" . $big_array[$i]{$subfield_code} . "</td>";
293     }
294     my %row_data;
295     $row_data{item_value} = $items_data;
296     push( @item_value_loop, \%row_data );
297 }
298 foreach my $subfield_code ( keys(%witness) ) {
299     my %header_value;
300     $header_value{header_value} = $witness{$subfield_code};
301     push( @header_value_loop, \%header_value );
302 }
303
304 my $subscriptionscount = CountSubscriptionFromBiblionumber($biblionumber);
305
306 if ($subscriptionscount) {
307     my $subscriptions = GetSubscriptionsFromBiblionumber($biblionumber);
308     my $subscriptiontitle = $subscriptions->[0]{'bibliotitle'};
309     $template->param(
310         subscriptiontitle   => $subscriptiontitle,
311         subscriptionsnumber => $subscriptionscount,
312     );
313 }
314
315 $template->param (
316     norequests              => $norequests, 
317     item_loop               => \@item_value_loop,
318     item_header_loop        => \@header_value_loop,
319     biblionumber            => $biblionumber,
320     popup                   => $popup,
321     hide_marc               => C4::Context->preference('hide_marc'),
322         marcview => 1,
323         z3950_search_params             => C4::Search::z3950_search_args($biblio),
324         C4::Search::enabled_staff_search_views,
325 );
326
327 output_html_with_http_headers $query, $cookie, $template->output;