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

(-)a/C4/AuthoritiesMarc.pm (-1 lines)
Lines 88-94 sub GetAuthMARCFromKohaField { Link Here
88
  my $dbh=C4::Context->dbh;
88
  my $dbh=C4::Context->dbh;
89
  return 0, 0 unless $kohafield;
89
  return 0, 0 unless $kohafield;
90
  $authtypecode="" unless $authtypecode;
90
  $authtypecode="" unless $authtypecode;
91
  my $marcfromkohafield;
92
  my $sth = $dbh->prepare("select tagfield,tagsubfield from auth_subfield_structure where kohafield= ? and authtypecode=? ");
91
  my $sth = $dbh->prepare("select tagfield,tagsubfield from auth_subfield_structure where kohafield= ? and authtypecode=? ");
93
  $sth->execute($kohafield,$authtypecode);
92
  $sth->execute($kohafield,$authtypecode);
94
  my ($tagfield,$tagsubfield) = $sth->fetchrow;
93
  my ($tagfield,$tagsubfield) = $sth->fetchrow;
(-)a/C4/Biblio.pm (-54 / +61 lines)
Lines 1209-1226 C<$frameworkcode> is the framework code. Link Here
1209
1209
1210
sub GetUsedMarcStructure {
1210
sub GetUsedMarcStructure {
1211
    my $frameworkcode = shift || '';
1211
    my $frameworkcode = shift || '';
1212
    my $query = qq/
1212
    my $query = q{
1213
        SELECT *
1213
        SELECT *
1214
        FROM   marc_subfield_structure
1214
        FROM   marc_subfield_structure
1215
        WHERE   tab > -1 
1215
        WHERE   tab > -1 
1216
            AND frameworkcode = ?
1216
            AND frameworkcode = ?
1217
        ORDER BY tagfield, tagsubfield
1217
        ORDER BY tagfield, tagsubfield
1218
    /;
1218
    };
1219
    my $sth = C4::Context->dbh->prepare($query);
1219
    my $sth = C4::Context->dbh->prepare($query);
1220
    $sth->execute($frameworkcode);
1220
    $sth->execute($frameworkcode);
1221
    return $sth->fetchall_arrayref( {} );
1221
    return $sth->fetchall_arrayref( {} );
1222
}
1222
}
1223
1223
1224
=head2 GetMarcSubfieldStructure
1225
1226
=cut
1227
1228
sub GetMarcSubfieldStructure {
1229
    my ( $frameworkcode ) = @_;
1230
1231
    $frameworkcode //= '';
1232
1233
    my $cache     = Koha::Cache->get_instance();
1234
    my $cache_key = "MarcSubfieldStructure-$frameworkcode";
1235
    my $cached    = $cache->get_from_cache($cache_key);
1236
    return $cached if $cached;
1237
1238
    my $dbh = C4::Context->dbh;
1239
    my $subfield_structure = $dbh->selectall_hashref( q|
1240
        SELECT *
1241
        FROM marc_subfield_structure
1242
        WHERE frameworkcode = ?
1243
        AND kohafield > ''
1244
    |, 'kohafield', {}, $frameworkcode );
1245
1246
    $cache->set_in_cache( $cache_key, $subfield_structure );
1247
    return $subfield_structure;
1248
}
1249
1224
=head2 GetMarcFromKohaField
1250
=head2 GetMarcFromKohaField
1225
1251
1226
  ($MARCfield,$MARCsubfield)=GetMarcFromKohaField($kohafield,$frameworkcode);
1252
  ($MARCfield,$MARCsubfield)=GetMarcFromKohaField($kohafield,$frameworkcode);
Lines 1231-1244 for the given frameworkcode or default framework if $frameworkcode is missing Link Here
1231
=cut
1257
=cut
1232
1258
1233
sub GetMarcFromKohaField {
1259
sub GetMarcFromKohaField {
1234
    my $kohafield = shift;
1260
    my ( $kohafield, $frameworkcode ) = @_;
1235
    my $frameworkcode = shift || '';
1236
    return (0, undef) unless $kohafield;
1261
    return (0, undef) unless $kohafield;
1237
    my $relations = C4::Context->marcfromkohafield;
1262
    my $mss = GetMarcSubfieldStructure( $frameworkcode );
1238
    if ( my $mf = $relations->{$frameworkcode}->{$kohafield} ) {
1263
    return ( $mss->{$kohafield}{tagfield}, $mss->{$kohafield}{tagsubfield} );
1239
        return @$mf;
1240
    }
1241
    return (0, undef);
1242
}
1264
}
1243
1265
1244
=head2 GetMarcSubfieldStructureFromKohaField
1266
=head2 GetMarcSubfieldStructureFromKohaField
Lines 1253-1276 $frameworkcode is optional. If not given, then the default framework is used. Link Here
1253
=cut
1275
=cut
1254
1276
1255
sub GetMarcSubfieldStructureFromKohaField {
1277
sub GetMarcSubfieldStructureFromKohaField {
1256
    my ($kohafield, $frameworkcode) = @_;
1278
    my ( $kohafield, $frameworkcode ) = @_;
1257
1279
1258
    return undef unless $kohafield;
1280
    return unless $kohafield;
1259
    $frameworkcode //= '';
1260
1281
1261
    my $dbh = C4::Context->dbh;
1282
    my $mss = GetMarcSubfieldStructure( $frameworkcode );
1262
    my $query = qq{
1283
    return exists $mss->{$kohafield}
1263
        SELECT *
1284
        ? $mss->{$kohafield}
1264
        FROM marc_subfield_structure
1285
        : undef;
1265
        WHERE kohafield = ?
1266
          AND frameworkcode = ?
1267
    };
1268
    my $sth = $dbh->prepare($query);
1269
    $sth->execute($kohafield, $frameworkcode);
1270
    my $result = $sth->fetchrow_hashref;
1271
    $sth->finish;
1272
1273
    return $result;
1274
}
1286
}
1275
1287
1276
=head2 GetMarcBiblio
1288
=head2 GetMarcBiblio
Lines 2231-2247 sub TransformKohaToMarc { Link Here
2231
    my $hash = shift;
2243
    my $hash = shift;
2232
    my $record = MARC::Record->new();
2244
    my $record = MARC::Record->new();
2233
    SetMarcUnicodeFlag( $record, C4::Context->preference("marcflavour") );
2245
    SetMarcUnicodeFlag( $record, C4::Context->preference("marcflavour") );
2234
    my $db_to_marc = C4::Context->marcfromkohafield;
2246
    # FIXME Do not we want to get the marc subfield structure for the biblio framework?
2247
    my $mss = GetMarcSubfieldStructure();
2235
    my $tag_hr = {};
2248
    my $tag_hr = {};
2236
    while ( my ($name, $value) = each %$hash ) {
2249
    while ( my ($kohafield, $value) = each %$hash ) {
2237
        next unless my $dtm = $db_to_marc->{''}->{$name};
2250
        next unless exists $mss->{$kohafield};
2238
        next unless ( scalar( @$dtm ) );
2251
        next unless $mss->{$kohafield};
2239
        my ($tag, $letter) = @$dtm;
2252
        my $tagfield    = $mss->{$kohafield}{tagfield} . '';
2240
        $tag .= '';
2253
        my $tagsubfield = $mss->{$kohafield}{tagsubfield};
2241
        foreach my $value ( split(/\s?\|\s?/, $value, -1) ) {
2254
        foreach my $value ( split(/\s?\|\s?/, $value, -1) ) {
2242
            next if $value eq '';
2255
            next if $value eq '';
2243
            $tag_hr->{$tag} //= [];
2256
            $tag_hr->{$tagfield} //= [];
2244
            push @{$tag_hr->{$tag}}, [($letter, $value)];
2257
            push @{$tag_hr->{$tagfield}}, [($tagsubfield, $value)];
2245
        }
2258
        }
2246
    }
2259
    }
2247
    foreach my $tag (sort keys %$tag_hr) {
2260
    foreach my $tag (sort keys %$tag_hr) {
Lines 2606-2614 sub TransformHtmlToMarc { Link Here
2606
    return $record;
2619
    return $record;
2607
}
2620
}
2608
2621
2609
# cache inverted MARC field map
2610
our $inverted_field_map;
2611
2612
=head2 TransformMarcToKoha
2622
=head2 TransformMarcToKoha
2613
2623
2614
  $result = TransformMarcToKoha( $record, $frameworkcode )
2624
  $result = TransformMarcToKoha( $record, $frameworkcode )
Lines 2632-2640 sub TransformMarcToKoha { Link Here
2632
    $limit_table = $limit_table || 0;
2642
    $limit_table = $limit_table || 0;
2633
    $frameworkcode = '' unless defined $frameworkcode;
2643
    $frameworkcode = '' unless defined $frameworkcode;
2634
2644
2635
    unless ( defined $inverted_field_map ) {
2645
    my $inverted_field_map = _get_inverted_marc_field_map($frameworkcode);
2636
        $inverted_field_map = _get_inverted_marc_field_map();
2637
    }
2638
2646
2639
    my %tables = ();
2647
    my %tables = ();
2640
    if ( defined $limit_table && $limit_table eq 'items' ) {
2648
    if ( defined $limit_table && $limit_table eq 'items' ) {
Lines 2648-2656 sub TransformMarcToKoha { Link Here
2648
    # traverse through record
2656
    # traverse through record
2649
  MARCFIELD: foreach my $field ( $record->fields() ) {
2657
  MARCFIELD: foreach my $field ( $record->fields() ) {
2650
        my $tag = $field->tag();
2658
        my $tag = $field->tag();
2651
        next MARCFIELD unless exists $inverted_field_map->{$frameworkcode}->{$tag};
2659
        next MARCFIELD unless exists $inverted_field_map->{$tag};
2652
        if ( $field->is_control_field() ) {
2660
        if ( $field->is_control_field() ) {
2653
            my $kohafields = $inverted_field_map->{$frameworkcode}->{$tag}->{list};
2661
            my $kohafields = $inverted_field_map->{$tag}->{list};
2654
          ENTRY: foreach my $entry ( @{$kohafields} ) {
2662
          ENTRY: foreach my $entry ( @{$kohafields} ) {
2655
                my ( $subfield, $table, $column ) = @{$entry};
2663
                my ( $subfield, $table, $column ) = @{$entry};
2656
                next ENTRY unless exists $tables{$table};
2664
                next ENTRY unless exists $tables{$table};
Lines 2668-2676 sub TransformMarcToKoha { Link Here
2668
            # deal with subfields
2676
            # deal with subfields
2669
          MARCSUBFIELD: foreach my $sf ( $field->subfields() ) {
2677
          MARCSUBFIELD: foreach my $sf ( $field->subfields() ) {
2670
                my $code = $sf->[0];
2678
                my $code = $sf->[0];
2671
                next MARCSUBFIELD unless exists $inverted_field_map->{$frameworkcode}->{$tag}->{sfs}->{$code};
2679
                next MARCSUBFIELD unless exists $inverted_field_map->{$tag}->{sfs}->{$code};
2672
                my $value = $sf->[1];
2680
                my $value = $sf->[1];
2673
              SFENTRY: foreach my $entry ( @{ $inverted_field_map->{$frameworkcode}->{$tag}->{sfs}->{$code} } ) {
2681
              SFENTRY: foreach my $entry ( @{ $inverted_field_map->{$tag}->{sfs}->{$code} } ) {
2674
                    my ( $table, $column ) = @{$entry};
2682
                    my ( $table, $column ) = @{$entry};
2675
                    next SFENTRY unless exists $tables{$table};
2683
                    next SFENTRY unless exists $tables{$table};
2676
                    my $key = _disambiguate( $table, $column );
2684
                    my $key = _disambiguate( $table, $column );
Lines 2713-2730 sub TransformMarcToKoha { Link Here
2713
}
2721
}
2714
2722
2715
sub _get_inverted_marc_field_map {
2723
sub _get_inverted_marc_field_map {
2724
    my ( $frameworkcode ) = @_;
2716
    my $field_map = {};
2725
    my $field_map = {};
2717
    my $relations = C4::Context->marcfromkohafield;
2726
    my $mss = GetMarcSubfieldStructure( $frameworkcode );
2718
2727
2719
    foreach my $frameworkcode ( keys %{$relations} ) {
2728
    foreach my $kohafield ( keys %{ $mss } ) {
2720
        foreach my $kohafield ( keys %{ $relations->{$frameworkcode} } ) {
2729
        next unless exists $mss->{$kohafield};    # not all columns are mapped to MARC tag & subfield
2721
            next unless @{ $relations->{$frameworkcode}->{$kohafield} };    # not all columns are mapped to MARC tag & subfield
2730
        my $tag      = $mss->{$kohafield}{tagfield};
2722
            my $tag      = $relations->{$frameworkcode}->{$kohafield}->[0];
2731
        my $subfield = $mss->{$kohafield}{tagsubfield};
2723
            my $subfield = $relations->{$frameworkcode}->{$kohafield}->[1];
2732
        my ( $table, $column ) = split /[.]/, $kohafield, 2;
2724
            my ( $table, $column ) = split /[.]/, $kohafield, 2;
2733
        push @{ $field_map->{$tag}->{list} }, [ $subfield, $table, $column ];
2725
            push @{ $field_map->{$frameworkcode}->{$tag}->{list} }, [ $subfield, $table, $column ];
2734
        push @{ $field_map->{$tag}->{sfs}->{$subfield} }, [ $table, $column ];
2726
            push @{ $field_map->{$frameworkcode}->{$tag}->{sfs}->{$subfield} }, [ $table, $column ];
2727
        }
2728
    }
2735
    }
2729
    return $field_map;
2736
    return $field_map;
2730
}
2737
}
(-)a/C4/Context.pm (-40 lines)
Lines 373-379 sub new { Link Here
373
    return if !defined($self->{"config"});
373
    return if !defined($self->{"config"});
374
374
375
    $self->{"Zconn"} = undef;    # Zebra Connections
375
    $self->{"Zconn"} = undef;    # Zebra Connections
376
    $self->{"marcfromkohafield"} = undef; # the hash with relations between koha table fields and MARC field/subfield
377
    $self->{"userenv"} = undef;        # User env
376
    $self->{"userenv"} = undef;        # User env
378
    $self->{"activeuser"} = undef;        # current active user
377
    $self->{"activeuser"} = undef;        # current active user
379
    $self->{"shelves"} = undef;
378
    $self->{"shelves"} = undef;
Lines 931-975 sub _new_queryparser { Link Here
931
    return;
930
    return;
932
}
931
}
933
932
934
=head2 marcfromkohafield
935
936
  $dbh = C4::Context->marcfromkohafield;
937
938
Returns a hash with marcfromkohafield.
939
940
This hash is cached for future use: if you call
941
C<C4::Context-E<gt>marcfromkohafield> twice, you will get the same hash without real DB access
942
943
=cut
944
945
#'
946
sub marcfromkohafield
947
{
948
    my $retval = {};
949
950
    # If the hash already exists, return it.
951
    return $context->{"marcfromkohafield"} if defined($context->{"marcfromkohafield"});
952
953
    # No hash. Create one.
954
    $context->{"marcfromkohafield"} = &_new_marcfromkohafield();
955
956
    return $context->{"marcfromkohafield"};
957
}
958
959
# _new_marcfromkohafield
960
sub _new_marcfromkohafield
961
{
962
    my $dbh = C4::Context->dbh;
963
    my $marcfromkohafield;
964
    my $sth = $dbh->prepare("select frameworkcode,kohafield,tagfield,tagsubfield from marc_subfield_structure where kohafield > ''");
965
    $sth->execute;
966
    while (my ($frameworkcode,$kohafield,$tagfield,$tagsubfield) = $sth->fetchrow) {
967
        my $retval = {};
968
        $marcfromkohafield->{$frameworkcode}->{$kohafield} = [$tagfield,$tagsubfield];
969
    }
970
    return $marcfromkohafield;
971
}
972
973
=head2 userenv
933
=head2 userenv
974
934
975
  C4::Context->userenv;
935
  C4::Context->userenv;
(-)a/admin/biblio_framework.pl (+2 lines)
Lines 81-86 if ( $op eq 'add_form' ) { Link Here
81
    $cache->clear_from_cache("MarcStructure-0-$frameworkcode");
81
    $cache->clear_from_cache("MarcStructure-0-$frameworkcode");
82
    $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
82
    $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
83
    $cache->clear_from_cache("default_value_for_mod_marc-$frameworkcode");
83
    $cache->clear_from_cache("default_value_for_mod_marc-$frameworkcode");
84
    $cache->clear_from_cache("MarcSubfieldStructure-$frameworkcode");
84
    $op = 'list';
85
    $op = 'list';
85
} elsif ( $op eq 'delete_confirm' ) {
86
} elsif ( $op eq 'delete_confirm' ) {
86
    my $framework = Koha::BiblioFrameworks->find($frameworkcode);
87
    my $framework = Koha::BiblioFrameworks->find($frameworkcode);
Lines 111-116 if ( $op eq 'add_form' ) { Link Here
111
    $cache->clear_from_cache("MarcStructure-0-$frameworkcode");
112
    $cache->clear_from_cache("MarcStructure-0-$frameworkcode");
112
    $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
113
    $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
113
    $cache->clear_from_cache("default_value_for_mod_marc-$frameworkcode");
114
    $cache->clear_from_cache("default_value_for_mod_marc-$frameworkcode");
115
    $cache->clear_from_cache("MarcSubfieldStructure-$frameworkcode");
114
    $op = 'list';
116
    $op = 'list';
115
}
117
}
116
118
(-)a/admin/marc_subfields_structure.pl (+2 lines)
Lines 342-347 elsif ( $op eq 'add_validate' ) { Link Here
342
    $cache->clear_from_cache("MarcStructure-0-$frameworkcode");
342
    $cache->clear_from_cache("MarcStructure-0-$frameworkcode");
343
    $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
343
    $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
344
    $cache->clear_from_cache("default_value_for_mod_marc-$frameworkcode");
344
    $cache->clear_from_cache("default_value_for_mod_marc-$frameworkcode");
345
    $cache->clear_from_cache("MarcSubfieldStructure-$frameworkcode");
345
346
346
    print $input->redirect("/cgi-bin/koha/admin/marc_subfields_structure.pl?tagfield=$tagfield&amp;frameworkcode=$frameworkcode");
347
    print $input->redirect("/cgi-bin/koha/admin/marc_subfields_structure.pl?tagfield=$tagfield&amp;frameworkcode=$frameworkcode");
347
    exit;
348
    exit;
Lines 386-391 elsif ( $op eq 'delete_confirmed' ) { Link Here
386
    $cache->clear_from_cache("MarcStructure-0-$frameworkcode");
387
    $cache->clear_from_cache("MarcStructure-0-$frameworkcode");
387
    $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
388
    $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
388
    $cache->clear_from_cache("default_value_for_mod_marc-$frameworkcode");
389
    $cache->clear_from_cache("default_value_for_mod_marc-$frameworkcode");
390
    $cache->clear_from_cache("MarcSubfieldStructure-$frameworkcode");
389
    print $input->redirect("/cgi-bin/koha/admin/marc_subfields_structure.pl?tagfield=$tagfield&amp;frameworkcode=$frameworkcode");
391
    print $input->redirect("/cgi-bin/koha/admin/marc_subfields_structure.pl?tagfield=$tagfield&amp;frameworkcode=$frameworkcode");
390
    exit;
392
    exit;
391
393
(-)a/admin/marctagstructure.pl (+3 lines)
Lines 165-170 if ($op eq 'add_form') { Link Here
165
        $cache->clear_from_cache("MarcStructure-0-$frameworkcode");
165
        $cache->clear_from_cache("MarcStructure-0-$frameworkcode");
166
        $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
166
        $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
167
        $cache->clear_from_cache("default_value_for_mod_marc-$frameworkcode");
167
        $cache->clear_from_cache("default_value_for_mod_marc-$frameworkcode");
168
        $cache->clear_from_cache("MarcSubfieldStructure-$frameworkcode");
168
    }
169
    }
169
    print $input->redirect("/cgi-bin/koha/admin/marctagstructure.pl?searchfield=$tagfield&frameworkcode=$frameworkcode");
170
    print $input->redirect("/cgi-bin/koha/admin/marctagstructure.pl?searchfield=$tagfield&frameworkcode=$frameworkcode");
170
    exit;
171
    exit;
Lines 192-197 if ($op eq 'add_form') { Link Here
192
        $cache->clear_from_cache("MarcStructure-0-$frameworkcode");
193
        $cache->clear_from_cache("MarcStructure-0-$frameworkcode");
193
        $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
194
        $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
194
        $cache->clear_from_cache("default_value_for_mod_marc-$frameworkcode");
195
        $cache->clear_from_cache("default_value_for_mod_marc-$frameworkcode");
196
        $cache->clear_from_cache("MarcSubfieldStructure-$frameworkcode");
195
	}
197
	}
196
	$template->param(
198
	$template->param(
197
          searchfield => $searchfield,
199
          searchfield => $searchfield,
Lines 358-362 sub duplicate_framework { Link Here
358
    $cache->clear_from_cache("MarcStructure-0-$newframeworkcode");
360
    $cache->clear_from_cache("MarcStructure-0-$newframeworkcode");
359
    $cache->clear_from_cache("MarcStructure-1-$newframeworkcode");
361
    $cache->clear_from_cache("MarcStructure-1-$newframeworkcode");
360
    $cache->clear_from_cache("default_value_for_mod_marc-$frameworkcode");
362
    $cache->clear_from_cache("default_value_for_mod_marc-$frameworkcode");
363
    $cache->clear_from_cache("MarcSubfieldStructure-$frameworkcode");
361
}
364
}
362
365
(-)a/t/db_dependent/Biblio.t (-65 / +56 lines)
Lines 33-42 my $dbh = C4::Context->dbh; Link Here
33
$dbh->{AutoCommit} = 0;
33
$dbh->{AutoCommit} = 0;
34
$dbh->{RaiseError} = 1;
34
$dbh->{RaiseError} = 1;
35
35
36
# Mocking variables
36
subtest 'GetMarcSubfieldStructureFromKohaField' => sub {
37
my $context = new Test::MockModule('C4::Context');
37
    plan tests => 23;
38
39
    my @columns = qw(
40
        tagfield tagsubfield liblibrarian libopac repeatable mandatory kohafield tab
41
        authorised_value authtypecode value_builder isurl hidden frameworkcode
42
        seealso link defaultvalue maxlength
43
    );
44
45
    # biblio.biblionumber must be mapped so this should return something
46
    my $marc_subfield_structure = GetMarcSubfieldStructureFromKohaField('biblio.biblionumber', '');
47
48
    ok(defined $marc_subfield_structure, "There is a result");
49
    is(ref $marc_subfield_structure, "HASH", "Result is a hashref");
50
    foreach my $col (@columns) {
51
        ok(exists $marc_subfield_structure->{$col}, "Hashref contains key '$col'");
52
    }
53
    is($marc_subfield_structure->{kohafield}, 'biblio.biblionumber', "Result is the good result");
54
    like($marc_subfield_structure->{tagfield}, qr/^\d{3}$/, "tagfield is a valid tagfield");
38
55
39
mock_marcfromkohafield();
56
    # foo.bar does not exist so this should return undef
57
    $marc_subfield_structure = GetMarcSubfieldStructureFromKohaField('foo.bar', '');
58
    is($marc_subfield_structure, undef, "invalid kohafield returns undef");
59
};
60
61
62
# Mocking variables
63
my $biblio_module = new Test::MockModule('C4::Biblio');
64
$biblio_module->mock(
65
    'GetMarcSubfieldStructure',
66
    sub {
67
        my ($self) = shift;
68
69
        if (   C4::Context->preference('marcflavour') eq 'MARC21'
70
            || C4::Context->preference('marcflavour') eq 'NORMARC' ) {
71
72
            return {
73
                'biblio.title'                 => { tagfield => '245', tagsubfield => 'a' },
74
                'biblio.biblionumber'          => { tagfield => '999', tagsubfield => 'c' },
75
                'biblioitems.isbn'             => { tagfield => '020', tagsubfield => 'a' },
76
                'biblioitems.issn'             => { tagfield => '022', tagsubfield => 'a' },
77
                'biblioitems.biblioitemnumber' => { tagfield => '999', tagsubfield => 'd' },
78
                'items.itemnumber'             => { tagfield => '952', tagsubfield => '9' },
79
            };
80
        } elsif ( C4::Context->preference('marcflavour') eq 'UNIMARC' ) {
81
82
            return {
83
                'biblio.title'                 => { tagfield => '200', tagsubfield => 'a' },
84
                'biblio.biblionumber'          => { tagfield => '999', tagsubfield => 'c' },
85
                'biblioitems.isbn'             => { tagfield => '010', tagsubfield => 'a' },
86
                'biblioitems.issn'             => { tagfield => '011', tagsubfield => 'a' },
87
                'biblioitems.biblioitemnumber' => { tagfield => '090', tagsubfield => 'a' },
88
                'items.itemnumber'             => { tagfield => '995', tagsubfield => '9' },
89
            };
90
        }
91
    }
92
);
40
93
41
my $currency = new Test::MockModule('Koha::Acquisition::Currencies');
94
my $currency = new Test::MockModule('Koha::Acquisition::Currencies');
42
$currency->mock(
95
$currency->mock(
Lines 54-63 $currency->mock( Link Here
54
107
55
sub run_tests {
108
sub run_tests {
56
109
57
    # Undef C4::Biblio::inverted_field_map to avoid problems introduced
58
    # by caching in TransformMarcToKoha
59
    undef $C4::Biblio::inverted_field_map;
60
61
    my $marcflavour = shift;
110
    my $marcflavour = shift;
62
    t::lib::Mocks::mock_preference('marcflavour', $marcflavour);
111
    t::lib::Mocks::mock_preference('marcflavour', $marcflavour);
63
112
Lines 226-264 sub run_tests { Link Here
226
        'Check the number of returned notes of GetMarcNotes' );
275
        'Check the number of returned notes of GetMarcNotes' );
227
}
276
}
228
277
229
sub mock_marcfromkohafield {
230
231
    $context->mock('marcfromkohafield',
232
        sub {
233
            my ( $self ) = shift;
234
235
            if ( C4::Context->preference('marcflavour') eq 'MARC21' ||
236
                 C4::Context->preference('marcflavour') eq 'NORMARC' ) {
237
238
                return  {
239
                '' => {
240
                    'biblio.title' => [ '245', 'a' ],
241
                    'biblio.biblionumber' => [ '999', 'c' ],
242
                    'biblioitems.isbn' => [ '020', 'a' ],
243
                    'biblioitems.issn' => [ '022', 'a' ],
244
                    'biblioitems.biblioitemnumber' => [ '999', 'd' ]
245
                    }
246
                };
247
            } elsif ( C4::Context->preference('marcflavour') eq 'UNIMARC' ) {
248
249
                return {
250
                '' => {
251
                    'biblio.title' => [ '200', 'a' ],
252
                    'biblio.biblionumber' => [ '999', 'c' ],
253
                    'biblioitems.isbn' => [ '010', 'a' ],
254
                    'biblioitems.issn' => [ '011', 'a' ],
255
                    'biblioitems.biblioitemnumber' => [ '090', 'a' ]
256
                    }
257
                };
258
            }
259
        });
260
}
261
262
sub create_title_field {
278
sub create_title_field {
263
    my ( $title, $marcflavour ) = @_;
279
    my ( $title, $marcflavour ) = @_;
264
280
Lines 307-337 subtest 'NORMARC' => sub { Link Here
307
    $dbh->rollback;
323
    $dbh->rollback;
308
};
324
};
309
325
310
subtest 'GetMarcSubfieldStructureFromKohaField' => sub {
311
    plan tests => 23;
312
313
    my @columns = qw(
314
        tagfield tagsubfield liblibrarian libopac repeatable mandatory kohafield tab
315
        authorised_value authtypecode value_builder isurl hidden frameworkcode
316
        seealso link defaultvalue maxlength
317
    );
318
319
    # biblio.biblionumber must be mapped so this should return something
320
    my $marc_subfield_structure = GetMarcSubfieldStructureFromKohaField('biblio.biblionumber', '');
321
322
    ok(defined $marc_subfield_structure, "There is a result");
323
    is(ref $marc_subfield_structure, "HASH", "Result is a hashref");
324
    foreach my $col (@columns) {
325
        ok(exists $marc_subfield_structure->{$col}, "Hashref contains key '$col'");
326
    }
327
    is($marc_subfield_structure->{kohafield}, 'biblio.biblionumber', "Result is the good result");
328
    like($marc_subfield_structure->{tagfield}, qr/^\d{3}$/, "tagfield is a valid tagfield");
329
330
    # foo.bar does not exist so this should return undef
331
    $marc_subfield_structure = GetMarcSubfieldStructureFromKohaField('foo.bar', '');
332
    is($marc_subfield_structure, undef, "invalid kohafield returns undef");
333
};
334
335
subtest 'IsMarcStructureInternal' => sub {
326
subtest 'IsMarcStructureInternal' => sub {
336
    plan tests => 6;
327
    plan tests => 6;
337
    my $tagslib = GetMarcStructure();
328
    my $tagslib = GetMarcStructure();
(-)a/t/db_dependent/Items.t (-18 / +20 lines)
Lines 372-386 subtest 'SearchItems test' => sub { Link Here
372
    }
372
    }
373
    ok($found, "item1 found");
373
    ok($found, "item1 found");
374
374
375
    my ($itemfield) = GetMarcFromKohaField('items.itemnumber', '');
375
    my $frameworkcode = q||;
376
    my ($itemfield) = GetMarcFromKohaField('items.itemnumber', $frameworkcode);
376
377
377
    # Create item subfield 'z' without link
378
    # Create item subfield 'z' without link
378
    $dbh->do('DELETE FROM marc_subfield_structure WHERE tagfield=? AND tagsubfield="z" AND frameworkcode=""', undef, $itemfield);
379
    $dbh->do('DELETE FROM marc_subfield_structure WHERE tagfield=? AND tagsubfield="z" AND frameworkcode=?', undef, $itemfield, $frameworkcode);
379
    $dbh->do('INSERT INTO marc_subfield_structure (tagfield, tagsubfield, frameworkcode) VALUES (?, "z", "")', undef, $itemfield);
380
    $dbh->do('INSERT INTO marc_subfield_structure (tagfield, tagsubfield, frameworkcode) VALUES (?, "z", ?)', undef, $itemfield, $frameworkcode);
380
381
381
    # Clear cache
382
    # Clear cache
382
    $C4::Context::context->{marcfromkohafield} = undef;
383
    my $cache = Koha::Cache->get_instance();
383
    $C4::Biblio::inverted_field_map = undef;
384
    $cache->clear_from_cache("MarcStructure-0-$frameworkcode");
385
    $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
386
    $cache->clear_from_cache("default_value_for_mod_marc-$frameworkcode");
387
    $cache->clear_from_cache("MarcSubfieldStructure-$frameworkcode");
384
388
385
    my $item3_record = new MARC::Record;
389
    my $item3_record = new MARC::Record;
386
    $item3_record->append_fields(
390
    $item3_record->append_fields(
Lines 400-411 subtest 'SearchItems test' => sub { Link Here
400
404
401
    # Link $z to items.itemnotes (and make sure there is no other subfields
405
    # Link $z to items.itemnotes (and make sure there is no other subfields
402
    # linked to it)
406
    # linked to it)
403
    $dbh->do('DELETE FROM marc_subfield_structure WHERE kohafield="items.itemnotes" AND frameworkcode=""', undef, $itemfield);
407
    $dbh->do('DELETE FROM marc_subfield_structure WHERE kohafield="items.itemnotes" AND frameworkcode=?', undef, $itemfield, $frameworkcode);
404
    $dbh->do('UPDATE marc_subfield_structure SET kohafield="items.itemnotes" WHERE tagfield=? AND tagsubfield="z" AND frameworkcode=""', undef, $itemfield);
408
    $dbh->do('UPDATE marc_subfield_structure SET kohafield="items.itemnotes" WHERE tagfield=? AND tagsubfield="z" AND frameworkcode=?', undef, $itemfield, $frameworkcode);
405
409
406
    # Clear cache
410
    # Clear cache
407
    $C4::Context::context->{marcfromkohafield} = undef;
411
    $cache->clear_from_cache("MarcStructure-0-$frameworkcode");
408
    $C4::Biblio::inverted_field_map = undef;
412
    $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
413
    $cache->clear_from_cache("default_value_for_mod_marc-$frameworkcode");
414
    $cache->clear_from_cache("MarcSubfieldStructure-$frameworkcode");
409
415
410
    ModItemFromMarc($item3_record, $biblionumber, $item3_itemnumber);
416
    ModItemFromMarc($item3_record, $biblionumber, $item3_itemnumber);
411
417
Lines 559-568 subtest 'C4::Items::_build_default_values_for_mod_marc' => sub { Link Here
559
565
560
    $schema->storage->txn_begin();
566
    $schema->storage->txn_begin();
561
567
562
    # Clear cache
563
    $C4::Context::context->{marcfromkohafield} = undef;
564
    $C4::Biblio::inverted_field_map = undef;
565
566
    my $builder = t::lib::TestBuilder->new;
568
    my $builder = t::lib::TestBuilder->new;
567
    my $framework = $builder->build({
569
    my $framework = $builder->build({
568
        source => 'BiblioFramework',
570
        source => 'BiblioFramework',
Lines 642-651 subtest 'C4::Items::_build_default_values_for_mod_marc' => sub { Link Here
642
    |, undef, $framework->{frameworkcode} );
644
    |, undef, $framework->{frameworkcode} );
643
645
644
    # And make sure the caches are cleared
646
    # And make sure the caches are cleared
645
    $C4::Context::context->{marcfromkohafield} = undef;
647
    my $cache = Koha::Cache->get_instance();
646
    $C4::Biblio::inverted_field_map = undef;
648
    $cache->clear_from_cache("MarcStructure-0-" . $framework->{frameworkcode});
647
    my $cache     = Koha::Cache->get_instance();
649
    $cache->clear_from_cache("MarcStructure-1-" . $framework->{frameworkcode});
648
    $cache->clear_from_cache("default_value_for_mod_marc-" . $framework->{frameworkcode} );
650
    $cache->clear_from_cache("default_value_for_mod_marc-" . $framework->{frameworkcode});
651
    $cache->clear_from_cache("MarcSubfieldStructure-" . $framework->{frameworkcode});
649
652
650
    # Update the MARC field with another value
653
    # Update the MARC field with another value
651
    $item_record->delete_fields( $barcode_field );
654
    $item_record->delete_fields( $barcode_field );
652
- 

Return to bug 16431