Bug 17196: Fix build_oai_sets.pl
[koha.git] / authorities / detail.pl
blob1ba280d38a202630d1708732fdd1d29584cbf2f8
1 #!/usr/bin/perl
3 # Copyright 2000-2002 Katipo Communications
5 # This file is part of Koha.
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.
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.
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>.
20 =head1 NAME
22 detail.pl : script to show an authority in MARC format
24 =head1 SYNOPSIS
26 =cut
28 =head1 DESCRIPTION
30 This script needs an authid
32 It shows the authority in a (nice) MARC format depending on authority MARC
33 parameters tables.
35 =head1 FUNCTIONS
37 =cut
40 use strict;
41 use warnings;
43 use C4::AuthoritiesMarc;
44 use C4::Auth;
45 use C4::Context;
46 use C4::Output;
47 use CGI qw ( -utf8 );
48 use MARC::Record;
49 use C4::Koha;
50 use Koha::Authorities;
52 use Koha::Authority::Types;
54 our ($tagslib);
56 sub build_tabs {
57 my ( $template, $record, $dbh, $encoding,$input ) = @_;
59 # fill arrays
60 my @loop_data = ();
61 my $tag;
63 # in this array, we will push all the 10 tabs
64 # to avoid having 10 tabs in the template : they will all be in the same BIG_LOOP
65 my @BIG_LOOP;
66 my %seen;
67 my @tab_data; # all tags to display
69 foreach my $used ( keys %$tagslib ){
70 push @tab_data,$used if not $seen{$used};
71 $seen{$used}++;
74 my $max_num_tab=9;
75 # loop through each tab 0 through 9
76 for ( my $tabloop = 0 ; $tabloop <= $max_num_tab ; $tabloop++ ) {
77 my @loop_data = (); #innerloop in the template.
78 my $i = 0;
79 foreach my $tag (sort @tab_data) {
80 $i++;
81 next if ! $tag;
83 # if MARC::Record is not empty =>use it as master loop, then add missing subfields that should be in the tab.
84 # if MARC::Record is empty => use tab as master loop.
85 if ( $record != -1 && ( $record->field($tag) || $tag eq '000' ) ) {
86 my @fields;
87 if ( $tag ne '000' ) {
88 @fields = $record->field($tag);
90 else {
91 push @fields, MARC::Field->new('000', $record->leader()); # if tag == 000
93 # loop through each field
94 foreach my $field (@fields) {
95 my @subfields_data;
96 if ($field->tag()<10) {
97 next
98 if (
99 $tagslib->{ $field->tag() }->{ '@' }->{tab}
100 ne $tabloop );
101 next if ($tagslib->{$field->tag()}->{'@'}->{hidden});
102 my %subfield_data;
103 $subfield_data{marc_lib}=$tagslib->{$field->tag()}->{'@'}->{lib};
104 $subfield_data{marc_value}=$field->data();
105 $subfield_data{marc_subfield}='@';
106 $subfield_data{marc_tag}=$field->tag();
107 push(@subfields_data, \%subfield_data);
108 } else {
109 my @subf=$field->subfields;
110 # loop through each subfield
111 for my $i (0..$#subf) {
112 $subf[$i][0] = "@" unless defined $subf[$i][0];
113 next
114 if (
115 $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{tab}
116 ne $tabloop );
117 next
118 if ( $tagslib->{ $field->tag() }->{ $subf[$i][0] }
119 ->{hidden} );
120 my %subfield_data;
121 $subfield_data{marc_lib}=$tagslib->{$field->tag()}->{$subf[$i][0]}->{lib};
122 if ($tagslib->{$field->tag()}->{$subf[$i][0]}->{isurl}) {
123 $subfield_data{marc_value}="<a href=\"$subf[$i][1]\">$subf[$i][1]</a>";
124 } else {
125 $subfield_data{marc_value}=$subf[$i][1];
127 $subfield_data{short_desc} = substr(
128 $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{lib},
129 0, 20
131 $subfield_data{long_desc} =
132 $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{lib};
133 $subfield_data{marc_subfield}=$subf[$i][0];
134 $subfield_data{marc_tag}=$field->tag();
135 push(@subfields_data, \%subfield_data);
138 if ($#subfields_data>=0) {
139 my %tag_data;
140 $tag_data{tag}=$field->tag(). ' '
141 . C4::Koha::display_marc_indicators($field)
142 . ' - '
143 . $tagslib->{$field->tag()}->{lib};
144 $tag_data{subfield} = \@subfields_data;
145 push (@loop_data, \%tag_data);
150 if ( $#loop_data >= 0 ) {
151 push @BIG_LOOP, {
152 number => $tabloop,
153 innerloop => \@loop_data,
157 $template->param( singletab => (scalar(@BIG_LOOP)==1), BIG_LOOP => \@BIG_LOOP );
162 my $query=new CGI;
164 my $dbh=C4::Context->dbh;
166 # open template
167 my ($template, $loggedinuser, $cookie) = get_template_and_user(
169 template_name => "authorities/detail.tt",
170 query => $query,
171 type => "intranet",
172 authnotrequired => 0,
173 flagsrequired => { catalogue => 1 },
174 debug => 1,
178 my $authid = $query->param('authid');
180 my $authtypecode = Koha::Authorities->find($authid)->authtypecode;
181 $tagslib = &GetTagsLabels(1,$authtypecode);
183 # Build list of authtypes for showing them
184 my $authority_types = Koha::Authority::Types->search({}, { order_by => ['authtypecode']});
186 my $record=GetAuthority($authid);
188 if (not defined $record) {
189 # authid invalid
190 $template->param ( errauthid => $authid,
191 unknownauthid => 1,
192 authority_types => $authority_types, );
193 output_html_with_http_headers $query, $cookie, $template->output;
194 exit;
197 if (C4::Context->preference("AuthDisplayHierarchy")){
198 $template->{VARS}->{'displayhierarchy'} = C4::Context->preference("AuthDisplayHierarchy");
199 $template->{VARS}->{'loophierarchies'} = GenerateHierarchy($authid);
202 my $count = CountUsage($authid);
204 # find the marc field/subfield used in biblio by this authority
205 my $sth = $dbh->prepare("select distinct tagfield from marc_subfield_structure where authtypecode=?");
206 $sth->execute($authtypecode);
207 my $biblio_fields;
208 while (my ($tagfield) = $sth->fetchrow) {
209 $biblio_fields.= $tagfield."9,";
211 chop $biblio_fields;
213 build_tabs ($template, $record, $dbh,"",$query);
215 $template->param(
216 authid => $authid,
217 count => $count,
218 biblio_fields => $biblio_fields,
219 authtypetext => $authority_types->find($authtypecode)->authtypetext,
220 authtypecode => $authtypecode,
221 authority_types => $authority_types,
224 $template->{VARS}->{marcflavour} = C4::Context->preference("marcflavour");
225 output_html_with_http_headers $query, $cookie, $template->output;