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>.
24 Koha::Util::MARC - utility class with routines for working with MARC records
28 =head2 createMergeHash
30 Create a hash to use when merging MARC records
35 my ( $record, $tagslib ) = @_;
37 return unless $record;
40 my @fields = $record->fields();
42 foreach my $field (@fields) {
43 my $fieldtag = $field->tag();
44 if ( $fieldtag < 10 ) {
47 || ( defined( $tagslib->{$fieldtag} )
48 && $tagslib->{$fieldtag}->{'@'}->{'tab'} >= 0 )
54 value => $field->data(),
59 my @subfields = $field->subfields();
61 foreach my $subfield (@subfields) {
64 || ( defined $tagslib->{$fieldtag}
65 && defined $tagslib->{$fieldtag}->{ @$subfield[0] }
66 && defined $tagslib->{$fieldtag}->{ @$subfield[0] }->{'tab'}
67 && $tagslib->{$fieldtag}->{ @$subfield[0] }->{'tab'} >= 0 )
70 push @subfield_array, {
71 subtag => @$subfield[0],
72 subkey => _createKey(),
73 value => @$subfield[1],
81 !defined($tagslib) || ( defined $tagslib->{$fieldtag}
82 && defined $tagslib->{$fieldtag}->{'tab'}
83 && $tagslib->{$fieldtag}->{'tab'} >= 0 )
91 indicator1 => $field->indicator(1),
92 indicator2 => $field->indicator(2),
93 subfield => [@subfield_array],
104 Create a random value to set it into the input name
109 return int(rand(1000000));
112 =head2 getAuthorityAuthorizedHeading
114 Retrieve the authorized heading from a MARC authority record
118 sub getAuthorityAuthorizedHeading {
119 my ( $record, $schema ) = @_;
120 return unless ( ref $record eq 'MARC::Record' );
121 if ( $schema eq 'unimarc' ) {
123 # construct UNIMARC summary, that is quite different from MARC21 one
125 foreach my $field ( $record->field('2..') ) {
126 return $field->as_string('abcdefghijlmnopqrstuvwxyz');
130 foreach my $field ( $record->field('1..') ) {
131 my $tag = $field->tag();
132 next if "152" eq $tag;
134 # FIXME - 152 is not a good tag to use
135 # in MARC21 -- purely local tags really ought to be
137 if ( $tag eq '100' ) {
138 return $field->as_string('abcdefghjklmnopqrstvxyz68');
140 elsif ( $tag eq '110' ) {
141 return $field->as_string('abcdefghklmnoprstvxyz68');
143 elsif ( $tag eq '111' ) {
144 return $field->as_string('acdefghklnpqstvxyz68');
146 elsif ( $tag eq '130' ) {
147 return $field->as_string('adfghklmnoprstvxyz68');
149 elsif ( $tag eq '148' ) {
150 return $field->as_string('abvxyz68');
152 elsif ( $tag eq '150' ) {
153 return $field->as_string('abvxyz68');
155 elsif ( $tag eq '151' ) {
156 return $field->as_string('avxyz68');
158 elsif ( $tag eq '155' ) {
159 return $field->as_string('abvxyz68');
161 elsif ( $tag eq '180' ) {
162 return $field->as_string('vxyz68');
164 elsif ( $tag eq '181' ) {
165 return $field->as_string('vxyz68');
167 elsif ( $tag eq '182' ) {
168 return $field->as_string('vxyz68');
170 elsif ( $tag eq '185' ) {
171 return $field->as_string('vxyz68');
174 return $field->as_string();
181 =head2 set_marc_field
183 set_marc_field($record, $marcField, $value);
185 Set the value of $marcField to $value in $record. If the field exists, it will
186 be updated. If not, it will be created.
198 the MARC field to modify, a string in the form of 'XXX$y'
209 my ($record, $marcField, $value) = @_;
212 my ($fieldTag, $subfieldCode) = split /\$/, $marcField;
213 if( !$subfieldCode ) {
214 warn "set_marc_field: Invalid marcField format: $marcField\n";
217 my $field = $record->field($fieldTag);
219 $field->update($subfieldCode => $value);
221 $field = MARC::Field->new($fieldTag, ' ', ' ',
222 $subfieldCode => $value);
223 $record->append_fields($field);