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