RFID: keyboard shortcuts now update rfid_action
[koha.git] / Koha / Authority.pm
index b07db92..b4cf946 100644 (file)
@@ -1,12 +1,12 @@
 package Koha::Authority;
 
-# Copyright 2012 C & P Bibliography Services
+# Copyright 2015 Koha Development Team
 #
 # This file is part of Koha.
 #
 # Koha is free software; you can redistribute it and/or modify it under the
 # terms of the GNU General Public License as published by the Free Software
-# Foundation; either version 2 of the License, or (at your option) any later
+# Foundation; either version 3 of the License, or (at your option) any later
 # version.
 #
 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
@@ -17,76 +17,111 @@ package Koha::Authority;
 # with Koha; if not, write to the Free Software Foundation, Inc.,
 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 
+use Modern::Perl;
+
+use base qw(Koha::Object);
+
+use Koha::Authority::ControlledIndicators;
+use Koha::SearchEngine::Search;
+
 =head1 NAME
 
-Koha::Authority - class to encapsulate authority records in Koha
+Koha::Authority - Koha Authority Object class
+
+=head1 API
 
-=head1 SYNOPSIS
+=head2 Instance Methods
 
-Object-oriented class that encapsulates authority records in Koha.
+=head3 get_usage_count
 
-=head1 DESCRIPTION
+    $count = $self->get_usage_count;
 
-Authority data.
+    Returns the number of linked biblio records.
 
 =cut
 
-use strict;
-use warnings;
-use C4::Context;
-use MARC::Record;
-use MARC::File::XML;
-use C4::Charset;
+sub get_usage_count {
+    my ( $self ) = @_;
+    return Koha::Authorities->get_usage_count({ authid => $self->authid });
+}
 
-use base qw(Class::Accessor);
+=head3 linked_biblionumbers
 
-__PACKAGE__->mk_accessors(qw( authid authtype record marcflavour ));
+    my @biblios = $self->linked_biblionumbers({
+        [ max_results => $max ], [ offset => $offset ],
+    });
 
-=head2 new
+    Returns an array of biblionumbers.
 
-    my $auth = Koha::Authority->new($record);
+=cut
 
-Create a new Koha::Authority object based on the provided record.
+sub linked_biblionumbers {
+    my ( $self, $params ) = @_;
+    $params->{authid} = $self->authid;
+    return Koha::Authorities->linked_biblionumbers( $params );
+}
 
-=cut
-sub new {
-    my $class = shift;
-    my $record = shift;
+=head3 controlled_indicators
 
-    my $self = $class->SUPER::new( { record => $record });
+    Some authority types control the indicators of some corresponding
+    biblio fields (especially in MARC21).
+    For example, if you have a PERSO_NAME authority (report tag 100), the
+    first indicator of biblio field 600 directly comes from the authority,
+    and the second indicator depends on thesaurus settings in the authority
+    record. Use this method to obtain such controlled values. In this example
+    you should pass 600 in the biblio_tag parameter.
 
-    bless $self, $class;
-    return $self;
-}
+    my $result = $self->controlled_indicators({
+        record => $auth_marc, biblio_tag => $bib_tag
+    });
+    my $ind1 = $result->{ind1};
+    my $ind2 = $result->{ind2};
+    my $subfield_2 = $result->{sub2}; # Optional subfield 2 when ind==7
+
+    If an indicator is not controlled, the result hash does not contain a key
+    for its value. (Same for the sub2 key for an optional subfield $2.)
 
-=head2 get_from_authid
+    Note: The record parameter is a temporary bypass in order to prevent
+    needless conversion of $self->marcxml.
 
-    my $auth = Koha::Authority->get_from_authid($authid);
+=cut
 
-Create the Koha::Authority object associated with the provided authid.
+sub controlled_indicators {
+    my ( $self, $params ) = @_;
+    my $tag = $params->{biblio_tag} // q{};
+    my $record = $params->{record};
+
+    my $flavour = C4::Context->preference('marcflavour') eq 'UNIMARC'
+        ? 'UNIMARCAUTH'
+        : 'MARC21';
+    if( !$record ) {
+        $record = MARC::Record->new_from_xml(
+            $self->marcxml, 'UTF-8', $flavour );
+    }
+
+    if( !$self->{_report_tag} ) {
+        my $authtype = Koha::Authority::Types->find( $self->authtypecode );
+        return {} if !$authtype; # very exceptional
+        $self->{_report_tag} = $authtype->auth_tag_to_report;
+    }
+
+    $self->{_ControlledInds} //= Koha::Authority::ControlledIndicators->new;
+    return $self->{_ControlledInds}->get({
+        auth_record => $record,
+        report_tag  => $self->{_report_tag},
+        biblio_tag  => $tag,
+        flavour     => $flavour,
+    });
+}
+
+=head2 Class Methods
+
+=head3 type
 
 =cut
-sub get_from_authid {
-    my $class = shift;
-    my $authid = shift;
-    my $marcflavour = C4::Context->preference("marcflavour");
-
-    my $dbh=C4::Context->dbh;
-    my $sth=$dbh->prepare("select authtypecode, marcxml from auth_header where authid=?");
-    $sth->execute($authid);
-    my ($authtypecode, $marcxml) = $sth->fetchrow;
-    my $record=eval {MARC::Record->new_from_xml(StripNonXmlChars($marcxml),'UTF-8',
-        (C4::Context->preference("marcflavour") eq "UNIMARC"?"UNIMARCAUTH":C4::Context->preference("marcflavour")))};
-    return if ($@);
-    $record->encoding('UTF-8');
-
-    my $self = $class->SUPER::new( { authid => $authid,
-                                     marcflavour => $marcflavour,
-                                     authtype => $authtypecode,
-                                     record => $record });
-
-    bless $self, $class;
-    return $self;
+
+sub _type {
+    return 'AuthHeader';
 }
 
 1;