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