Merge remote-tracking branch 'origin/new/bug_8382'
[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(Class::Accessor);
42
43 __PACKAGE__->mk_accessors(qw( authid authtype record marcflavour ));
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 =head2 get_from_authid
63
64     my $auth = Koha::Authority->get_from_authid($authid);
65
66 Create the Koha::Authority object associated with the provided authid.
67
68 =cut
69 sub get_from_authid {
70     my $class = shift;
71     my $authid = shift;
72     my $marcflavour = C4::Context->preference("marcflavour");
73
74     my $dbh=C4::Context->dbh;
75     my $sth=$dbh->prepare("select authtypecode, marcxml from auth_header where authid=?");
76     $sth->execute($authid);
77     my ($authtypecode, $marcxml) = $sth->fetchrow;
78     my $record=eval {MARC::Record->new_from_xml(StripNonXmlChars($marcxml),'UTF-8',
79         (C4::Context->preference("marcflavour") eq "UNIMARC"?"UNIMARCAUTH":C4::Context->preference("marcflavour")))};
80     return if ($@);
81     $record->encoding('UTF-8');
82
83     my $self = $class->SUPER::new( { authid => $authid,
84                                      marcflavour => $marcflavour,
85                                      authtype => $authtypecode,
86                                      record => $record });
87
88     bless $self, $class;
89     return $self;
90 }
91
92 1;