-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCrud.pm
More file actions
150 lines (108 loc) · 3.43 KB
/
Crud.pm
File metadata and controls
150 lines (108 loc) · 3.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package MarcSimple::Crud;
use strict;
use warnings;
use MARC::Record;
use MarcSimple::Utils;
require Exporter;
use vars qw(@ISA @EXPORT);
@ISA = qw(Exporter);
@EXPORT = qw(
&GetSubfield
&AddSubfield
&UpdateSubfield
&SubfieldsToMarc
);
=head1 NAME
MarcSimple::Crud - crud API for Marc records.
=head1 DESCRIPTION
Crud.pm contains subroutines for create, update and delete
fields or subfields in a marc records.
=head1 EXPORTED FUNCTIONS
=head2 GetSubfield
$value = GetSubfield($record, $zone);
Exported function for getting a unique (or the first one) subfield value.
The first argument is the MARC::Record object from which retrieving value.
The second argument is a the Marc zone (Field tag and subfield code separated
by a &. i.e '200$f').
=cut
sub GetSubfield {
my ($record, $zone) = @_;
return '' unless IsMarcRecord($record);
my ($tag, $code) = split(/\$/, $zone);
my $value = '';
if ($record->field($tag) && $record->field($tag)->subfield($code)) {
$value = $record->field($tag)->subfield($code);
}
return $value;
}
=head2 AddSubfield
my $success = AddSubfield($record, $zone, $value);
Add a subfield in the first tag found in the marc record.
If no field match the tag, create it.
The first argument is the MARC::Record object from which retrieving value.
The second argument is a the Marc zone (Field tag and subfield code separated
The last argument is the value ti insert.
=cut
sub AddSubfield {
my ($record, $zone, $value) = @_;
return 0 unless DefNoNull($value);
return 0 unless IsMarcRecord($record);
my ($tag, $code) = split(/\$/, $zone);
if ($tag && $code) {
if ($record->field($tag)) {
eval { $record->field($tag)->add_subfields( $code => $value ); };
return 0 if $@;
}
else {
my $field = MARC::Field->new($tag, '', '', $code => $value);
eval{ $record->insert_fields_ordered($field); };
return 0 if $@;
}
return 1;
}
return 0;
}
=head2 UpdateSubfield
my $sucess = UpdateSubfield($record, $zone, $value);
Update value of a subfield described by <tag>$<code>. Return 0 if
the subfield has not been updated.
=cut
sub UpdateSubfield {
my ($record, $zone, $value) = @_;
return 0 unless DefNoNull($value);
return 0 unless IsMarcRecord($record);
my ($tag, $code) = split(/\$/, $zone);
if ($tag && $code) {
if ($record->field($tag) && $record->field($tag)->subfield($code)) {
eval { $record->field($tag)->update( $code => $value ); };
if ($@) {
return 0;
}
return 1;
}
}
return 0;
}
=head2 SubfieldsToMarc
my $sucess = SubfieldsToMarc($record, $tag, [ [ 'a', 'foo' ], [ 'b', 'bar'] ]);
Put a subfields array in a record ($record) with a given tag ($tag).
=cut
sub SubfieldsToMarc {
my ($record, $tag, $subfields) = @_;
return 0 unless IsMarcRecord($record);
return 0 unless IsValidTag($tag);
eval { my $a = @$subfields; };
# Not an array reference.
return 0 if $@;
my $success = 1;
foreach my $subfield ( @$subfields ) {
my $code = $subfield->[0] if $subfield->[0];
my $value = $subfield->[1] if $subfield->[1];
next unless IsValidCode($code);
my $zone = "$tag\$$code";
my $result = AddSubfield($record, $zone, $value);
$success = 0 unless $result;
}
return $success;
}
1;