c85c533d40b7a73806ee283127389beb1b8de188
[srvgit] / 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 Import the record to replace an existing record which is passed to this sub
116
117     $import_record->replace({ biblio => $biblio_object });
118
119 =cut
120
121 sub replace {
122     my ($self, $params) = @_;
123     my $biblio = $params->{biblio};
124     my $authority = $params->{authority};
125
126     my $userenv = C4::Context->userenv;
127     my $logged_in_patron = Koha::Patrons->find( $userenv->{number} );
128
129     my $marc_record = $self->get_marc_record;
130     my $xmlrecord;
131     if( $biblio ){
132         my $record = $biblio->metadata->record;
133         $xmlrecord = $record->as_xml;
134         my $context = { source => 'batchimport' };
135         if ($logged_in_patron) {
136             $context->{categorycode} = $logged_in_patron->categorycode;
137             $context->{userid} = $logged_in_patron->userid;
138         }
139         ModBiblio(
140             $marc_record,
141             $biblio->id,
142             $biblio->frameworkcode,
143             {
144                 overlay_context   => $context,
145                 skip_record_index => 1
146             }
147         );
148         $self->import_biblio->matched_biblionumber( $biblio->id )->store;
149     } elsif( $authority ) {
150         $xmlrecord = $authority->marcxml;
151         ModAuthority(
152             $authority->id,
153             $marc_record,
154             GuessAuthTypeCode($marc_record)
155         );
156         $self->import_auth->matched_authid( $authority->id )->store;
157     } else {
158         # We could also throw an exception
159         return;
160     }
161     $self->marcxml_old( $xmlrecord );
162     $self->status('imported');
163     $self->overlay_status('match_applied');
164     $self->store;
165
166     return 1;
167 }
168
169 =head2 Internal methods
170
171 =head3 _type
172
173 Returns name of corresponding DBIC resultset
174
175 =cut
176
177 sub _type {
178     return 'ImportRecord';
179 }
180
181 1;