Bug 13799: Rename t/.../v1/borrowers.t to t/.../v1/patrons.t
[koha.git] / Koha / Authority.pm
blob7097bd99887265e473c2173c37b8ef4982a2ac30
1 package Koha::Authority;
3 # Copyright 2012 C & P Bibliography Services
5 # This file is part of Koha.
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.
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.
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.
20 =head1 NAME
22 Koha::Authority - class to encapsulate authority records in Koha
24 =head1 SYNOPSIS
26 Object-oriented class that encapsulates authority records in Koha.
28 =head1 DESCRIPTION
30 Authority data.
32 =cut
34 use strict;
35 use warnings;
36 use C4::Context;
37 use MARC::Record;
38 use MARC::File::XML;
39 use C4::Charset;
40 use Koha::Util::MARC;
42 use base qw(Koha::MetadataRecord);
44 __PACKAGE__->mk_accessors(qw( authid authtype ));
46 =head2 new
48 my $auth = Koha::Authority->new($record);
50 Create a new Koha::Authority object based on the provided record.
52 =cut
54 sub new {
55 my $class = shift;
56 my $record = shift;
58 my $self = $class->SUPER::new(
60 'record' => $record,
61 'schema' => lc C4::Context->preference("marcflavour")
65 bless $self, $class;
66 return $self;
70 =head2 get_from_authid
72 my $auth = Koha::Authority->get_from_authid($authid);
74 Create the Koha::Authority object associated with the provided authid.
75 Note that this routine currently retrieves a MARC record because
76 authorities in Koha are MARC records by definition. This is an
77 unfortunate but unavoidable fact.
79 =cut
81 sub get_from_authid {
82 my $class = shift;
83 my $authid = shift;
84 my $marcflavour = lc C4::Context->preference("marcflavour");
86 my $dbh=C4::Context->dbh;
87 my $sth=$dbh->prepare("select authtypecode, marcxml from auth_header where authid=?");
88 $sth->execute($authid);
89 my ($authtypecode, $marcxml) = $sth->fetchrow;
90 my $record=eval {MARC::Record->new_from_xml(StripNonXmlChars($marcxml),'UTF-8',
91 (C4::Context->preference("marcflavour") eq "UNIMARC"?"UNIMARCAUTH":C4::Context->preference("marcflavour")))};
92 return if ($@);
93 $record->encoding('UTF-8');
95 # NOTE: GuessAuthTypeCode has no business in Koha::Authority, which is an
96 # object-oriented class. Eventually perhaps there will be utility
97 # classes in the Koha:: namespace, but there are not at the moment,
98 # so this shim seems like the best option all-around.
99 require C4::AuthoritiesMarc;
100 $authtypecode ||= C4::AuthoritiesMarc::GuessAuthTypeCode($record);
102 my $self = $class->SUPER::new( { authid => $authid,
103 authtype => $authtypecode,
104 schema => $marcflavour,
105 record => $record });
107 bless $self, $class;
108 return $self;
111 =head2 get_from_breeding
113 my $auth = Koha::Authority->get_from_authid($authid);
115 Create the Koha::Authority object associated with the provided authid.
117 =cut
119 sub get_from_breeding {
120 my $class = shift;
121 my $import_record_id = shift;
122 my $marcflavour = lc C4::Context->preference("marcflavour");
124 my $dbh=C4::Context->dbh;
125 my $sth=$dbh->prepare("select marcxml from import_records where import_record_id=? and record_type='auth';");
126 $sth->execute($import_record_id);
127 my $marcxml = $sth->fetchrow;
128 my $record=eval {MARC::Record->new_from_xml(StripNonXmlChars($marcxml),'UTF-8',
129 (C4::Context->preference("marcflavour") eq "UNIMARC"?"UNIMARCAUTH":C4::Context->preference("marcflavour")))};
130 return if ($@);
131 $record->encoding('UTF-8');
133 # NOTE: GuessAuthTypeCode has no business in Koha::Authority, which is an
134 # object-oriented class. Eventually perhaps there will be utility
135 # classes in the Koha:: namespace, but there are not at the moment,
136 # so this shim seems like the best option all-around.
137 require C4::AuthoritiesMarc;
138 my $authtypecode = C4::AuthoritiesMarc::GuessAuthTypeCode($record);
140 my $self = $class->SUPER::new( {
141 schema => $marcflavour,
142 authtype => $authtypecode,
143 record => $record });
145 bless $self, $class;
146 return $self;
149 sub authorized_heading {
150 my ($self) = @_;
151 if ($self->schema =~ m/marc/) {
152 return Koha::Util::MARC::getAuthorityAuthorizedHeading($self->record, $self->schema);
154 return;