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