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