Bug 19059: Remove CancelReserve - do not log DELETE
[koha.git] / Koha / Util / MARC.pm
1 package Koha::Util::MARC;
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 use Modern::Perl;
21 use MARC::Record;
22
23 =head1 NAME
24
25 Koha::Util::MARC - utility class with routines for working with MARC records
26
27 =head1 METHODS
28
29 =head2 createMergeHash
30
31 Create a hash to use when merging MARC records
32
33 =cut
34
35 sub createMergeHash {
36     my ( $record, $tagslib ) = @_;
37
38     return unless $record;
39
40     my @array;
41     my @fields = $record->fields();
42
43     foreach my $field (@fields) {
44         my $fieldtag = $field->tag();
45         if ( $fieldtag < 10 ) {
46             if (
47                 !defined($tagslib)
48                 || ( defined( $tagslib->{$fieldtag} )
49                     && $tagslib->{$fieldtag}->{'@'}->{'tab'} >= 0 )
50               )
51             {
52                 push @array, {
53                     tag   => $fieldtag,
54                     key   => _createKey(),
55                     value => $field->data(),
56                 };
57             }
58         }
59         else {
60             my @subfields = $field->subfields();
61             my @subfield_array;
62             foreach my $subfield (@subfields) {
63                 if (
64                     !defined($tagslib)
65                     || (   defined $tagslib->{$fieldtag}
66                         && defined $tagslib->{$fieldtag}->{ @$subfield[0] }
67                         && defined $tagslib->{$fieldtag}->{ @$subfield[0] }->{'tab'}
68                         && $tagslib->{$fieldtag}->{ @$subfield[0] }->{'tab'} >= 0 )
69                   )
70                 {
71                     push @subfield_array, {
72                         subtag => @$subfield[0],
73                         subkey => _createKey(),
74                         value  => @$subfield[1],
75                     };
76                 }
77
78             }
79
80             if (
81                 (
82                     !defined($tagslib) || ( defined $tagslib->{$fieldtag}
83                         && defined $tagslib->{$fieldtag}->{'tab'}
84                         && $tagslib->{$fieldtag}->{'tab'} >= 0 )
85                 )
86                 && @subfield_array
87               )
88             {
89                 push @array, {
90                       tag        => $fieldtag,
91                       key        => _createKey(),
92                       indicator1 => $field->indicator(1),
93                       indicator2 => $field->indicator(2),
94                       subfield   => [@subfield_array],
95                   };
96             }
97
98         }
99     }
100     return [@array];
101 }
102
103 =head2 _createKey
104
105 Create a random value to set it into the input name
106
107 =cut
108
109 sub _createKey {
110     return int(rand(1000000));
111 }
112
113 =head2 getAuthorityAuthorizedHeading
114
115 Retrieve the authorized heading from a MARC authority record
116
117 =cut
118
119 sub getAuthorityAuthorizedHeading {
120     my ( $record, $schema ) = @_;
121     return unless ( ref $record eq 'MARC::Record' );
122     if ( $schema eq 'unimarc' ) {
123
124         # construct UNIMARC summary, that is quite different from MARC21 one
125         # accepted form
126         foreach my $field ( $record->field('2..') ) {
127             return $field->as_string('abcdefghijlmnopqrstuvwxyz');
128         }
129     }
130     else {
131         foreach my $field ( $record->field('1..') ) {
132             my $tag = $field->tag();
133             next if "152" eq $tag;
134
135             # FIXME - 152 is not a good tag to use
136             # in MARC21 -- purely local tags really ought to be
137             # 9XX
138             if ( $tag eq '100' ) {
139                 return $field->as_string('abcdefghjklmnopqrstvxyz68');
140             }
141             elsif ( $tag eq '110' ) {
142                 return $field->as_string('abcdefghklmnoprstvxyz68');
143             }
144             elsif ( $tag eq '111' ) {
145                 return $field->as_string('acdefghklnpqstvxyz68');
146             }
147             elsif ( $tag eq '130' ) {
148                 return $field->as_string('adfghklmnoprstvxyz68');
149             }
150             elsif ( $tag eq '148' ) {
151                 return $field->as_string('abvxyz68');
152             }
153             elsif ( $tag eq '150' ) {
154                 return $field->as_string('abvxyz68');
155             }
156             elsif ( $tag eq '151' ) {
157                 return $field->as_string('avxyz68');
158             }
159             elsif ( $tag eq '155' ) {
160                 return $field->as_string('abvxyz68');
161             }
162             elsif ( $tag eq '180' ) {
163                 return $field->as_string('vxyz68');
164             }
165             elsif ( $tag eq '181' ) {
166                 return $field->as_string('vxyz68');
167             }
168             elsif ( $tag eq '182' ) {
169                 return $field->as_string('vxyz68');
170             }
171             elsif ( $tag eq '185' ) {
172                 return $field->as_string('vxyz68');
173             }
174             else {
175                 return $field->as_string();
176             }
177         }
178     }
179     return;
180 }
181
182 1;