BUGFIX for : NoZebra
[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 with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA  02111-1307 USA
19
20 =head1 NAME
21
22 MARCdetail.pl : script to show a biblio in MARC format
23
24 =head1 SYNOPSIS
25
26
27 =head1 DESCRIPTION
28
29 This script needs a biblionumber as parameter
30
31 It shows the biblio in a (nice) MARC format depending on MARC
32 parameters tables.
33
34 The template is in <templates_dir>/catalogue/MARCdetail.tmpl.
35 this template must be divided into 11 "tabs".
36
37 The first 10 tabs present the biblio, the 11th one presents
38 the items attached to the biblio
39
40 =head1 FUNCTIONS
41
42 =over 2
43
44 =cut
45
46 use strict;
47 require Exporter;
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::Acquisition;
56 use C4::Serials;    #uses getsubscriptionsfrombiblionumber GetSubscriptionsFromBiblionumber
57
58 #---- Internal function ---
59 sub get_authorised_value_desc ($$$$$$) {
60     my ( $tagslib, $tag, $subfield, $value, $framework, $dbh ) = @_;
61
62     #---- branch
63     if ( $tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "branches" ) {
64
65         #       return GetBranchDetail($value)->{branchname};
66     }
67
68     #---- itemtypes
69     if ( $tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "itemtypes" ) {
70
71         #          my $itemtypedef = getitemtypeinfo($itemtype);
72         #      return $itemtypedef->{description};
73     }
74
75     #---- "true" authorized value
76     my $category = $tagslib->{$tag}->{$subfield}->{'authorised_value'};
77
78     if ( $category ne "" ) {
79         my $sth =
80           $dbh->prepare(
81 "select lib from authorised_values where category = ? and authorised_value = ?"
82           );
83         $sth->execute( $category, $value );
84         my $data = $sth->fetchrow_hashref;
85         return $data->{'lib'};
86     }
87     else {
88         return $value;    # if nothing is found return the original value
89     }
90 }
91
92 #---------
93
94 my $query        = new CGI;
95 my $dbh          = C4::Context->dbh;
96 my $biblionumber = $query->param('biblionumber');
97 my $frameworkcode = GetFrameworkCode( $biblionumber );
98 my $popup        =
99   $query->param('popup')
100   ;    # if set to 1, then don't insert links, it's just to show the biblio
101 my $subscriptionid = $query->param('subscriptionid');
102
103 my $tagslib = &GetMarcStructure($dbh,1,$frameworkcode);
104
105 my $record = GetMarcBiblio($biblionumber);
106
107 # open template
108 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
109     {
110         template_name   => "catalogue/MARCdetail.tmpl",
111         query           => $query,
112         type            => "intranet",
113         authnotrequired => 0,
114         flagsrequired   => { catalogue => 1 },
115         debug           => 1,
116     }
117 );
118
119 #Getting the list of all frameworks
120 my $queryfwk =
121   $dbh->prepare("select frameworktext, frameworkcode from biblio_framework");
122 $queryfwk->execute;
123 my %select_fwk;
124 my @select_fwk;
125 my $curfwk;
126 push @select_fwk, "Default";
127 $select_fwk{"Default"} = "Default";
128
129 while ( my ( $description, $fwk ) = $queryfwk->fetchrow ) {
130     push @select_fwk, $fwk;
131     $select_fwk{$fwk} = $description;
132 }
133 $curfwk=$frameworkcode;
134 my $framework=CGI::scrolling_list( -name     => 'Frameworks',
135             -id => 'Frameworks',
136             -default => $curfwk,
137             -OnChange => 'Changefwk(this);',
138             -values   => \@select_fwk,
139             -labels   => \%select_fwk,
140             -size     => 1,
141             -multiple => 0 );
142 $template->param(framework => $framework);
143 # fill arrays
144 my @loop_data = ();
145 my $tag;
146
147 # loop through each tab 0 through 9
148 for ( my $tabloop = 0 ; $tabloop <= 10 ; $tabloop++ ) {
149
150     # loop through each tag
151     my @fields    = $record->fields();
152     my @loop_data = ();
153     my @subfields_data;
154
155     # deal with leader
156     unless ( $tagslib->{'000'}->{'@'}->{tab} ne $tabloop )
157     {    #  or ($tagslib->{'000'}->{'@'}->{hidden}==(-7|-4|-3|-2|2|3|5|8))) {
158         my %subfield_data;
159         $subfield_data{marc_lib}      = $tagslib->{'000'}->{'@'}->{lib};
160         $subfield_data{marc_value}    = $record->leader();
161         $subfield_data{marc_subfield} = '@';
162         $subfield_data{marc_tag}      = '000';
163         push( @subfields_data, \%subfield_data );
164         my %tag_data;
165         $tag_data{tag} = '000 -' . $tagslib->{'000'}->{lib};
166         my @tmp = @subfields_data;
167         $tag_data{subfield} = \@tmp;
168         push( @loop_data, \%tag_data );
169         undef @subfields_data;
170     }
171     @fields = $record->fields();
172     for ( my $x_i = 0 ; $x_i <= $#fields ; $x_i++ ) {
173
174         # if tag <10, there's no subfield, use the "@" trick
175         if ( $fields[$x_i]->tag() < 10 ) {
176             next
177               if (
178                 $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{tab} ne $tabloop );
179             next if ( $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{hidden} );
180             my %subfield_data;
181             $subfield_data{marc_lib} =
182               $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{lib};
183             $subfield_data{marc_value}    = $fields[$x_i]->data();
184             $subfield_data{marc_subfield} = '@';
185             $subfield_data{marc_tag}      = $fields[$x_i]->tag();
186             push( @subfields_data, \%subfield_data );
187         }
188         else {
189             my @subf = $fields[$x_i]->subfields;
190
191             # loop through each subfield
192             for my $i ( 0 .. $#subf ) {
193                 $subf[$i][0] = "@" unless $subf[$i][0];
194                 next
195                   if (
196                     $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }->{tab}
197                     ne $tabloop );
198                 next
199                   if ( $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }
200                     ->{hidden} );
201                 my %subfield_data;
202                 $subfield_data{short_desc} = substr(
203                     $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }->{lib},
204                     0, 20
205                 );
206                 $subfield_data{long_desc} =
207                   $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }->{lib};
208                 $subfield_data{link} =
209                   $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }->{link};
210
211 #                 warn "tag : ".$tagslib->{$fields[$x_i]->tag()}." subfield :".$tagslib->{$fields[$x_i]->tag()}->{$subf[$i][0]}."lien koha? : "$subfield_data{link};
212                 if ( $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }
213                     ->{isurl} )
214                 {
215                     $subfield_data{marc_value} =
216                       "<a href=\"$subf[$i][1]\">$subf[$i][1]</a>";
217                 }
218                 elsif ( $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }
219                     ->{kohafield} eq "biblioitems.isbn" )
220                 {
221
222 #                    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]);
223                     $subfield_data{marc_value} = DisplayISBN( $subf[$i][1] );
224                 }
225                 else {
226                     if ( $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }
227                         ->{authtypecode} )
228                     {
229                         $subfield_data{authority} = $fields[$x_i]->subfield(9);
230                     }
231                     $subfield_data{marc_value} =
232                       get_authorised_value_desc( $tagslib, $fields[$x_i]->tag(),
233                         $subf[$i][0], $subf[$i][1], '', $dbh );
234                 }
235                 $subfield_data{marc_subfield} = $subf[$i][0];
236                 $subfield_data{marc_tag}      = $fields[$x_i]->tag();
237                 push( @subfields_data, \%subfield_data );
238             }
239         }
240         if ( $#subfields_data == 0 ) {
241             $subfields_data[0]->{marc_lib}      = '';
242             $subfields_data[0]->{marc_subfield} = '';
243         }
244         if ( $#subfields_data >= 0 ) {
245             my %tag_data;
246             if ( $fields[$x_i]->tag() eq $fields[ $x_i - 1 ]->tag() ) {
247                 $tag_data{tag} = "";
248             }
249             else {
250                 if ( C4::Context->preference('hide_marc') ) {
251                     $tag_data{tag} = $tagslib->{ $fields[$x_i]->tag() }->{lib};
252                 }
253                 else {
254                     $tag_data{tag} =
255                         $fields[$x_i]->tag() . ' -'
256                       . $tagslib->{ $fields[$x_i]->tag() }->{lib};
257                 }
258             }
259             my @tmp = @subfields_data;
260             $tag_data{subfield} = \@tmp;
261             push( @loop_data, \%tag_data );
262             undef @subfields_data;
263         }
264     }
265     $template->param( $tabloop . "XX" => \@loop_data );
266 }
267
268 # now, build item tab !
269 # the main difference is that datas are in lines and not in columns : thus, we build the <th> first, then the values...
270 # loop through each tag
271 # warning : we may have differents number of columns in each row. Thus, we first build a hash, complete it if necessary
272 # then construct template.
273 my @fields = $record->fields();
274 my %witness
275   ; #---- stores the list of subfields used at least once, with the "meaning" of the code
276 my @big_array;
277 foreach my $field (@fields) {
278     next if ( $field->tag() < 10 );
279     my @subf = $field->subfields;
280     my %this_row;
281
282     # loop through each subfield
283     for my $i ( 0 .. $#subf ) {
284         next if ( $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{tab} ne 10 );
285         next if ( $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{hidden} );
286         $witness{ $subf[$i][0] } =
287           $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{lib};
288         $this_row{ $subf[$i][0] } = $subf[$i][1];
289     }
290     if (%this_row) {
291         push( @big_array, \%this_row );
292     }
293 }
294 my ($holdingbrtagf,$holdingbrtagsubf) = &GetMarcFromKohaField($dbh,"items.holdingbranch",$frameworkcode);
295 @big_array = sort {$a->{$holdingbrtagsubf} cmp $b->{$holdingbrtagsubf}} @big_array;
296
297 #fill big_row with missing datas
298 foreach my $subfield_code ( keys(%witness) ) {
299     for ( my $i = 0 ; $i <= $#big_array ; $i++ ) {
300         $big_array[$i]{$subfield_code} = "&nbsp;"
301           unless ( $big_array[$i]{$subfield_code} );
302     }
303 }
304
305 # now, construct template !
306 my @item_value_loop;
307 my @header_value_loop;
308 for ( my $i = 0 ; $i <= $#big_array ; $i++ ) {
309     my $items_data;
310     foreach my $subfield_code ( keys(%witness) ) {
311         $items_data .= "<td>" . $big_array[$i]{$subfield_code} . "</td>";
312     }
313     my %row_data;
314     $row_data{item_value} = $items_data;
315     push( @item_value_loop, \%row_data );
316 }
317 foreach my $subfield_code ( keys(%witness) ) {
318     my %header_value;
319     $header_value{header_value} = $witness{$subfield_code};
320     push( @header_value_loop, \%header_value );
321 }
322
323 my $subscriptionsnumber = CountSubscriptionFromBiblionumber($biblionumber);
324
325 if ($subscriptionsnumber) {
326     my $subscriptions = GetSubscriptionsFromBiblionumber($biblionumber);
327     my $subscriptiontitle = $subscriptions->[0]{'bibliotitle'};
328     $template->param(
329         subscriptiontitle   => $subscriptiontitle,
330         subscriptionsnumber => $subscriptionsnumber,
331     );
332 }
333
334 $template->param (
335     item_loop               => \@item_value_loop,
336     item_header_loop        => \@header_value_loop,
337     biblionumber            => $biblionumber,
338     popup                   => $popup,
339     hide_marc               => C4::Context->preference('hide_marc'),
340     intranetcolorstylesheet =>
341       C4::Context->preference("intranetcolorstylesheet"),
342     intranetstylesheet => C4::Context->preference("intranetstylesheet"),
343     IntranetNav        => C4::Context->preference("IntranetNav"),
344
345 );
346
347 output_html_with_http_headers $query, $cookie, $template->output;