Bug 34163: Handle both anonymous userenv when generating CSRF tokens
[koha.git] / Koha / Authority.pm
1 package Koha::Authority;
2
3 # Copyright 2015 Koha Development Team
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 use Modern::Perl;
21
22 use base qw(Koha::Object);
23
24 use Koha::Authority::ControlledIndicators;
25 use Koha::SearchEngine::Search;
26
27 =head1 NAME
28
29 Koha::Authority - Koha Authority Object class
30
31 =head1 API
32
33 =head2 Class methods
34
35 =head3 get_usage_count
36
37     $count = $self->get_usage_count;
38
39     Returns the number of linked biblio records.
40
41 =cut
42
43 sub get_usage_count {
44     my ( $self ) = @_;
45     return Koha::Authorities->get_usage_count({ authid => $self->authid });
46 }
47
48 =head3 linked_biblionumbers
49
50     my @biblios = $self->linked_biblionumbers({
51         [ max_results => $max ], [ offset => $offset ],
52     });
53
54     Returns an array of biblionumbers.
55
56 =cut
57
58 sub linked_biblionumbers {
59     my ( $self, $params ) = @_;
60     $params->{authid} = $self->authid;
61     return Koha::Authorities->linked_biblionumbers( $params );
62 }
63
64 =head3 controlled_indicators
65
66     Some authority types control the indicators of some corresponding
67     biblio fields (especially in MARC21).
68     For example, if you have a PERSO_NAME authority (report tag 100), the
69     first indicator of biblio field 600 directly comes from the authority,
70     and the second indicator depends on thesaurus settings in the authority
71     record. Use this method to obtain such controlled values. In this example
72     you should pass 600 in the biblio_tag parameter.
73
74     my $result = $self->controlled_indicators({
75         record => $auth_marc, biblio_tag => $bib_tag
76     });
77     my $ind1 = $result->{ind1};
78     my $ind2 = $result->{ind2};
79     my $subfield_2 = $result->{sub2}; # Optional subfield 2 when ind==7
80
81     If an indicator is not controlled, the result hash does not contain a key
82     for its value. (Same for the sub2 key for an optional subfield $2.)
83
84     Note: The record parameter is a temporary bypass in order to prevent
85     needless conversion of $self->marcxml.
86
87 =cut
88
89 sub controlled_indicators {
90     my ( $self, $params ) = @_;
91     my $tag = $params->{biblio_tag} // q{};
92     my $record = $params->{record};
93
94     my $flavour = C4::Context->preference('marcflavour') eq 'UNIMARC'
95         ? 'UNIMARCAUTH'
96         : 'MARC21';
97     if( !$record ) {
98         $record = $self->record;
99     }
100
101     if( !$self->{_report_tag} ) {
102         my $authtype = Koha::Authority::Types->find( $self->authtypecode );
103         return {} if !$authtype; # very exceptional
104         $self->{_report_tag} = $authtype->auth_tag_to_report;
105     }
106
107     $self->{_ControlledInds} //= Koha::Authority::ControlledIndicators->new;
108     return $self->{_ControlledInds}->get({
109         auth_record => $record,
110         report_tag  => $self->{_report_tag},
111         biblio_tag  => $tag,
112         flavour     => $flavour,
113     });
114 }
115
116 =head3 get_identifiers
117
118     my $identifiers = $author->get_identifiers;
119
120 Return a list of identifiers of the authors which are in 024$2$a
121
122 =cut
123
124 sub get_identifiers {
125     my ( $self, $params ) = @_;
126
127     my $record = $self->record;
128
129     my @identifiers;
130     for my $field ( $record->field('024') ) {
131         my $sf_2 = $field->subfield('2');
132         my $sf_a = $field->subfield('a');
133         next unless $sf_2 && $sf_a;
134         push @identifiers, {source => $sf_2, number => $sf_a, };
135     }
136
137     return \@identifiers;
138 }
139
140 =head3 record
141
142     my $record = $authority->record()
143
144 Return the MARC::Record for this authority
145
146 =cut
147
148 sub record {
149     my ( $self ) = @_;
150
151     my $flavour = $self->record_schema;
152     return MARC::Record->new_from_xml( $self->marcxml, 'UTF-8', $flavour );
153 }
154
155 =head3 record_schema
156
157 my $schema = $biblio->record_schema();
158
159 Returns the record schema (MARC21 or UNIMARCAUTH).
160
161 =cut
162
163 sub record_schema {
164     my ( $self ) = @_;
165
166     return C4::Context->preference('marcflavour') eq 'UNIMARC'
167       ? 'UNIMARCAUTH'
168       : 'MARC21';
169 }
170
171 =head3 to_api_mapping
172
173 This method returns the mapping for representing a Koha::Authority object
174 on the API.
175
176 =cut
177
178 sub to_api_mapping {
179     return {
180         authid            => 'authority_id',
181         authtrees         => undef,
182         authtypecode      => 'framework_id',
183         datecreated       => 'created_date',
184         linkid            => undef,
185         marc              => undef,
186         marcxml           => undef,
187         modification_time => 'modified_date',
188         origincode        => undef,
189     };
190 }
191
192 =head2 Internal methods
193
194 =head3 _type
195
196 =cut
197
198 sub _type {
199     return 'AuthHeader';
200 }
201
202 1;