Fixing broken code to show list of branches
[koha.git] / opac / opac-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 require Exporter;
50 use C4::Auth;
51 use C4::Context;
52 use C4::Output;
53 use C4::Interface::CGI::Output;
54 use CGI;
55 use C4::Search;
56 use MARC::Record;
57 use C4::Biblio;
58 use C4::Acquisition;
59 use HTML::Template;
60
61 my $query=new CGI;
62
63 my $dbh=C4::Context->dbh;
64
65 my $biblionumber=$query->param('bib');
66 my $bibid = $query->param('bibid');
67 $bibid = &MARCfind_MARCbibid_from_oldbiblionumber($dbh,$biblionumber) unless $bibid;
68 $biblionumber = &MARCfind_oldbiblionumber_from_MARCbibid($dbh,$bibid) unless $biblionumber;
69 my $itemtype = &MARCfind_frameworkcode($dbh,$bibid);
70 my $tagslib = &MARCgettagslib($dbh,0,$itemtype);
71
72 my $record =MARCgetbiblio($dbh,$bibid);
73 # open template
74 my ($template, $loggedinuser, $cookie)
75                 = get_template_and_user({template_name => "opac-MARCdetail.tmpl",
76                              query => $query,
77                              type => "opac",
78                              authnotrequired => 1,
79                              debug => 1,
80                              });
81 $template->param(LibraryName => C4::Context->preference("LibraryName"),
82                                 suggestion => C4::Context->preference("suggestion"),
83                                 virtualshelves => C4::Context->preference("virtualshelves"),
84 );
85
86 # fill arrays
87 my @loop_data =();
88 my $tag;
89 # loop through each tab 0 through 9
90 for (my $tabloop = 0; $tabloop<=10;$tabloop++) {
91 # loop through each tag
92         my @loop_data =();
93         my @subfields_data;
94         # deal with leader
95         unless ($tagslib->{'000'}->{'@'}->{tab}  ne $tabloop  or $tagslib->{'000'}->{'@'}->{hidden}) {
96                 my %subfield_data;
97                 $subfield_data{marc_lib}=$tagslib->{'000'}->{'@'}->{lib};
98                 $subfield_data{marc_value}=$record->leader();
99                 $subfield_data{marc_subfield}='@';
100                 $subfield_data{marc_tag}='000';
101                 push(@subfields_data, \%subfield_data);
102                 my %tag_data;
103                         $tag_data{tag}='000 -'. $tagslib->{'000'}->{lib};
104                 my @tmp = @subfields_data;
105                 $tag_data{subfield} = \@tmp;
106                 push (@loop_data, \%tag_data);
107                 undef @subfields_data;
108         }
109         my @fields = $record->fields();
110         for (my $x_i=0;$x_i<=$#fields;$x_i++) {
111                 # if tag <10, there's no subfield, use the "@" trick
112                 if ($fields[$x_i]->tag()<10) {
113                         next if ($tagslib->{$fields[$x_i]->tag()}->{'@'}->{tab}  ne $tabloop);
114                         next if ($tagslib->{$fields[$x_i]->tag()}->{'@'}->{hidden});
115                         my %subfield_data;
116                         $subfield_data{marc_lib}=$tagslib->{$fields[$x_i]->tag()}->{'@'}->{lib};
117                         $subfield_data{marc_value}=$fields[$x_i]->data();
118                         $subfield_data{marc_subfield}='@';
119                         $subfield_data{marc_tag}=$fields[$x_i]->tag();
120                         push(@subfields_data, \%subfield_data);
121                 } else {
122                         my @subf=$fields[$x_i]->subfields;
123         # loop through each subfield
124                         for my $i (0..$#subf) {
125                                 $subf[$i][0] = "@" unless $subf[$i][0];
126                                 next if ($tagslib->{$fields[$x_i]->tag()}->{$subf[$i][0]}->{tab}  ne $tabloop);
127                                 next if ($tagslib->{$fields[$x_i]->tag()}->{$subf[$i][0]}->{hidden});
128                                 my %subfield_data;
129                                 $subfield_data{marc_lib}=$tagslib->{$fields[$x_i]->tag()}->{$subf[$i][0]}->{lib};
130                                 $subfield_data{link}=$tagslib->{$fields[$x_i]->tag()}->{$subf[$i][0]}->{link};
131                                 $subf[$i][1] =~ s/\n/<br\/>/g;
132                                 if ($tagslib->{$fields[$x_i]->tag()}->{$subf[$i][0]}->{isurl}) {
133                                         $subfield_data{marc_value}="<a href=\"$subf[$i][1]\">$subf[$i][1]</a>";
134                                 } elsif ($tagslib->{$fields[$x_i]->tag()}->{$subf[$i][0]}->{kohafield} eq "biblioitems.isbn") {
135 #                                       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]);
136                                         $subfield_data{marc_value}=DisplayISBN($subf[$i][1]);
137                                 } else {
138                                         if ($tagslib->{$fields[$x_i]->tag()}->{$subf[$i][0]}->{authtypecode}) {
139                                                 $subfield_data{authority}=$fields[$x_i]->subfield(9);
140                                         }
141                                         $subfield_data{marc_value}=get_authorised_value_desc($fields[$x_i]->tag(), $subf[$i][0], $subf[$i][1], '', $dbh);
142                                 }
143                                 $subfield_data{marc_subfield}=$subf[$i][0];
144                                 $subfield_data{marc_tag}=$fields[$x_i]->tag();
145                                 push(@subfields_data, \%subfield_data);
146                         }
147                 }
148                 if ($#subfields_data>=0) {
149                         my %tag_data;
150                         if ($fields[$x_i]->tag() eq $fields[$x_i-1]->tag()) {
151                                 $tag_data{tag}="";
152                         } else {
153                                 $tag_data{tag}=$fields[$x_i]->tag().' -'. $tagslib->{$fields[$x_i]->tag()}->{lib};
154                         }
155                         my @tmp = @subfields_data;
156                         $tag_data{subfield} = \@tmp;
157                         push (@loop_data, \%tag_data);
158                         undef @subfields_data;
159                 }
160         }
161         $template->param($tabloop."XX" =>\@loop_data);
162 }
163 # now, build item tab !
164 # the main difference is that datas are in lines and not in columns : thus, we build the <th> first, then the values...
165 # loop through each tag
166 # warning : we may have differents number of columns in each row. Thus, we first build a hash, complete it if necessary
167 # then construct template.
168 my @fields = $record->fields();
169 my %witness; #---- stores the list of subfields used at least once, with the "meaning" of the code
170 my @big_array;
171 foreach my $field (@fields) {
172         next if ($field->tag()<10);
173         my @subf=$field->subfields;
174         my %this_row;
175 # loop through each subfield
176         for my $i (0..$#subf) {
177                 next if ($tagslib->{$field->tag()}->{$subf[$i][0]}->{tab}  ne 10);
178                 $witness{$subf[$i][0]} = $tagslib->{$field->tag()}->{$subf[$i][0]}->{lib};
179                 $this_row{$subf[$i][0]} =$subf[$i][1];
180         }
181         if (%this_row) {
182                 push(@big_array, \%this_row);
183         }
184 }
185 my ($holdingbrtagf,$holdingbrtagsubf) = &MARCfind_marc_from_kohafield($dbh,"items.holdingbranch",$itemtype);
186 @big_array = sort {$a->{$holdingbrtagsubf} cmp $b->{$holdingbrtagsubf}} @big_array;
187 #fill big_row with missing datas
188 foreach my $subfield_code  (keys(%witness)) {
189         for (my $i=0;$i<=$#big_array;$i++) {
190                 $big_array[$i]{$subfield_code}="&nbsp;" unless ($big_array[$i]{$subfield_code});
191         }
192 }
193 # now, construct template !
194 my @item_value_loop;
195 my @header_value_loop;
196 for (my $i=0;$i<=$#big_array; $i++) {
197         my $items_data;
198         foreach my $subfield_code (keys(%witness)) {
199                 $items_data .="<td>".$big_array[$i]{$subfield_code}."</td>";
200         }
201         my %row_data;
202         $row_data{item_value} = $items_data;
203         push(@item_value_loop,\%row_data);
204 }
205 foreach my $subfield_code (keys(%witness)) {
206         my %header_value;
207         $header_value{header_value} = $witness{$subfield_code};
208         push(@header_value_loop, \%header_value);
209 }
210
211 $template->param(item_loop => \@item_value_loop,
212                                                 item_header_loop => \@header_value_loop,
213                                                 biblionumber => $biblionumber,
214                                                 bibid => $bibid,
215                                                 biblionumber => $biblionumber);
216 output_html_with_http_headers $query, $cookie, $template->output;
217
218 sub get_authorised_value_desc ($$$$$) {
219    my($tag, $subfield, $value, $framework, $dbh) = @_;
220
221    #---- branch
222     if ($tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "branches" ) {
223        return getbranchdetail($value)->{branchname};
224     }
225
226    #---- itemtypes
227    if ($tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "itemtypes" ) {
228        return ItemType($value);
229     }
230
231    #---- "true" authorized value
232    my $category = $tagslib->{$tag}->{$subfield}->{'authorised_value'};
233
234    if ($category ne "") {
235        my $sth = $dbh->prepare("select lib from authorised_values where category = ? and authorised_value = ?");
236        $sth->execute($category, $value);
237        my $data = $sth->fetchrow_hashref;
238        return $data->{'lib'};
239    } else {
240        return $value; # if nothing is found return the original value
241    }
242 }