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>.
25 Koha::Util::MARC - utility class with routines for working with MARC records
29 =head2 createMergeHash
31 Create a hash to use when merging MARC records
36 my ( $record, $tagslib ) = @_;
38 return unless $record;
41 my @fields = $record->fields();
43 foreach my $field (@fields) {
44 my $fieldtag = $field->tag();
45 if ( $fieldtag < 10 ) {
48 || ( defined( $tagslib->{$fieldtag} )
49 && $tagslib->{$fieldtag}->{'@'}->{'tab'} >= 0 )
55 value
=> $field->data(),
60 my @subfields = $field->subfields();
62 foreach my $subfield (@subfields) {
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],
82 !defined($tagslib) || ( defined $tagslib->{$fieldtag}
83 && defined $tagslib->{$fieldtag}->{'tab'}
84 && $tagslib->{$fieldtag}->{'tab'} >= 0 )
92 indicator1
=> $field->indicator(1),
93 indicator2
=> $field->indicator(2),
94 subfield
=> [@subfield_array],
105 Create a random value to set it into the input name
110 return int(rand(1000000));
113 =head2 getAuthorityAuthorizedHeading
115 Retrieve the authorized heading from a MARC authority record
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
126 foreach my $field ( $record->field('2..') ) {
127 return $field->as_string('abcdefghijlmnopqrstuvwxyz');
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
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');
175 return $field->as_string();
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.
199 the MARC field to modify, a string in the form of 'XXX$y'
210 my ($record, $marcField, $value) = @_;
213 my ($fieldTag, $subfieldCode) = split /\$/, $marcField;
214 if( !$subfieldCode ) {
215 warn "set_marc_field: Invalid marcField format: $marcField\n";
218 my $field = $record->field($fieldTag);
220 $field->update($subfieldCode => $value);
222 $field = MARC
::Field
->new($fieldTag, ' ', ' ',
223 $subfieldCode => $value);
224 $record->append_fields($field);