Lines 459-469
sub ModCourseItem {
Link Here
|
459 |
my (%params) = @_; |
459 |
my (%params) = @_; |
460 |
warn identify_myself(%params) if $DEBUG; |
460 |
warn identify_myself(%params) if $DEBUG; |
461 |
|
461 |
|
462 |
my $itemnumber = $params{'itemnumber'}; |
462 |
my $itemnumber = $params{'itemnumber'}; |
463 |
my $itype = $params{'itype'}; |
|
|
464 |
my $ccode = $params{'ccode'}; |
465 |
my $holdingbranch = $params{'holdingbranch'}; |
466 |
my $location = $params{'location'}; |
467 |
|
463 |
|
468 |
return unless ($itemnumber); |
464 |
return unless ($itemnumber); |
469 |
|
465 |
|
Lines 503-512
sub _AddCourseItem {
Link Here
|
503 |
push( @values, $params{'itemnumber'} ); |
499 |
push( @values, $params{'itemnumber'} ); |
504 |
|
500 |
|
505 |
foreach (@FIELDS) { |
501 |
foreach (@FIELDS) { |
506 |
if ( $params{$_} ) { |
502 |
push( @fields, "$_ = ?" ); |
507 |
push( @fields, "$_ = ?" ); |
503 |
push( @values, $params{$_} || undef ); |
508 |
push( @values, $params{$_} ); |
|
|
509 |
} |
510 |
} |
504 |
} |
511 |
|
505 |
|
512 |
my $query = "INSERT INTO course_items SET " . join( ',', @fields ); |
506 |
my $query = "INSERT INTO course_items SET " . join( ',', @fields ); |
Lines 530-539
sub _UpdateCourseItem {
Link Here
|
530 |
|
524 |
|
531 |
my $ci_id = $params{'ci_id'}; |
525 |
my $ci_id = $params{'ci_id'}; |
532 |
my $course_item = $params{'course_item'}; |
526 |
my $course_item = $params{'course_item'}; |
533 |
my $itype = $params{'itype'}; |
|
|
534 |
my $ccode = $params{'ccode'}; |
535 |
my $holdingbranch = $params{'holdingbranch'}; |
536 |
my $location = $params{'location'}; |
537 |
|
527 |
|
538 |
return unless ( $ci_id || $course_item ); |
528 |
return unless ( $ci_id || $course_item ); |
539 |
|
529 |
|
Lines 541-589
sub _UpdateCourseItem {
Link Here
|
541 |
unless ($course_item); |
531 |
unless ($course_item); |
542 |
$ci_id = $course_item->{'ci_id'} unless ($ci_id); |
532 |
$ci_id = $course_item->{'ci_id'} unless ($ci_id); |
543 |
|
533 |
|
544 |
## Revert fields that had an 'original' value, but now don't |
|
|
545 |
## Update the item fields to the stored values from course_items |
546 |
## and then set those fields in course_items to NULL |
547 |
my @fields_to_revert; |
548 |
foreach (@FIELDS) { |
549 |
if ( !$params{$_} && $course_item->{$_} ) { |
550 |
push( @fields_to_revert, $_ ); |
551 |
} |
552 |
} |
553 |
_RevertFields( |
554 |
ci_id => $ci_id, |
555 |
fields => \@fields_to_revert, |
556 |
course_item => $course_item |
557 |
) if (@fields_to_revert); |
558 |
|
559 |
## Update fields that still have an original value, but it has changed |
560 |
## This necessitates only changing the current item values, as we still |
561 |
## have the original values stored in course_items |
562 |
my %mod_params; |
563 |
foreach (@FIELDS) { |
564 |
if ( $params{$_} |
565 |
&& $course_item->{$_} |
566 |
&& $params{$_} ne $course_item->{$_} ) { |
567 |
$mod_params{$_} = $params{$_}; |
568 |
} |
569 |
} |
570 |
ModItem( \%mod_params, undef, $course_item->{'itemnumber'} ) if %mod_params; |
571 |
|
534 |
|
572 |
## Update fields that didn't have an original value, but now do |
535 |
my %mod_params; |
573 |
## We must save the original value in course_items, and also |
|
|
574 |
## update the item fields to the new value |
575 |
my $item = GetItem( $course_item->{'itemnumber'} ); |
576 |
my %mod_params_new; |
577 |
my %mod_params_old; |
578 |
foreach (@FIELDS) { |
536 |
foreach (@FIELDS) { |
579 |
if ( $params{$_} && !$course_item->{$_} ) { |
537 |
$mod_params{$_} = $params{$_}; |
580 |
$mod_params_new{$_} = $params{$_}; |
|
|
581 |
$mod_params_old{$_} = $item->{$_}; |
582 |
} |
583 |
} |
538 |
} |
584 |
_ModStoredFields( 'ci_id' => $params{'ci_id'}, %mod_params_old ); |
|
|
585 |
ModItem( \%mod_params_new, undef, $course_item->{'itemnumber'} ) if %mod_params_new; |
586 |
|
539 |
|
|
|
540 |
ModItem( \%mod_params, undef, $course_item->{'itemnumber'} ); |
587 |
} |
541 |
} |
588 |
|
542 |
|
589 |
=head2 _ModStoredFields |
543 |
=head2 _ModStoredFields |
Lines 627-657
sub _RevertFields {
Link Here
|
627 |
my (%params) = @_; |
581 |
my (%params) = @_; |
628 |
warn identify_myself(%params) if $DEBUG; |
582 |
warn identify_myself(%params) if $DEBUG; |
629 |
|
583 |
|
630 |
my $ci_id = $params{'ci_id'}; |
584 |
my $ci_id = $params{'ci_id'}; |
631 |
my $course_item = $params{'course_item'}; |
|
|
632 |
my $fields = $params{'fields'}; |
633 |
my @fields = @$fields; |
634 |
|
585 |
|
635 |
return unless ($ci_id); |
586 |
return unless ($ci_id); |
636 |
|
587 |
|
637 |
$course_item = GetCourseItem( ci_id => $params{'ci_id'} ) |
588 |
my $course_item = GetCourseItem( ci_id => $params{'ci_id'} ); |
638 |
unless ($course_item); |
|
|
639 |
|
589 |
|
640 |
my $mod_item_params; |
590 |
my $mod_item_params; |
641 |
my @fields_to_null; |
591 |
foreach my $field ( @FIELDS ) { |
642 |
foreach my $field (@fields) { |
592 |
$mod_item_params->{$field} = $course_item->{$field}; |
643 |
foreach (@FIELDS) { |
|
|
644 |
if ( $field eq $_ && $course_item->{$_} ) { |
645 |
$mod_item_params->{$_} = $course_item->{$_}; |
646 |
push( @fields_to_null, $_ ); |
647 |
} |
648 |
} |
649 |
} |
593 |
} |
650 |
ModItem( $mod_item_params, undef, $course_item->{'itemnumber'} ) if $mod_item_params && %$mod_item_params; |
|
|
651 |
|
594 |
|
652 |
my $query = "UPDATE course_items SET " . join( ',', map { "$_=NULL" } @fields_to_null ) . " WHERE ci_id = ?"; |
595 |
ModItem( $mod_item_params, undef, $course_item->{'itemnumber'} ) if $mod_item_params && %$mod_item_params; |
653 |
|
|
|
654 |
C4::Context->dbh->do( $query, undef, $ci_id ) if (@fields_to_null); |
655 |
} |
596 |
} |
656 |
|
597 |
|
657 |
=head2 _SwapAllFields |
598 |
=head2 _SwapAllFields |
Lines 736-742
sub DelCourseItem {
Link Here
|
736 |
|
677 |
|
737 |
return unless ($ci_id); |
678 |
return unless ($ci_id); |
738 |
|
679 |
|
739 |
_RevertFields( ci_id => $ci_id, fields => \@FIELDS ); |
680 |
_RevertFields( ci_id => $ci_id ); |
740 |
|
681 |
|
741 |
my $query = " |
682 |
my $query = " |
742 |
DELETE FROM course_items |
683 |
DELETE FROM course_items |