Bug 15381: Remove getauthtypes and getauthtype
[koha.git] / authorities / detail.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 detail.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
40 use strict;
41 use warnings;
42
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
51 use Koha::Authority::Types;
52
53 our ($tagslib);
54
55 sub build_tabs {
56     my ( $template, $record, $dbh, $encoding,$input ) = @_;
57
58     # fill arrays
59     my @loop_data = ();
60     my $tag;
61
62     # in this array, we will push all the 10 tabs
63     # to avoid having 10 tabs in the template : they will all be in the same BIG_LOOP
64     my @BIG_LOOP;
65     my %seen;
66     my @tab_data; # all tags to display
67     
68     foreach my $used ( keys %$tagslib ){
69         push @tab_data,$used if not $seen{$used};
70         $seen{$used}++;
71     }
72         
73     my $max_num_tab=9;
74     # loop through each tab 0 through 9
75     for ( my $tabloop = 0 ; $tabloop <= $max_num_tab ; $tabloop++ ) {
76         my @loop_data = (); #innerloop in the template.
77         my $i = 0;
78         foreach my $tag (sort @tab_data) {
79             $i++;
80             next if ! $tag;
81
82             # if MARC::Record is not empty =>use it as master loop, then add missing subfields that should be in the tab.
83             # if MARC::Record is empty => use tab as master loop.
84             if ( $record != -1 && ( $record->field($tag) || $tag eq '000' ) ) {
85                 my @fields;
86                 if ( $tag ne '000' ) {
87                     @fields = $record->field($tag);
88                 }
89                 else {
90                   push @fields, MARC::Field->new('000', $record->leader()); # if tag == 000
91                 }
92                 # loop through each field
93                 foreach my $field (@fields) {
94                     my @subfields_data;
95                     if ($field->tag()<10) {
96                         next
97                         if (
98                             $tagslib->{ $field->tag() }->{ '@' }->{tab}
99                             ne $tabloop );
100                       next if ($tagslib->{$field->tag()}->{'@'}->{hidden});
101                       my %subfield_data;
102                       $subfield_data{marc_lib}=$tagslib->{$field->tag()}->{'@'}->{lib};
103                       $subfield_data{marc_value}=$field->data();
104                       $subfield_data{marc_subfield}='@';
105                       $subfield_data{marc_tag}=$field->tag();
106                       push(@subfields_data, \%subfield_data);
107                     } else {
108                       my @subf=$field->subfields;
109                   # loop through each subfield
110                       for my $i (0..$#subf) {
111                         $subf[$i][0] = "@" unless defined $subf[$i][0];
112                         next
113                         if (
114                             $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{tab}
115                             ne $tabloop );
116                         next
117                         if ( $tagslib->{ $field->tag() }->{ $subf[$i][0] }
118                             ->{hidden} );
119                         my %subfield_data;
120                         $subfield_data{marc_lib}=$tagslib->{$field->tag()}->{$subf[$i][0]}->{lib};
121                         if ($tagslib->{$field->tag()}->{$subf[$i][0]}->{isurl}) {
122                           $subfield_data{marc_value}="<a href=\"$subf[$i][1]\">$subf[$i][1]</a>";
123                         } else {
124                           $subfield_data{marc_value}=$subf[$i][1];
125                         }
126                               $subfield_data{short_desc} = substr(
127                                   $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{lib},
128                                   0, 20
129                               );
130                               $subfield_data{long_desc} =
131                                 $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{lib};
132                         $subfield_data{marc_subfield}=$subf[$i][0];
133                         $subfield_data{marc_tag}=$field->tag();
134                         push(@subfields_data, \%subfield_data);
135                       }
136                     }
137                     if ($#subfields_data>=0) {
138                       my %tag_data;
139                       $tag_data{tag}=$field->tag(). ' '  
140                                      . C4::Koha::display_marc_indicators($field) 
141                                      . ' - '
142                                      . $tagslib->{$field->tag()}->{lib};
143                       $tag_data{subfield} = \@subfields_data;
144                       push (@loop_data, \%tag_data);
145                     }
146                   }
147               }
148             }
149             if ( $#loop_data >= 0 ) {
150                 push @BIG_LOOP, {
151                     number    => $tabloop,
152                     innerloop => \@loop_data,
153                 };
154             }
155         }
156         $template->param( singletab => (scalar(@BIG_LOOP)==1), BIG_LOOP => \@BIG_LOOP );
157 }
158
159
160
161 my $query=new CGI;
162
163 my $dbh=C4::Context->dbh;
164
165 # open template
166 my ($template, $loggedinuser, $cookie) = get_template_and_user(
167     {
168         template_name   => "authorities/detail.tt",
169         query           => $query,
170         type            => "intranet",
171         authnotrequired => 0,
172         flagsrequired   => { catalogue => 1 },
173         debug           => 1,
174     }
175 );
176
177 my $authid = $query->param('authid');
178
179 my $authtypecode = GetAuthTypeCode($authid);
180 $tagslib = &GetTagsLabels(1,$authtypecode);
181
182 # Build list of authtypes for showing them
183 my $authority_types = Koha::Authority::Types->search({}, { order_by => ['authtypecode']});
184
185 my $record=GetAuthority($authid);
186
187 if (not defined $record) {
188     # authid invalid
189     $template->param ( errauthid => $authid,
190                        unknownauthid => 1,
191                        authority_types => $authority_types, );
192     output_html_with_http_headers $query, $cookie, $template->output;
193     exit;
194 }
195
196 if (C4::Context->preference("AuthDisplayHierarchy")){
197     $template->{VARS}->{'displayhierarchy'} = C4::Context->preference("AuthDisplayHierarchy");
198     $template->{VARS}->{'loophierarchies'} = GenerateHierarchy($authid);
199 }
200
201 my $count = CountUsage($authid);
202
203 # find the marc field/subfield used in biblio by this authority
204 my $sth = $dbh->prepare("select distinct tagfield from marc_subfield_structure where authtypecode=?");
205 $sth->execute($authtypecode);
206 my $biblio_fields;
207 while (my ($tagfield) = $sth->fetchrow) {
208         $biblio_fields.= $tagfield."9,";
209 }
210 chop $biblio_fields;
211
212 build_tabs ($template, $record, $dbh,"",$query);
213
214 $template->param(
215     authid          => $authid,
216     count           => $count,
217     biblio_fields   => $biblio_fields,
218     authtypetext    => $authority_types->find($authtypecode)->authtypetext,
219     authtypecode    => $authtypecode,
220     authority_types => $authority_types,
221 );
222
223 $template->{VARS}->{marcflavour} = C4::Context->preference("marcflavour");
224 output_html_with_http_headers $query, $cookie, $template->output;
225