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

(-)a/C4/Biblio.pm (-4 / +25 lines)
Lines 3006-3021 Generate the host item entry for an analytic child entry Link Here
3006
sub prepare_host_field {
3006
sub prepare_host_field {
3007
    my ( $hostbiblio, $marcflavour ) = @_;
3007
    my ( $hostbiblio, $marcflavour ) = @_;
3008
    $marcflavour ||= C4::Context->preference('marcflavour');
3008
    $marcflavour ||= C4::Context->preference('marcflavour');
3009
3009
    my $biblio              = Koha::Biblios->find($hostbiblio);
3010
    my $biblio = Koha::Biblios->find($hostbiblio);
3010
    my $host                = $biblio->metadata->record;
3011
    my $host   = $biblio->metadata->record;
3011
    my $prepared_host_field = C4::Context->yaml_preference('PrepareHostField') || q{};
3012
3012
3013
    # unfortunately as_string does not 'do the right thing'
3013
    # unfortunately as_string does not 'do the right thing'
3014
    # if field returns undef
3014
    # if field returns undef
3015
    my %sfd;
3015
    my %sfd;
3016
    my $field;
3016
    my $field;
3017
    my $host_field;
3017
    my $host_field;
3018
    if ( $marcflavour eq 'MARC21' ) {
3018
3019
    if ($prepared_host_field) {
3020
        foreach my $key ( keys %$prepared_host_field ) {
3021
            my ( $tag,        $subfields )     = split( /\$/, $key );
3022
            my ( $part_field, $part_subfield ) = split( /\$/, $prepared_host_field->{$key} );
3023
            my $field = $host->field($tag);
3024
            if ($field) {
3025
                if ( $field->is_control_field ) {
3026
                    $sfd{$part_subfield} = $field->data();
3027
                } else {
3028
                    my $s = $field->as_string($subfields);
3029
                    if ($s) {
3030
                        $sfd{$part_subfield} = $s;
3031
                    }
3032
                }
3033
            }
3034
            if (%sfd) {
3035
                $host_field = MARC::Field->new( $part_field, '0', ' ', %sfd );
3036
            }
3037
        }
3038
        return $host_field;
3039
    } elsif ( $marcflavour eq 'MARC21' ) {
3019
        if ( $field = $host->field('100') || $host->field('110') || $host->field('11') ) {
3040
        if ( $field = $host->field('100') || $host->field('110') || $host->field('11') ) {
3020
            my $s = $field->as_string('ab');
3041
            my $s = $field->as_string('ab');
3021
            if ($s) {
3042
            if ($s) {
(-)a/installer/data/mysql/atomicupdate/bug_37061.pl (+19 lines)
Line 0 Link Here
1
use Modern::Perl;
2
3
return {
4
    bug_number  => "37061",
5
    description => "Add a new system preference PrepareHostField",
6
    up          => sub {
7
        my ($args) = @_;
8
        my ( $dbh, $out ) = @$args{qw(dbh out)};
9
10
        # Do you stuffs here
11
        $dbh->do(
12
            q{
13
            INSERT IGNORE INTO systempreferences (variable, value, options, type, explanation)
14
            VALUES ('PrepareHostField', '', '', 'Textarea', 'Define YAML rules for copying host data to component parts.');}
15
        );
16
17
        say $out "Added new system preference 'PrepareHostField'";
18
    },
19
};
(-)a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/cataloguing.pref (+14 lines)
Lines 359-364 Cataloging: Link Here
359
                  dsc: descending.
359
                  dsc: descending.
360
                  az: from A to Z.
360
                  az: from A to Z.
361
                  za: from Z to A.
361
                  za: from Z to A.
362
        -
363
            - "Define rules for copying host data to component parts.<br/>"
364
            - pref: PrepareHostField
365
              type: textarea
366
              syntax: text/x-yaml
367
              class: code
368
            - "Example:<br/>"
369
            - "001: 773$w<br/>"
370
            - "020$a: 773$z<br/>"
371
            - "022$a: 773$x<br/>"
372
            - "028$ba: 773$o<br/>"
373
            - "245$abnpc: 773$t<br/>"
374
            - "264$abc: 773$d<br/>"
375
            - "300$ae: 773$h"
362
    Importing:
376
    Importing:
363
        -
377
        -
364
            - When matching on ISBN with the record import tool,
378
            - When matching on ISBN with the record import tool,
(-)a/t/db_dependent/Biblio.t (-3 / +52 lines)
Lines 17-23 Link Here
17
17
18
use Modern::Perl;
18
use Modern::Perl;
19
19
20
use Test::More tests => 25;
20
use Test::More tests => 26;
21
use Test::MockModule;
21
use Test::MockModule;
22
use Test::Warn;
22
use Test::Warn;
23
use List::MoreUtils qw( uniq );
23
use List::MoreUtils qw( uniq );
Lines 33-43 use Koha::Cache::Memory::Lite; Link Here
33
use Koha::MarcSubfieldStructures;
33
use Koha::MarcSubfieldStructures;
34
34
35
use C4::Linker::Default qw( get_link );
35
use C4::Linker::Default qw( get_link );
36
use YAML::XS;
36
37
37
BEGIN {
38
BEGIN {
38
    use_ok(
39
    use_ok(
39
        'C4::Biblio',
40
        'C4::Biblio',
40
        qw( AddBiblio GetMarcFromKohaField BiblioAutoLink GetMarcSubfieldStructure GetMarcSubfieldStructureFromKohaField LinkBibHeadingsToAuthorities GetBiblioData ModBiblio GetMarcISSN GetMarcISBN GetMarcPrice GetFrameworkCode GetMarcUrls IsMarcStructureInternal GetMarcStructure GetXmlBiblio DelBiblio )
41
        qw( AddBiblio GetMarcFromKohaField BiblioAutoLink GetMarcSubfieldStructure GetMarcSubfieldStructureFromKohaField LinkBibHeadingsToAuthorities GetBiblioData ModBiblio GetMarcISSN GetMarcISBN GetMarcPrice GetFrameworkCode GetMarcUrls IsMarcStructureInternal GetMarcStructure GetXmlBiblio DelBiblio prepare_host_field )
41
    );
42
    );
42
}
43
}
43
44
Lines 1378-1383 subtest 'AddBiblio/ModBiblio calling ModBiblioMarc for field 005' => sub { Link Here
1378
    ok( $field && $field->data, 'Record contains field 005 after ModBiblio' );
1379
    ok( $field && $field->data, 'Record contains field 005 after ModBiblio' );
1379
};
1380
};
1380
1381
1382
subtest 'Prepare host field test' => sub {
1383
    plan tests => 3;
1384
1385
    # PrepareHostField system preference is set
1386
    my $prepare_host_fields = {
1387
        "001" => "773\$w",
1388
        "245" => "773\$t",
1389
    };
1390
1391
    # Mock the system preference PrepareHostField
1392
    t::lib::Mocks::mock_preference( 'PrepareHostField', YAML::XS::Dump($prepare_host_fields) );
1393
1394
    my $record = MARC::Record->new;
1395
    my $field  = MARC::Field->new( '245', '', '', 'a' => 'Example' );
1396
    $record->append_fields($field);
1397
    C4::Biblio::AddBiblio( $record, '' );
1398
    my ($biblionumber) = C4::Biblio::AddBiblio( $record, '' );
1399
    my $host_field     = C4::Biblio::prepare_host_field( $biblionumber, '' );
1400
    my $comp_record    = MARC::Record->new;
1401
    $comp_record->append_fields($host_field);
1402
    is( $comp_record->subfield( '773', 't' ), 'Example', 'System preference host field is correct' );
1403
1404
    t::lib::Mocks::mock_preference( 'PrepareHostField', '' );
1405
1406
    #MARC21
1407
    t::lib::Mocks::mock_preference( 'marcflavour', 'MARC21' );
1408
    my $marc_record = MARC::Record->new;
1409
    my $marc_field  = MARC::Field->new( '245', '', '', 'a' => 'Example' );
1410
    $marc_record->append_fields($marc_field);
1411
    C4::Biblio::AddBiblio( $marc_record, '' );
1412
    my ($biblio_number) = C4::Biblio::AddBiblio( $marc_record, '' );
1413
    my $host_marc_field = C4::Biblio::prepare_host_field( $biblio_number, '' );
1414
    my $compiled_record = MARC::Record->new;
1415
    $compiled_record->append_fields($host_marc_field);
1416
    is( $compiled_record->subfield( '773', 't' ), 'Example', 'MARC21 host field is correct' );
1417
1418
    # UNIMARC
1419
    t::lib::Mocks::mock_preference( 'marcflavour', 'UNIMARC' );
1420
    my $unimarc_record = MARC::Record->new;
1421
    my $unimarc_field  = MARC::Field->new( '200', '', '', 'a' => 'Example' );
1422
    $unimarc_record->append_fields($unimarc_field);
1423
    my ($unimarc_biblionumber) = C4::Biblio::AddBiblio( $unimarc_record, '' );
1424
    my $unimarc_host_field     = C4::Biblio::prepare_host_field( $unimarc_biblionumber, '' );
1425
    my $unimarc_comp_record    = MARC::Record->new;
1426
    $unimarc_comp_record->append_fields($unimarc_host_field);
1427
    is( $unimarc_comp_record->subfield( '461', 't' ), 'Example', 'UNIMARC host field is correct' );
1428
1429
};
1430
1381
# Cleanup
1431
# Cleanup
1382
Koha::Caches->get_instance->clear_from_cache("MarcSubfieldStructure-");
1432
Koha::Caches->get_instance->clear_from_cache("MarcSubfieldStructure-");
1383
$schema->storage->txn_rollback;
1433
$schema->storage->txn_rollback;
1384
- 

Return to bug 37061