View | Details | Raw Unified | Return to bug 33749
Collapse All | Expand All

(-)a/C4/AuthoritiesMarc.pm (-79 / +84 lines)
Lines 562-697 returns authid of the newly created authority Link Here
562
=cut
562
=cut
563
563
564
sub AddAuthority {
564
sub AddAuthority {
565
# pass the MARC::Record to this function, and it will create the records in the authority table
565
566
    # pass the MARC::Record to this function, and it will create the records in the authority table
566
    my ( $record, $authid, $authtypecode, $params ) = @_;
567
    my ( $record, $authid, $authtypecode, $params ) = @_;
567
568
568
    my $skip_record_index = $params->{skip_record_index} || 0;
569
    my $skip_record_index = $params->{skip_record_index} || 0;
569
570
570
  my $dbh=C4::Context->dbh;
571
    my $dbh    = C4::Context->dbh;
571
	my $leader='     nz  a22     o  4500';#Leader for incomplete MARC21 record
572
    my $leader = '     nz  a22     o  4500';    #Leader for incomplete MARC21 record
572
573
573
# if authid empty => true add, find a new authid number
574
    # if authid empty => true add, find a new authid number
574
    my $format;
575
    my $format;
575
    if (uc(C4::Context->preference('marcflavour')) eq 'UNIMARC') {
576
    if ( uc( C4::Context->preference('marcflavour') ) eq 'UNIMARC' ) {
576
        $format= 'UNIMARCAUTH';
577
        $format = 'UNIMARCAUTH';
577
    }
578
    } else {
578
    else {
579
        $format = 'MARC21';
579
        $format= 'MARC21';
580
    }
580
    }
581
581
582
    #update date/time to 005 for marc and unimarc
582
    #update date/time to 005 for marc and unimarc
583
    my $time=POSIX::strftime("%Y%m%d%H%M%S",localtime);
583
    my $time = POSIX::strftime( "%Y%m%d%H%M%S", localtime );
584
    my $f5=$record->field('005');
584
    my $f5   = $record->field('005');
585
    if (!$f5) {
585
    if ( !$f5 ) {
586
      $record->insert_fields_ordered( MARC::Field->new('005',$time.".0") );
586
        $record->insert_fields_ordered( MARC::Field->new( '005', $time . ".0" ) );
587
    }
587
    } else {
588
    else {
588
        $f5->update( $time . ".0" );
589
      $f5->update($time.".0");
590
    }
589
    }
591
590
592
    SetUTF8Flag($record);
591
    SetUTF8Flag($record);
593
	if ($format eq "MARC21") {
592
    if ( $format eq "MARC21" ) {
594
        my $userenv = C4::Context->userenv;
593
        my $userenv = C4::Context->userenv;
595
        my $library;
594
        my $library;
596
        my $marcorgcode = C4::Context->preference('MARCOrgCode');
595
        my $marcorgcode = C4::Context->preference('MARCOrgCode');
597
        if ( $userenv && $userenv->{'branch'} ) {
596
        if ( $userenv && $userenv->{'branch'} ) {
598
            $library = Koha::Libraries->find( $userenv->{'branch'} );
597
            $library = Koha::Libraries->find( $userenv->{'branch'} );
598
599
            # userenv's library could not exist because of a trick in misc/commit_file.pl (see FIXME and set_userenv statement)
599
            # userenv's library could not exist because of a trick in misc/commit_file.pl (see FIXME and set_userenv statement)
600
            $marcorgcode = $library ? $library->get_effective_marcorgcode : $marcorgcode;
600
            $marcorgcode = $library ? $library->get_effective_marcorgcode : $marcorgcode;
601
        }
601
        }
602
		if (!$record->leader) {
602
        if ( !$record->leader ) {
603
			$record->leader($leader);
603
            $record->leader($leader);
604
		}
604
        }
605
		if (!$record->field('003')) {
605
        if ( !$record->field('003') ) {
606
			$record->insert_fields_ordered(
606
            $record->insert_fields_ordered(
607
                MARC::Field->new('003', $marcorgcode),
607
                MARC::Field->new( '003', $marcorgcode ),
608
			);
608
            );
609
		}
609
        }
610
		my $date=POSIX::strftime("%y%m%d",localtime);
610
        my $date = POSIX::strftime( "%y%m%d", localtime );
611
		if (!$record->field('008')) {
611
        if ( !$record->field('008') ) {
612
612
            # Get a valid default value for field 008
613
            # Get a valid default value for field 008
613
            my $default_008 = C4::Context->preference('MARCAuthorityControlField008');
614
            my $default_008 = C4::Context->preference('MARCAuthorityControlField008');
614
            if(!$default_008 or length($default_008)<34) {
615
            if ( !$default_008 or length($default_008) < 34 ) {
615
                $default_008 = '|| aca||aabn           | a|a     d';
616
                $default_008 = '|| aca||aabn           | a|a     d';
617
            } else {
618
                $default_008 = substr( $default_008, 0, 34 );
616
            }
619
            }
617
            else {
618
                $default_008 = substr($default_008,0,34);
619
            }
620
620
621
            $record->insert_fields_ordered( MARC::Field->new('008',$date.$default_008) );
621
            $record->insert_fields_ordered( MARC::Field->new( '008', $date . $default_008 ) );
622
		}
622
        }
623
		if (!$record->field('040')) {
623
        if ( !$record->field('040') ) {
624
		 $record->insert_fields_ordered(
624
            $record->insert_fields_ordered(
625
        MARC::Field->new('040','','',
625
                MARC::Field->new(
626
            'a' => $marcorgcode,
626
                    '040', '', '',
627
            'c' => $marcorgcode,
627
                    'a' => $marcorgcode,
628
				) 
628
                    'c' => $marcorgcode,
629
			);
629
                )
630
            );
631
        }
632
    }
633
634
    if ( $format eq "UNIMARCAUTH" ) {
635
        $record->leader("     nx  j22             ") unless ( $record->leader() );
636
        my $date            = POSIX::strftime( "%Y%m%d", localtime );
637
        my $defaultfield100 = C4::Context->preference('UNIMARCAuthorityField100');
638
        if ( my $string = $record->subfield( '100', "a" ) ) {
639
            $string =~ s/fre50/frey50/;
640
            $record->field('100')->update( 'a' => $string );
641
        } elsif ( $record->field('100') ) {
642
            $record->field('100')->update( 'a' => $date . $defaultfield100 );
643
        } else {
644
            $record->append_fields(
645
                MARC::Field->new(
646
                    '100', ' ', ' '
647
                    , 'a' => $date . $defaultfield100
648
                )
649
            );
650
        }
630
    }
651
    }
631
	}
652
    my ( $auth_type_tag, $auth_type_subfield ) = get_auth_type_location($authtypecode);
632
653
    if ( !$authid and $format eq "MARC21" ) {
633
  if ($format eq "UNIMARCAUTH") {
654
634
        $record->leader("     nx  j22             ") unless ($record->leader());
655
        # only need to do this fix when modifying an existing authority
635
        my $date=POSIX::strftime("%Y%m%d",localtime);
656
        C4::AuthoritiesMarc::MARC21::fix_marc21_auth_type_location( $record, $auth_type_tag, $auth_type_subfield );
636
	my $defaultfield100 = C4::Context->preference('UNIMARCAuthorityField100');
657
    }
637
    if (my $string=$record->subfield('100',"a")){
658
    if ( my $field = $record->field($auth_type_tag) ) {
638
      	$string=~s/fre50/frey50/;
659
        $field->update( $auth_type_subfield => $authtypecode );
639
      	$record->field('100')->update('a'=>$string);
660
    } else {
661
        $record->add_fields( $auth_type_tag, '', '', $auth_type_subfield => $authtypecode );
640
    }
662
    }
641
    elsif ($record->field('100')){
642
          $record->field('100')->update('a'=>$date.$defaultfield100);
643
    } else {      
644
        $record->append_fields(
645
        MARC::Field->new('100',' ',' '
646
            ,'a'=>$date.$defaultfield100)
647
        );
648
    }      
649
  }
650
  my ($auth_type_tag, $auth_type_subfield) = get_auth_type_location($authtypecode);
651
  if (!$authid and $format eq "MARC21") {
652
    # only need to do this fix when modifying an existing authority
653
    C4::AuthoritiesMarc::MARC21::fix_marc21_auth_type_location($record, $auth_type_tag, $auth_type_subfield);
654
  } 
655
  if (my $field=$record->field($auth_type_tag)){
656
    $field->update($auth_type_subfield=>$authtypecode);
657
  }
658
  else {
659
    $record->add_fields($auth_type_tag,'','', $auth_type_subfield=>$authtypecode);
660
  }
661
663
662
    if ( C4::Context->preference('StripWhitespaceChars') ) {
664
    if ( C4::Context->preference('StripWhitespaceChars') ) {
663
        my $p = Koha::RecordProcessor->new({ filters => qw(TrimFields) });
665
        my $p = Koha::RecordProcessor->new( { filters => qw(TrimFields) } );
664
        $p->process( $record );
666
        $p->process($record);
665
    }
667
    }
666
668
667
    # Save record into auth_header, update 001
669
    # Save record into auth_header, update 001
668
    my $action;
670
    my $action;
669
    my $authority;
671
    my $authority;
670
    if (!$authid ) {
672
    if ( !$authid ) {
671
        $action = 'create';
673
        $action = 'create';
674
672
        # Save a blank record, get authid
675
        # Save a blank record, get authid
673
        $authority = Koha::Authority->new({ datecreated => \'NOW()', marcxml => '' })->store();
676
        $authority = Koha::Authority->new( { datecreated => \'NOW()', marcxml => '' } )->store();
674
        $authority->discard_changes();
677
        $authority->discard_changes();
675
        $authid = $authority->authid;
678
        $authid = $authority->authid;
676
        logaction( "AUTHORITIES", "ADD", $authid, "authority" ) if C4::Context->preference("AuthoritiesLog");
679
        logaction( "AUTHORITIES", "ADD", $authid, "authority" ) if C4::Context->preference("AuthoritiesLog");
677
    } else {
680
    } else {
678
        $action = 'modify';
681
        $action    = 'modify';
679
        $authority = Koha::Authorities->find($authid);
682
        $authority = Koha::Authorities->find($authid);
680
    }
683
    }
681
684
682
    # Insert/update the recordID in MARC record
685
    # Insert/update the recordID in MARC record
683
    $record->delete_field( $record->field('001') );
686
    $record->delete_field( $record->field('001') );
684
    $record->insert_fields_ordered( MARC::Field->new( '001', $authid ) );
687
    $record->insert_fields_ordered( MARC::Field->new( '001', $authid ) );
688
685
    # Update
689
    # Update
686
    $authority->update({ authtypecode => $authtypecode, marc => $record->as_usmarc, marcxml => $record->as_xml_record($format) });
690
    $authority->update(
691
        { authtypecode => $authtypecode, marc => $record->as_usmarc, marcxml => $record->as_xml_record($format) } );
687
692
688
    unless ( $skip_record_index ) {
693
    unless ($skip_record_index) {
689
        my $indexer = Koha::SearchEngine::Indexer->new({ index => $Koha::SearchEngine::AUTHORITIES_INDEX });
694
        my $indexer = Koha::SearchEngine::Indexer->new( { index => $Koha::SearchEngine::AUTHORITIES_INDEX } );
690
        $indexer->index_records( $authid, "specialUpdate", "authorityserver", $record );
695
        $indexer->index_records( $authid, "specialUpdate", "authorityserver", $record );
691
    }
696
    }
692
697
693
    _after_authority_action_hooks({ action => $action, authority_id => $authid });
698
    _after_authority_action_hooks( { action => $action, authority_id => $authid } );
694
    return ( $authid );
699
    return ($authid);
695
}
700
}
696
701
697
=head2 DelAuthority
702
=head2 DelAuthority
(-)a/C4/Biblio.pm (-2 / +2 lines)
Lines 2841-2848 sub ModBiblioMarc { Link Here
2841
    }
2841
    }
2842
2842
2843
    if ( C4::Context->preference('StripWhitespaceChars') ) {
2843
    if ( C4::Context->preference('StripWhitespaceChars') ) {
2844
        my $p = Koha::RecordProcessor->new({ filters => qw(TrimFields) });
2844
        my $p = Koha::RecordProcessor->new( { filters => qw(TrimFields) } );
2845
        $p->process( $record );
2845
        $p->process($record);
2846
    }
2846
    }
2847
2847
2848
    my $metadata = {
2848
    my $metadata = {
(-)a/Koha/Filter/MARC/TrimFields.pm (-3 / +3 lines)
Lines 51-57 sub filter { Link Here
51
    my ( $self, $record ) = @_;
51
    my ( $self, $record ) = @_;
52
52
53
    return
53
    return
54
      unless $record and ref($record) eq 'MARC::Record';
54
        unless $record and ref($record) eq 'MARC::Record';
55
55
56
    foreach my $field ( $record->fields ) {
56
    foreach my $field ( $record->fields ) {
57
        unless ( $field->is_control_field ) {
57
        unless ( $field->is_control_field ) {
Lines 60-67 sub filter { Link Here
60
                my $value = $subfield->[1];
60
                my $value = $subfield->[1];
61
                $value =~ s/[\n\r]+/ /g;
61
                $value =~ s/[\n\r]+/ /g;
62
                $value =~ s/^\s+|\s+$//g;
62
                $value =~ s/^\s+|\s+$//g;
63
                $field->add_subfields( $key => $value ); # add subfield to the end of the subfield list
63
                $field->add_subfields( $key => $value );    # add subfield to the end of the subfield list
64
                $field->delete_subfield( pos => 0 ); # delete the subfield at the top of the subfield list
64
                $field->delete_subfield( pos => 0 );        # delete the subfield at the top of the subfield list
65
            }
65
            }
66
        }
66
        }
67
    }
67
    }
(-)a/t/RecordProcessor.t (-5 / +4 lines)
Lines 217-229 subtest "'TrimFields' filter tests" => sub { Link Here
217
        [ '521', ' ', ' ', a => "This is a\t test!\t" ],
217
        [ '521', ' ', ' ', a => "This is a\t test!\t" ],
218
    );
218
    );
219
219
220
    my $p = Koha::RecordProcessor->new({ filters => ['TrimFields'] });
220
    my $p = Koha::RecordProcessor->new( { filters => ['TrimFields'] } );
221
    $p->process( $record );
221
    $p->process($record);
222
222
223
    my $get520a = $record->subfield('520','a');
223
    my $get520a = $record->subfield( '520', 'a' );
224
    is( $get520a, "This is a test!", "Whitespace characters are appropriately stripped or replaced with spaces" );
224
    is( $get520a, "This is a test!", "Whitespace characters are appropriately stripped or replaced with spaces" );
225
225
226
    my $get521a = $record->subfield('521','a');
226
    my $get521a = $record->subfield( '521', 'a' );
227
    is( $get521a, "This is a\t test!", "Trailing tabs are stripped while inner tabs are kept" );
227
    is( $get521a, "This is a\t test!", "Trailing tabs are stripped while inner tabs are kept" );
228
};
228
};
229
229
230
- 

Return to bug 33749