Bug 9755 QA follow-up: move MARC-specific functionality to utility class
[koha.git] / Koha / Authority.pm
1 package Koha::Authority;
2
3 # Copyright 2012 C & P Bibliography Services
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 2 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 =head1 NAME
21
22 Koha::Authority - class to encapsulate authority records in Koha
23
24 =head1 SYNOPSIS
25
26 Object-oriented class that encapsulates authority records in Koha.
27
28 =head1 DESCRIPTION
29
30 Authority data.
31
32 =cut
33
34 use strict;
35 use warnings;
36 use C4::Context;
37 use MARC::Record;
38 use MARC::File::XML;
39 use C4::Charset;
40
41 use base qw(Koha::MetadataRecord);
42
43 __PACKAGE__->mk_accessors(qw( authid authtype ));
44
45 =head2 new
46
47     my $auth = Koha::Authority->new($record);
48
49 Create a new Koha::Authority object based on the provided record.
50
51 =cut
52 sub new {
53     my $class = shift;
54     my $record = shift;
55
56     my $self = $class->SUPER::new( { record => $record });
57
58     bless $self, $class;
59     return $self;
60 }
61
62
63 =head2 get_from_authid
64
65     my $auth = Koha::Authority->get_from_authid($authid);
66
67 Create the Koha::Authority object associated with the provided authid.
68 Note that this routine currently retrieves a MARC record because
69 authorities in Koha are MARC records by definition. This is an
70 unfortunate but unavoidable fact.
71
72 =cut
73 sub get_from_authid {
74     my $class = shift;
75     my $authid = shift;
76     my $marcflavour = lc C4::Context->preference("marcflavour");
77
78     my $dbh=C4::Context->dbh;
79     my $sth=$dbh->prepare("select authtypecode, marcxml from auth_header where authid=?");
80     $sth->execute($authid);
81     my ($authtypecode, $marcxml) = $sth->fetchrow;
82     my $record=eval {MARC::Record->new_from_xml(StripNonXmlChars($marcxml),'UTF-8',
83         (C4::Context->preference("marcflavour") eq "UNIMARC"?"UNIMARCAUTH":C4::Context->preference("marcflavour")))};
84     return if ($@);
85     $record->encoding('UTF-8');
86
87     my $self = $class->SUPER::new( { authid => $authid,
88                                      authtype => $authtypecode,
89                                      schema => $marcflavour,
90                                      record => $record });
91
92     bless $self, $class;
93     return $self;
94 }
95
96 1;