bugfixes (various), handling utf-8 without guessencoding (as suggested by joshua...
[koha.git] / authorities / detail.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 detail.pl : script to show an authority in MARC format
23
24 =head1 SYNOPSIS
25
26
27 =head1 DESCRIPTION
28
29 This script needs an authid
30
31 It shows the authority in a (nice) MARC format depending on authority MARC
32 parameters tables.
33
34 =head1 FUNCTIONS
35
36 =over 2
37
38 =cut
39
40
41 use strict;
42 require Exporter;
43 use C4::AuthoritiesMarc;
44 use C4::Auth;
45 use C4::Context;
46 use C4::Output;
47 use CGI;
48 use MARC::Record;
49 use C4::Koha;
50 # use C4::Biblio;
51 # use C4::Catalogue;
52
53
54 my $query=new CGI;
55
56 my $dbh=C4::Context->dbh;
57
58 # open template
59 my ($template, $loggedinuser, $cookie)
60                 = get_template_and_user({template_name => "authorities/detail.tmpl",
61                              query => $query,
62                              type => "intranet",
63                              authnotrequired => 0,
64                              flagsrequired => {catalogue => 1},
65                              debug => 1,
66                              });
67
68 my $authid = $query->param('authid');
69
70
71
72 my $authtypecode = &GetAuthTypeCode($authid);
73 my $tagslib = &GetTagsLabels(1,$authtypecode);
74
75 my $record;
76 if (C4::Context->preference("AuthDisplayHierarchy")){
77   my $trees=BuildUnimarcHierarchies($authid);
78   my @trees = split /;/,$trees ;
79   push @trees,$trees unless (@trees);
80   my @loophierarchies;
81   foreach my $tree (@trees){
82     my @tree=split /,/,$tree;
83     push @tree,$tree unless (@tree);
84     my $cnt=0;
85     my @loophierarchy;
86     foreach my $element (@tree){
87       my $cell;
88       my $elementdata = GetAuthority($element);
89       $record= $elementdata if ($authid==$element);
90       push @loophierarchy, BuildUnimarcHierarchy($elementdata,"child".$cnt, $authid);
91       $cnt++;
92     }
93     push @loophierarchies, { 'loopelement' =>\@loophierarchy};
94   }
95   $template->param(
96     'displayhierarchy' =>C4::Context->preference("AuthDisplayHierarchy"),
97     'loophierarchies' =>\@loophierarchies,
98   );
99 } else {
100   $record=GetAuthority($authid);
101 }
102 my $count = CountUsage($authid);
103
104 # find the marc field/subfield used in biblio by this authority
105 my $sth = $dbh->prepare("select distinct tagfield from marc_subfield_structure where authtypecode=?");
106 $sth->execute($authtypecode);
107 my $biblio_fields;
108 while (my ($tagfield) = $sth->fetchrow) {
109         $biblio_fields.= $tagfield."9,";
110 }
111 chop $biblio_fields;
112
113
114 # fill arrays
115 my @loop_data =();
116 my $tag;
117 # loop through each tab 0 through 9
118 # for (my $tabloop = 0; $tabloop<=10;$tabloop++) {
119 # loop through each tag
120 my @fields = $record->fields();
121 my @loop_data =();
122 foreach my $field (@fields) {
123                 my @subfields_data;
124         # if tag <10, there's no subfield, use the "@" trick
125         if ($field->tag()<10) {
126                 next if ($tagslib->{$field->tag()}->{'@'}->{hidden});
127                 my %subfield_data;
128                 $subfield_data{marc_lib}=$tagslib->{$field->tag()}->{'@'}->{lib};
129                 $subfield_data{marc_value}=$field->data();
130                 $subfield_data{marc_subfield}='@';
131                 $subfield_data{marc_tag}=$field->tag();
132                 push(@subfields_data, \%subfield_data);
133         } else {
134                 my @subf=$field->subfields;
135 # loop through each subfield
136                 for my $i (0..$#subf) {
137                         $subf[$i][0] = "@" unless $subf[$i][0];
138                         next if ($tagslib->{$field->tag()}->{$subf[$i][0]}->{hidden});
139                         my %subfield_data;
140                         $subfield_data{marc_lib}=$tagslib->{$field->tag()}->{$subf[$i][0]}->{lib};
141                         if ($tagslib->{$field->tag()}->{$subf[$i][0]}->{isurl}) {
142                                 $subfield_data{marc_value}="<a href=\"$subf[$i][1]\">$subf[$i][1]</a>";
143                         } else {
144                                 $subfield_data{marc_value}=$subf[$i][1];
145                         }
146             $subfield_data{short_desc} = substr(
147                 $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{lib},
148                 0, 20
149             );
150             $subfield_data{long_desc} =
151               $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{lib};
152                         $subfield_data{marc_subfield}=$subf[$i][0];
153                         $subfield_data{marc_tag}=$field->tag();
154                         push(@subfields_data, \%subfield_data);
155                 }
156         }
157         if ($#subfields_data>=0) {
158                 my %tag_data;
159                 $tag_data{tag}=$field->tag().' -'. $tagslib->{$field->tag()}->{lib};
160                 $tag_data{subfield} = \@subfields_data;
161                 push (@loop_data, \%tag_data);
162         }
163 }
164 $template->param("0XX" =>\@loop_data);
165
166 my $authtypes = getauthtypes;
167 my @authtypesloop;
168 foreach my $thisauthtype (keys %$authtypes) {
169         my $selected = 1 if $thisauthtype eq $authtypecode;
170         my %row =(value => $thisauthtype,
171                                 selected => $selected,
172                                 authtypetext => $authtypes->{$thisauthtype}{'authtypetext'},
173                         );
174         push @authtypesloop, \%row;
175 }
176
177 $template->param(authid => $authid,
178                 count => $count,
179                 biblio_fields => $biblio_fields,
180                 authtypetext => $authtypes->{$authtypecode}{'authtypetext'},
181                 authtypesloop => \@authtypesloop,
182                 intranetcolorstylesheet => C4::Context->preference("intranetcolorstylesheet"),
183                 intranetstylesheet => C4::Context->preference("intranetstylesheet"),
184                 IntranetNav => C4::Context->preference("IntranetNav"),
185                 );
186 output_html_with_http_headers $query, $cookie, $template->output;
187