Lines 38-44
use Koha::ItemTypes;
Link Here
|
38 |
use Koha::Libraries; |
38 |
use Koha::Libraries; |
39 |
use Koha::Patrons; |
39 |
use Koha::Patrons; |
40 |
use Koha::SearchEngine::Indexer; |
40 |
use Koha::SearchEngine::Indexer; |
41 |
use List::MoreUtils qw/any/; |
|
|
42 |
use C4::Search; |
41 |
use C4::Search; |
43 |
use Storable qw(thaw freeze); |
42 |
use Storable qw(thaw freeze); |
44 |
use URI::Escape; |
43 |
use URI::Escape; |
Lines 47-106
use C4::Members;
Link Here
|
47 |
use MARC::File::XML; |
46 |
use MARC::File::XML; |
48 |
use URI::Escape; |
47 |
use URI::Escape; |
49 |
use MIME::Base64 qw(decode_base64url encode_base64url); |
48 |
use MIME::Base64 qw(decode_base64url encode_base64url); |
|
|
49 |
use List::Util qw( first ); |
50 |
use List::MoreUtils qw( any uniq ); |
50 |
|
51 |
|
51 |
our $dbh = C4::Context->dbh; |
52 |
our $dbh = C4::Context->dbh; |
52 |
|
53 |
|
53 |
sub find_value { |
|
|
54 |
my ($tagfield,$insubfield,$record) = @_; |
55 |
my $result; |
56 |
my $indicator; |
57 |
foreach my $field ($record->field($tagfield)) { |
58 |
my @subfields = $field->subfields(); |
59 |
foreach my $subfield (@subfields) { |
60 |
if (@$subfield[0] eq $insubfield) { |
61 |
$result .= @$subfield[1]; |
62 |
$indicator = $field->indicator(1).$field->indicator(2); |
63 |
} |
64 |
} |
65 |
} |
66 |
return($indicator,$result); |
67 |
} |
68 |
|
69 |
sub get_item_from_barcode { |
70 |
my ($barcode)=@_; |
71 |
my $dbh=C4::Context->dbh; |
72 |
my $result; |
73 |
my $rq=$dbh->prepare("SELECT itemnumber from items where items.barcode=?"); |
74 |
$rq->execute($barcode); |
75 |
($result)=$rq->fetchrow; |
76 |
return($result); |
77 |
} |
78 |
|
79 |
# NOTE: This code is subject to change in the future with the implemenation of ajax based autobarcode code |
80 |
# NOTE: 'incremental' is the ONLY autoBarcode option available to those not using javascript |
81 |
sub _increment_barcode { |
82 |
my ($record, $frameworkcode) = @_; |
83 |
my ($tagfield,$tagsubfield) = &GetMarcFromKohaField( "items.barcode" ); |
84 |
unless ($record->field($tagfield)->subfield($tagsubfield)) { |
85 |
my $sth_barcode = $dbh->prepare("select max(abs(barcode)) from items"); |
86 |
$sth_barcode->execute; |
87 |
my ($newbarcode) = $sth_barcode->fetchrow; |
88 |
$newbarcode++; |
89 |
# OK, we have the new barcode, now create the entry in MARC record |
90 |
my $fieldItem = $record->field($tagfield); |
91 |
$record->delete_field($fieldItem); |
92 |
$fieldItem->add_subfields($tagsubfield => $newbarcode); |
93 |
$record->insert_fields_ordered($fieldItem); |
94 |
} |
95 |
return $record; |
96 |
} |
97 |
|
98 |
|
99 |
sub generate_subfield_form { |
54 |
sub generate_subfield_form { |
100 |
my ($tag, $subfieldtag, $value, $tagslib,$subfieldlib, $branches, $biblionumber, $temp, $loop_data, $i, $restrictededition, $item) = @_; |
55 |
my ($tag, $subfieldtag, $value, $tagslib,$subfieldlib, $branches, $biblionumber, $temp, $subfields, $i, $restrictededition, $item) = @_; |
101 |
|
56 |
|
102 |
my $frameworkcode = &GetFrameworkCode($biblionumber); |
57 |
my $frameworkcode = &GetFrameworkCode($biblionumber); |
103 |
|
58 |
|
|
|
59 |
$item //= {}; |
60 |
|
104 |
my %subfield_data; |
61 |
my %subfield_data; |
105 |
my $dbh = C4::Context->dbh; |
62 |
my $dbh = C4::Context->dbh; |
106 |
|
63 |
|
Lines 171-177
sub generate_subfield_form {
Link Here
|
171 |
# builds list, depending on authorised value... |
128 |
# builds list, depending on authorised value... |
172 |
if ( $subfieldlib->{authorised_value} eq "LOST" ) { |
129 |
if ( $subfieldlib->{authorised_value} eq "LOST" ) { |
173 |
my $ClaimReturnedLostValue = C4::Context->preference('ClaimReturnedLostValue'); |
130 |
my $ClaimReturnedLostValue = C4::Context->preference('ClaimReturnedLostValue'); |
174 |
my $item_is_return_claim = $ClaimReturnedLostValue && $item && $item->itemlost && $ClaimReturnedLostValue eq $item->itemlost; |
131 |
my $item_is_return_claim = $ClaimReturnedLostValue && exists $item->{itemlost} && $ClaimReturnedLostValue eq $item->{itemlost}; |
175 |
$subfield_data{IS_RETURN_CLAIM} = $item_is_return_claim; |
132 |
$subfield_data{IS_RETURN_CLAIM} = $item_is_return_claim; |
176 |
|
133 |
|
177 |
$subfield_data{IS_LOST_AV} = 1; |
134 |
$subfield_data{IS_LOST_AV} = 1; |
Lines 276-282
sub generate_subfield_form {
Link Here
|
276 |
item_style => 1, |
233 |
item_style => 1, |
277 |
}); |
234 |
}); |
278 |
my $pars= { dbh => $dbh, record => $temp, tagslib =>$tagslib, |
235 |
my $pars= { dbh => $dbh, record => $temp, tagslib =>$tagslib, |
279 |
id => $subfield_data{id}, tabloop => $loop_data }; |
236 |
id => $subfield_data{id}, tabloop => $subfields }; |
280 |
$plugin->build( $pars ); |
237 |
$plugin->build( $pars ); |
281 |
if( !$plugin->errstr ) { |
238 |
if( !$plugin->errstr ) { |
282 |
my $class= 'buttonDot'. ( $plugin->noclick? ' disabled': '' ); |
239 |
my $class= 'buttonDot'. ( $plugin->noclick? ' disabled': '' ); |
Lines 363-396
sub generate_subfield_form {
Link Here
|
363 |
return \%subfield_data; |
320 |
return \%subfield_data; |
364 |
} |
321 |
} |
365 |
|
322 |
|
366 |
# Removes some subfields when prefilling items |
|
|
367 |
# This function will remove any subfield that is not in the SubfieldsToUseWhenPrefill syspref |
368 |
sub removeFieldsForPrefill { |
369 |
|
370 |
my $item = shift; |
371 |
|
372 |
# Getting item tag |
373 |
my ($tag, $subtag) = GetMarcFromKohaField( "items.barcode" ); |
374 |
|
375 |
# Getting list of subfields to keep |
376 |
my $subfieldsToUseWhenPrefill = C4::Context->preference('SubfieldsToUseWhenPrefill'); |
377 |
|
378 |
# Removing subfields that are not in the syspref |
379 |
if ($tag && $subfieldsToUseWhenPrefill) { |
380 |
my $field = $item->field($tag); |
381 |
my @subfieldsToUse= split(/ /,$subfieldsToUseWhenPrefill); |
382 |
foreach my $subfield ($field->subfields()) { |
383 |
if (!grep { $subfield->[0] eq $_ } @subfieldsToUse) { |
384 |
$field->delete_subfield(code => $subfield->[0]); |
385 |
} |
386 |
|
387 |
} |
388 |
} |
389 |
|
390 |
return $item; |
391 |
|
392 |
} |
393 |
|
394 |
my $input = CGI->new; |
323 |
my $input = CGI->new; |
395 |
my $error = $input->param('error'); |
324 |
my $error = $input->param('error'); |
396 |
|
325 |
|
Lines 405-410
if( $input->param('itemnumber') && !$input->param('biblionumber') ){
Link Here
|
405 |
$itemnumber = $input->param('itemnumber'); |
334 |
$itemnumber = $input->param('itemnumber'); |
406 |
} |
335 |
} |
407 |
|
336 |
|
|
|
337 |
my $biblio = Koha::Biblios->find($biblionumber); |
338 |
|
408 |
my $op = $input->param('op') || q{}; |
339 |
my $op = $input->param('op') || q{}; |
409 |
my $hostitemnumber = $input->param('hostitemnumber'); |
340 |
my $hostitemnumber = $input->param('hostitemnumber'); |
410 |
my $marcflavour = C4::Context->preference("marcflavour"); |
341 |
my $marcflavour = C4::Context->preference("marcflavour"); |
Lines 416-422
my $fa_branch = $input->param('branch');
Link Here
|
416 |
my $fa_stickyduedate = $input->param('stickyduedate'); |
347 |
my $fa_stickyduedate = $input->param('stickyduedate'); |
417 |
my $fa_duedatespec = $input->param('duedatespec'); |
348 |
my $fa_duedatespec = $input->param('duedatespec'); |
418 |
|
349 |
|
419 |
my $frameworkcode = &GetFrameworkCode($biblionumber); |
350 |
our $frameworkcode = &GetFrameworkCode($biblionumber); |
420 |
|
351 |
|
421 |
# Defining which userflag is needing according to the framework currently used |
352 |
# Defining which userflag is needing according to the framework currently used |
422 |
my $userflags; |
353 |
my $userflags; |
Lines 445-473
$restrictededition = 0 if ($restrictededition != 0 && C4::Context->IsSuperLibra
Link Here
|
445 |
# In case user has fast cataloging permission (and we're in fast cataloging), editing is not restricted |
376 |
# In case user has fast cataloging permission (and we're in fast cataloging), editing is not restricted |
446 |
$restrictededition = 0 if ($restrictededition != 0 && $frameworkcode eq 'FA' && haspermission($uid, {'editcatalogue' => 'fast_cataloging'})); |
377 |
$restrictededition = 0 if ($restrictededition != 0 && $frameworkcode eq 'FA' && haspermission($uid, {'editcatalogue' => 'fast_cataloging'})); |
447 |
|
378 |
|
448 |
my $tagslib = &GetMarcStructure(1,$frameworkcode); |
379 |
our $tagslib = &GetMarcStructure(1,$frameworkcode); |
449 |
my $record = GetMarcBiblio({ biblionumber => $biblionumber }); |
380 |
my $record = GetMarcBiblio({ biblionumber => $biblionumber }); |
450 |
|
381 |
|
451 |
output_and_exit_if_error( $input, $cookie, $template, |
382 |
output_and_exit_if_error( $input, $cookie, $template, |
452 |
{ module => 'cataloguing', record => $record } ); |
383 |
{ module => 'cataloguing', record => $record } ); |
453 |
|
384 |
|
454 |
my $oldrecord = TransformMarcToKoha($record); |
385 |
my $oldrecord = TransformMarcToKoha($record); |
455 |
my $itemrecord; |
386 |
my $current_item; |
456 |
my $nextop="additem"; |
387 |
my $nextop="additem"; |
457 |
my @errors; # store errors found while checking data BEFORE saving item. |
388 |
my @errors; # store errors found while checking data BEFORE saving item. |
458 |
|
389 |
|
459 |
# Getting last created item cookie |
390 |
# Getting last created item cookie |
460 |
my $prefillitem = C4::Context->preference('PrefillItem'); |
391 |
my $prefillitem = C4::Context->preference('PrefillItem'); |
461 |
my $justaddeditem; |
392 |
my $item_from_cookie; |
462 |
my $cookieitemrecord; |
|
|
463 |
if ($prefillitem) { |
393 |
if ($prefillitem) { |
464 |
my $lastitemcookie = $input->cookie('LastCreatedItem'); |
394 |
my $lastitemcookie = $input->cookie('LastCreatedItem'); |
465 |
if ($lastitemcookie) { |
395 |
if ($lastitemcookie) { |
466 |
$lastitemcookie = decode_base64url($lastitemcookie); |
396 |
$lastitemcookie = decode_base64url($lastitemcookie); |
467 |
eval { |
397 |
eval { |
468 |
if ( thaw($lastitemcookie) ) { |
398 |
if ( thaw($lastitemcookie) ) { |
469 |
$cookieitemrecord = thaw($lastitemcookie); |
399 |
$item_from_cookie = thaw($lastitemcookie); |
470 |
$cookieitemrecord = removeFieldsForPrefill($cookieitemrecord); |
|
|
471 |
} |
400 |
} |
472 |
}; |
401 |
}; |
473 |
if ($@) { |
402 |
if ($@) { |
Lines 478-501
if ($prefillitem) {
Link Here
|
478 |
} |
407 |
} |
479 |
|
408 |
|
480 |
#------------------------------------------------------------------------------- |
409 |
#------------------------------------------------------------------------------- |
481 |
my $current_item; |
|
|
482 |
if ($op eq "additem") { |
410 |
if ($op eq "additem") { |
483 |
|
411 |
|
484 |
my $add_submit = $input->param('add_submit'); |
412 |
my $add_submit = $input->param('add_submit'); |
485 |
my $add_duplicate_submit = $input->param('add_duplicate_submit'); |
|
|
486 |
my $add_multiple_copies_submit = $input->param('add_multiple_copies_submit'); |
413 |
my $add_multiple_copies_submit = $input->param('add_multiple_copies_submit'); |
487 |
my $number_of_copies = $input->param('number_of_copies'); |
414 |
my $number_of_copies = $input->param('number_of_copies'); |
488 |
|
415 |
|
489 |
# This is a bit tricky : if there is a cookie for the last created item and |
|
|
490 |
# we just added an item, the cookie value is not correct yet (it will be updated |
491 |
# next page). To prevent the form from being filled with outdated values, we |
492 |
# force the use of "add and duplicate" feature, so the form will be filled with |
493 |
# correct values. |
494 |
$add_duplicate_submit = 1 if ($prefillitem); |
495 |
$justaddeditem = 1; |
496 |
|
497 |
my @columns = Koha::Items->columns; |
416 |
my @columns = Koha::Items->columns; |
498 |
my $biblio = Koha::Biblios->find($biblionumber); |
|
|
499 |
my $item = Koha::Item->new; |
417 |
my $item = Koha::Item->new; |
500 |
$item->biblionumber($biblio->biblionumber); |
418 |
$item->biblionumber($biblio->biblionumber); |
501 |
for my $c ( @columns ) { |
419 |
for my $c ( @columns ) { |
Lines 517-525
if ($op eq "additem") {
Link Here
|
517 |
} |
435 |
} |
518 |
$item->more_subfields_xml(undef); |
436 |
$item->more_subfields_xml(undef); |
519 |
} else { |
437 |
} else { |
520 |
my $v = $input->param("items.".$c); |
438 |
my @v = $input->multi_param("items.".$c); |
521 |
next unless defined $v; |
439 |
next unless @v; |
522 |
$item->$c($v); |
440 |
$item->$c(join ' | ', uniq @v); |
523 |
} |
441 |
} |
524 |
} |
442 |
} |
525 |
|
443 |
|
Lines 530-573
if ($op eq "additem") {
Link Here
|
530 |
} |
448 |
} |
531 |
|
449 |
|
532 |
# If we have to add or add & duplicate, we add the item |
450 |
# If we have to add or add & duplicate, we add the item |
533 |
if ( $add_submit || $add_duplicate_submit ) { |
451 |
if ( $add_submit || $prefillitem) { |
534 |
|
452 |
|
535 |
# check for item barcode # being unique |
453 |
# check for item barcode # being unique |
536 |
if ( Koha::Items->search({ barcode => $item->barcode })->count ) { |
454 |
if ( Koha::Items->search({ barcode => $item->barcode })->count ) { |
537 |
# if barcode exists, don't create, but report The problem. |
455 |
# if barcode exists, don't create, but report The problem. |
538 |
push @errors, "barcode_not_unique"; |
456 |
push @errors, "barcode_not_unique"; |
|
|
457 |
|
458 |
$current_item = $item->unblessed; # Restore edit form for the same item |
539 |
} |
459 |
} |
540 |
else { |
460 |
else { |
541 |
$item->store->discard_changes; |
461 |
$item->store->discard_changes; |
542 |
|
462 |
|
|
|
463 |
# This is a bit tricky : if there is a cookie for the last created item and |
464 |
# we just added an item, the cookie value is not correct yet (it will be updated |
465 |
# next page). To prevent the form from being filled with outdated values, we |
466 |
# force the use of "add and duplicate" feature, so the form will be filled with |
467 |
# correct values. |
468 |
|
543 |
# FIXME This need to be rewritten, we must store $item->unblessed instead |
469 |
# FIXME This need to be rewritten, we must store $item->unblessed instead |
544 |
## Pushing the last created item cookie back |
470 |
# Pushing the last created item cookie back |
545 |
#if ($prefillitem && defined $record) { |
471 |
if ( $prefillitem ) { |
546 |
# my $itemcookie = $input->cookie( |
472 |
$item_from_cookie = $input->cookie( |
547 |
# -name => 'LastCreatedItem', |
473 |
-name => 'LastCreatedItem', |
548 |
# # We encode_base64url the whole freezed structure so we're sure we won't have any encoding problems |
474 |
# We encode_base64url the whole freezed structure so we're sure we won't have any encoding problems |
549 |
# -value => encode_base64url( freeze( $record ) ), |
475 |
-value => encode_base64url( freeze( { %{$item->unblessed}, itemnumber => undef } ) ), |
550 |
# -HttpOnly => 1, |
476 |
-HttpOnly => 1, |
551 |
# -expires => '' |
477 |
-expires => '' |
552 |
# ); |
478 |
); |
553 |
|
479 |
|
554 |
# $cookie = [ $cookie, $itemcookie ]; |
480 |
$cookie = [ $cookie, $item_from_cookie ]; |
555 |
#} |
481 |
} |
556 |
|
482 |
|
557 |
} |
483 |
} |
558 |
$nextop = "additem"; |
484 |
$nextop = "additem"; |
559 |
|
485 |
|
560 |
|
|
|
561 |
# FIXME reset item to the item we were editing |
562 |
#if ($exist_itemnumber) { |
563 |
|
564 |
# $itemrecord = $record; |
565 |
#} |
566 |
$current_item = $item->unblessed; |
567 |
} |
486 |
} |
568 |
|
487 |
|
569 |
# If we have to add & duplicate |
488 |
# If we have to add & duplicate |
570 |
if ($add_duplicate_submit) { |
489 |
if ($prefillitem) { |
571 |
if (C4::Context->preference('autoBarcode') eq 'incremental') { |
490 |
if (C4::Context->preference('autoBarcode') eq 'incremental') { |
572 |
my ( $barcode ) = C4::Barcodes::ValueBuilder::incremental::get_barcode; |
491 |
my ( $barcode ) = C4::Barcodes::ValueBuilder::incremental::get_barcode; |
573 |
$current_item->{barcode} = $barcode; |
492 |
$current_item->{barcode} = $barcode; |
Lines 576-589
if ($op eq "additem") {
Link Here
|
576 |
# we have to clear the barcode field in the duplicate item record to make way for the new one generated by the javascript plugin |
495 |
# we have to clear the barcode field in the duplicate item record to make way for the new one generated by the javascript plugin |
577 |
$current_item->{barcode} = undef; # FIXME or delete? |
496 |
$current_item->{barcode} = undef; # FIXME or delete? |
578 |
} |
497 |
} |
579 |
# FIXME This subroutine needs to be adjusted |
|
|
580 |
# We want to pass $item |
581 |
# $itemrecord = removeFieldsForPrefill($itemrecord) if ($prefillitem); |
582 |
} |
498 |
} |
583 |
|
499 |
|
584 |
# If we have to add multiple copies |
500 |
# If we have to add multiple copies |
585 |
if ($add_multiple_copies_submit) { |
501 |
if ($add_multiple_copies_submit) { |
586 |
|
502 |
|
|
|
503 |
$current_item = $item->unblessed; |
504 |
|
587 |
my $copynumber = $current_item->{copynumber}; |
505 |
my $copynumber = $current_item->{copynumber}; |
588 |
my $oldbarcode = $current_item->{barcode}; |
506 |
my $oldbarcode = $current_item->{barcode}; |
589 |
|
507 |
|
Lines 594-600
if ($op eq "additem") {
Link Here
|
594 |
if ( $oldbarcode && !$testbarcode ) { |
512 |
if ( $oldbarcode && !$testbarcode ) { |
595 |
|
513 |
|
596 |
push @errors, "no_next_barcode"; |
514 |
push @errors, "no_next_barcode"; |
597 |
$itemrecord = $record; |
|
|
598 |
|
515 |
|
599 |
} |
516 |
} |
600 |
else { |
517 |
else { |
Lines 642-648
if ($op eq "additem") {
Link Here
|
642 |
if ( !$exist_itemnumber ) { |
559 |
if ( !$exist_itemnumber ) { |
643 |
delete $current_item->{itemnumber}; |
560 |
delete $current_item->{itemnumber}; |
644 |
$current_item = Koha::Item->new($current_item)->store( |
561 |
$current_item = Koha::Item->new($current_item)->store( |
645 |
{ skip_record_index => 1 } )->discard_changes->unblessed; |
562 |
{ skip_record_index => 1 } ); |
|
|
563 |
$current_item->discard_changes; # Cannot chain discard_changes |
564 |
$current_item = $current_item->unblessed; |
646 |
set_item_default_location($current_item->{itemnumber}); |
565 |
set_item_default_location($current_item->{itemnumber}); |
647 |
|
566 |
|
648 |
# We count the item only if it was really added |
567 |
# We count the item only if it was really added |
Lines 682-722
if ($op eq "additem") {
Link Here
|
682 |
} elsif ($op eq "edititem") { |
601 |
} elsif ($op eq "edititem") { |
683 |
#------------------------------------------------------------------------------- |
602 |
#------------------------------------------------------------------------------- |
684 |
# retrieve item if exist => then, it's a modif |
603 |
# retrieve item if exist => then, it's a modif |
685 |
$itemrecord = C4::Items::GetMarcItem($biblionumber,$itemnumber); |
604 |
$current_item = Koha::Items->find($itemnumber)->unblessed; |
|
|
605 |
# FIXME Handle non existent item |
686 |
$nextop = "saveitem"; |
606 |
$nextop = "saveitem"; |
687 |
#------------------------------------------------------------------------------- |
607 |
#------------------------------------------------------------------------------- |
688 |
} elsif ($op eq "dupeitem") { |
608 |
} elsif ($op eq "dupeitem") { |
689 |
#------------------------------------------------------------------------------- |
609 |
#------------------------------------------------------------------------------- |
690 |
# retrieve item if exist => then, it's a modif |
610 |
# retrieve item if exist => then, it's a modif |
691 |
$itemrecord = C4::Items::GetMarcItem($biblionumber,$itemnumber); |
611 |
my $item = Koha::Items->find($itemnumber); |
|
|
612 |
# FIXME Handle non existent item |
692 |
if (C4::Context->preference('autoBarcode') eq 'incremental') { |
613 |
if (C4::Context->preference('autoBarcode') eq 'incremental') { |
693 |
$itemrecord = _increment_barcode($itemrecord, $frameworkcode); |
614 |
my ( $barcode ) = C4::Barcodes::ValueBuilder::incremental::get_barcode; |
|
|
615 |
$item->barcode($barcode); |
694 |
} |
616 |
} |
695 |
else { |
617 |
else { |
696 |
# we have to clear the barcode field in the duplicate item record to make way for the new one generated by the javascript plugin |
618 |
$item->barcode(undef); # Don't save it! |
697 |
my ($tagfield,$tagsubfield) = &GetMarcFromKohaField( "items.barcode" ); |
|
|
698 |
my $fieldItem = $itemrecord->field($tagfield); |
699 |
$itemrecord->delete_field($fieldItem); |
700 |
$fieldItem->delete_subfields($tagsubfield); |
701 |
$itemrecord->insert_fields_ordered($fieldItem); |
702 |
} |
619 |
} |
703 |
|
620 |
|
704 |
#check for hidden subfield and remove them for the duplicated item |
|
|
705 |
foreach my $field ($itemrecord->fields()){ |
706 |
my $tag = $field->{_tag}; |
707 |
foreach my $subfield ($field->subfields()){ |
708 |
my $subfieldtag = $subfield->[0]; |
709 |
if ($tagslib->{$tag}->{$subfieldtag}->{'tab'} ne "10" |
710 |
|| abs($tagslib->{$tag}->{$subfieldtag}->{hidden})>4 ){ |
711 |
my $fieldItem = $itemrecord->field($tag); |
712 |
$itemrecord->delete_field($fieldItem); |
713 |
$fieldItem->delete_subfields($subfieldtag); |
714 |
$itemrecord->insert_fields_ordered($fieldItem); |
715 |
} |
716 |
} |
717 |
} |
718 |
|
719 |
$itemrecord = removeFieldsForPrefill($itemrecord) if ($prefillitem); |
720 |
$nextop = "additem"; |
621 |
$nextop = "additem"; |
721 |
#------------------------------------------------------------------------------- |
622 |
#------------------------------------------------------------------------------- |
722 |
} elsif ($op eq "delitem") { |
623 |
} elsif ($op eq "delitem") { |
Lines 785-793
if ($op eq "additem") {
Link Here
|
785 |
} |
686 |
} |
786 |
$item->more_subfields_xml(undef); |
687 |
$item->more_subfields_xml(undef); |
787 |
} else { |
688 |
} else { |
788 |
my $v = $input->param("items.".$c); |
689 |
my @v = $input->multi_param("items.".$c); |
789 |
next unless defined $v; |
690 |
next unless @v; |
790 |
$item->$c($v); |
691 |
$item->$c(join ' | ', uniq @v); |
791 |
} |
692 |
} |
792 |
} |
693 |
} |
793 |
|
694 |
|
Lines 795-800
if ($op eq "additem") {
Link Here
|
795 |
if ( Koha::Items->search({ barcode => $item->barcode, itemnumber => { '!=' => $item->itemnumber } })->count ) { |
696 |
if ( Koha::Items->search({ barcode => $item->barcode, itemnumber => { '!=' => $item->itemnumber } })->count ) { |
796 |
# FIXME We shouldn't need that, ->store would explode as there is a unique constraint on items.barcode |
697 |
# FIXME We shouldn't need that, ->store would explode as there is a unique constraint on items.barcode |
797 |
push @errors,"barcode_not_unique"; |
698 |
push @errors,"barcode_not_unique"; |
|
|
699 |
$current_item = $item->unblessed; # Restore edit form for the same item |
798 |
} else { |
700 |
} else { |
799 |
my $newitemlost = $item->itemlost; |
701 |
my $newitemlost = $item->itemlost; |
800 |
if ( $newitemlost && $newitemlost ge '1' && !$olditemlost ) { |
702 |
if ( $newitemlost && $newitemlost ge '1' && !$olditemlost ) { |
Lines 834-1046
if ($op) {
Link Here
|
834 |
#------------------------------------------------------------------------------- |
736 |
#------------------------------------------------------------------------------- |
835 |
|
737 |
|
836 |
# now, build existiing item list |
738 |
# now, build existiing item list |
837 |
my $temp = GetMarcBiblio({ biblionumber => $biblionumber }); |
|
|
838 |
#my @fields = $record->fields(); |
839 |
|
840 |
|
739 |
|
841 |
my %witness; #---- stores the list of subfields used at least once, with the "meaning" of the code |
740 |
my @items; |
842 |
my @big_array; |
741 |
for my $item ( $biblio->items->as_list, $biblio->host_items->as_list ) { |
843 |
#---- finds where items.itemnumber is stored |
742 |
push @items, $item->columns_to_str; |
844 |
my ( $itemtagfield, $itemtagsubfield) = &GetMarcFromKohaField( "items.itemnumber" ); |
|
|
845 |
my ($branchtagfield, $branchtagsubfield) = &GetMarcFromKohaField( "items.homebranch" ); |
846 |
C4::Biblio::EmbedItemsInMarcBiblio({ |
847 |
marc_record => $temp, |
848 |
biblionumber => $biblionumber }); |
849 |
my @fields = $temp->fields(); |
850 |
|
851 |
|
852 |
my @hostitemnumbers; |
853 |
if ( C4::Context->preference('EasyAnalyticalRecords') ) { |
854 |
my $analyticfield = '773'; |
855 |
if ($marcflavour eq 'MARC21' || $marcflavour eq 'NORMARC') { |
856 |
$analyticfield = '773'; |
857 |
} elsif ($marcflavour eq 'UNIMARC') { |
858 |
$analyticfield = '461'; |
859 |
} |
860 |
foreach my $hostfield ($temp->field($analyticfield)){ |
861 |
my $hostbiblionumber = $hostfield->subfield('0'); |
862 |
if ($hostbiblionumber){ |
863 |
my $hostrecord = GetMarcBiblio({ |
864 |
biblionumber => $hostbiblionumber, |
865 |
embed_items => 1 }); |
866 |
if ($hostrecord) { |
867 |
my ($itemfield, undef) = GetMarcFromKohaField( 'items.itemnumber' ); |
868 |
foreach my $hostitem ($hostrecord->field($itemfield)){ |
869 |
if ($hostitem->subfield('9') eq $hostfield->subfield('9')){ |
870 |
push (@fields, $hostitem); |
871 |
push (@hostitemnumbers, $hostfield->subfield('9')); |
872 |
} |
873 |
} |
874 |
} |
875 |
} |
876 |
} |
877 |
} |
743 |
} |
878 |
|
744 |
|
879 |
foreach my $field (@fields) { |
745 |
my @witness_attributes = uniq map { |
880 |
next if ( $field->tag() < 10 ); |
746 |
my $item = $_; |
881 |
|
747 |
map { defined $item->{$_} && $item->{$_} ne "" ? $_ : () } keys %$item |
882 |
my @subf = $field->subfields or (); # don't use ||, as that forces $field->subfelds to be interpreted in scalar context |
748 |
} @items; |
883 |
my %this_row; |
|
|
884 |
# loop through each subfield |
885 |
my $i = 0; |
886 |
foreach my $subfield (@subf){ |
887 |
my $subfieldcode = $subfield->[0]; |
888 |
my $subfieldvalue= $subfield->[1]; |
889 |
|
890 |
next if ($tagslib->{$field->tag()}->{$subfieldcode}->{tab} ne 10 |
891 |
&& ($field->tag() ne $itemtagfield |
892 |
&& $subfieldcode ne $itemtagsubfield)); |
893 |
$witness{$subfieldcode} = $tagslib->{$field->tag()}->{$subfieldcode}->{lib} if ($tagslib->{$field->tag()}->{$subfieldcode}->{tab} eq 10); |
894 |
if ($tagslib->{$field->tag()}->{$subfieldcode}->{tab} eq 10) { |
895 |
$this_row{$subfieldcode} .= " | " if($this_row{$subfieldcode}); |
896 |
$this_row{$subfieldcode} .= GetAuthorisedValueDesc( $field->tag(), |
897 |
$subfieldcode, $subfieldvalue, '', $tagslib) |
898 |
|| $subfieldvalue; |
899 |
} |
900 |
|
749 |
|
901 |
if (($field->tag eq $branchtagfield) && ($subfieldcode eq $branchtagsubfield) && C4::Context->preference("IndependentBranches")) { |
750 |
our ( $itemtagfield, $itemtagsubfield ) = &GetMarcFromKohaField("items.itemnumber"); |
902 |
#verifying rights |
|
|
903 |
my $userenv = C4::Context->userenv(); |
904 |
unless (C4::Context->IsSuperLibrarian() or (($userenv->{'branch'} eq $subfieldvalue))){ |
905 |
$this_row{'nomod'} = 1; |
906 |
} |
907 |
} |
908 |
$this_row{itemnumber} = $subfieldvalue if ($field->tag() eq $itemtagfield && $subfieldcode eq $itemtagsubfield); |
909 |
|
910 |
if ( C4::Context->preference('EasyAnalyticalRecords') ) { |
911 |
foreach my $hostitemnumber (@hostitemnumbers) { |
912 |
my $item = Koha::Items->find( $hostitemnumber ); |
913 |
if ($this_row{itemnumber} eq $hostitemnumber) { |
914 |
$this_row{hostitemflag} = 1; |
915 |
$this_row{hostbiblionumber}= $item->biblio->biblionumber; |
916 |
last; |
917 |
} |
918 |
} |
919 |
} |
920 |
} |
921 |
if (%this_row) { |
922 |
push(@big_array, \%this_row); |
923 |
} |
924 |
} |
925 |
|
751 |
|
926 |
my ($holdingbrtagf,$holdingbrtagsubf) = &GetMarcFromKohaField( "items.holdingbranch" ); |
752 |
my $subfieldcode_attribute_mappings; |
927 |
@big_array = sort {$a->{$holdingbrtagsubf} cmp $b->{$holdingbrtagsubf}} @big_array; |
753 |
for my $subfield_code ( keys %{ $tagslib->{$itemtagfield} } ) { |
928 |
|
|
|
929 |
# now, construct template ! |
930 |
# First, the existing items for display |
931 |
my @item_value_loop; |
932 |
my @header_value_loop; |
933 |
for my $row ( @big_array ) { |
934 |
my %row_data; |
935 |
my @item_fields; |
936 |
foreach my $key (sort keys %witness){ |
937 |
my $item_field; |
938 |
if ( $row->{$key} ){ |
939 |
$item_field->{field} = $row->{$key}; |
940 |
} else { |
941 |
$item_field->{field} = ''; |
942 |
} |
943 |
|
754 |
|
944 |
for my $kohafield ( |
755 |
my $subfield = $tagslib->{$itemtagfield}->{$subfield_code}; |
945 |
qw( items.dateaccessioned items.onloan items.datelastseen items.datelastborrowed items.replacementpricedate ) |
|
|
946 |
) |
947 |
{ |
948 |
my ( undef, $subfield ) = GetMarcFromKohaField($kohafield); |
949 |
next unless $key eq $subfield; |
950 |
$item_field->{datatype} = 'date'; |
951 |
} |
952 |
|
756 |
|
953 |
push @item_fields, $item_field; |
757 |
next if IsMarcStructureInternal( $subfield ); |
|
|
758 |
next unless $subfield->{tab} eq 10; # Is this really needed? |
759 |
|
760 |
my $attribute; |
761 |
if ( $subfield->{kohafield} ) { |
762 |
( $attribute = $subfield->{kohafield} ) =~ s|^items\.||; |
763 |
} else { |
764 |
$attribute = $subfield_code; # It's in more_subfields_xml |
954 |
} |
765 |
} |
955 |
$row_data{item_value} = [ @item_fields ]; |
766 |
next unless grep { $attribute eq $_ } @witness_attributes; |
956 |
$row_data{itemnumber} = $row->{itemnumber}; |
767 |
$subfieldcode_attribute_mappings->{$subfield_code} = $attribute; |
957 |
#reporting this_row values |
|
|
958 |
$row_data{'nomod'} = $row->{'nomod'}; |
959 |
$row_data{'hostitemflag'} = $row->{'hostitemflag'}; |
960 |
$row_data{'hostbiblionumber'} = $row->{'hostbiblionumber'}; |
961 |
# $row_data{'countanalytics'} = $row->{'countanalytics'}; |
962 |
push(@item_value_loop,\%row_data); |
963 |
} |
768 |
} |
964 |
foreach my $subfield_code (sort keys(%witness)) { |
|
|
965 |
my %header_value; |
966 |
$header_value{header_value} = $witness{$subfield_code}; |
967 |
|
968 |
my $subfieldlib = $tagslib->{$itemtagfield}->{$subfield_code}; |
969 |
my $kohafield = $subfieldlib->{kohafield}; |
970 |
if ( $kohafield && $kohafield =~ /items.(.+)/ ) { |
971 |
$header_value{column_name} = $1; |
972 |
} |
973 |
|
769 |
|
974 |
push(@header_value_loop, \%header_value); |
770 |
my @header_value_loop = map { |
975 |
} |
771 |
{ |
|
|
772 |
header_value => $tagslib->{$itemtagfield}->{$_}->{lib}, |
773 |
attribute => $subfieldcode_attribute_mappings->{$_}, |
774 |
subfield_code => $_, |
775 |
} |
776 |
} sort keys %$subfieldcode_attribute_mappings; |
976 |
|
777 |
|
977 |
# now, build the item form for entering a new item |
778 |
# now, build the item form for entering a new item |
978 |
my @loop_data =(); |
|
|
979 |
my $i=0; |
980 |
|
981 |
my $branch = $input->param('branch') || C4::Context->userenv->{branch}; |
779 |
my $branch = $input->param('branch') || C4::Context->userenv->{branch}; |
982 |
my $libraries = Koha::Libraries->search({}, { order_by => ['branchname'] })->unblessed;# build once ahead of time, instead of multiple times later. |
780 |
my $libraries = Koha::Libraries->search({}, { order_by => ['branchname'] })->unblessed;# build once ahead of time, instead of multiple times later. |
983 |
for my $library ( @$libraries ) { |
781 |
for my $library ( @$libraries ) { |
984 |
$library->{selected} = 1 if $library->{branchcode} eq $branch |
782 |
$library->{selected} = 1 if $library->{branchcode} eq $branch |
985 |
} |
783 |
} |
986 |
|
784 |
|
987 |
my $item = Koha::Items->find($itemnumber); |
|
|
988 |
|
785 |
|
989 |
# We generate form, from actuel record |
786 |
# Using last created item if it exists |
990 |
@fields = (); |
787 |
$current_item = $item_from_cookie |
991 |
if($itemrecord){ |
788 |
if $item_from_cookie |
992 |
foreach my $field ($itemrecord->fields()){ |
789 |
&& $prefillitem |
993 |
my $tag = $field->{_tag}; |
790 |
&& $op ne "additem" |
994 |
foreach my $subfield ( $field->subfields() ){ |
791 |
&& $op ne "edititem"; |
995 |
|
792 |
|
996 |
my $subfieldtag = $subfield->[0]; |
793 |
my @subfields_to_prefill = split ' ', C4::Context->preference('SubfieldsToUseWhenPrefill'); |
997 |
my $value = $subfield->[1]; |
|
|
998 |
my $subfieldlib = $tagslib->{$tag}->{$subfieldtag}; |
999 |
|
794 |
|
1000 |
next if ($tagslib->{$tag}->{$subfieldtag}->{'tab'} ne "10"); |
795 |
if ( $current_item->{more_subfields_xml} ) { |
|
|
796 |
$current_item->{marc_more_subfields_xml} = MARC::Record->new_from_xml($current_item->{more_subfields_xml}, 'UTF-8'); |
797 |
} |
1001 |
|
798 |
|
1002 |
my $subfield_data = generate_subfield_form($tag, $subfieldtag, $value, $tagslib, $subfieldlib, $libraries, $biblionumber, $temp, \@loop_data, $i, $restrictededition, $item); |
799 |
# We generate form, and fill with values if defined |
1003 |
push @fields, "$tag$subfieldtag"; |
800 |
my $temp = GetMarcBiblio({ biblionumber => $biblionumber }); |
1004 |
push (@loop_data, $subfield_data); |
801 |
my $i = 0; |
1005 |
$i++; |
802 |
my @subfields; |
|
|
803 |
foreach my $tag ( keys %{$tagslib} ) { |
804 |
foreach my $subtag ( keys %{ $tagslib->{$tag} } ) { |
805 |
|
806 |
my $subfield = $tagslib->{$tag}{$subtag}; |
807 |
|
808 |
next if IsMarcStructureInternal( $subfield ); |
809 |
next if ( $subfield->{tab} ne "10" ); |
810 |
|
811 |
my @values = (); |
812 |
|
813 |
my $subfield_data; |
814 |
|
815 |
# If we are not adding a new item |
816 |
# OR |
817 |
# If the subfield must be prefilled with last catalogued item |
818 |
if ( |
819 |
$nextop ne 'additem' |
820 |
|| ( |
821 |
!$prefillitem |
822 |
|| ( $prefillitem && grep { $_ eq $subtag } |
823 |
@subfields_to_prefill ) |
824 |
) |
825 |
) |
826 |
{ |
827 |
my $kohafield = $subfield->{kohafield}; |
828 |
if ($kohafield) { |
829 |
|
830 |
# This is a mapped field |
831 |
( my $attribute = $kohafield ) =~ s|^items\.||; |
832 |
push @values, $subfield->{repeatable} |
833 |
? split '\s\|\s', $current_item->{$attribute} |
834 |
: $current_item->{$attribute} |
835 |
if defined $current_item->{$attribute}; |
836 |
} else { |
837 |
# Not mapped, picked the values from more_subfields_xml's MARC |
838 |
my $marc_more = $current_item->{marc_more_subfields_xml}; |
839 |
if ( $marc_more ) { |
840 |
for my $f ( $marc_more->fields($tag) ) { |
841 |
push @values, $f->subfield($subtag); |
1006 |
} |
842 |
} |
1007 |
|
|
|
1008 |
} |
843 |
} |
1009 |
} |
844 |
} |
1010 |
# and now we add fields that are empty |
845 |
} |
1011 |
|
|
|
1012 |
# Using last created item if it exists |
1013 |
|
846 |
|
1014 |
$itemrecord = $cookieitemrecord if ($prefillitem and not $justaddeditem and $op ne "edititem"); |
847 |
@values = ('') unless @values; |
1015 |
|
848 |
|
1016 |
# We generate form, and fill with values if defined |
849 |
for my $value (@values) { |
1017 |
foreach my $tag ( keys %{$tagslib}){ |
850 |
my $subfield_data = generate_subfield_form( |
1018 |
foreach my $subtag (keys %{$tagslib->{$tag}}){ |
851 |
$tag, $subtag, |
1019 |
next if IsMarcStructureInternal($tagslib->{$tag}{$subtag}); |
852 |
$value, $tagslib, |
1020 |
next if ($tagslib->{$tag}->{$subtag}->{'tab'} ne "10"); |
853 |
$subfield, $libraries, |
1021 |
next if any { /^$tag$subtag$/ } @fields; |
854 |
$biblionumber, $temp, |
1022 |
|
855 |
\@subfields, $i, |
1023 |
my @values = (undef); |
856 |
$restrictededition, $current_item, |
1024 |
@values = $itemrecord->field($tag)->subfield($subtag) if ($itemrecord && defined($itemrecord->field($tag)) && defined($itemrecord->field($tag)->subfield($subtag))); |
857 |
); |
1025 |
for my $value (@values){ |
858 |
push @subfields, $subfield_data; |
1026 |
my $subfield_data = generate_subfield_form($tag, $subtag, $value, $tagslib, $tagslib->{$tag}->{$subtag}, $libraries, $biblionumber, $temp, \@loop_data, $i, $restrictededition, $item); |
|
|
1027 |
push (@loop_data, $subfield_data); |
1028 |
$i++; |
859 |
$i++; |
1029 |
} |
860 |
} |
1030 |
} |
861 |
} |
1031 |
} |
862 |
} |
1032 |
@loop_data = sort { $a->{display_order} <=> $b->{display_order} || $a->{subfield} cmp $b->{subfield} } @loop_data; |
863 |
@subfields = sort { $a->{display_order} <=> $b->{display_order} || $a->{subfield} cmp $b->{subfield} } @subfields; |
1033 |
|
864 |
|
1034 |
# what's the next op ? it's what we are not in : an add if we're editing, otherwise, and edit. |
865 |
# what's the next op ? it's what we are not in : an add if we're editing, otherwise, and edit. |
1035 |
$template->param( |
866 |
$template->param( |
1036 |
biblionumber => $biblionumber, |
867 |
biblionumber => $biblionumber, |
1037 |
title => $oldrecord->{title}, |
868 |
title => $oldrecord->{title}, |
1038 |
author => $oldrecord->{author}, |
869 |
author => $oldrecord->{author}, |
1039 |
item_loop => \@item_value_loop, |
870 |
items => \@items, |
1040 |
item_header_loop => \@header_value_loop, |
871 |
item_header_loop => \@header_value_loop, |
1041 |
item => \@loop_data, |
872 |
subfields => \@subfields, |
1042 |
itemnumber => $itemnumber, |
873 |
itemnumber => $itemnumber, |
1043 |
barcode => $item ? $item->barcode : undef, |
874 |
barcode => $current_item->{barcode}, |
1044 |
itemtagfield => $itemtagfield, |
875 |
itemtagfield => $itemtagfield, |
1045 |
itemtagsubfield => $itemtagsubfield, |
876 |
itemtagsubfield => $itemtagsubfield, |
1046 |
op => $nextop, |
877 |
op => $nextop, |