Bug 17078 - Format fines on opac-account.pl
[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 ( !defined($tagslib)
47                 || $tagslib->{$fieldtag}->{'@'}->{'tab'} >= 0 )
48             {
49                 push @array, {
50                     tag   => $fieldtag,
51                     key   => _createKey(),
52                     value => $field->data(),
53                 };
54             }
55         }
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 )
66                   )
67                 {
68                     push @subfield_array, {
69                         subtag => @$subfield[0],
70                         subkey => _createKey(),
71                         value  => @$subfield[1],
72                     };
73                 }
74
75             }
76
77             if (
78                 (
79                     !defined($tagslib) || ( defined $tagslib->{$fieldtag}
80                         && defined $tagslib->{$fieldtag}->{'tab'}
81                         && $tagslib->{$fieldtag}->{'tab'} >= 0 )
82                 )
83                 && @subfield_array
84               )
85             {
86                 push @array, {
87                       tag        => $fieldtag,
88                       key        => _createKey(),
89                       indicator1 => $field->indicator(1),
90                       indicator2 => $field->indicator(2),
91                       subfield   => [@subfield_array],
92                   };
93             }
94
95         }
96     }
97     return [@array];
98 }
99
100 =head2 _createKey
101
102 Create a random value to set it into the input name
103
104 =cut
105
106 sub _createKey {
107     return int(rand(1000000));
108 }
109
110 =head2 getAuthorityAuthorizedHeading
111
112 Retrieve the authorized heading from a MARC authority record
113
114 =cut
115
116 sub getAuthorityAuthorizedHeading {
117     my ( $record, $schema ) = @_;
118     return unless ( ref $record eq 'MARC::Record' );
119     if ( $schema eq 'unimarc' ) {
120
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');
125         }
126     }
127     else {
128         foreach my $field ( $record->field('1..') ) {
129             my $tag = $field->tag();
130             next if "152" eq $tag;
131
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');
137             }
138             elsif ( $tag eq '110' ) {
139                 return $field->as_string('abcdefghklmnoprstvxyz68');
140             }
141             elsif ( $tag eq '111' ) {
142                 return $field->as_string('acdefghklnpqstvxyz68');
143             }
144             elsif ( $tag eq '130' ) {
145                 return $field->as_string('adfghklmnoprstvxyz68');
146             }
147             elsif ( $tag eq '148' ) {
148                 return $field->as_string('abvxyz68');
149             }
150             elsif ( $tag eq '150' ) {
151                 return $field->as_string('abvxyz68');
152             }
153             elsif ( $tag eq '151' ) {
154                 return $field->as_string('avxyz68');
155             }
156             elsif ( $tag eq '155' ) {
157                 return $field->as_string('abvxyz68');
158             }
159             elsif ( $tag eq '180' ) {
160                 return $field->as_string('vxyz68');
161             }
162             elsif ( $tag eq '181' ) {
163                 return $field->as_string('vxyz68');
164             }
165             elsif ( $tag eq '182' ) {
166                 return $field->as_string('vxyz68');
167             }
168             elsif ( $tag eq '185' ) {
169                 return $field->as_string('vxyz68');
170             }
171             else {
172                 return $field->as_string();
173             }
174         }
175     }
176     return;
177 }
178
179 1;