Merge remote-tracking branch 'origin/new/bug_7889'
[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') || 1; # Currently only MARC view is available
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 $authtypecode = &GetAuthTypeCode( $authid );
71 my $tagslib      = &GetTagsLabels( 0, $authtypecode );
72
73
74 my $record;
75 if ($display_hierarchy){
76   my $trees=BuildUnimarcHierarchies($authid);
77   my @trees = split /;/,$trees ;
78   push @trees,$trees unless (@trees);
79   my @loophierarchies;
80   foreach my $tree (@trees){
81     my @tree=split /,/,$tree;
82     push @tree,$tree unless (@tree);
83     my $cnt=0;
84     my @loophierarchy;
85     foreach my $element (@tree){
86       my $cell;
87       my $elementdata = GetAuthority($element);
88       $record= $elementdata if ($authid==$element);
89       push @loophierarchy, BuildUnimarcHierarchy($elementdata,"child".$cnt, $authid);
90       $cnt++;
91     }
92     push @loophierarchies, { 'loopelement' =>\@loophierarchy};
93   }
94   $template->param(
95     'displayhierarchy' => $display_hierarchy,
96     'loophierarchies' =>\@loophierarchies,
97   );
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 =
106   $dbh->prepare(
107     "select distinct tagfield from marc_subfield_structure where authtypecode=?"
108   );
109 $sth->execute($authtypecode);
110 my $biblio_fields;
111 while ( my ($tagfield) = $sth->fetchrow ) {
112     $biblio_fields .= $tagfield . "9,";
113 }
114 chop $biblio_fields;
115
116 # fill arrays
117 my @loop_data = ();
118 my $tag;
119
120 # loop through each tab 0 through 9
121 # for (my $tabloop = 0; $tabloop<=10;$tabloop++) {
122 # loop through each tag
123 my @fields    = $record->fields();
124 foreach my $field (@fields) {
125     my @subfields_data;
126
127     # skip UNIMARC fields <200, they are useless for a patron
128     next if C4::Context->preference('MarcFlavour') eq 'UNIMARC' && $field->tag() <200;
129
130     # if tag <10, there's no subfield, use the "@" trick
131     if ( $field->tag() < 10 ) {
132         next if ( $tagslib->{ $field->tag() }->{'@'}->{hidden} );
133         my %subfield_data;
134         $subfield_data{marc_lib}   = $tagslib->{ $field->tag() }->{'@'}->{lib};
135         $subfield_data{marc_value} = $field->data();
136         $subfield_data{marc_subfield} = '@';
137         $subfield_data{marc_tag}      = $field->tag();
138         push( @subfields_data, \%subfield_data );
139     }
140     else {
141         my @subf = $field->subfields;
142
143         # loop through each subfield
144         for my $i ( 0 .. $#subf ) {
145             $subf[$i][0] = "@" unless $subf[$i][0];
146             next if ( $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{hidden} );
147             # skip useless subfields (for patrons)
148             next if $subf[$i][0] =~ /7|8|9/;
149             my %subfield_data;
150             $subfield_data{marc_lib} =
151               $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{lib};
152             $subfield_data{marc_subfield} = $subf[$i][0];
153             $subfield_data{marc_tag}      = $field->tag();
154             $subfield_data{isurl} =  $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{isurl};
155             $subfield_data{marc_value} = $subf[$i][1];
156             push( @subfields_data, \%subfield_data );
157         }
158     }
159     if ( $#subfields_data >= 0 ) {
160         my %tag_data;
161         $tag_data{tag} =
162           $field->tag() 
163           . ' '
164           . C4::Koha::display_marc_indicators($field)
165           . ' - ' . $tagslib->{ $field->tag() }->{lib};
166         $tag_data{subfield} = \@subfields_data;
167         push( @loop_data, \%tag_data );
168     }
169 }
170 $template->param( "Tab0XX" => \@loop_data );
171
172 my $authtypes     = getauthtypes();
173 my @authtypesloop = ();
174 foreach my $thisauthtype ( keys %{$authtypes} ) {
175     push @authtypesloop,
176       { value        => $thisauthtype,
177         selected     => $thisauthtype eq $authtypecode,
178         authtypetext => $authtypes->{$thisauthtype}{'authtypetext'},
179       };
180 }
181
182 $template->param(
183     authid               => $authid,
184     count                => $count,
185     biblio_fields        => $biblio_fields,
186     authtypetext         => $authtypes->{$authtypecode}{'authtypetext'},
187     authtypesloop        => \@authtypesloop,
188 );
189 output_html_with_http_headers $query, $cookie, $template->output;
190