road to 1.3.1 : viewing biblio MARC detail
[koha.git] / 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 use strict;
21 require Exporter;
22 use C4::Context;
23 use C4::Output;
24 use CGI;
25 use C4::Search;
26 use MARC::Record;
27 use C4::Biblio;
28 use C4::Catalogue;
29 use HTML::Template;
30
31 my $query=new CGI;
32
33 my $dbh=C4::Context->dbh;
34
35 my $biblionumber=$query->param('biblionumber');
36 my $bibid = &MARCfind_MARCbibid_from_oldbiblionumber($dbh,$biblionumber);
37
38 my $tagslib = &MARCgettagslib($dbh,1);
39
40 my $record =MARCgetbiblio($dbh,$bibid);
41 # open template
42 my $template = gettemplate("catalogue/MARCdetail.tmpl",0);
43 # fill arrays
44 my @loop_data =();
45 my $tag;
46 # loop through each tab 0 through 9
47 for (my $tabloop = 0; $tabloop<=10;$tabloop++) {
48 # loop through each tag
49         my @fields = $record->fields();
50         my @loop_data =();
51         foreach my $field (@fields) {
52                 my @subf=$field->subfields;
53                 my $previous_tag = '';
54                 my @subfields_data;
55 # loop through each subfield
56                 for my $i (0..$#subf) {
57                         $previous_tag = $field->tag();
58                         next if ($tagslib->{$field->tag()}->{$subf[$i][0]}->{tab}  ne $tabloop);
59                         my %subfield_data;
60                         $subfield_data{marc_lib}=$tagslib->{$field->tag()}->{$subf[$i][0]}->{lib};
61                         $subfield_data{marc_value}=$subf[$i][1];
62                         $subfield_data{marc_tag}=$field->tag().$subf[$i][0];
63                         push(@subfields_data, \%subfield_data);
64                 }
65                 if ($#subfields_data>=0) {
66                         my %tag_data;
67                         $tag_data{tag}=$field->tag().' '. $tagslib->{$field->tag()}->{lib};
68                         $tag_data{subfield} = \@subfields_data;
69                         push (@loop_data, \%tag_data);
70                 }
71         }
72         $template->param($tabloop."XX" =>\@loop_data);
73 }
74 # now, build item tab !
75 # the main difference is that datas are in lines and not in columns : thus, we build the <th> first, then the values...
76 # loop through each tag
77 # warning : we may have differents number of columns in each row. Thus, we first build a hash, complete it if necessary
78 # then construct template.
79 my @fields = $record->fields();
80 my %witness; #---- stores the list of subfields used at least once, with the "meaning" of the code
81 my @big_array;
82 warn "loop 1";
83 foreach my $field (@fields) {
84         my @subf=$field->subfields;
85         my %this_row;
86 # loop through each subfield
87         for my $i (0..$#subf) {
88                 next if ($tagslib->{$field->tag()}->{$subf[$i][0]}->{tab}  ne 10);
89                 $witness{$subf[$i][0]} = $tagslib->{$field->tag()}->{$subf[$i][0]}->{lib};
90                 $this_row{$subf[$i][0]} =$subf[$i][1];
91         }
92         if (%this_row) {
93                 push(@big_array, \%this_row);
94         }
95 }
96 #fill big_row with missing datas
97 #warn "loop 2";
98 foreach my $subfield_code  (keys(%witness)) {
99         for (my $i=0;$i<=$#big_array;$i++) {
100                 $big_array[$i]{$subfield_code}="&nbsp;" unless ($big_array[$i]{$subfield_code});
101 #               warn "filled : ".$big_array[$i]{$subfield_code};
102         }
103 }
104 # now, construct template !
105 #warn "loop 3";
106 my @item_value_loop;
107 my @header_value_loop;
108 for (my $i=0;$i<=$#big_array; $i++) {
109         my $items_data;
110         foreach my $subfield_code (keys(%witness)) {
111                 $items_data .="<td>".$big_array[$i]{$subfield_code}."</td>";
112         }
113 #       warn $items_data;
114         my %row_data;
115         $row_data{item_value} = $items_data;
116         push(@item_value_loop,\%row_data);
117 }
118 foreach my $subfield_code (keys(%witness)) {
119         my %header_value;
120         $header_value{header_value} = $witness{$subfield_code};
121         warn "$subfield_code => ".$witness{$subfield_code};
122         push(@header_value_loop, \%header_value);
123 }
124
125 $template->param(item_loop => \@item_value_loop,
126                                                 item_header_loop => \@header_value_loop,
127                                                 biblionumber => $biblionumber,
128                                                 bibid => $bibid);
129 print "Content-Type: text/html\n\n", $template->output;
130