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