Merge remote-tracking branch 'origin/new/bug_8408'
[koha.git] / opac / opac-authoritiesdetail.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
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20 =head1 NAME
21
22 opac-authoritiesdetail.pl : script to show an authority in MARC format
23
24 =head1 SYNOPSIS
25
26 =cut
27
28 =head1 DESCRIPTION
29
30 This script needs an authid
31
32 It shows the authority in a (nice) MARC format depending on authority MARC
33 parameters tables.
34
35 =head1 FUNCTIONS
36
37 =cut
38
39 use strict;
40 use warnings;
41
42 use C4::AuthoritiesMarc;
43 use C4::Auth;
44 use C4::Context;
45 use C4::Output;
46 use CGI;
47 use MARC::Record;
48 use C4::Koha;
49
50
51 my $query = new CGI;
52
53 my $dbh = C4::Context->dbh;
54
55 my $display_hierarchy = C4::Context->preference("AuthDisplayHierarchy");
56 my $show_marc = $query->param('marc');
57
58 # open template
59 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
60     {
61         template_name   => $show_marc ? "opac-auth-MARCdetail.tt" : "opac-auth-detail.tt",
62         query           => $query,
63         type            => "opac",
64         authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ),
65         debug           => 1,
66     }
67 );
68
69 my $authid       = $query->param('authid');
70 my $record = GetAuthority( $authid );
71 if ( ! $record ) {
72     print $query->redirect("/cgi-bin/koha/errors/404.pl"); # escape early
73     exit;
74 }
75
76 my $authtypecode = &GetAuthTypeCode( $authid );
77
78 if ($display_hierarchy){
79   my $trees=BuildUnimarcHierarchies($authid);
80   my @trees = split /;/,$trees ;
81   push @trees,$trees unless (@trees);
82   my @loophierarchies;
83   foreach my $tree (@trees){
84     my @tree=split /,/,$tree;
85     push @tree,$tree unless (@tree);
86     my $cnt=0;
87     my @loophierarchy;
88     foreach my $element (@tree){
89       my $cell;
90       my $elementdata = GetAuthority($element);
91       push @loophierarchy, BuildUnimarcHierarchy($elementdata,"child".$cnt, $authid);
92       $cnt++;
93     }
94     push @loophierarchies, { 'loopelement' =>\@loophierarchy};
95   }
96   $template->param(
97     'displayhierarchy' => $display_hierarchy,
98     'loophierarchies' =>\@loophierarchies,
99   );
100 }
101 my $count = CountUsage($authid);
102
103
104 my $authtypes     = getauthtypes();
105 my @authtypesloop = ();
106 foreach my $thisauthtype ( keys %{$authtypes} ) {
107     push @authtypesloop,
108          { value        => $thisauthtype,
109            selected     => $thisauthtype eq $authtypecode,
110            authtypetext => $authtypes->{$thisauthtype}{'authtypetext'},
111          };
112 }
113 $template->{VARS}->{'authtypesloop'} = \@authtypesloop;
114 $template->{VARS}->{'authtypetext'}  = $authtypes->{$authtypecode}{'authtypetext'};
115 $template->{VARS}->{'authid'}        = $authid;
116 $template->{VARS}->{'count'}         = $count;
117
118 # find the marc field/subfield used in biblio by this authority
119 if ($show_marc) {
120     my $tagslib = &GetTagsLabels( 0, $authtypecode );
121     my $sth =
122         $dbh->prepare(
123                 "select distinct tagfield from marc_subfield_structure where authtypecode=?"
124                 );
125     $sth->execute($authtypecode);
126     my $biblio_fields;
127     while ( my ($tagfield) = $sth->fetchrow ) {
128         $biblio_fields .= $tagfield . "9,";
129     }
130     chop $biblio_fields;
131
132 # fill arrays
133     my @loop_data = ();
134     my $tag;
135
136 # loop through each tag
137     my @fields    = $record->fields();
138     foreach my $field (@fields) {
139         my @subfields_data;
140
141 # skip UNIMARC fields <200, they are useless for a patron
142         next if C4::Context->preference('MarcFlavour') eq 'UNIMARC' && $field->tag() <200;
143
144 # if tag <10, there's no subfield, use the "@" trick
145         if ( $field->tag() < 10 ) {
146             next if ( $tagslib->{ $field->tag() }->{'@'}->{hidden} );
147             my %subfield_data;
148             $subfield_data{marc_lib}   = $tagslib->{ $field->tag() }->{'@'}->{lib};
149             $subfield_data{marc_value} = $field->data();
150             $subfield_data{marc_subfield} = '@';
151             $subfield_data{marc_tag}      = $field->tag();
152             push( @subfields_data, \%subfield_data );
153         }
154         else {
155             my @subf = $field->subfields;
156
157 # loop through each subfield
158             for my $i ( 0 .. $#subf ) {
159                 $subf[$i][0] = "@" unless $subf[$i][0];
160                 next if ( $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{hidden} );
161 # skip useless subfields (for patrons)
162                 next if $subf[$i][0] =~ /7|8|9/;
163                 my %subfield_data;
164                 $subfield_data{marc_lib} =
165                     $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{lib};
166                 $subfield_data{marc_subfield} = $subf[$i][0];
167                 $subfield_data{marc_tag}      = $field->tag();
168                 $subfield_data{isurl} =  $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{isurl};
169                 $subfield_data{marc_value} = $subf[$i][1];
170                 push( @subfields_data, \%subfield_data );
171             }
172         }
173         if ( $#subfields_data >= 0 ) {
174             my %tag_data;
175             $tag_data{tag} =
176                 $field->tag()
177                 . ' '
178                 . C4::Koha::display_marc_indicators($field)
179                 . ' - ' . $tagslib->{ $field->tag() }->{lib};
180             $tag_data{subfield} = \@subfields_data;
181             push( @loop_data, \%tag_data );
182         }
183     }
184     $template->param( "Tab0XX" => \@loop_data );
185 } else {
186     my $summary = BuildSummary($record, $authid, $authtypecode);
187     $template->{VARS}->{'summary'} = $summary;
188 }
189
190 output_html_with_http_headers $query, $cookie, $template->output;
191