Bug 9988: [QA Follow-up] Satisfy QA issues
[koha.git] / Koha / Authority / MergeRequest.pm
1 package Koha::Authority::MergeRequest;
2
3 # Copyright Rijksmuseum 2017
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 3 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 use Modern::Perl;
21
22 use parent qw(Koha::Object);
23
24 use Koha::Authorities;
25 use Koha::Authority::Types;
26
27 =head1 NAME
28
29 Koha::Authority::MergeRequest - Koha::Object class for single need_merge_authorities record
30
31 =head1 SYNOPSIS
32
33 use Koha::Authority::MergeRequest;
34
35 =head1 DESCRIPTION
36
37 Description
38
39 =head1 METHODS
40
41 =head2 INSTANCE METHODS
42
43 =head3 new
44
45     $self->new({
46         authid => $id,
47         [ authid_new => $new, ]
48         [ oldrecord => $marc, ]
49     });
50
51     authid refers to the old authority id,
52     authid_new optionally refers to a new different authority id
53
54     oldrecord is the MARC record belonging to the original authority record
55
56     This method returns an object and initializes the reportxml property.
57
58 =cut
59
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 );
65
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;
70     }
71     return $self;
72 }
73
74 =head3 oldmarc
75
76     my $record = $self->oldmarc;
77
78     Convert reportxml back to MARC::Record.
79
80 =cut
81
82 sub oldmarc {
83     my ( $self ) = @_;
84     return if !$self->reportxml;
85     return MARC::Record->new_from_xml( $self->reportxml, 'UTF-8' );
86 }
87
88 =head2 CLASS METHODS
89
90 =head3 reporting_tag_xml
91
92     my $xml = Koha::Authority::MergeRequest->reporting_tag_xml({
93         record => $record, tag => $tag,
94     });
95
96 =cut
97
98 sub reporting_tag_xml {
99     my ( $class, $params ) = @_;
100     return if !$params->{record} || !$params->{tag};
101
102     my $newrecord = MARC::Record->new;
103     $newrecord->encoding( 'UTF-8' );
104     my $reportfield = $params->{record}->field( $params->{tag} );
105     return if !$reportfield;
106
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' ),
112         );
113     }
114
115     $newrecord->append_fields( $reportfield );
116     return $newrecord->as_xml(
117         C4::Context->preference('marcflavour') eq 'UNIMARC' ?
118         'UNIMARCAUTH' :
119         'MARC21'
120     );
121 }
122
123 =head3 _type
124
125 Returns name of corresponding DBIC resultset
126
127 =cut
128
129 sub _type {
130     return 'NeedMergeAuthority';
131 }
132
133 =head1 AUTHOR
134
135 Marcel de Rooy (Rijksmuseum)
136
137 Koha Development Team
138
139 =cut
140
141 1;