Bug 18501: (follow-up) Test undefined userenv behaviour
[koha.git] / Koha / Util / MARC.pm
blob56f0a9845118b32e0b57c6419491705e8d01d34f
1 package Koha::Util::MARC;
3 # Copyright 2013 C & P Bibliography Services
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20 use Modern::Perl;
21 use MARC::Record;
23 =head1 NAME
25 Koha::Util::MARC - utility class with routines for working with MARC records
27 =head1 METHODS
29 =head2 createMergeHash
31 Create a hash to use when merging MARC records
33 =cut
35 sub createMergeHash {
36 my ( $record, $tagslib ) = @_;
38 return unless $record;
40 my @array;
41 my @fields = $record->fields();
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 )
52 push @array, {
53 tag => $fieldtag,
54 key => _createKey(),
55 value => $field->data(),
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 )
71 push @subfield_array, {
72 subtag => @$subfield[0],
73 subkey => _createKey(),
74 value => @$subfield[1],
80 if (
82 !defined($tagslib) || ( defined $tagslib->{$fieldtag}
83 && defined $tagslib->{$fieldtag}->{'tab'}
84 && $tagslib->{$fieldtag}->{'tab'} >= 0 )
86 && @subfield_array
89 push @array, {
90 tag => $fieldtag,
91 key => _createKey(),
92 indicator1 => $field->indicator(1),
93 indicator2 => $field->indicator(2),
94 subfield => [@subfield_array],
100 return [@array];
103 =head2 _createKey
105 Create a random value to set it into the input name
107 =cut
109 sub _createKey {
110 return int(rand(1000000));
113 =head2 getAuthorityAuthorizedHeading
115 Retrieve the authorized heading from a MARC authority record
117 =cut
119 sub getAuthorityAuthorizedHeading {
120 my ( $record, $schema ) = @_;
121 return unless ( ref $record eq 'MARC::Record' );
122 if ( $schema eq 'unimarc' ) {
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');
130 else {
131 foreach my $field ( $record->field('1..') ) {
132 my $tag = $field->tag();
133 next if "152" eq $tag;
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');
141 elsif ( $tag eq '110' ) {
142 return $field->as_string('abcdefghklmnoprstvxyz68');
144 elsif ( $tag eq '111' ) {
145 return $field->as_string('acdefghklnpqstvxyz68');
147 elsif ( $tag eq '130' ) {
148 return $field->as_string('adfghklmnoprstvxyz68');
150 elsif ( $tag eq '148' ) {
151 return $field->as_string('abvxyz68');
153 elsif ( $tag eq '150' ) {
154 return $field->as_string('abvxyz68');
156 elsif ( $tag eq '151' ) {
157 return $field->as_string('avxyz68');
159 elsif ( $tag eq '155' ) {
160 return $field->as_string('abvxyz68');
162 elsif ( $tag eq '180' ) {
163 return $field->as_string('vxyz68');
165 elsif ( $tag eq '181' ) {
166 return $field->as_string('vxyz68');
168 elsif ( $tag eq '182' ) {
169 return $field->as_string('vxyz68');
171 elsif ( $tag eq '185' ) {
172 return $field->as_string('vxyz68');
174 else {
175 return $field->as_string();
179 return;
182 =head2 set_marc_field
184 set_marc_field($record, $marcField, $value);
186 Set the value of $marcField to $value in $record. If the field exists, it will
187 be updated. If not, it will be created.
189 =head3 Parameters
191 =over 4
193 =item C<$record>
195 MARC::Record object
197 =item C<$marcField>
199 the MARC field to modify, a string in the form of 'XXX$y'
201 =item C<$value>
203 the value
205 =back
207 =cut
209 sub set_marc_field {
210 my ($record, $marcField, $value) = @_;
212 if ($marcField) {
213 my ($fieldTag, $subfieldCode) = split /\$/, $marcField;
214 if( !$subfieldCode ) {
215 warn "set_marc_field: Invalid marcField format: $marcField\n";
216 return;
218 my $field = $record->field($fieldTag);
219 if ($field) {
220 $field->update($subfieldCode => $value);
221 } else {
222 $field = MARC::Field->new($fieldTag, ' ', ' ',
223 $subfieldCode => $value);
224 $record->append_fields($field);