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

(-)a/C4/AuthoritiesMarc.pm (-31 / +14 lines)
Lines 663-700 sub AddAuthority { Link Here
663
    $record->add_fields($auth_type_tag,'','', $auth_type_subfield=>$authtypecode); 
663
    $record->add_fields($auth_type_tag,'','', $auth_type_subfield=>$authtypecode); 
664
  }
664
  }
665
665
666
  my $auth_exists=0;
666
    # Save record into auth_header, update 001
667
  my $oldRecord;
667
    if (!$authid ) {
668
  if (!$authid) {
668
        # Save a blank record, get authid
669
    my $sth=$dbh->prepare("select max(authid) from auth_header");
669
        $dbh->do( "INSERT INTO auth_header (datecreated,marcxml) values (NOW(),?)", undef, '' );
670
    $sth->execute;
670
        $authid = $dbh->last_insert_id( undef, undef, 'auth_header', 'authid' );
671
    ($authid)=$sth->fetchrow;
671
        logaction( "AUTHORITIES", "ADD", $authid, "authority" ) if C4::Context->preference("AuthoritiesLog");
672
    $authid=$authid+1;
673
  ##Insert the recordID in MARC record 
674
    unless ($record->field('001') && $record->field('001')->data() eq $authid){
675
        $record->delete_field($record->field('001'));
676
        $record->insert_fields_ordered(MARC::Field->new('001',$authid));
677
    }
672
    }
678
  } else {
673
    # Insert/update the recordID in MARC record
679
    $auth_exists=$dbh->do(qq(select authid from auth_header where authid=?),undef,$authid);
674
    $record->delete_field( $record->field('001') );
680
#     warn "auth_exists = $auth_exists";
675
    $record->insert_fields_ordered( MARC::Field->new( '001', $authid ) );
681
  }
676
    # Update
682
  if ($auth_exists>0){
677
    $dbh->do( "UPDATE auth_header SET authtypecode=?, marc=?, marcxml=? WHERE authid=?", undef, $authtypecode, $record->as_usmarc, $record->as_xml_record($format), $authid ) or die $DBI::errstr;
683
      $oldRecord=GetAuthority($authid);
678
    ModZebra( $authid, 'specialUpdate', 'authorityserver', $record );
684
      $record->add_fields('001',$authid) unless ($record->field('001'));
679
685
#       warn "\n\n\n enregistrement".$record->as_formatted;
680
    return ( $authid );
686
      my $sth=$dbh->prepare("update auth_header set authtypecode=?,marc=?,marcxml=? where authid=?");
687
      $sth->execute($authtypecode,$record->as_usmarc,$record->as_xml_record($format),$authid) or die $sth->errstr;
688
      $sth->finish;
689
  }
690
  else {
691
    my $sth=$dbh->prepare("insert into auth_header (authid,datecreated,authtypecode,marc,marcxml) values (?,now(),?,?,?)");
692
    $sth->execute($authid,$authtypecode,$record->as_usmarc,$record->as_xml_record($format));
693
    $sth->finish;
694
    logaction( "AUTHORITIES", "ADD", $authid, "authority" ) if C4::Context->preference("AuthoritiesLog");
695
  }
696
  ModZebra($authid,'specialUpdate',"authorityserver",$oldRecord,$record);
697
  return ($authid);
698
}
681
}
699
682
700
683
(-)a/t/db_dependent/AuthoritiesMarc.t (-4 / +6 lines)
Lines 192-206 is_deeply( Link Here
192
);
192
);
193
193
194
subtest 'AddAuthority should respect AUTO_INCREMENT (BZ 18104)' => sub {
194
subtest 'AddAuthority should respect AUTO_INCREMENT (BZ 18104)' => sub {
195
    plan tests => 1;
195
    plan tests => 3;
196
196
197
    t::lib::Mocks::mock_preference( 'marcflavour', 'MARC21' );
197
    t::lib::Mocks::mock_preference( 'marcflavour', 'MARC21' );
198
    my $record = C4::AuthoritiesMarc::GetAuthority(1);
198
    my $record = C4::AuthoritiesMarc::GetAuthority(1);
199
    my $id1 = AddAuthority( $record, undef, 'GEOGR_NAME' );
199
    my $id1 = AddAuthority( $record, undef, 'GEOGR_NAME' );
200
    DelAuthority( $id1 );
200
    DelAuthority( $id1 );
201
    my $id2 = AddAuthority( $record, undef, 'GEOGR_NAME' );
201
    my $id2 = AddAuthority( $record, undef, 'GEOGR_NAME' );
202
    is( $id1, $id2, 'FIXME: Got the same id back, let\'s fix that behavior' );
202
    isnt( $id1, $id2, 'Do not return the same id again' );
203
203
    t::lib::Mocks::mock_preference( 'marcflavour', 'UNIMARC' );
204
    my $id3 = AddAuthority( $record, undef, 'GEOGR_NAME' );
205
    is( $id3 > 0, 1, 'Tested AddAuthority with UNIMARC' );
206
    is( $record->field('001')->data, $id3, 'Check updated 001' );
204
};
207
};
205
208
206
$schema->storage->txn_rollback;
209
$schema->storage->txn_rollback;
207
- 

Return to bug 18014