Bug 9535 - Patron card creator "Remove duplicates" function doesn't work
[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 $authid = int($authid);
71 my $record = GetAuthority( $authid );
72 if ( ! $record ) {
73     print $query->redirect("/cgi-bin/koha/errors/404.pl"); # escape early
74     exit;
75 }
76
77 my $authtypecode = &GetAuthTypeCode( $authid );
78
79 if ($display_hierarchy){
80   my $trees=BuildUnimarcHierarchies($authid);
81   my @trees = split /;/,$trees ;
82   push @trees,$trees unless (@trees);
83   my @loophierarchies;
84   foreach my $tree (@trees){
85     my @tree=split /,/,$tree;
86     push @tree,$tree unless (@tree);
87     my $cnt=0;
88     my @loophierarchy;
89     foreach my $element (@tree){
90       my $cell;
91       my $elementdata = GetAuthority($element);
92       push @loophierarchy, BuildUnimarcHierarchy($elementdata,"child".$cnt, $authid);
93       $cnt++;
94     }
95     push @loophierarchies, { 'loopelement' =>\@loophierarchy};
96   }
97   $template->param(
98     'displayhierarchy' => $display_hierarchy,
99     'loophierarchies' =>\@loophierarchies,
100   );
101 }
102 my $count = CountUsage($authid);
103
104
105 my $authtypes     = getauthtypes();
106 my @authtypesloop = ();
107 foreach my $thisauthtype ( keys %{$authtypes} ) {
108     push @authtypesloop,
109          { value        => $thisauthtype,
110            selected     => $thisauthtype eq $authtypecode,
111            authtypetext => $authtypes->{$thisauthtype}{'authtypetext'},
112          };
113 }
114 $template->{VARS}->{'authtypesloop'} = \@authtypesloop;
115 $template->{VARS}->{'authtypetext'}  = $authtypes->{$authtypecode}{'authtypetext'};
116 $template->{VARS}->{'authid'}        = $authid;
117 $template->{VARS}->{'count'}         = $count;
118
119 # find the marc field/subfield used in biblio by this authority
120 if ($show_marc) {
121     my $tagslib = &GetTagsLabels( 0, $authtypecode );
122     my $sth =
123         $dbh->prepare(
124                 "select distinct tagfield from marc_subfield_structure where authtypecode=?"
125                 );
126     $sth->execute($authtypecode);
127     my $biblio_fields;
128     while ( my ($tagfield) = $sth->fetchrow ) {
129         $biblio_fields .= $tagfield . "9,";
130     }
131     chop $biblio_fields;
132
133 # fill arrays
134     my @loop_data = ();
135     my $tag;
136
137 # loop through each tag
138     my @fields    = $record->fields();
139     foreach my $field (@fields) {
140         my @subfields_data;
141
142 # skip UNIMARC fields <200, they are useless for a patron
143         next if C4::Context->preference('marcflavour') eq 'UNIMARC' && $field->tag() <200;
144
145 # if tag <10, there's no subfield, use the "@" trick
146         if ( $field->tag() < 10 ) {
147             next if ( $tagslib->{ $field->tag() }->{'@'}->{hidden} );
148             my %subfield_data;
149             $subfield_data{marc_lib}   = $tagslib->{ $field->tag() }->{'@'}->{lib};
150             $subfield_data{marc_value} = $field->data();
151             $subfield_data{marc_subfield} = '@';
152             $subfield_data{marc_tag}      = $field->tag();
153             push( @subfields_data, \%subfield_data );
154         }
155         elsif ( C4::Context->preference('marcflavour') eq 'MARC21' && $field->tag() eq 667 ) {
156             # tagfield 667 is a nonpublic general note in MARC21, which shouldn't be shown in the OPAC
157         }
158         else {
159             my @subf = $field->subfields;
160
161 # loop through each subfield
162             for my $i ( 0 .. $#subf ) {
163                 $subf[$i][0] = "@" unless $subf[$i][0];
164                 next if ( $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{hidden} );
165 # skip useless subfields (for patrons)
166                 next if $subf[$i][0] =~ /7|8|9/;
167                 my %subfield_data;
168                 $subfield_data{marc_lib} =
169                     $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{lib};
170                 $subfield_data{marc_subfield} = $subf[$i][0];
171                 $subfield_data{marc_tag}      = $field->tag();
172                 $subfield_data{isurl} =  $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{isurl};
173                 $subfield_data{marc_value} = $subf[$i][1];
174                 push( @subfields_data, \%subfield_data );
175             }
176         }
177         if ( $#subfields_data >= 0 ) {
178             my %tag_data;
179             $tag_data{tag} =
180                 $field->tag()
181                 . ' '
182                 . C4::Koha::display_marc_indicators($field)
183                 . ' - ' . $tagslib->{ $field->tag() }->{lib};
184             $tag_data{subfield} = \@subfields_data;
185             push( @loop_data, \%tag_data );
186         }
187     }
188     $template->param( "Tab0XX" => \@loop_data );
189 } else {
190     my $summary = BuildSummary($record, $authid, $authtypecode);
191     $template->{VARS}->{'summary'} = $summary;
192 }
193
194 output_html_with_http_headers $query, $cookie, $template->output;
195