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