Bug 12478 - authorities can now be stored in ES
[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 under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 3 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 use Modern::Perl;
21
22 use Carp;
23
24 use Koha::Database;
25 use C4::Context;
26 use MARC::Record;
27
28 use base qw(Koha::Object);
29
30 =head1 NAME
31
32 Koha::Authority - Koha Authority Object class
33
34 =head1 API
35
36 =head2 Class Methods
37
38 =cut
39
40 =head3 type
41
42 =cut
43
44 sub _type {
45     return 'AuthHeader';
46 }
47
48 =head2 get_all_authorities_iterator
49
50     my $it = Koha::Authority->get_all_authorities_iterator();
51
52 This will provide an iterator object that will, one by one, provide the
53 Koha::Authority of each authority.
54
55 The iterator is a Koha::MetadataIterator object.
56
57 =cut
58
59 sub get_all_authorities_iterator {
60     my $database = Koha::Database->new();
61     my $schema   = $database->schema();
62     my $rs =
63       $schema->resultset('AuthHeader')->search( { marcxml => { '!=', undef } },
64         { columns => [qw/ authid authtypecode marcxml /] } );
65     my $next_func = sub {
66         my $row = $rs->next();
67         return undef if !$row;
68         my $authid       = $row->authid;
69         my $authtypecode = $row->authtypecode;
70         my $marcxml      = $row->marcxml;
71
72         my $record = eval {
73             MARC::Record->new_from_xml(
74                 StripNonXmlChars($marcxml),
75                 'UTF-8',
76                 (
77                     C4::Context->preference("marcflavour") eq "UNIMARC"
78                     ? "UNIMARCAUTH"
79                     : C4::Context->preference("marcflavour")
80                 )
81             );
82         };
83         confess $@ if ($@);
84         $record->encoding('UTF-8');
85
86         # I'm not sure why we don't use the authtypecode from the database,
87         # but this is how the original code does it.
88         require C4::AuthoritiesMarc;
89         $authtypecode = C4::AuthoritiesMarc::GuessAuthTypeCode($record);
90
91         my $auth = __PACKAGE__->new( $record, $authid, $authtypecode );
92
93         return $auth;
94       };
95       return Koha::MetadataIterator->new($next_func);
96 }
97
98 1;