Merge remote branch 'kc/new/enh/bug_5462' into kcmaster
[koha.git] / catalogue / MARCdetail.pl
1 #!/usr/bin/perl
2
3 # Copyright 2000-2002 Katipo Communications
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20 =head1 NAME
21
22 MARCdetail.pl : script to show a biblio in MARC format
23
24 =head1 SYNOPSIS
25
26 =cut
27
28 =head1 DESCRIPTION
29
30 This script needs a biblionumber as parameter
31
32 It shows the biblio in a (nice) MARC format depending on MARC
33 parameters tables.
34
35 The template is in <templates_dir>/catalogue/MARCdetail.tmpl.
36 this template must be divided into 11 "tabs".
37
38 The first 10 tabs present the biblio, the 11th one presents
39 the items attached to the biblio
40
41 =head1 FUNCTIONS
42
43 =cut
44
45 use strict;
46 #use warnings; FIXME - Bug 2505
47
48 use C4::Auth;
49 use C4::Context;
50 use C4::Output;
51 use CGI;
52 use C4::Koha;
53 use MARC::Record;
54 use C4::Biblio;
55 use C4::Items;
56 use C4::Acquisition;
57 use C4::Serials;    #uses getsubscriptionsfrombiblionumber GetSubscriptionsFromBiblionumber
58 use C4::Search;         # enabled_staff_search_views
59
60
61 my $query        = new CGI;
62 my $dbh          = C4::Context->dbh;
63 my $biblionumber = $query->param('biblionumber');
64 my $frameworkcode = $query->param('frameworkcode');
65 $frameworkcode = GetFrameworkCode( $biblionumber ) unless ($frameworkcode);
66 my $popup        =
67   $query->param('popup')
68   ;    # if set to 1, then don't insert links, it's just to show the biblio
69 my $subscriptionid = $query->param('subscriptionid');
70
71 my $tagslib = &GetMarcStructure(1,$frameworkcode);
72
73 my $record = GetMarcBiblio($biblionumber);
74 my $biblio = GetBiblioData($biblionumber);
75 # open template
76 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
77     {
78         template_name   => "catalogue/MARCdetail.tmpl",
79         query           => $query,
80         type            => "intranet",
81         authnotrequired => 0,
82         flagsrequired   => { catalogue => 1 },
83         debug           => 1,
84     }
85 );
86
87 #count of item linked
88 my $itemcount = GetItemsCount($biblionumber);
89 $template->param( count => $itemcount,
90                                         bibliotitle => $biblio->{title}, );
91
92 # Getting the list of all frameworks
93 # get framework list
94 my $frameworks = getframeworks;
95 my @frameworkcodeloop;
96 foreach my $thisframeworkcode ( keys %$frameworks ) {
97     my %row = (
98         value         => $thisframeworkcode,
99         frameworktext => $frameworks->{$thisframeworkcode}->{'frameworktext'},
100     );
101     if ($frameworkcode eq $thisframeworkcode){
102         $row{'selected'}= 1;
103         }
104     push @frameworkcodeloop, \%row;
105 }
106 $template->param( frameworkcodeloop => \@frameworkcodeloop, );
107 # fill arrays
108 my @loop_data = ();
109 my $tag;
110
111 # loop through each tab 0 through 9
112 for ( my $tabloop = 0 ; $tabloop <= 10 ; $tabloop++ ) {
113
114     # loop through each tag
115     my @fields    = $record->fields();
116     my @loop_data = ();
117     my @subfields_data;
118
119     # deal with leader
120     unless ( $tagslib->{'000'}->{'@'}->{tab} ne $tabloop )
121     {    #  or ($tagslib->{'000'}->{'@'}->{hidden} =~ /-7|-4|-3|-2|2|3|5|8/ )) {
122         my %subfield_data;
123         $subfield_data{marc_lib}      = $tagslib->{'000'}->{'@'}->{lib};
124         $subfield_data{marc_value}    = $record->leader();
125         $subfield_data{marc_subfield} = '@';
126         $subfield_data{marc_tag}      = '000';
127         push( @subfields_data, \%subfield_data );
128         my %tag_data;
129         $tag_data{tag} = '000 -' . $tagslib->{'000'}->{lib};
130         my @tmp = @subfields_data;
131         $tag_data{subfield} = \@tmp;
132         push( @loop_data, \%tag_data );
133         undef @subfields_data;
134     }
135     @fields = $record->fields();
136     for ( my $x_i = 0 ; $x_i <= $#fields ; $x_i++ ) {
137
138         # if tag <10, there's no subfield, use the "@" trick
139         if ( $fields[$x_i]->tag() < 10 ) {
140             next
141               if (
142                 $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{tab} ne $tabloop );
143             next if ( $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{hidden} =~ /-7|-4|-3|-2|2|3|5|8/);
144             my %subfield_data;
145             $subfield_data{marc_lib} =
146               $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{lib};
147             $subfield_data{marc_value}    = $fields[$x_i]->data();
148             $subfield_data{marc_subfield} = '@';
149             $subfield_data{marc_tag}      = $fields[$x_i]->tag();
150             push( @subfields_data, \%subfield_data );
151         }
152         else {
153             my @subf = $fields[$x_i]->subfields;
154
155             # loop through each subfield
156             for my $i ( 0 .. $#subf ) {
157                 $subf[$i][0] = "@" unless $subf[$i][0];
158                 next
159                   if (
160                     $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }->{tab}
161                     ne $tabloop );
162                 next
163                   if ( $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }
164                     ->{hidden} =~ /-7|-4|-3|-2|2|3|5|8/);
165                 my %subfield_data;
166                 $subfield_data{short_desc} = $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }->{lib};
167                 $subfield_data{long_desc} =
168                   $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }->{lib};
169                 $subfield_data{link} =
170                   $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }->{link};
171
172 #                 warn "tag : ".$tagslib->{$fields[$x_i]->tag()}." subfield :".$tagslib->{$fields[$x_i]->tag()}->{$subf[$i][0]}."lien koha? : "$subfield_data{link};
173                 if ( $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }
174                     ->{isurl} )
175                 {
176                     $subfield_data{marc_value} = $subf[$i][1];
177                                         $subfield_data{is_url} = 1;
178                 }
179                 elsif ( $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }
180                     ->{kohafield} eq "biblioitems.isbn" )
181                 {
182
183 #                    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]);
184                     $subfield_data{marc_value} = $subf[$i][1];
185                 }
186                 else {
187                     if ( $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }
188                         ->{authtypecode} )
189                     {
190                         $subfield_data{authority} = $fields[$x_i]->subfield(9);
191                     }
192                     $subfield_data{marc_value} =
193                       GetAuthorisedValueDesc( $fields[$x_i]->tag(),
194                         $subf[$i][0], $subf[$i][1], '', $tagslib) || $subf[$i][1];
195
196                 }
197                 $subfield_data{marc_subfield} = $subf[$i][0];
198                 $subfield_data{marc_tag}      = $fields[$x_i]->tag();
199                 push( @subfields_data, \%subfield_data );
200             }
201         }
202         if ( $#subfields_data == 0 ) {
203             $subfields_data[0]->{marc_lib}      = '';
204 #            $subfields_data[0]->{marc_subfield} = '';
205         }
206         if ( $#subfields_data >= 0) {
207             my %tag_data;
208             if ( $fields[$x_i]->tag() eq $fields[ $x_i - 1 ]->tag() && (C4::Context->preference('LabelMARCView') eq 'economical')) {
209                 $tag_data{tag} = "";
210             }
211             else {
212                 if ( C4::Context->preference('hide_marc') ) {
213                     $tag_data{tag} = $tagslib->{ $fields[$x_i]->tag() }->{lib};
214                 }
215                 else {
216                     $tag_data{tag} =
217                         $fields[$x_i]->tag() 
218                       . ' '
219                       . C4::Koha::display_marc_indicators($fields[$x_i])
220                       . ' - '
221                       . $tagslib->{ $fields[$x_i]->tag() }->{lib};
222                 }
223             }
224             my @tmp = @subfields_data;
225             $tag_data{subfield} = \@tmp;
226             push( @loop_data, \%tag_data );
227             undef @subfields_data;
228         }
229     }
230     $template->param( "tab" . $tabloop . "XX" => \@loop_data );
231 }
232
233 # now, build item tab !
234 # the main difference is that datas are in lines and not in columns : thus, we build the <th> first, then the values...
235 # loop through each tag
236 # warning : we may have differents number of columns in each row. Thus, we first build a hash, complete it if necessary
237 # then construct template.
238 my @fields = $record->fields();
239 my %witness
240   ; #---- stores the list of subfields used at least once, with the "meaning" of the code
241 my @big_array;
242 my $norequests = 1;
243 foreach my $field (@fields) {
244     next if ( $field->tag() < 10 );
245     my @subf = $field->subfields;
246     my %this_row;
247
248     # loop through each subfield
249     for my $i ( 0 .. $#subf ) {
250         next if ( $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{tab} ne 10 );
251         next if ( $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{hidden} =~ /-7|-4|-3|-2|2|3|5|8/);
252         $witness{ $subf[$i][0] } =
253         $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{lib};
254         $this_row{ $subf[$i][0] } = GetAuthorisedValueDesc( $field->tag(),
255                         $subf[$i][0], $subf[$i][1], '', $tagslib) || $subf[$i][1];
256         $norequests = 0 if $subf[$i][1] ==0 and $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{kohafield} eq 'items.notforloan';
257     }
258     if (%this_row) {
259         push( @big_array, \%this_row );
260     }
261 }
262
263 my ($holdingbrtagf,$holdingbrtagsubf) = &GetMarcFromKohaField("items.holdingbranch",$frameworkcode);
264 @big_array = sort {$a->{$holdingbrtagsubf} cmp $b->{$holdingbrtagsubf}} @big_array;
265
266 #fill big_row with missing datas
267 foreach my $subfield_code ( keys(%witness) ) {
268     for ( my $i = 0 ; $i <= $#big_array ; $i++ ) {
269         $big_array[$i]{$subfield_code} = "&nbsp;"
270           unless ( $big_array[$i]{$subfield_code} );
271     }
272 }
273
274 # now, construct template !
275 my @item_value_loop;
276 my @header_value_loop;
277 for ( my $i = 0 ; $i <= $#big_array ; $i++ ) {
278     my $items_data;
279     foreach my $subfield_code ( keys(%witness) ) {
280         $items_data .= "<td>" . $big_array[$i]{$subfield_code} . "</td>";
281     }
282     my %row_data;
283     $row_data{item_value} = $items_data;
284     push( @item_value_loop, \%row_data );
285 }
286 foreach my $subfield_code ( keys(%witness) ) {
287     my %header_value;
288     $header_value{header_value} = $witness{$subfield_code};
289     push( @header_value_loop, \%header_value );
290 }
291
292 my $subscriptionscount = CountSubscriptionFromBiblionumber($biblionumber);
293
294 if ($subscriptionscount) {
295     my $subscriptions = GetSubscriptionsFromBiblionumber($biblionumber);
296     my $subscriptiontitle = $subscriptions->[0]{'bibliotitle'};
297     $template->param(
298         subscriptiontitle   => $subscriptiontitle,
299         subscriptionsnumber => $subscriptionscount,
300     );
301 }
302
303 $template->param (
304     norequests              => $norequests, 
305     item_loop               => \@item_value_loop,
306     item_header_loop        => \@header_value_loop,
307     biblionumber            => $biblionumber,
308     popup                   => $popup,
309     hide_marc               => C4::Context->preference('hide_marc'),
310         marcview => 1,
311         z3950_search_params             => C4::Search::z3950_search_args($biblio),
312         C4::Search::enabled_staff_search_views,
313 );
314
315 output_html_with_http_headers $query, $cookie, $template->output;