Lines 220-226
sub move_to_biblio {
Link Here
|
220 |
|
220 |
|
221 |
=head3 batch_update |
221 |
=head3 batch_update |
222 |
|
222 |
|
223 |
Koha::Items->search->batch_update |
223 |
Koha::Items->search->batch_update( |
224 |
{ |
224 |
{ |
225 |
new_values => { |
225 |
new_values => { |
226 |
itemnotes => $new_item_notes, |
226 |
itemnotes => $new_item_notes, |
Lines 280-405
Callback function to call after an item has been modified
Link Here
|
280 |
sub batch_update { |
280 |
sub batch_update { |
281 |
my ( $self, $params ) = @_; |
281 |
my ( $self, $params ) = @_; |
282 |
|
282 |
|
283 |
my $regex_mod = $params->{regex_mod} || {}; |
283 |
my $regex_mod = $params->{regex_mod} || {}; |
284 |
my $new_values = $params->{new_values} || {}; |
284 |
my $new_values = $params->{new_values} || {}; |
285 |
my $exclude_from_local_holds_priority = $params->{exclude_from_local_holds_priority}; |
285 |
my $exclude_from_local_holds_priority = $params->{exclude_from_local_holds_priority}; |
286 |
my $mark_items_returned = $params->{mark_items_returned}; |
286 |
my $mark_items_returned = $params->{mark_items_returned}; |
287 |
my $callback = $params->{callback}; |
287 |
my $sort1 = $params->{sort1}; |
|
|
288 |
my $sort2 = $params->{sort2}; |
289 |
my $callback = $params->{callback}; |
288 |
|
290 |
|
289 |
my (@modified_itemnumbers, $modified_fields); |
291 |
my (@modified_itemnumbers, $modified_fields); |
290 |
my $i; |
292 |
my $i; |
291 |
my $schema = Koha::Database->new->schema; |
293 |
my $schema = Koha::Database->new->schema; |
292 |
while ( my $item = $self->next ) { |
294 |
while ( my $item = $self->next ) { |
293 |
|
295 |
|
294 |
try {$schema->txn_do(sub { |
296 |
try { |
295 |
my $modified_holds_priority = 0; |
297 |
$schema->txn_do( |
296 |
my $item_returned = 0; |
298 |
sub { |
297 |
if ( defined $exclude_from_local_holds_priority ) { |
299 |
my $modified_holds_priority = 0; |
298 |
if(!defined $item->exclude_from_local_holds_priority || $item->exclude_from_local_holds_priority != $exclude_from_local_holds_priority) { |
300 |
my $item_returned = 0; |
299 |
$item->exclude_from_local_holds_priority($exclude_from_local_holds_priority)->store; |
301 |
my $sort_fields_modified = 0; |
300 |
$modified_holds_priority = 1; |
302 |
if ( defined $exclude_from_local_holds_priority ) { |
301 |
} |
303 |
if ( !defined $item->exclude_from_local_holds_priority |
302 |
} |
304 |
|| $item->exclude_from_local_holds_priority != $exclude_from_local_holds_priority ) |
|
|
305 |
{ |
306 |
$item->exclude_from_local_holds_priority($exclude_from_local_holds_priority)->store; |
307 |
$modified_holds_priority = 1; |
308 |
} |
309 |
} |
310 |
if ( defined $sort1 || defined $sort2 ) { |
311 |
$item->sort1($sort1)->store if $sort1 ne ""; |
312 |
$sort_fields_modified++ if $sort1 ne ""; |
313 |
$item->sort2($sort2)->store if $sort2 ne ""; |
314 |
$sort_fields_modified++ if $sort2 ne ""; |
315 |
} |
303 |
|
316 |
|
304 |
my $modified = 0; |
317 |
my $modified = 0; |
305 |
my $new_values = {%$new_values}; # Don't modify the original |
318 |
my $new_values = {%$new_values}; # Don't modify the original |
306 |
|
319 |
|
307 |
my $old_values = $item->unblessed; |
320 |
my $old_values = $item->unblessed; |
308 |
if ( $item->more_subfields_xml ) { |
321 |
if ( $item->more_subfields_xml ) { |
309 |
$old_values = { |
322 |
$old_values = { |
310 |
%$old_values, |
323 |
%$old_values, |
311 |
%{$item->additional_attributes->to_hashref}, |
324 |
%{ $item->additional_attributes->to_hashref }, |
312 |
}; |
325 |
}; |
313 |
} |
326 |
} |
|
|
327 |
|
328 |
for my $attr ( keys %$regex_mod ) { |
329 |
my $old_value = $old_values->{$attr}; |
314 |
|
330 |
|
315 |
for my $attr ( keys %$regex_mod ) { |
331 |
next unless $old_value; |
316 |
my $old_value = $old_values->{$attr}; |
|
|
317 |
|
332 |
|
318 |
next unless $old_value; |
333 |
my $value = apply_regex( |
|
|
334 |
{ |
335 |
%{ $regex_mod->{$attr} }, |
336 |
value => $old_value, |
337 |
} |
338 |
); |
319 |
|
339 |
|
320 |
my $value = apply_regex( |
340 |
$new_values->{$attr} = $value; |
321 |
{ |
|
|
322 |
%{ $regex_mod->{$attr} }, |
323 |
value => $old_value, |
324 |
} |
341 |
} |
325 |
); |
|
|
326 |
|
342 |
|
327 |
$new_values->{$attr} = $value; |
343 |
for my $attribute ( keys %$new_values ) { |
328 |
} |
344 |
next if $attribute eq 'more_subfields_xml'; # Already counted before |
329 |
|
345 |
|
330 |
for my $attribute ( keys %$new_values ) { |
346 |
my $old = $old_values->{$attribute}; |
331 |
next if $attribute eq 'more_subfields_xml'; # Already counted before |
347 |
my $new = $new_values->{$attribute}; |
|
|
348 |
$modified++ |
349 |
if ( defined $old xor defined $new ) |
350 |
|| ( defined $old && defined $new && $new ne $old ); |
351 |
} |
332 |
|
352 |
|
333 |
my $old = $old_values->{$attribute}; |
353 |
{ # Dealing with more_subfields_xml |
334 |
my $new = $new_values->{$attribute}; |
|
|
335 |
$modified++ |
336 |
if ( defined $old xor defined $new ) |
337 |
|| ( defined $old && defined $new && $new ne $old ); |
338 |
} |
339 |
|
354 |
|
340 |
{ # Dealing with more_subfields_xml |
355 |
my $frameworkcode = $item->biblio->frameworkcode; |
341 |
|
356 |
my $tagslib = C4::Biblio::GetMarcStructure( 1, $frameworkcode, { unsafe => 1 } ); |
342 |
my $frameworkcode = $item->biblio->frameworkcode; |
357 |
my ( $itemtag, $itemsubfield ) = C4::Biblio::GetMarcFromKohaField("items.itemnumber"); |
343 |
my $tagslib = C4::Biblio::GetMarcStructure( 1, $frameworkcode, { unsafe => 1 }); |
|
|
344 |
my ( $itemtag, $itemsubfield ) = C4::Biblio::GetMarcFromKohaField( "items.itemnumber" ); |
345 |
|
346 |
my @more_subfield_tags = map { |
347 |
( |
348 |
ref($_) |
349 |
&& %$_ |
350 |
&& !$_->{kohafield} # Get subfields that are not mapped |
351 |
) |
352 |
? $_->{tagsubfield} |
353 |
: () |
354 |
} values %{ $tagslib->{$itemtag} }; |
355 |
|
356 |
my $more_subfields_xml = Koha::Item::Attributes->new( |
357 |
{ |
358 |
map { |
359 |
exists $new_values->{$_} ? ( $_ => $new_values->{$_} ) |
360 |
: exists $old_values->{$_} |
361 |
? ( $_ => $old_values->{$_} ) |
362 |
: () |
363 |
} @more_subfield_tags |
364 |
} |
365 |
)->to_marcxml($frameworkcode); |
366 |
|
358 |
|
367 |
$new_values->{more_subfields_xml} = $more_subfields_xml; |
359 |
my @more_subfield_tags = map { |
|
|
360 |
( |
361 |
ref($_) |
362 |
&& %$_ |
363 |
&& !$_->{kohafield} # Get subfields that are not mapped |
364 |
) |
365 |
? $_->{tagsubfield} |
366 |
: () |
367 |
} values %{ $tagslib->{$itemtag} }; |
368 |
|
368 |
|
369 |
delete $new_values->{$_} for @more_subfield_tags; # Clean the hash |
369 |
my $more_subfields_xml = Koha::Item::Attributes->new( |
|
|
370 |
{ |
371 |
map { |
372 |
exists $new_values->{$_} ? ( $_ => $new_values->{$_} ) |
373 |
: exists $old_values->{$_} ? ( $_ => $old_values->{$_} ) |
374 |
: () |
375 |
} @more_subfield_tags |
376 |
} |
377 |
)->to_marcxml($frameworkcode); |
370 |
|
378 |
|
371 |
} |
379 |
$new_values->{more_subfields_xml} = $more_subfields_xml; |
372 |
|
380 |
|
373 |
if ( $modified ) { |
381 |
delete $new_values->{$_} for @more_subfield_tags; # Clean the hash |
374 |
my $itemlost_pre = $item->itemlost; |
|
|
375 |
$item->set($new_values)->store({skip_record_index => 1}); |
376 |
|
382 |
|
377 |
C4::Circulation::LostItem( |
383 |
} |
378 |
$item->itemnumber, 'batchmod', undef, |
384 |
|
379 |
{ skip_record_index => 1 } |
385 |
if ($modified) { |
380 |
) if $item->itemlost |
386 |
my $itemlost_pre = $item->itemlost; |
381 |
and not $itemlost_pre; |
387 |
$item->set($new_values)->store( { skip_record_index => 1 } ); |
382 |
} |
388 |
|
383 |
if ( $mark_items_returned ){ |
389 |
C4::Circulation::LostItem( |
384 |
my $issue = $item->checkout; |
390 |
$item->itemnumber, 'batchmod', undef, |
385 |
if( $issue ){ |
391 |
{ skip_record_index => 1 } |
386 |
$item_returned = 1; |
392 |
) |
387 |
C4::Circulation::MarkIssueReturned( |
393 |
if $item->itemlost |
388 |
$issue->borrowernumber, |
394 |
and not $itemlost_pre; |
389 |
$item->itemnumber, |
395 |
} |
390 |
undef, |
396 |
if ($mark_items_returned) { |
391 |
$issue->patron->privacy, |
397 |
my $issue = $item->checkout; |
392 |
{ |
398 |
if ($issue) { |
393 |
skip_record_index => 1, |
399 |
$item_returned = 1; |
394 |
skip_holds_queue => 1, |
400 |
C4::Circulation::MarkIssueReturned( |
|
|
401 |
$issue->borrowernumber, |
402 |
$item->itemnumber, |
403 |
undef, |
404 |
$issue->patron->privacy, |
405 |
{ |
406 |
skip_record_index => 1, |
407 |
skip_holds_queue => 1, |
408 |
} |
409 |
); |
395 |
} |
410 |
} |
396 |
); |
411 |
} |
397 |
} |
|
|
398 |
} |
399 |
|
412 |
|
400 |
push @modified_itemnumbers, $item->itemnumber if $modified || $modified_holds_priority || $item_returned; |
413 |
push @modified_itemnumbers, $item->itemnumber |
401 |
$modified_fields += $modified + $modified_holds_priority + $item_returned; |
414 |
if $modified || $modified_holds_priority || $item_returned || $sort_fields_modified; |
402 |
})} |
415 |
$modified_fields += $modified + $modified_holds_priority + $item_returned + $sort_fields_modified; |
|
|
416 |
} |
417 |
) |
418 |
} |
403 |
catch { |
419 |
catch { |
404 |
warn $_ |
420 |
warn $_ |
405 |
}; |
421 |
}; |