Line 0
Link Here
|
|
|
1 |
package Koha::Plugin::MarcFieldValues; |
2 |
|
3 |
use Modern::Perl; |
4 |
use MARC::Field; |
5 |
use MARC::Record; |
6 |
|
7 |
use base qw(Koha::Plugins::Base); |
8 |
|
9 |
our $VERSION = 1.00; |
10 |
our $metadata = { |
11 |
name => 'MarcFieldValues', |
12 |
author => 'M. de Rooy', |
13 |
class => 'Koha::Plugin::MarcFieldValues', |
14 |
description => 'Convert MARC fields from plain text', |
15 |
date_authored => '2017-08-08', |
16 |
date_updated => '2017-08-08', |
17 |
minimum_version => '16.11', |
18 |
maximum_version => undef, |
19 |
version => $VERSION, |
20 |
input_format => 'Plain text', |
21 |
}; |
22 |
|
23 |
=head1 METHODS |
24 |
|
25 |
=head2 new |
26 |
|
27 |
Create new object |
28 |
|
29 |
=cut |
30 |
|
31 |
sub new { |
32 |
my ( $class, $args ) = @_; |
33 |
$args->{'metadata'} = $metadata; |
34 |
my $self = $class->SUPER::new($args); |
35 |
return $self; |
36 |
} |
37 |
|
38 |
=head2 to_marc |
39 |
|
40 |
Create MARC record from plain text lines in the form: |
41 |
field [,ind1|,ind2|,subcode] = value |
42 |
Example: |
43 |
003 = OrgCode |
44 |
100,a = Author |
45 |
245,ind2 = 0 |
46 |
245,a = Title |
47 |
|
48 |
=cut |
49 |
|
50 |
sub to_marc { |
51 |
my ( $self, $args ) = @_; |
52 |
# $args->{data} contains text to convert to MARC |
53 |
my $retval = ''; |
54 |
my @records = split "\n\n", $args->{data}; |
55 |
foreach my $rec ( @records ) { |
56 |
my @lines = split "\n", $rec; |
57 |
my $marc = MARC::Record->new; |
58 |
my $inds = {}; |
59 |
foreach my $line ( @lines ) { |
60 |
# each line is of the form field [,ind1|,ind2|,subcode] = value |
61 |
my @temp = split /\s*=\s*/, $line, 2; |
62 |
next if @temp < 2; |
63 |
$temp[0] =~ s/^\s*//; |
64 |
$temp[1] =~ s/\s*$//; |
65 |
my $value = $temp[1]; |
66 |
@temp = split /\s*,\s*/, $temp[0]; |
67 |
if( @temp > 1 && $temp[1] =~ /ind[12]/ ) { |
68 |
$inds->{$temp[0]}->{$temp[1]} = substr($value, 0, 1); |
69 |
next; |
70 |
} |
71 |
$marc->append_fields( MARC::Field->new( |
72 |
$temp[0], |
73 |
$temp[0] < 10 |
74 |
? () |
75 |
: ( ( $inds->{$temp[0]} ? $inds->{$temp[0]}->{ind1} // '' : '', $inds->{$temp[0]} ? $inds->{$temp[0]}->{ind2} // '' : ''), substr( $temp[1], 0, 1 ) ), |
76 |
$value, |
77 |
)); |
78 |
} |
79 |
$retval .= $marc->as_usmarc . "\n"; |
80 |
} |
81 |
return $retval; |
82 |
} |
83 |
|
84 |
1; |