Bug 35285: Add non-html template support to html_content wrapping
[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 use C4::Heading qw( new_from_field );
28
29 =head1 NAME
30
31 Koha::Authority - Koha Authority Object class
32
33 =head1 API
34
35 =head2 Class methods
36
37 =head3 get_usage_count
38
39     $count = $self->get_usage_count;
40
41     Returns the number of linked biblio records.
42
43 =cut
44
45 sub get_usage_count {
46     my ( $self ) = @_;
47     return Koha::Authorities->get_usage_count({ authid => $self->authid });
48 }
49
50 =head3 linked_biblionumbers
51
52     my @biblios = $self->linked_biblionumbers({
53         [ max_results => $max ], [ offset => $offset ],
54     });
55
56     Returns an array of biblionumbers.
57
58 =cut
59
60 sub linked_biblionumbers {
61     my ( $self, $params ) = @_;
62     $params->{authid} = $self->authid;
63     return Koha::Authorities->linked_biblionumbers( $params );
64 }
65
66 =head3 heading_object
67
68     Routine to return the C4::Heading object for this authority
69
70 =cut
71
72 sub heading_object {
73
74     my ( $self, $params ) = @_;
75     my $record = $params->{record};
76
77     if ( !$self->{_report_tag} ) {
78         my $authtype = Koha::Authority::Types->find( $self->authtypecode );
79         return {} if !$authtype;    # very exceptional
80         $self->{_report_tag} = $authtype->auth_tag_to_report;
81     }
82
83     if ( !$record ) {
84         $record = $self->record;
85     }
86     my $field   = $record->field( $self->{_report_tag} );
87     my $heading = C4::Heading->new_from_field( $field, undef, 1 );    #new auth heading
88     return $heading;
89
90 }
91
92 =head3 controlled_indicators
93
94     Some authority types control the indicators of some corresponding
95     biblio fields (especially in MARC21).
96     For example, if you have a PERSO_NAME authority (report tag 100), the
97     first indicator of biblio field 600 directly comes from the authority,
98     and the second indicator depends on thesaurus settings in the authority
99     record. Use this method to obtain such controlled values. In this example
100     you should pass 600 in the biblio_tag parameter.
101
102     my $result = $self->controlled_indicators({
103         record => $auth_marc, biblio_tag => $bib_tag
104     });
105     my $ind1 = $result->{ind1};
106     my $ind2 = $result->{ind2};
107     my $subfield_2 = $result->{sub2}; # Optional subfield 2 when ind==7
108
109     If an indicator is not controlled, the result hash does not contain a key
110     for its value. (Same for the sub2 key for an optional subfield $2.)
111
112     Note: The record parameter is a temporary bypass in order to prevent
113     needless conversion of $self->marcxml.
114
115 =cut
116
117 sub controlled_indicators {
118     my ( $self, $params ) = @_;
119     my $tag = $params->{biblio_tag} // q{};
120     my $record = $params->{record};
121
122     my $flavour = C4::Context->preference('marcflavour') eq 'UNIMARC'
123         ? 'UNIMARCAUTH'
124         : 'MARC21';
125     if( !$record ) {
126         $record = $self->record;
127     }
128
129     if( !$self->{_report_tag} ) {
130         my $authtype = Koha::Authority::Types->find( $self->authtypecode );
131         return {} if !$authtype; # very exceptional
132         $self->{_report_tag} = $authtype->auth_tag_to_report;
133     }
134
135     $self->{_ControlledInds} //= Koha::Authority::ControlledIndicators->new;
136     return $self->{_ControlledInds}->get({
137         auth_record => $record,
138         report_tag  => $self->{_report_tag},
139         biblio_tag  => $tag,
140         flavour     => $flavour,
141     });
142 }
143
144 =head3 get_identifiers_and_information
145
146     my $information = $author->get_identifiers_and_information;
147
148 Return a list of information of the authors (syspref OPACAuthorIdentifiersAndInformation)
149
150 =cut
151
152 sub get_identifiers_and_information {
153     my ($self) = @_;
154
155     my $record = $self->record;
156
157     # FIXME UNIMARC not supported yet.
158     return if C4::Context->preference('marcflavour') eq 'UNIMARC';
159
160     my $information;
161     for my $info ( split ',', C4::Context->preference('OPACAuthorIdentifiersAndInformation') ) {
162         if ( $info eq 'identifiers' ) {
163
164             # identifiers (024$2$a)
165             for my $field ( $record->field('024') ) {
166                 my $sf_2 = $field->subfield('2');
167                 my $sf_a = $field->subfield('a');
168                 next unless $sf_2 && $sf_a;
169                 push @{ $information->{identifiers} }, { source => $sf_2, number => $sf_a, };
170             }
171         } elsif ( $info eq 'activity' ) {
172
173             # activity: Activity (372$a$s$t)
174             for my $field ( $record->field('372') ) {
175                 my $sf_a = $field->subfield('a');
176                 my $sf_s = $field->subfield('s');
177                 my $sf_t = $field->subfield('t');
178                 push @{ $information->{activity} },
179                     { field_of_activity => $sf_a, start_period => $sf_s, end_period => $sf_t, };
180             }
181         } elsif ( $info eq 'address' ) {
182
183             # address: Address (371$a$b$d$e)
184             for my $field ( $record->field('371') ) {
185                 my $sf_a = $field->subfield('a');
186                 my $sf_b = $field->subfield('b');
187                 my $sf_d = $field->subfield('d');
188                 my $sf_e = $field->subfield('e');
189                 push @{ $information->{address} },
190                     { address => $sf_a, city => $sf_b, country => $sf_d, postal_code => $sf_e, };
191             }
192         } elsif ( $info eq 'associated_group' ) {
193
194             # associated_group: Associated group (373$a$s$t$u$v$0)
195             for my $field ( $record->field('373') ) {
196                 my $sf_a = $field->subfield('a');
197                 my $sf_s = $field->subfield('s');
198                 my $sf_t = $field->subfield('t');
199                 my $sf_u = $field->subfield('u');
200                 my $sf_v = $field->subfield('v');
201                 my $sf_0 = $field->subfield('0');
202                 push @{ $information->{associated_group} },
203                     {
204                     associated_group      => $sf_a, start_period            => $sf_s, end_period => $sf_t, uri => $sf_u,
205                     source_of_information => $sf_v, authority_record_number => $sf_0,
206                     };
207             }
208         } elsif ( $info eq 'email_address' ) {
209
210             # email_address: Electronic mail address (371$m)
211             for my $field ( $record->field('371') ) {
212                 my $sf_m = $field->subfield('m');
213                 push @{ $information->{email_address} }, { email_address => $sf_m, };
214             }
215         } elsif ( $info eq 'occupation' ) {
216
217             # occupation: Occupation (374$a$s$t$u$v$0)
218             for my $field ( $record->field('374') ) {
219                 my $sf_a = $field->subfield('a');
220                 my $sf_s = $field->subfield('s');
221                 my $sf_t = $field->subfield('t');
222                 my $sf_u = $field->subfield('u');
223                 my $sf_v = $field->subfield('v');
224                 my $sf_0 = $field->subfield('0');
225                 push @{ $information->{occupation} },
226                     {
227                     occupation            => $sf_a, start_period            => $sf_s, end_period => $sf_t, uri => $sf_u,
228                     source_of_information => $sf_v, authority_record_number => $sf_0,
229                     };
230             }
231         } elsif ( $info eq 'place_of_birth' ) {
232
233             # place_of_birth: Place of birth (370$a)
234             for my $field ( $record->field('370') ) {
235                 my $sf_a = $field->subfield('a');
236                 push @{ $information->{place_of_birth} }, { place_of_birth => $sf_a, };
237             }
238         } elsif ( $info eq 'place_of_death' ) {
239
240             # place_of_death: Place of death (370$b)
241             for my $field ( $record->field('370') ) {
242                 my $sf_b = $field->subfield('b');
243                 push @{ $information->{place_of_death} }, { place_of_death => $sf_b, };
244             }
245         } elsif ( $info eq 'uri' ) {
246
247             # uri: URI (371$u)
248             for my $field ( $record->field('371') ) {
249                 my $sf_u = $field->subfield('u');
250                 push @{ $information->{uri} }, { uri => $sf_u, };
251             }
252         }
253     }
254
255     return $information;
256 }
257
258
259 =head3 record
260
261     my $record = $authority->record()
262
263 Return the MARC::Record for this authority
264
265 =cut
266
267 sub record {
268     my ( $self ) = @_;
269
270     my $flavour = $self->record_schema;
271     return MARC::Record->new_from_xml( $self->marcxml, 'UTF-8', $flavour );
272 }
273
274 =head3 record_schema
275
276 my $schema = $biblio->record_schema();
277
278 Returns the record schema (MARC21 or UNIMARCAUTH).
279
280 =cut
281
282 sub record_schema {
283     my ( $self ) = @_;
284
285     return C4::Context->preference('marcflavour') eq 'UNIMARC'
286       ? 'UNIMARCAUTH'
287       : 'MARC21';
288 }
289
290 =head3 to_api_mapping
291
292 This method returns the mapping for representing a Koha::Authority object
293 on the API.
294
295 =cut
296
297 sub to_api_mapping {
298     return {
299         authid            => 'authority_id',
300         authtrees         => undef,
301         authtypecode      => 'framework_id',
302         datecreated       => 'created_date',
303         linkid            => undef,
304         marc              => undef,
305         marcxml           => undef,
306         modification_time => 'modified_date',
307         origincode        => undef,
308     };
309 }
310
311 =head2 Internal methods
312
313 =head3 _type
314
315 =cut
316
317 sub _type {
318     return 'AuthHeader';
319 }
320
321 1;