RFID: keyboard shortcuts now update rfid_action
[koha.git] / Koha / Authority.pm
index 9de8be5..b4cf946 100644 (file)
@@ -19,80 +19,109 @@ package Koha::Authority;
 
 use Modern::Perl;
 
-use Carp;
-
-use Koha::Database;
-use C4::Context;
-use MARC::Record;
-
 use base qw(Koha::Object);
 
+use Koha::Authority::ControlledIndicators;
+use Koha::SearchEngine::Search;
+
 =head1 NAME
 
 Koha::Authority - Koha Authority Object class
 
 =head1 API
 
-=head2 Class Methods
+=head2 Instance Methods
+
+=head3 get_usage_count
+
+    $count = $self->get_usage_count;
+
+    Returns the number of linked biblio records.
 
 =cut
 
-=head3 type
+sub get_usage_count {
+    my ( $self ) = @_;
+    return Koha::Authorities->get_usage_count({ authid => $self->authid });
+}
+
+=head3 linked_biblionumbers
+
+    my @biblios = $self->linked_biblionumbers({
+        [ max_results => $max ], [ offset => $offset ],
+    });
+
+    Returns an array of biblionumbers.
 
 =cut
 
-sub _type {
-    return 'AuthHeader';
+sub linked_biblionumbers {
+    my ( $self, $params ) = @_;
+    $params->{authid} = $self->authid;
+    return Koha::Authorities->linked_biblionumbers( $params );
 }
 
-=head2 get_all_authorities_iterator
+=head3 controlled_indicators
+
+    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.
+
+    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.)
+
+    Note: The record parameter is a temporary bypass in order to prevent
+    needless conversion of $self->marcxml.
+
+=cut
 
-    my $it = Koha::Authority->get_all_authorities_iterator();
+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,
+    });
+}
 
-This will provide an iterator object that will, one by one, provide the
-Koha::Authority of each authority.
+=head2 Class Methods
 
-The iterator is a Koha::MetadataIterator object.
+=head3 type
 
 =cut
 
-sub get_all_authorities_iterator {
-    my $database = Koha::Database->new();
-    my $schema   = $database->schema();
-    my $rs =
-      $schema->resultset('AuthHeader')->search( { marcxml => { '!=', undef } },
-        { columns => [qw/ authid authtypecode marcxml /] } );
-    my $next_func = sub {
-        my $row = $rs->next();
-        return undef if !$row;
-        my $authid       = $row->authid;
-        my $authtypecode = $row->authtypecode;
-        my $marcxml      = $row->marcxml;
-
-        my $record = eval {
-            MARC::Record->new_from_xml(
-                StripNonXmlChars($marcxml),
-                'UTF-8',
-                (
-                    C4::Context->preference("marcflavour") eq "UNIMARC"
-                    ? "UNIMARCAUTH"
-                    : C4::Context->preference("marcflavour")
-                )
-            );
-        };
-        confess $@ if ($@);
-        $record->encoding('UTF-8');
-
-        # I'm not sure why we don't use the authtypecode from the database,
-        # but this is how the original code does it.
-        require C4::AuthoritiesMarc;
-        $authtypecode = C4::AuthoritiesMarc::GuessAuthTypeCode($record);
-
-        my $auth = __PACKAGE__->new( $record, $authid, $authtypecode );
-
-        return $auth;
-      };
-      return Koha::MetadataIterator->new($next_func);
+sub _type {
+    return 'AuthHeader';
 }
 
 1;