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