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