Bug 20700: MARC21 add/update leader/007/008 codes
[koha.git] / Koha / Authority / MergeRequest.pm
blob4c67910a95c6d14f1cac6c6a9792ccaa5e5e4862
1 package Koha::Authority::MergeRequest;
3 # Copyright Rijksmuseum 2017
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 3 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 use Modern::Perl;
22 use parent qw(Koha::Object);
24 use Koha::Authorities;
25 use Koha::Authority::Types;
27 =head1 NAME
29 Koha::Authority::MergeRequest - Koha::Object class for single need_merge_authorities record
31 =head1 SYNOPSIS
33 use Koha::Authority::MergeRequest;
35 =head1 DESCRIPTION
37 Description
39 =head1 METHODS
41 =head2 INSTANCE METHODS
43 =head3 new
45 $self->new({
46 authid => $id,
47 [ authid_new => $new, ]
48 [ oldrecord => $marc, ]
49 });
51 authid refers to the old authority id,
52 authid_new optionally refers to a new different authority id
54 oldrecord is the MARC record belonging to the original authority record
56 This method returns an object and initializes the reportxml property.
58 =cut
60 sub new {
61 my ( $class, $params ) = @_;
62 my $oldrecord = delete $params->{oldrecord};
63 delete $params->{reportxml}; # just making sure it is empty
64 my $self = $class->SUPER::new( $params );
66 if( $self->authid && $oldrecord ) {
67 my $auth = Koha::Authorities->find( $self->authid );
68 my $type = $auth ? Koha::Authority::Types->find($auth->authtypecode) : undef;
69 $self->reportxml( $self->reporting_tag_xml({ record => $oldrecord, tag => $type->auth_tag_to_report })) if $type;
71 return $self;
74 =head3 oldmarc
76 my $record = $self->oldmarc;
78 Convert reportxml back to MARC::Record.
80 =cut
82 sub oldmarc {
83 my ( $self ) = @_;
84 return if !$self->reportxml;
85 return MARC::Record->new_from_xml( $self->reportxml, 'UTF-8' );
88 =head2 CLASS METHODS
90 =head3 reporting_tag_xml
92 my $xml = Koha::Authority::MergeRequest->reporting_tag_xml({
93 record => $record, tag => $tag,
94 });
96 =cut
98 sub reporting_tag_xml {
99 my ( $class, $params ) = @_;
100 return if !$params->{record} || !$params->{tag};
102 my $newrecord = MARC::Record->new;
103 $newrecord->encoding( 'UTF-8' );
104 my $reportfield = $params->{record}->field( $params->{tag} );
105 return if !$reportfield;
107 # For UNIMARC we need a field 100 that includes the encoding
108 # at position 13 and 14
109 if( C4::Context->preference('marcflavour') eq 'UNIMARC' ) {
110 $newrecord->append_fields(
111 MARC::Field->new( '100', '', '', a => ' 'x13 . '50' ),
115 $newrecord->append_fields( $reportfield );
116 return $newrecord->as_xml(
117 C4::Context->preference('marcflavour') eq 'UNIMARC' ?
118 'UNIMARCAUTH' :
119 'MARC21'
123 =head3 _type
125 Returns name of corresponding DBIC resultset
127 =cut
129 sub _type {
130 return 'NeedMergeAuthority';
133 =head1 AUTHOR
135 Marcel de Rooy (Rijksmuseum)
137 Koha Development Team
139 =cut