Bug 33940: Consider NULL as valid
[koha.git] / Koha / Import / Record.pm
1 package Koha::Import::Record;
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 use Modern::Perl;
19
20 use Carp;
21 use MARC::Record;
22
23 use C4::Context;
24 use C4::Biblio qw(ModBiblio);
25 use C4::AuthoritiesMarc qw(GuessAuthTypeCode ModAuthority);
26 use Koha::Database;
27 use Koha::Import::Record::Biblios;
28 use Koha::Import::Record::Auths;
29 use Koha::Import::Record::Matches;
30
31 use base qw(Koha::Object);
32
33 =head1 NAME
34
35 Koha::Import::Record - Koha Import Record Object class
36
37 =head1 API
38
39 =head2 Class methods
40
41 =head3 get_marc_record
42
43 Returns a MARC::Record object
44
45     my $marc_record = $import_record->get_marc_record()
46
47 =cut
48
49 sub get_marc_record {
50     my ($self) = @_;
51
52     my $marcflavour = C4::Context->preference('marcflavour');
53
54     my $format = $marcflavour eq 'UNIMARC' ? 'UNIMARC' : 'USMARC';
55     if ($marcflavour eq 'UNIMARC' && $self->record_type eq 'auth') {
56         $format = 'UNIMARCAUTH';
57     }
58
59     my $record = MARC::Record->new_from_xml($self->marcxml, $self->encoding, $format);
60
61     return $record;
62 }
63
64 =head3 import_biblio
65
66 Returns the import biblio object for this import record
67
68     my $import_biblio = $import_record->import_biblio()
69
70 =cut
71
72 sub import_biblio {
73     my ( $self ) = @_;
74     my $import_biblio_rs = $self->_result->import_biblio;
75     return Koha::Import::Record::Biblio->_new_from_dbic( $import_biblio_rs );
76 }
77
78 =head3 import_auth
79
80 Returns the import auth object for this import record
81
82     my $import_auth = $import_record->import_auth()
83
84 =cut
85
86 sub import_auth {
87     my ( $self ) = @_;
88     my $import_auth_rs = $self->_result->import_auth;
89     return Koha::Import::Record::Auth->_new_from_dbic( $import_auth_rs );
90 }
91
92 =head3 get_import_record_matches
93
94 Returns the Import::Record::Matches for the record
95 optionally specify a 'chosen' param to get only the chosen match
96
97     my $matches = $import_record->get_import_record_matches([{ chosen => 1 }])
98
99 =cut
100
101 sub get_import_record_matches {
102     my ($self, $params) = @_;
103     my $chosen = $params->{chosen};
104
105     my $matches = $self->_result->import_record_matches;
106     $matches = Koha::Import::Record::Matches->_new_from_dbic( $matches );
107
108     return $matches->filter_by_chosen() if $chosen;
109
110     return $matches->search({},{ order_by => { -desc => ['score','candidate_match_id'] } });
111 }
112
113 =head3 replace
114
115 Replace an existing record ($auth or $biblio) with the import record.
116
117     $import_record->replace({ biblio => $biblio | authority => $auth });
118     # where $biblio and $auth are Koha objects
119
120 =cut
121
122 sub replace {
123     my ($self, $params) = @_;
124     my $biblio = $params->{biblio};
125     my $authority = $params->{authority};
126
127     my $userenv = C4::Context->userenv;
128     my $logged_in_patron = Koha::Patrons->find( $userenv->{number} );
129
130     my $marc_record = $self->get_marc_record;
131     my $xmlrecord;
132     if( $biblio ){
133         my $record = $biblio->metadata->record;
134         $xmlrecord = $record->as_xml;
135         my $context = { source => 'batchimport' };
136         if ($logged_in_patron) {
137             $context->{categorycode} = $logged_in_patron->categorycode;
138             $context->{userid} = $logged_in_patron->userid;
139         }
140         ModBiblio(
141             $marc_record,
142             $biblio->id,
143             $biblio->frameworkcode,
144             {
145                 overlay_context   => $context,
146                 skip_record_index => 1
147             }
148         );
149         $self->import_biblio->matched_biblionumber( $biblio->id )->store;
150     } elsif( $authority ) {
151         $xmlrecord = $authority->marcxml;
152         ModAuthority(
153             $authority->id,
154             $marc_record,
155             GuessAuthTypeCode($marc_record)
156         );
157         $self->import_auth->matched_authid( $authority->id )->store;
158     } else {
159         # We could also throw an exception
160         return;
161     }
162     $self->marcxml_old( $xmlrecord );
163     $self->status('imported');
164     $self->overlay_status('match_applied');
165     $self->store;
166
167     return 1;
168 }
169
170 =head2 Internal methods
171
172 =head3 _type
173
174 Returns name of corresponding DBIC resultset
175
176 =cut
177
178 sub _type {
179     return 'ImportRecord';
180 }
181
182 1;