Bug 11401: QA followup
[koha.git] / authorities / blinddetail-biblio-search.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 blinddetail-biblio-search.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 use strict;
40 use warnings;
41
42 use C4::AuthoritiesMarc;
43 use C4::Auth;
44 use C4::Context;
45 use C4::Output;
46 use CGI;
47 use MARC::Record;
48 use C4::Koha;
49
50 my $query = new CGI;
51
52 my $dbh = C4::Context->dbh;
53
54 my $authid       = $query->param('authid');
55 my $index        = $query->param('index');
56 my $tagid        = $query->param('tagid');
57 my $relationship = $query->param('relationship');
58 my $authtypecode = &GetAuthTypeCode($authid);
59 my $tagslib      = &GetTagsLabels( 1, $authtypecode );
60
61 my $auth_type = GetAuthType($authtypecode);
62 my $record;
63 if ($authid) {
64     $record = GetAuthority($authid);
65 }
66
67 # open template
68 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
69     {
70         template_name   => "authorities/blinddetail-biblio-search.tt",
71         query           => $query,
72         type            => "intranet",
73         authnotrequired => 0,
74         flagsrequired   => { editcatalogue => 'edit_catalogue' },
75     }
76 );
77
78 # fill arrays
79 my @subfield_loop;
80 my ($indicator1, $indicator2);
81 if ($authid) {
82     my @fields = $record->field( $auth_type->{auth_tag_to_report} );
83     my $repet = ($query->param('repet') || 1) - 1;
84     my $field = $fields[$repet];
85
86     # Get all values for each distinct subfield
87     my %subfields;
88     for ( $field->subfields ) {
89         next if $_->[0] eq '9'; # $9 will be set with authid value
90         my $letter = $_->[0];
91         next if defined $subfields{$letter};
92         my @values = $field->subfield($letter);
93         $subfields{$letter} = \@values;
94     }
95
96     # Add all subfields to the subfield_loop
97     for( keys %subfields ) {
98         my $letter = $_ || '@';
99         push( @subfield_loop, {marc_subfield => $letter, marc_values => $subfields{$_}} );
100     }
101
102     push( @subfield_loop, { marc_subfield => 'w', marc_values => $relationship } ) if ( $relationship );
103     if (C4::Context->preference('marcflavour') eq 'UNIMARC') {
104         $indicator1 = $field->indicator('1');
105         $indicator2 = $field->indicator('2');
106     } elsif (C4::Context->preference('marcflavour') eq 'MARC21') {
107         my $tag_from = $auth_type->{auth_tag_to_report};
108         my $tag_to = $index;
109         $tag_to =~ s/^tag_(\d*)_.*$/$1/;
110         if ($tag_to =~ /^6/) {  # subject heading
111             my %thes_mapping = qw / a 0
112                                     b 1
113                                     c 2
114                                     d 3
115                                     k 5
116                                     n 4
117                                     r 7
118                                     s 7
119                                     v 6
120                                     z 7
121                                     | 4 /;
122             my $thes_008_11 = '';
123             $thes_008_11 = substr($record->field('008')->data(), 11, 1) if $record->field('008')->data();
124             $indicator2 = defined $thes_mapping{$thes_008_11} ? $thes_mapping{$thes_008_11} : $thes_008_11;
125             if ($indicator2 eq '7') {
126                 if ($thes_008_11 eq 'r') {
127                     $subfields{'2'} = ['aat'];
128                 } elsif ($thes_008_11 eq 's') {
129                     $subfields{'2'} = ['sears'];
130                 }
131             }
132         }
133         if ($tag_from eq '130') {  # unified title -- the special case
134             if ($tag_to eq '830' || $tag_to eq '240') {
135                 $indicator2 = $field->indicator('2');
136             } else {
137                 $indicator1 = $field->indicator('2');
138             }
139         } else {
140             $indicator1 = $field->indicator('1');
141         }
142     }
143 }
144 else {
145     # authid is empty => the user want to empty the entry.
146     $template->param( "clear" => 1 );
147 }
148
149 # Extract the tag number from the index
150 my $tag_number = $index;
151 $tag_number =~ s/^tag_(\d*)_.*$/$1/;
152
153 # Remove spaces in indicators
154 $indicator1 =~ s/\s//g;
155 $indicator2 =~ s/\s//g;
156
157 $template->param(
158     authid          => $authid ? $authid : "",
159     index           => $index,
160     tagid           => $tagid,
161     indicator1      => $indicator1,
162     indicator2      => $indicator2,
163     SUBFIELD_LOOP   => \@subfield_loop,
164     tag_number      => $tag_number,
165 );
166
167 output_html_with_http_headers $query, $cookie, $template->output;
168