*** empty log message ***
[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 =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 from koha style DB. Automaticaly maps to marc biblionumber).
30 It shows the biblio in a (nice) MARC format depending on MARC parameters tables.
31 The template is in <templates_dir>/catalogue/MARCdetail.tmpl. this template must be divided in 11 "tabs".
32 The 10 firsts presents the biblio, the 11th one presents the items attached to the biblio
33
34 =head1 FUNCTIONS
35
36 =over 2
37
38 =cut
39
40
41 use strict;
42 require Exporter;
43 use C4::Auth;
44 use C4::Context;
45 use C4::Output;
46 use CGI;
47 use C4::Search;
48 use MARC::Record;
49 use C4::Biblio;
50 use C4::Catalogue;
51 use HTML::Template;
52
53 my $query=new CGI;
54
55 my $dbh=C4::Context->dbh;
56
57 my $biblionumber=$query->param('bib');
58 my $bibid = &MARCfind_MARCbibid_from_oldbiblionumber($dbh,$biblionumber);
59
60 my $tagslib = &MARCgettagslib($dbh,1);
61
62 my $record =MARCgetbiblio($dbh,$bibid);
63 #warn $record->as_formatted();
64 # open template
65 my ($template, $loggedinuser, $cookie)
66                 = get_template_and_user({template_name => "catalogue/MARCdetail.tmpl",
67                              query => $query,
68                              type => "intranet",
69                              authnotrequired => 0,
70                              flagsrequired => {catalogue => 1},
71                              debug => 1,
72                              });
73
74 # fill arrays
75 my @loop_data =();
76 my $tag;
77 # loop through each tab 0 through 9
78 for (my $tabloop = 0; $tabloop<=10;$tabloop++) {
79 # loop through each tag
80         my @fields = $record->fields();
81         my @loop_data =();
82         foreach my $field (@fields) {
83                 my @subf=$field->subfields;
84                 my $previous_tag = '';
85                 my @subfields_data;
86 # loop through each subfield
87                 for my $i (0..$#subf) {
88                         $previous_tag = $field->tag();
89                         next if ($tagslib->{$field->tag()}->{$subf[$i][0]}->{tab}  ne $tabloop);
90                         my %subfield_data;
91                         $subfield_data{marc_lib}=$tagslib->{$field->tag()}->{$subf[$i][0]}->{lib};
92                         $subfield_data{marc_value}=$subf[$i][1];
93                         $subfield_data{marc_tag}=$subf[$i][0];
94                         push(@subfields_data, \%subfield_data);
95                 }
96                 if ($#subfields_data>=0) {
97                         my %tag_data;
98                         $tag_data{tag}=$field->tag().' -'. $tagslib->{$field->tag()}->{lib};
99                         $tag_data{subfield} = \@subfields_data;
100                         push (@loop_data, \%tag_data);
101                 }
102         }
103         $template->param($tabloop."XX" =>\@loop_data);
104 }
105 # now, build item tab !
106 # the main difference is that datas are in lines and not in columns : thus, we build the <th> first, then the values...
107 # loop through each tag
108 # warning : we may have differents number of columns in each row. Thus, we first build a hash, complete it if necessary
109 # then construct template.
110 my @fields = $record->fields();
111 my %witness; #---- stores the list of subfields used at least once, with the "meaning" of the code
112 my @big_array;
113 foreach my $field (@fields) {
114         my @subf=$field->subfields;
115         my %this_row;
116 # loop through each subfield
117         for my $i (0..$#subf) {
118                 next if ($tagslib->{$field->tag()}->{$subf[$i][0]}->{tab}  ne 10);
119                 $witness{$subf[$i][0]} = $tagslib->{$field->tag()}->{$subf[$i][0]}->{lib};
120                 $this_row{$subf[$i][0]} =$subf[$i][1];
121         }
122         if (%this_row) {
123                 push(@big_array, \%this_row);
124         }
125 }
126 #fill big_row with missing datas
127 foreach my $subfield_code  (keys(%witness)) {
128         for (my $i=0;$i<=$#big_array;$i++) {
129                 $big_array[$i]{$subfield_code}="&nbsp;" unless ($big_array[$i]{$subfield_code});
130 #               warn "filled : ".$big_array[$i]{$subfield_code};
131         }
132 }
133 # now, construct template !
134 my @item_value_loop;
135 my @header_value_loop;
136 for (my $i=0;$i<=$#big_array; $i++) {
137         my $items_data;
138         foreach my $subfield_code (keys(%witness)) {
139                 $items_data .="<td>".$big_array[$i]{$subfield_code}."</td>";
140         }
141 #       warn $items_data;
142         my %row_data;
143         $row_data{item_value} = $items_data;
144         push(@item_value_loop,\%row_data);
145 }
146 foreach my $subfield_code (keys(%witness)) {
147         my %header_value;
148         $header_value{header_value} = $witness{$subfield_code};
149         push(@header_value_loop, \%header_value);
150 }
151
152 $template->param(item_loop => \@item_value_loop,
153                                                 item_header_loop => \@header_value_loop,
154                                                 biblionumber => $biblionumber,
155                                                 bibid => $bibid);
156 print $query->header(-cookie => $cookie),$template->output;
157