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 2 of the License, or (at your option) any later
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.
22 Koha::Authority - class to encapsulate authority records in Koha
26 Object-oriented class that encapsulates authority records in Koha.
42 use base qw(Koha::MetadataRecord);
44 __PACKAGE__->mk_accessors(qw( authid authtype ));
48 my $auth = Koha::Authority->new($record);
50 Create a new Koha::Authority object based on the provided record.
57 my $self = $class->SUPER::new(
60 'schema' => lc C4::Context->preference("marcflavour")
69 =head2 get_from_authid
71 my $auth = Koha::Authority->get_from_authid($authid);
73 Create the Koha::Authority object associated with the provided authid.
74 Note that this routine currently retrieves a MARC record because
75 authorities in Koha are MARC records by definition. This is an
76 unfortunate but unavoidable fact.
82 my $marcflavour = lc C4::Context->preference("marcflavour");
84 my $dbh=C4::Context->dbh;
85 my $sth=$dbh->prepare("select authtypecode, marcxml from auth_header where authid=?");
86 $sth->execute($authid);
87 my ($authtypecode, $marcxml) = $sth->fetchrow;
88 my $record=eval {MARC::Record->new_from_xml(StripNonXmlChars($marcxml),'UTF-8',
89 (C4::Context->preference("marcflavour") eq "UNIMARC"?"UNIMARCAUTH":C4::Context->preference("marcflavour")))};
91 $record->encoding('UTF-8');
93 # NOTE: GuessAuthTypeCode has no business in Koha::Authority, which is an
94 # object-oriented class. Eventually perhaps there will be utility
95 # classes in the Koha:: namespace, but there are not at the moment,
96 # so this shim seems like the best option all-around.
97 require C4::AuthoritiesMarc;
98 $authtypecode ||= C4::AuthoritiesMarc::GuessAuthTypeCode($record);
100 my $self = $class->SUPER::new( { authid => $authid,
101 authtype => $authtypecode,
102 schema => $marcflavour,
103 record => $record });
109 =head2 get_from_breeding
111 my $auth = Koha::Authority->get_from_authid($authid);
113 Create the Koha::Authority object associated with the provided authid.
116 sub get_from_breeding {
118 my $import_record_id = shift;
119 my $marcflavour = lc C4::Context->preference("marcflavour");
121 my $dbh=C4::Context->dbh;
122 my $sth=$dbh->prepare("select marcxml from import_records where import_record_id=? and record_type='auth';");
123 $sth->execute($import_record_id);
124 my $marcxml = $sth->fetchrow;
125 my $record=eval {MARC::Record->new_from_xml(StripNonXmlChars($marcxml),'UTF-8',
126 (C4::Context->preference("marcflavour") eq "UNIMARC"?"UNIMARCAUTH":C4::Context->preference("marcflavour")))};
128 $record->encoding('UTF-8');
130 # NOTE: GuessAuthTypeCode has no business in Koha::Authority, which is an
131 # object-oriented class. Eventually perhaps there will be utility
132 # classes in the Koha:: namespace, but there are not at the moment,
133 # so this shim seems like the best option all-around.
134 require C4::AuthoritiesMarc;
135 my $authtypecode = C4::AuthoritiesMarc::GuessAuthTypeCode($record);
137 my $self = $class->SUPER::new( {
138 schema => $marcflavour,
139 authtype => $authtypecode,
140 record => $record });
146 sub authorized_heading {
148 if ($self->schema =~ m/marc/) {
149 return Koha::Util::MARC::getAuthorityAuthorizedHeading($self->record, $self->schema);