432e2982d494c145c2a078af41af6891aabf20e3
[srvgit] / 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
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
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 Modern::Perl;
40
41 use C4::AuthoritiesMarc;
42 use C4::Auth;
43 use C4::Biblio qw(GetMarcUrls);
44 use C4::Context;
45 use C4::Output;
46 use CGI qw ( -utf8 );
47 use MARC::Record;
48 use C4::Koha;
49
50 use Koha::Authorities;
51 use Koha::Authority::Types;
52
53 my $query = CGI->new;
54
55 my $dbh = C4::Context->dbh;
56
57 my $display_hierarchy = C4::Context->preference("AuthDisplayHierarchy");
58 my $marcflavour       = C4::Context->preference("marcflavour");
59 my $show_marc = $query->param('marc');
60
61 # open template
62 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
63     {
64         template_name   => $show_marc ? "opac-auth-MARCdetail.tt" : "opac-auth-detail.tt",
65         query           => $query,
66         type            => "opac",
67         authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ),
68     }
69 );
70
71 my $authid = $query->param('authid');
72 $authid = int($authid);
73 my $record = GetAuthority( $authid );
74 if ( ! $record ) {
75     print $query->redirect("/cgi-bin/koha/errors/404.pl"); # escape early
76     exit;
77 }
78
79 my $authority = Koha::Authorities->find( $authid );
80 my $authtypecode = $authority ? $authority->authtypecode : q{};
81
82 if ($display_hierarchy){
83     $template->{VARS}->{'displayhierarchy'} = $display_hierarchy;
84     $template->{VARS}->{'loophierarchies'} = GenerateHierarchy($authid);
85 }
86
87 my $count = $authority ? $authority->get_usage_count : 0;
88
89 my $authority_types = Koha::Authority::Types->search( {}, { order_by => ['authtypecode'] } );
90 my $marcurlsarray = GetMarcUrls( $record, $marcflavour );
91
92 $template->param(
93     authority_types => $authority_types,
94     authtypetext    => $authority_types->find($authtypecode)->authtypetext,
95     authid          => $authid,
96     count           => $count,
97     MARCURLS        => $marcurlsarray,
98 );
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
117 # loop through each tag
118     my @fields    = $record->fields();
119     foreach my $field (@fields) {
120         my @subfields_data;
121
122 # skip UNIMARC fields <200, they are useless for a patron
123         next if $marcflavour eq 'UNIMARC' && $field->tag() <200;
124
125 # if tag <10, there's no subfield, use the "@" trick
126         if ( $field->tag() < 10 ) {
127             next if ( $tagslib->{ $field->tag() }->{'@'}->{hidden} );
128             my %subfield_data;
129             $subfield_data{marc_lib}   = $tagslib->{ $field->tag() }->{'@'}->{lib};
130             $subfield_data{marc_value} = $field->data();
131             $subfield_data{marc_subfield} = '@';
132             $subfield_data{marc_tag}      = $field->tag();
133             push( @subfields_data, \%subfield_data );
134         }
135         elsif ( $marcflavour eq 'MARC21' && $field->tag() eq 667 ) {
136             # tagfield 667 is a nonpublic general note in MARC21, which shouldn't be shown in the OPAC
137         }
138         else {
139             my @subf = $field->subfields;
140
141 # loop through each subfield
142             for my $i ( 0 .. $#subf ) {
143                 $subf[$i][0] = "@" unless defined $subf[$i][0];
144                 next if ( $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{hidden} );
145 # skip useless subfields (for patrons)
146                 next if $subf[$i][0] =~ /7|8|9/;
147                 my %subfield_data;
148                 $subfield_data{marc_lib} =
149                     $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{lib};
150                 $subfield_data{marc_subfield} = $subf[$i][0];
151                 $subfield_data{marc_tag}      = $field->tag();
152                 $subfield_data{isurl} =  $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{isurl};
153                 $subfield_data{marc_value} = $subf[$i][1];
154                 push( @subfields_data, \%subfield_data );
155             }
156         }
157         if ( $#subfields_data >= 0 ) {
158             my %tag_data;
159             $tag_data{tag} =
160                 $field->tag()
161                 . ' '
162                 . C4::Koha::display_marc_indicators($field)
163                 . ' - ' . $tagslib->{ $field->tag() }->{lib};
164             $tag_data{subfield} = \@subfields_data;
165             push( @loop_data, \%tag_data );
166         }
167     }
168     $template->param( "Tab0XX" => \@loop_data );
169 } else {
170     my $summary = BuildSummary($record, $authid, $authtypecode);
171     $template->{VARS}->{'summary'} = $summary;
172 }
173
174 output_html_with_http_headers $query, $cookie, $template->output;