Merge remote branch 'kc/new/enh/bug_5733' into kcmaster
[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 $authid       = $query->param('authid');
56 my $authtypecode = &GetAuthTypeCode( $authid );
57 my $tagslib      = &GetTagsLabels( 1, $authtypecode );
58
59 # open template
60 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
61     {
62         template_name   => "opac-authoritiesdetail.tmpl",
63         query           => $query,
64         type            => "opac",
65         authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ),
66         debug           => 1,
67     }
68 );
69
70 my $record;
71 if (C4::Context->preference("AuthDisplayHierarchy")){
72   my $trees=BuildUnimarcHierarchies($authid);
73   my @trees = split /;/,$trees ;
74   push @trees,$trees unless (@trees);
75   my @loophierarchies;
76   foreach my $tree (@trees){
77     my @tree=split /,/,$tree;
78     push @tree,$tree unless (@tree);
79     my $cnt=0;
80     my @loophierarchy;
81     foreach my $element (@tree){
82       my $cell;
83       my $elementdata = GetAuthority($element);
84       $record= $elementdata if ($authid==$element);
85       push @loophierarchy, BuildUnimarcHierarchy($elementdata,"child".$cnt, $authid);
86       $cnt++;
87     }
88     push @loophierarchies, { 'loopelement' =>\@loophierarchy};
89   }
90   $template->param(
91     'displayhierarchy' =>C4::Context->preference("AuthDisplayHierarchy"),
92     'loophierarchies' =>\@loophierarchies,
93   );
94 }
95 else {
96     $record = GetAuthority( $authid );
97 }
98 my $count = CountUsage($authid);
99
100 # find the marc field/subfield used in biblio by this authority
101 my $sth =
102   $dbh->prepare(
103     "select distinct tagfield from marc_subfield_structure where authtypecode=?"
104   );
105 $sth->execute($authtypecode);
106 my $biblio_fields;
107 while ( my ($tagfield) = $sth->fetchrow ) {
108     $biblio_fields .= $tagfield . "9,";
109 }
110 chop $biblio_fields;
111
112 # fill arrays
113 my @loop_data = ();
114 my $tag;
115
116 # loop through each tab 0 through 9
117 # for (my $tabloop = 0; $tabloop<=10;$tabloop++) {
118 # loop through each tag
119 my @fields    = $record->fields();
120 foreach my $field (@fields) {
121     my @subfields_data;
122
123     # if tag <10, there's no subfield, use the "@" trick
124     if ( $field->tag() < 10 ) {
125         next if ( $tagslib->{ $field->tag() }->{'@'}->{hidden} );
126         my %subfield_data;
127         $subfield_data{marc_lib}   = $tagslib->{ $field->tag() }->{'@'}->{lib};
128         $subfield_data{marc_value} = $field->data();
129         $subfield_data{marc_subfield} = '@';
130         $subfield_data{marc_tag}      = $field->tag();
131         push( @subfields_data, \%subfield_data );
132     }
133     else {
134         my @subf = $field->subfields;
135
136         # loop through each subfield
137         for my $i ( 0 .. $#subf ) {
138             $subf[$i][0] = "@" unless $subf[$i][0];
139             next if ( $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{hidden} );
140             my %subfield_data;
141             $subfield_data{marc_lib} =
142               $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{lib};
143             if ( $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{isurl} ) {
144                 $subfield_data{marc_value} =
145                   "<a href=\"$subf[$i][1]\">$subf[$i][1]</a>";
146             }
147             else {
148                 $subfield_data{marc_value} = $subf[$i][1];
149             }
150             $subfield_data{marc_subfield} = $subf[$i][0];
151             $subfield_data{marc_tag}      = $field->tag();
152             push( @subfields_data, \%subfield_data );
153         }
154     }
155     if ( $#subfields_data >= 0 ) {
156         my %tag_data;
157         $tag_data{tag} =
158           $field->tag() 
159           . ' '
160           . C4::Koha::display_marc_indicators($field)
161           . ' - ' . $tagslib->{ $field->tag() }->{lib};
162         $tag_data{subfield} = \@subfields_data;
163         push( @loop_data, \%tag_data );
164     }
165 }
166 $template->param( "0XX" => \@loop_data );
167
168 my $authtypes = getauthtypes;
169 my @authtypesloop;
170 foreach my $thisauthtype ( keys %$authtypes ) {
171     my $selected = 1 if $thisauthtype eq $authtypecode;
172     my %row = (
173         value        => $thisauthtype,
174         selected     => $selected,
175         authtypetext => $authtypes->{$thisauthtype}{'authtypetext'},
176     );
177     push @authtypesloop, \%row;
178 }
179
180 $template->param(
181     authid               => $authid,
182     count                => $count,
183     biblio_fields        => $biblio_fields,
184     authtypetext         => $authtypes->{$authtypecode}{'authtypetext'},
185     authtypesloop        => \@authtypesloop,
186     LibraryName          => C4::Context->preference("LibraryName"),
187     OpacNav              => C4::Context->preference("OpacNav"),
188     opaccredits          => C4::Context->preference("opaccredits"),
189     opacsmallimage       => C4::Context->preference("opacsmallimage"),
190     opaclayoutstylesheet => C4::Context->preference("opaclayoutstylesheet"),
191     opaccolorstylesheet  => C4::Context->preference("opaccolorstylesheet"),
192 );
193 output_html_with_http_headers $query, $cookie, $template->output;
194