Lines 80-86
sub copy_field {
Link Here
|
80 |
my $toFieldName = $params->{to_field}; |
80 |
my $toFieldName = $params->{to_field}; |
81 |
my $toSubfieldName = $params->{to_subfield}; |
81 |
my $toSubfieldName = $params->{to_subfield}; |
82 |
my $regex = $params->{regex}; |
82 |
my $regex = $params->{regex}; |
83 |
my $n = $params->{n}; |
83 |
my $field_numbers = $params->{field_numbers} // []; |
84 |
|
84 |
|
85 |
if ( ! ( $record && $fromFieldName && $toFieldName ) ) { return; } |
85 |
if ( ! ( $record && $fromFieldName && $toFieldName ) ) { return; } |
86 |
|
86 |
|
Lines 93-99
sub copy_field {
Link Here
|
93 |
from_field => $fromFieldName, |
93 |
from_field => $fromFieldName, |
94 |
to_field => $toFieldName, |
94 |
to_field => $toFieldName, |
95 |
regex => $regex, |
95 |
regex => $regex, |
96 |
n => $n |
96 |
field_numbers => $field_numbers, |
97 |
}); |
97 |
}); |
98 |
} else { |
98 |
} else { |
99 |
_copy_subfield({ |
99 |
_copy_subfield({ |
Lines 103-109
sub copy_field {
Link Here
|
103 |
to_field => $toFieldName, |
103 |
to_field => $toFieldName, |
104 |
to_subfield => $toSubfieldName, |
104 |
to_subfield => $toSubfieldName, |
105 |
regex => $regex, |
105 |
regex => $regex, |
106 |
n => $n |
106 |
field_numbers => $field_numbers, |
107 |
}); |
107 |
}); |
108 |
} |
108 |
} |
109 |
|
109 |
|
Lines 115-128
sub _copy_field {
Link Here
|
115 |
my $fromFieldName = $params->{from_field}; |
115 |
my $fromFieldName = $params->{from_field}; |
116 |
my $toFieldName = $params->{to_field}; |
116 |
my $toFieldName = $params->{to_field}; |
117 |
my $regex = $params->{regex}; |
117 |
my $regex = $params->{regex}; |
118 |
my $n = $params->{n}; |
118 |
my $field_numbers = $params->{field_numbers} // []; |
119 |
|
119 |
|
120 |
_copy_move_field({ |
120 |
_copy_move_field({ |
121 |
record => $record, |
121 |
record => $record, |
122 |
from_field => $fromFieldName, |
122 |
from_field => $fromFieldName, |
123 |
to_field => $toFieldName, |
123 |
to_field => $toFieldName, |
124 |
regex => $regex, |
124 |
regex => $regex, |
125 |
n => $n |
125 |
field_numbers => $field_numbers, |
126 |
}); |
126 |
}); |
127 |
} |
127 |
} |
128 |
|
128 |
|
Lines 134-143
sub _copy_subfield {
Link Here
|
134 |
my $toFieldName = $params->{to_field}; |
134 |
my $toFieldName = $params->{to_field}; |
135 |
my $toSubfieldName = $params->{to_subfield}; |
135 |
my $toSubfieldName = $params->{to_subfield}; |
136 |
my $regex = $params->{regex}; |
136 |
my $regex = $params->{regex}; |
137 |
my $n = $params->{n}; |
137 |
my $field_numbers = $params->{field_numbers} // []; |
138 |
|
138 |
|
139 |
my @values = read_field({ record => $record, field => $fromFieldName, subfield => $fromSubfieldName }); |
139 |
my @values = read_field({ record => $record, field => $fromFieldName, subfield => $fromSubfieldName }); |
140 |
@values = ( $values[$n-1] ) if ( $n ); |
140 |
if ( @$field_numbers ) { |
|
|
141 |
@values = map { $_ <= @values ? $values[ $_ - 1 ] : () } @$field_numbers; |
142 |
} |
141 |
_modify_values({ values => \@values, regex => $regex }); |
143 |
_modify_values({ values => \@values, regex => $regex }); |
142 |
|
144 |
|
143 |
update_field({ record => $record, field => $toFieldName, subfield => $toSubfieldName, values => \@values }); |
145 |
update_field({ record => $record, field => $toFieldName, subfield => $toSubfieldName, values => \@values }); |
Lines 149-154
sub update_field {
Link Here
|
149 |
my $fieldName = $params->{field}; |
151 |
my $fieldName = $params->{field}; |
150 |
my $subfieldName = $params->{subfield}; |
152 |
my $subfieldName = $params->{subfield}; |
151 |
my @values = @{ $params->{values} }; |
153 |
my @values = @{ $params->{values} }; |
|
|
154 |
my $field_numbers = $params->{field_numbers} // []; |
152 |
|
155 |
|
153 |
if ( ! ( $record && $fieldName ) ) { return; } |
156 |
if ( ! ( $record && $fieldName ) ) { return; } |
154 |
|
157 |
|
Lines 157-163
sub update_field {
Link Here
|
157 |
die "This action is not implemented yet"; |
160 |
die "This action is not implemented yet"; |
158 |
#_update_field({ record => $record, field => $fieldName, values => \@values }); |
161 |
#_update_field({ record => $record, field => $fieldName, values => \@values }); |
159 |
} else { |
162 |
} else { |
160 |
_update_subfield({ record => $record, field => $fieldName, subfield => $subfieldName, values => \@values }); |
163 |
_update_subfield({ record => $record, field => $fieldName, subfield => $subfieldName, values => \@values, field_numbers => $field_numbers }); |
161 |
} |
164 |
} |
162 |
} |
165 |
} |
163 |
|
166 |
|
Lines 194-202
sub _update_subfield {
Link Here
|
194 |
my $subfieldName = $params->{subfield}; |
197 |
my $subfieldName = $params->{subfield}; |
195 |
my @values = @{ $params->{values} }; |
198 |
my @values = @{ $params->{values} }; |
196 |
my $dont_erase = $params->{dont_erase}; |
199 |
my $dont_erase = $params->{dont_erase}; |
|
|
200 |
my $field_numbers = $params->{field_numbers} // []; |
197 |
my $i = 0; |
201 |
my $i = 0; |
198 |
|
202 |
|
199 |
if ( my @fields = $record->field( $fieldName ) ) { |
203 |
my @fields = $record->field( $fieldName ); |
|
|
204 |
|
205 |
if ( @$field_numbers ) { |
206 |
@fields = map { $_ <= @fields ? $fields[ $_ - 1 ] : () } @$field_numbers; |
207 |
} |
208 |
|
209 |
if ( @fields ) { |
200 |
unless ( $dont_erase ) { |
210 |
unless ( $dont_erase ) { |
201 |
@values = ($values[0]) x scalar( @fields ) |
211 |
@values = ($values[0]) x scalar( @fields ) |
202 |
if @values == 1; |
212 |
if @values == 1; |
Lines 236-247
sub read_field {
Link Here
|
236 |
my $record = $params->{record}; |
246 |
my $record = $params->{record}; |
237 |
my $fieldName = $params->{field}; |
247 |
my $fieldName = $params->{field}; |
238 |
my $subfieldName = $params->{subfield}; |
248 |
my $subfieldName = $params->{subfield}; |
239 |
my $n = $params->{n}; |
249 |
my $field_numbers = $params->{field_numbers} // []; |
240 |
|
250 |
|
241 |
if ( not $subfieldName or $subfieldName eq '' ) { |
251 |
if ( not $subfieldName or $subfieldName eq '' ) { |
242 |
_read_field({ record => $record, field => $fieldName, n => $n }); |
252 |
_read_field({ record => $record, field => $fieldName, field_numbers => $field_numbers }); |
243 |
} else { |
253 |
} else { |
244 |
_read_subfield({ record => $record, field => $fieldName, subfield => $subfieldName, n => $n }); |
254 |
_read_subfield({ record => $record, field => $fieldName, subfield => $subfieldName, field_numbers => $field_numbers }); |
245 |
} |
255 |
} |
246 |
} |
256 |
} |
247 |
|
257 |
|
Lines 249-255
sub _read_field {
Link Here
|
249 |
my ( $params ) = @_; |
259 |
my ( $params ) = @_; |
250 |
my $record = $params->{record}; |
260 |
my $record = $params->{record}; |
251 |
my $fieldName = $params->{field}; |
261 |
my $fieldName = $params->{field}; |
252 |
my $n = $params->{n}; |
262 |
my $field_numbers = $params->{field_numbers} // []; |
253 |
|
263 |
|
254 |
my @fields = $record->field( $fieldName ); |
264 |
my @fields = $record->field( $fieldName ); |
255 |
|
265 |
|
Lines 259-268
sub _read_field {
Link Here
|
259 |
if $fieldName < 10; |
269 |
if $fieldName < 10; |
260 |
|
270 |
|
261 |
my @values; |
271 |
my @values; |
262 |
if ( $n ) { |
272 |
if ( @$field_numbers ) { |
263 |
if ( $n <= scalar( @fields ) ) { |
273 |
for my $field_number ( @$field_numbers ) { |
264 |
for my $sf ( $fields[$n - 1]->subfields ) { |
274 |
if ( $field_number <= scalar( @fields ) ) { |
265 |
push @values, $sf->[1]; |
275 |
for my $sf ( $fields[$field_number - 1]->subfields ) { |
|
|
276 |
push @values, $sf->[1]; |
277 |
} |
266 |
} |
278 |
} |
267 |
} |
279 |
} |
268 |
} else { |
280 |
} else { |
Lines 281-287
sub _read_subfield {
Link Here
|
281 |
my $record = $params->{record}; |
293 |
my $record = $params->{record}; |
282 |
my $fieldName = $params->{field}; |
294 |
my $fieldName = $params->{field}; |
283 |
my $subfieldName = $params->{subfield}; |
295 |
my $subfieldName = $params->{subfield}; |
284 |
my $n = $params->{n}; |
296 |
my $field_numbers = $params->{field_numbers} // []; |
285 |
|
297 |
|
286 |
my @fields = $record->field( $fieldName ); |
298 |
my @fields = $record->field( $fieldName ); |
287 |
|
299 |
|
Lines 293-308
sub _read_subfield {
Link Here
|
293 |
push( @values, @sf ); |
305 |
push( @values, @sf ); |
294 |
} |
306 |
} |
295 |
|
307 |
|
296 |
return $n |
308 |
if ( @values and @$field_numbers ) { |
297 |
? $values[$n-1] |
309 |
@values = map { $_ <= @values ? $values[ $_ - 1 ] : () } @$field_numbers; |
298 |
: @values; |
310 |
} |
|
|
311 |
|
312 |
return @values; |
299 |
} |
313 |
} |
300 |
|
314 |
|
301 |
=head2 field_exists |
315 |
=head2 field_exists |
302 |
|
316 |
|
303 |
$bool = field_exists( $record, $fieldName[, $subfieldName ]); |
317 |
@field_numbers = field_exists( $record, $fieldName[, $subfieldName ]); |
304 |
|
318 |
|
305 |
Returns true if the field exits, false otherwise. |
319 |
Returns the field numbers or an empty array. |
306 |
|
320 |
|
307 |
=cut |
321 |
=cut |
308 |
|
322 |
|
Lines 314-341
sub field_exists {
Link Here
|
314 |
|
328 |
|
315 |
if ( ! $record ) { return; } |
329 |
if ( ! $record ) { return; } |
316 |
|
330 |
|
317 |
my $return = 0; |
331 |
my @field_numbers = (); |
318 |
if ( $fieldName && $subfieldName ) { |
332 |
my $current_field_number = 1; |
319 |
$return = $record->field( $fieldName ) && $record->subfield( $fieldName, $subfieldName ); |
333 |
for my $field ( $record->field( $fieldName ) ) { |
320 |
} elsif ( $fieldName ) { |
334 |
if ( $subfieldName ) { |
321 |
$return = $record->field( $fieldName ) && 1; |
335 |
push @field_numbers, $current_field_number |
|
|
336 |
if $field->subfield( $subfieldName ); |
337 |
} else { |
338 |
push @field_numbers, $current_field_number; |
339 |
} |
340 |
$current_field_number++; |
322 |
} |
341 |
} |
323 |
|
342 |
|
324 |
return $return; |
343 |
return \@field_numbers; |
325 |
} |
344 |
} |
326 |
|
345 |
|
327 |
=head2 field_equals |
346 |
=head2 field_equals |
328 |
|
347 |
|
329 |
$bool = field_equals( $record, $value, $fieldName[, $subfieldName[, $regex [, $n ] ] ]); |
348 |
$bool = field_equals( $record, $value, $fieldName[, $subfieldName[, $regex ] ]); |
330 |
|
349 |
|
331 |
Returns true if the field equals the given value, false otherwise. |
350 |
Returns true if the field equals the given value, false otherwise. |
332 |
|
351 |
|
333 |
If a regular expression ( $regex ) is supplied, the value will be compared using |
352 |
If a regular expression ( $regex ) is supplied, the value will be compared using |
334 |
the given regex. Example: $regex = 'sought_text' |
353 |
the given regex. Example: $regex = 'sought_text' |
335 |
|
354 |
|
336 |
If $n is passed, the Nth field of a repeatable series will be used for comparison. |
|
|
337 |
Set $n to 1 or leave empty for a non-repeatable field. |
338 |
|
339 |
=cut |
355 |
=cut |
340 |
|
356 |
|
341 |
sub field_equals { |
357 |
sub field_equals { |
Lines 345-363
sub field_equals {
Link Here
|
345 |
my $fieldName = $params->{field}; |
361 |
my $fieldName = $params->{field}; |
346 |
my $subfieldName = $params->{subfield}; |
362 |
my $subfieldName = $params->{subfield}; |
347 |
my $regex = $params->{regex}; |
363 |
my $regex = $params->{regex}; |
348 |
my $n = $params->{n}; |
|
|
349 |
$n = 1 unless ( $n ); ## $n defaults to first field of a repeatable field series |
350 |
|
364 |
|
351 |
if ( ! $record ) { return; } |
365 |
if ( ! $record ) { return; } |
352 |
|
366 |
|
353 |
my @field_values = read_field({ record => $record, field => $fieldName, subfield => $subfieldName, n => $n }); |
367 |
my @field_numbers = (); |
354 |
my $field_value = $field_values[$n-1]; |
368 |
my $current_field_number = 1; |
355 |
|
369 |
FIELDS: for my $field ( $record->field( $fieldName ) ) { |
356 |
if ( $regex ) { |
370 |
my @subfield_values = $subfieldName |
357 |
return $field_value =~ m/$value/; |
371 |
? $field->subfield( $subfieldName ) |
358 |
} else { |
372 |
: map { $_->[1] } $field->subfields; |
359 |
return $field_value eq $value; |
373 |
|
|
|
374 |
SUBFIELDS: for my $subfield_value ( @subfield_values ) { |
375 |
if ( |
376 |
( |
377 |
$regex and $subfield_value =~ m/$value/ |
378 |
) or ( |
379 |
$subfield_value eq $value |
380 |
) |
381 |
) { |
382 |
push @field_numbers, $current_field_number; |
383 |
last SUBFIELDS; |
384 |
} |
385 |
} |
386 |
$current_field_number++; |
360 |
} |
387 |
} |
|
|
388 |
|
389 |
return \@field_numbers; |
361 |
} |
390 |
} |
362 |
|
391 |
|
363 |
=head2 move_field |
392 |
=head2 move_field |
Lines 381-387
sub move_field {
Link Here
|
381 |
my $toFieldName = $params->{to_field}; |
410 |
my $toFieldName = $params->{to_field}; |
382 |
my $toSubfieldName = $params->{to_subfield}; |
411 |
my $toSubfieldName = $params->{to_subfield}; |
383 |
my $regex = $params->{regex}; |
412 |
my $regex = $params->{regex}; |
384 |
my $n = $params->{n}; |
413 |
my $field_numbers = $params->{field_numbers} // []; |
385 |
|
414 |
|
386 |
if ( not $fromSubfieldName or $fromSubfieldName eq '' |
415 |
if ( not $fromSubfieldName or $fromSubfieldName eq '' |
387 |
or not $toSubfieldName or $toSubfieldName eq '' |
416 |
or not $toSubfieldName or $toSubfieldName eq '' |
Lines 391-397
sub move_field {
Link Here
|
391 |
from_field => $fromFieldName, |
420 |
from_field => $fromFieldName, |
392 |
to_field => $toFieldName, |
421 |
to_field => $toFieldName, |
393 |
regex => $regex, |
422 |
regex => $regex, |
394 |
n => $n, |
423 |
field_numbers => $field_numbers, |
395 |
}); |
424 |
}); |
396 |
} else { |
425 |
} else { |
397 |
_move_subfield({ |
426 |
_move_subfield({ |
Lines 401-407
sub move_field {
Link Here
|
401 |
to_field => $toFieldName, |
430 |
to_field => $toFieldName, |
402 |
to_subfield => $toSubfieldName, |
431 |
to_subfield => $toSubfieldName, |
403 |
regex => $regex, |
432 |
regex => $regex, |
404 |
n => $n, |
433 |
field_numbers => $field_numbers, |
405 |
}); |
434 |
}); |
406 |
} |
435 |
} |
407 |
} |
436 |
} |
Lines 412-424
sub _move_field {
Link Here
|
412 |
my $fromFieldName = $params->{from_field}; |
441 |
my $fromFieldName = $params->{from_field}; |
413 |
my $toFieldName = $params->{to_field}; |
442 |
my $toFieldName = $params->{to_field}; |
414 |
my $regex = $params->{regex}; |
443 |
my $regex = $params->{regex}; |
415 |
my $n = $params->{n}; |
444 |
my $field_numbers = $params->{field_numbers} // []; |
|
|
445 |
|
416 |
_copy_move_field({ |
446 |
_copy_move_field({ |
417 |
record => $record, |
447 |
record => $record, |
418 |
from_field => $fromFieldName, |
448 |
from_field => $fromFieldName, |
419 |
to_field => $toFieldName, |
449 |
to_field => $toFieldName, |
420 |
regex => $regex, |
450 |
regex => $regex, |
421 |
n => $n, |
451 |
field_numbers => $field_numbers, |
422 |
action => 'move', |
452 |
action => 'move', |
423 |
}); |
453 |
}); |
424 |
} |
454 |
} |
Lines 431-441
sub _move_subfield {
Link Here
|
431 |
my $toFieldName = $params->{to_field}; |
461 |
my $toFieldName = $params->{to_field}; |
432 |
my $toSubfieldName = $params->{to_subfield}; |
462 |
my $toSubfieldName = $params->{to_subfield}; |
433 |
my $regex = $params->{regex}; |
463 |
my $regex = $params->{regex}; |
434 |
my $n = $params->{n}; |
464 |
my $field_numbers = $params->{field_numbers} // []; |
435 |
|
465 |
|
436 |
# Copy |
466 |
# Copy |
437 |
my @values = read_field({ record => $record, field => $fromFieldName, subfield => $fromSubfieldName }); |
467 |
my @values = read_field({ record => $record, field => $fromFieldName, subfield => $fromSubfieldName }); |
438 |
@values = ( $values[$n-1] ) if $n; |
468 |
if ( @$field_numbers ) { |
|
|
469 |
@values = map { $_ <= @values ? $values[ $_ - 1 ] : () } @$field_numbers; |
470 |
} |
439 |
_modify_values({ values => \@values, regex => $regex }); |
471 |
_modify_values({ values => \@values, regex => $regex }); |
440 |
_update_subfield({ record => $record, field => $toFieldName, subfield => $toSubfieldName, dont_erase => 1, values => \@values }); |
472 |
_update_subfield({ record => $record, field => $toFieldName, subfield => $toSubfieldName, dont_erase => 1, values => \@values }); |
441 |
|
473 |
|
Lines 444-450
sub _move_subfield {
Link Here
|
444 |
record => $record, |
476 |
record => $record, |
445 |
field => $fromFieldName, |
477 |
field => $fromFieldName, |
446 |
subfield => $fromSubfieldName, |
478 |
subfield => $fromSubfieldName, |
447 |
n => $n, |
479 |
field_numbers => $field_numbers, |
448 |
}); |
480 |
}); |
449 |
} |
481 |
} |
450 |
|
482 |
|
Lines 464-475
sub delete_field {
Link Here
|
464 |
my $record = $params->{record}; |
496 |
my $record = $params->{record}; |
465 |
my $fieldName = $params->{field}; |
497 |
my $fieldName = $params->{field}; |
466 |
my $subfieldName = $params->{subfield}; |
498 |
my $subfieldName = $params->{subfield}; |
467 |
my $n = $params->{n}; |
499 |
my $field_numbers = $params->{field_numbers} // []; |
468 |
|
500 |
|
469 |
if ( not $subfieldName or $subfieldName eq '' ) { |
501 |
if ( not $subfieldName or $subfieldName eq '' ) { |
470 |
_delete_field({ record => $record, field => $fieldName, n => $n }); |
502 |
_delete_field({ record => $record, field => $fieldName, field_numbers => $field_numbers }); |
471 |
} else { |
503 |
} else { |
472 |
_delete_subfield({ record => $record, field => $fieldName, subfield => $subfieldName, n => $n }); |
504 |
_delete_subfield({ record => $record, field => $fieldName, subfield => $subfieldName, field_numbers => $field_numbers }); |
473 |
} |
505 |
} |
474 |
} |
506 |
} |
475 |
|
507 |
|
Lines 477-487
sub _delete_field {
Link Here
|
477 |
my ( $params ) = @_; |
509 |
my ( $params ) = @_; |
478 |
my $record = $params->{record}; |
510 |
my $record = $params->{record}; |
479 |
my $fieldName = $params->{field}; |
511 |
my $fieldName = $params->{field}; |
480 |
my $n = $params->{n}; |
512 |
my $field_numbers = $params->{field_numbers} // []; |
481 |
|
513 |
|
482 |
my @fields = $record->field( $fieldName ); |
514 |
my @fields = $record->field( $fieldName ); |
483 |
|
515 |
|
484 |
@fields = ( $fields[$n-1] ) if ( $n ); |
516 |
if ( @$field_numbers ) { |
|
|
517 |
@fields = map { $_ <= @fields ? $fields[ $_ - 1 ] : () } @$field_numbers; |
518 |
} |
485 |
foreach my $field ( @fields ) { |
519 |
foreach my $field ( @fields ) { |
486 |
$record->delete_field( $field ); |
520 |
$record->delete_field( $field ); |
487 |
} |
521 |
} |
Lines 492-502
sub _delete_subfield {
Link Here
|
492 |
my $record = $params->{record}; |
526 |
my $record = $params->{record}; |
493 |
my $fieldName = $params->{field}; |
527 |
my $fieldName = $params->{field}; |
494 |
my $subfieldName = $params->{subfield}; |
528 |
my $subfieldName = $params->{subfield}; |
495 |
my $n = $params->{n}; |
529 |
my $field_numbers = $params->{field_numbers} // []; |
496 |
|
530 |
|
497 |
my @fields = $record->field( $fieldName ); |
531 |
my @fields = $record->field( $fieldName ); |
498 |
|
532 |
|
499 |
@fields = ( $fields[$n-1] ) if ( $n ); |
533 |
if ( @$field_numbers ) { |
|
|
534 |
@fields = map { $_ <= @fields ? $fields[ $_ - 1 ] : () } @$field_numbers; |
535 |
} |
500 |
|
536 |
|
501 |
foreach my $field ( @fields ) { |
537 |
foreach my $field ( @fields ) { |
502 |
$field->delete_subfield( code => $subfieldName ); |
538 |
$field->delete_subfield( code => $subfieldName ); |
Lines 510-521
sub _copy_move_field {
Link Here
|
510 |
my $fromFieldName = $params->{from_field}; |
546 |
my $fromFieldName = $params->{from_field}; |
511 |
my $toFieldName = $params->{to_field}; |
547 |
my $toFieldName = $params->{to_field}; |
512 |
my $regex = $params->{regex}; |
548 |
my $regex = $params->{regex}; |
513 |
my $n = $params->{n}; |
549 |
my $field_numbers = $params->{field_numbers}; |
514 |
my $action = $params->{action} || 'copy'; |
550 |
my $action = $params->{action} || 'copy'; |
515 |
|
551 |
|
516 |
my @fields = $record->field( $fromFieldName ); |
552 |
my @fields = $record->field( $fromFieldName ); |
517 |
if ( $n and $n <= scalar( @fields ) ) { |
553 |
if ( @$field_numbers ) { |
518 |
@fields = ( $fields[$n - 1] ); |
554 |
@fields = map { $_ <= @fields ? $fields[ $_ - 1 ] : () } @$field_numbers; |
519 |
} |
555 |
} |
520 |
|
556 |
|
521 |
for my $field ( @fields ) { |
557 |
for my $field ( @fields ) { |