Merge remote-tracking branch 'origin/new/bug_8636'
[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     $template->{VARS}->{'displayhierarchy'} = $display_hierarchy;
80     $template->{VARS}->{'loophierarchies'} = GenerateHierarchy($authid);
81 }
82
83 my $count = CountUsage($authid);
84
85
86 my $authtypes     = getauthtypes();
87 my @authtypesloop = ();
88 foreach my $thisauthtype ( keys %{$authtypes} ) {
89     push @authtypesloop,
90          { value        => $thisauthtype,
91            selected     => $thisauthtype eq $authtypecode,
92            authtypetext => $authtypes->{$thisauthtype}{'authtypetext'},
93          };
94 }
95 $template->{VARS}->{'authtypesloop'} = \@authtypesloop;
96 $template->{VARS}->{'authtypetext'}  = $authtypes->{$authtypecode}{'authtypetext'};
97 $template->{VARS}->{'authid'}        = $authid;
98 $template->{VARS}->{'count'}         = $count;
99
100 # find the marc field/subfield used in biblio by this authority
101 if ($show_marc) {
102     my $tagslib = &GetTagsLabels( 0, $authtypecode );
103     my $sth =
104         $dbh->prepare(
105                 "select distinct tagfield from marc_subfield_structure where authtypecode=?"
106                 );
107     $sth->execute($authtypecode);
108     my $biblio_fields;
109     while ( my ($tagfield) = $sth->fetchrow ) {
110         $biblio_fields .= $tagfield . "9,";
111     }
112     chop $biblio_fields;
113
114 # fill arrays
115     my @loop_data = ();
116     my $tag;
117
118 # loop through each tag
119     my @fields    = $record->fields();
120     foreach my $field (@fields) {
121         my @subfields_data;
122
123 # skip UNIMARC fields <200, they are useless for a patron
124         next if C4::Context->preference('MarcFlavour') eq 'UNIMARC' && $field->tag() <200;
125
126 # if tag <10, there's no subfield, use the "@" trick
127         if ( $field->tag() < 10 ) {
128             next if ( $tagslib->{ $field->tag() }->{'@'}->{hidden} );
129             my %subfield_data;
130             $subfield_data{marc_lib}   = $tagslib->{ $field->tag() }->{'@'}->{lib};
131             $subfield_data{marc_value} = $field->data();
132             $subfield_data{marc_subfield} = '@';
133             $subfield_data{marc_tag}      = $field->tag();
134             push( @subfields_data, \%subfield_data );
135         }
136         else {
137             my @subf = $field->subfields;
138
139 # loop through each subfield
140             for my $i ( 0 .. $#subf ) {
141                 $subf[$i][0] = "@" unless defined $subf[$i][0];
142                 next if ( $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{hidden} );
143 # skip useless subfields (for patrons)
144                 next if $subf[$i][0] =~ /7|8|9/;
145                 my %subfield_data;
146                 $subfield_data{marc_lib} =
147                     $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{lib};
148                 $subfield_data{marc_subfield} = $subf[$i][0];
149                 $subfield_data{marc_tag}      = $field->tag();
150                 $subfield_data{isurl} =  $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{isurl};
151                 $subfield_data{marc_value} = $subf[$i][1];
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( "Tab0XX" => \@loop_data );
167 } else {
168     my $summary = BuildSummary($record, $authid, $authtypecode);
169     $template->{VARS}->{'summary'} = $summary;
170 }
171
172 output_html_with_http_headers $query, $cookie, $template->output;