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 |
} |