Bug 7534: (QA follow-up) Don't do pickup branch checking for determining holdability...
[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
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::Context;
44 use C4::Output;
45 use CGI qw ( -utf8 );
46 use MARC::Record;
47 use C4::Koha;
48
49 use Koha::Authorities;
50 use Koha::Authority::Types;
51
52 my $query = new CGI;
53
54 my $dbh = C4::Context->dbh;
55
56 my $display_hierarchy = C4::Context->preference("AuthDisplayHierarchy");
57 my $show_marc = $query->param('marc');
58
59 # open template
60 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
61     {
62         template_name   => $show_marc ? "opac-auth-MARCdetail.tt" : "opac-auth-detail.tt",
63         query           => $query,
64         type            => "opac",
65         authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ),
66         debug           => 1,
67     }
68 );
69
70 my $authid = $query->param('authid');
71 $authid = int($authid);
72 my $record = GetAuthority( $authid );
73 if ( ! $record ) {
74     print $query->redirect("/cgi-bin/koha/errors/404.pl"); # escape early
75     exit;
76 }
77
78 my $authority = Koha::Authorities->find( $authid );
79 my $authtypecode = $authority ? $authority->authtypecode : q{};
80
81 if ($display_hierarchy){
82     $template->{VARS}->{'displayhierarchy'} = $display_hierarchy;
83     $template->{VARS}->{'loophierarchies'} = GenerateHierarchy($authid);
84 }
85
86 my $count = $authority ? $authority->get_usage_count : 0;
87
88 my $authority_types = Koha::Authority::Types->search( {}, { order_by => ['authtypecode'] } );
89 $template->param(
90     authority_types => $authority_types,
91     authtypetext    => $authority_types->find($authtypecode)->authtypetext,
92     authid          => $authid,
93     count           => $count,
94 );
95
96 # find the marc field/subfield used in biblio by this authority
97 if ($show_marc) {
98     my $tagslib = &GetTagsLabels( 0, $authtypecode );
99     my $sth =
100         $dbh->prepare(
101                 "select distinct tagfield from marc_subfield_structure where authtypecode=?"
102                 );
103     $sth->execute($authtypecode);
104     my $biblio_fields;
105     while ( my ($tagfield) = $sth->fetchrow ) {
106         $biblio_fields .= $tagfield . "9,";
107     }
108     chop $biblio_fields;
109
110 # fill arrays
111     my @loop_data = ();
112     my $tag;
113
114 # loop through each tag
115     my @fields    = $record->fields();
116     foreach my $field (@fields) {
117         my @subfields_data;
118
119 # skip UNIMARC fields <200, they are useless for a patron
120         next if C4::Context->preference('marcflavour') eq 'UNIMARC' && $field->tag() <200;
121
122 # if tag <10, there's no subfield, use the "@" trick
123         if ( $field->tag() < 10 ) {
124             next if ( $tagslib->{ $field->tag() }->{'@'}->{hidden} );
125             my %subfield_data;
126             $subfield_data{marc_lib}   = $tagslib->{ $field->tag() }->{'@'}->{lib};
127             $subfield_data{marc_value} = $field->data();
128             $subfield_data{marc_subfield} = '@';
129             $subfield_data{marc_tag}      = $field->tag();
130             push( @subfields_data, \%subfield_data );
131         }
132         elsif ( C4::Context->preference('marcflavour') eq 'MARC21' && $field->tag() eq 667 ) {
133             # tagfield 667 is a nonpublic general note in MARC21, which shouldn't be shown in the OPAC
134         }
135         else {
136             my @subf = $field->subfields;
137
138 # loop through each subfield
139             for my $i ( 0 .. $#subf ) {
140                 $subf[$i][0] = "@" unless defined $subf[$i][0];
141                 next if ( $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{hidden} );
142 # skip useless subfields (for patrons)
143                 next if $subf[$i][0] =~ /7|8|9/;
144                 my %subfield_data;
145                 $subfield_data{marc_lib} =
146                     $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{lib};
147                 $subfield_data{marc_subfield} = $subf[$i][0];
148                 $subfield_data{marc_tag}      = $field->tag();
149                 $subfield_data{isurl} =  $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{isurl};
150                 $subfield_data{marc_value} = $subf[$i][1];
151                 push( @subfields_data, \%subfield_data );
152             }
153         }
154         if ( $#subfields_data >= 0 ) {
155             my %tag_data;
156             $tag_data{tag} =
157                 $field->tag()
158                 . ' '
159                 . C4::Koha::display_marc_indicators($field)
160                 . ' - ' . $tagslib->{ $field->tag() }->{lib};
161             $tag_data{subfield} = \@subfields_data;
162             push( @loop_data, \%tag_data );
163         }
164     }
165     $template->param( "Tab0XX" => \@loop_data );
166 } else {
167     my $summary = BuildSummary($record, $authid, $authtypecode);
168     $template->{VARS}->{'summary'} = $summary;
169 }
170
171 output_html_with_http_headers $query, $cookie, $template->output;