Bug 9755: Refactor record merge functionality
[koha.git] / Koha / Record.pm
1 package Koha::Record;
2
3 # Copyright 2013 C & P Bibliography Services
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 =head1 NAME
21
22 Koha::Record - base class for MARC records
23
24 =head1 SYNOPSIS
25
26     my $record = new Koha::Record({ 'record' => $marcrecord });
27
28 =head1 DESCRIPTION
29
30 Object-oriented class that encapsulates all records in Koha.
31
32 =cut
33
34 use strict;
35 use warnings;
36 use C4::Context;
37 use MARC::Record;
38
39 use base qw(Class::Accessor);
40
41 __PACKAGE__->mk_accessors(qw( record marcflavour ));
42
43
44 =head2 createMarcHash
45
46 Create a MARC hash for use when merging records.
47
48 =cut
49
50 sub createMarcHash {
51     my ($self, $tagslib) = @_;
52     my $record = $self->record;
53     my @array;
54     my @fields = $record->fields();
55
56
57     foreach my $field (@fields) {
58     my $fieldtag = $field->tag();
59     if ($fieldtag < 10) {
60         if (!defined($tagslib) || $tagslib->{$fieldtag}->{'@'}->{'tab'} >= 0) {
61         push @array, {
62             field => [
63                     {
64                     tag => $fieldtag,
65                     key => _createKey(),
66                     value => $field->data(),
67                     }
68                 ]
69                 };
70         }
71     } else {
72         my @subfields = $field->subfields();
73         my @subfield_array;
74         foreach my $subfield (@subfields) {
75         if (!defined($tagslib) || $tagslib->{$fieldtag}->{@$subfield[0]}->{'tab'} >= 0) {
76             push @subfield_array, {
77                                     subtag => @$subfield[0],
78                                     subkey => _createKey(),
79                                     value => @$subfield[1],
80                                   };
81         }
82
83         }
84
85         if ((!defined($tagslib) || $tagslib->{$fieldtag}->{'tab'} >= 0) && $fieldtag ne '995' && $fieldtag ne '999') {
86         push @array, {
87             field => [
88                 {
89                     tag => $fieldtag,
90                     key => _createKey(),
91                     indicator1 => $field->indicator(1),
92                     indicator2 => $field->indicator(2),
93                     subfield   => [@subfield_array],
94                 }
95             ]
96             };
97         }
98
99     }
100     }
101     return [@array];
102
103 }
104
105 =head2 _createKey
106
107 Create a random value to set it into the input name
108
109 =cut
110
111 sub _createKey {
112     return int(rand(1000000));
113 }
114
115 1;