Bug 11086: (follow-up) editing the database files also
[koha.git] / Koha / Authority.pm
blobb07db92895f96e813020ca9b019177ecd097e370
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
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;
41 use base qw(Class::Accessor);
43 __PACKAGE__->mk_accessors(qw( authid authtype record marcflavour ));
45 =head2 new
47 my $auth = Koha::Authority->new($record);
49 Create a new Koha::Authority object based on the provided record.
51 =cut
52 sub new {
53 my $class = shift;
54 my $record = shift;
56 my $self = $class->SUPER::new( { record => $record });
58 bless $self, $class;
59 return $self;
62 =head2 get_from_authid
64 my $auth = Koha::Authority->get_from_authid($authid);
66 Create the Koha::Authority object associated with the provided authid.
68 =cut
69 sub get_from_authid {
70 my $class = shift;
71 my $authid = shift;
72 my $marcflavour = C4::Context->preference("marcflavour");
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');
83 my $self = $class->SUPER::new( { authid => $authid,
84 marcflavour => $marcflavour,
85 authtype => $authtypecode,
86 record => $record });
88 bless $self, $class;
89 return $self;