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