Code Cleaning : AuthoritiesMARC.
[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 with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA  02111-1307 USA
19
20 =head1 NAME
21
22 opac-authoritiesdetail.pl : script to show an authority in MARC format
23
24 =head1 SYNOPSIS
25
26
27 =head1 DESCRIPTION
28
29 This script needs an authid
30
31 It shows the authority in a (nice) MARC format depending on authority MARC
32 parameters tables.
33
34 =head1 FUNCTIONS
35
36 =over 2
37
38 =cut
39
40 use strict;
41 require Exporter;
42 use C4::AuthoritiesMarc;
43 use C4::Auth;
44 use C4::Context;
45 use C4::Output;
46 use C4::Interface::CGI::Output;
47 use CGI;
48 use MARC::Record;
49 use C4::Koha;
50
51
52 my $query = new CGI;
53
54 my $dbh = C4::Context->dbh;
55
56 my $authid       = $query->param('authid');
57 my $authtypecode = &AUTHfind_authtypecode( $authid );
58 my $tagslib      = &GetTagsLabels( 1, $authtypecode );
59
60 # open template
61 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
62     {
63         template_name   => "opac-authoritiesdetail.tmpl",
64         query           => $query,
65         type            => "opac",
66         authnotrequired => 1,
67         debug           => 1,
68     }
69 );
70
71 my $record;
72 if ( C4::Context->preference("AuthDisplayHierarchy") ) {
73     my $trees = BuildUnimarcHierarchies($authid);
74
75     #   warn "trees :$trees";
76     my @trees = split /;/, $trees;
77     push @trees, $trees unless (@trees);
78     my @loophierarchies;
79     foreach my $tree (@trees) {
80
81         #     warn "tree :$tree";
82
83         my @tree = split /,/, $tree;
84         push @tree, $tree unless (@tree);
85         my $cnt = 0;
86         my @loophierarchy;
87         foreach my $element (@tree) {
88
89             #       warn "tree :$element";
90             my %cell;
91             my $elementdata = GetAuthority( $element );
92             $record = $elementdata if ( $authid == $element );
93             push @loophierarchy,
94               BuildUnimarcHierarchy( $elementdata, "child" . $cnt, $authid );
95             $cnt++;
96         }
97         push @loophierarchies, { 'loopelement' => \@loophierarchy };
98         $template->param(
99             'displayhierarchy' =>
100               C4::Context->preference("AuthDisplayHierarchy"),
101             'loophierarchies' => \@loophierarchies,
102         );
103     }
104 }
105 else {
106     $record = GetAuthority( $authid );
107 }
108 my $count = CountUsage($authid);
109
110 # find the marc field/subfield used in biblio by this authority
111 my $sth =
112   $dbh->prepare(
113     "select distinct tagfield from marc_subfield_structure where authtypecode=?"
114   );
115 $sth->execute($authtypecode);
116 my $biblio_fields;
117 while ( my ($tagfield) = $sth->fetchrow ) {
118     $biblio_fields .= $tagfield . "9,";
119 }
120 chop $biblio_fields;
121
122 # fill arrays
123 my @loop_data = ();
124 my $tag;
125
126 # loop through each tab 0 through 9
127 # for (my $tabloop = 0; $tabloop<=10;$tabloop++) {
128 # loop through each tag
129 my @fields    = $record->fields();
130 foreach my $field (@fields) {
131     my @subfields_data;
132
133     # if tag <10, there's no subfield, use the "@" trick
134     if ( $field->tag() < 10 ) {
135         next if ( $tagslib->{ $field->tag() }->{'@'}->{hidden} );
136         my %subfield_data;
137         $subfield_data{marc_lib}   = $tagslib->{ $field->tag() }->{'@'}->{lib};
138         $subfield_data{marc_value} = $field->data();
139         $subfield_data{marc_subfield} = '@';
140         $subfield_data{marc_tag}      = $field->tag();
141         push( @subfields_data, \%subfield_data );
142     }
143     else {
144         my @subf = $field->subfields;
145
146         # loop through each subfield
147         for my $i ( 0 .. $#subf ) {
148             $subf[$i][0] = "@" unless $subf[$i][0];
149             next if ( $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{hidden} );
150             my %subfield_data;
151             $subfield_data{marc_lib} =
152               $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{lib};
153             if ( $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{isurl} ) {
154                 $subfield_data{marc_value} =
155                   "<a href=\"$subf[$i][1]\">$subf[$i][1]</a>";
156             }
157             else {
158                 $subfield_data{marc_value} = $subf[$i][1];
159             }
160             $subfield_data{marc_subfield} = $subf[$i][0];
161             $subfield_data{marc_tag}      = $field->tag();
162             push( @subfields_data, \%subfield_data );
163         }
164     }
165     if ( $#subfields_data >= 0 ) {
166         my %tag_data;
167         $tag_data{tag} =
168           $field->tag() . ' -' . $tagslib->{ $field->tag() }->{lib};
169         $tag_data{subfield} = \@subfields_data;
170         push( @loop_data, \%tag_data );
171     }
172 }
173 $template->param( "0XX" => \@loop_data );
174
175 my $authtypes = getauthtypes;
176 my @authtypesloop;
177 foreach my $thisauthtype ( keys %$authtypes ) {
178     my $selected = 1 if $thisauthtype eq $authtypecode;
179     my %row = (
180         value        => $thisauthtype,
181         selected     => $selected,
182         authtypetext => $authtypes->{$thisauthtype}{'authtypetext'},
183     );
184     push @authtypesloop, \%row;
185 }
186
187 $template->param(
188     authid               => $authid,
189     count                => $count,
190     biblio_fields        => $biblio_fields,
191     authtypetext         => $authtypes->{$authtypecode}{'authtypetext'},
192     authtypesloop        => \@authtypesloop,
193     LibraryName          => C4::Context->preference("LibraryName"),
194     OpacNav              => C4::Context->preference("OpacNav"),
195     opaccredits          => C4::Context->preference("opaccredits"),
196     opacsmallimage       => C4::Context->preference("opacsmallimage"),
197     opaclayoutstylesheet => C4::Context->preference("opaclayoutstylesheet"),
198     opaccolorstylesheet  => C4::Context->preference("opaccolorstylesheet"),
199 );
200 output_html_with_http_headers $query, $cookie, $template->output;
201