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