Bug 16699: Remove requirement from borrowernumberQueryParam
[koha.git] / Koha / Util / MARC.pm
blob22774b63b2f88b62f4ee336b5c79b5e3772b6d76
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 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.
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.
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.
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 ( !defined($tagslib)
47 || $tagslib->{$fieldtag}->{'@'}->{'tab'} >= 0 )
49 push @array, {
50 tag => $fieldtag,
51 key => _createKey(),
52 value => $field->data(),
56 else {
57 my @subfields = $field->subfields();
58 my @subfield_array;
59 foreach my $subfield (@subfields) {
60 if (
61 !defined($tagslib)
62 || ( defined $tagslib->{$fieldtag}
63 && defined $tagslib->{$fieldtag}->{ @$subfield[0] }
64 && defined $tagslib->{$fieldtag}->{ @$subfield[0] }->{'tab'}
65 && $tagslib->{$fieldtag}->{ @$subfield[0] }->{'tab'} >= 0 )
68 push @subfield_array, {
69 subtag => @$subfield[0],
70 subkey => _createKey(),
71 value => @$subfield[1],
77 if (
79 !defined($tagslib) || ( defined $tagslib->{$fieldtag}
80 && defined $tagslib->{$fieldtag}->{'tab'}
81 && $tagslib->{$fieldtag}->{'tab'} >= 0 )
83 && @subfield_array
86 push @array, {
87 tag => $fieldtag,
88 key => _createKey(),
89 indicator1 => $field->indicator(1),
90 indicator2 => $field->indicator(2),
91 subfield => [@subfield_array],
97 return [@array];
100 =head2 _createKey
102 Create a random value to set it into the input name
104 =cut
106 sub _createKey {
107 return int(rand(1000000));
110 =head2 getAuthorityAuthorizedHeading
112 Retrieve the authorized heading from a MARC authority record
114 =cut
116 sub getAuthorityAuthorizedHeading {
117 my ( $record, $schema ) = @_;
118 return unless ( ref $record eq 'MARC::Record' );
119 if ( $schema eq 'unimarc' ) {
121 # construct UNIMARC summary, that is quite different from MARC21 one
122 # accepted form
123 foreach my $field ( $record->field('2..') ) {
124 return $field->as_string('abcdefghijlmnopqrstuvwxyz');
127 else {
128 foreach my $field ( $record->field('1..') ) {
129 my $tag = $field->tag();
130 next if "152" eq $tag;
132 # FIXME - 152 is not a good tag to use
133 # in MARC21 -- purely local tags really ought to be
134 # 9XX
135 if ( $tag eq '100' ) {
136 return $field->as_string('abcdefghjklmnopqrstvxyz68');
138 elsif ( $tag eq '110' ) {
139 return $field->as_string('abcdefghklmnoprstvxyz68');
141 elsif ( $tag eq '111' ) {
142 return $field->as_string('acdefghklnpqstvxyz68');
144 elsif ( $tag eq '130' ) {
145 return $field->as_string('adfghklmnoprstvxyz68');
147 elsif ( $tag eq '148' ) {
148 return $field->as_string('abvxyz68');
150 elsif ( $tag eq '150' ) {
151 return $field->as_string('abvxyz68');
153 elsif ( $tag eq '151' ) {
154 return $field->as_string('avxyz68');
156 elsif ( $tag eq '155' ) {
157 return $field->as_string('abvxyz68');
159 elsif ( $tag eq '180' ) {
160 return $field->as_string('vxyz68');
162 elsif ( $tag eq '181' ) {
163 return $field->as_string('vxyz68');
165 elsif ( $tag eq '182' ) {
166 return $field->as_string('vxyz68');
168 elsif ( $tag eq '185' ) {
169 return $field->as_string('vxyz68');
171 else {
172 return $field->as_string();
176 return;