Bug 18785: Force scalar context in Koha::Subscription::biblio
[koha.git] / t / Koha / Plugin / MarcFieldValues.pm
blobee763b883847ea957b98ca39f7b52fa33c97e5a1
1 package Koha::Plugin::MarcFieldValues;
3 use Modern::Perl;
4 use MARC::Field;
5 use MARC::Record;
7 use base qw(Koha::Plugins::Base);
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 => 'MARC field/value pairs in plain text',
23 =head1 METHODS
25 =head2 new
27 Create new object
29 =cut
31 sub new {
32 my ( $class, $args ) = @_;
33 $args->{'metadata'} = $metadata;
34 my $self = $class->SUPER::new($args);
35 return $self;
38 =head2 to_marc
40 Create string of MARC blobs 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
48 =cut
50 sub to_marc {
51 my ( $self, $args ) = @_;
52 # $args->{data} contains text to convert to MARC
53 my $retval = '';
54 my @records = split /\r?\n\r?\n/, $args->{data};
55 foreach my $rec ( @records ) {
56 my @lines = split /\r?\n/, $rec;
57 my $marc = MARC::Record->new;
58 my $inds = {};
59 my $fldcount = 0;
60 foreach my $line ( @lines ) {
61 # each line is of the form field [,ind1|,ind2|,subcode] = value
62 my @temp = split /\s*=\s*/, $line, 2;
63 next if @temp < 2;
64 $temp[0] =~ s/^\s*//;
65 $temp[1] =~ s/\s*$//;
66 my $value = $temp[1];
67 @temp = split /\s*,\s*/, $temp[0];
68 if( @temp > 1 && $temp[1] =~ /ind[12]/ ) {
69 $inds->{$temp[0]}->{$temp[1]} = substr($value, 0, 1);
70 next;
72 $fldcount++;
73 $marc->append_fields( MARC::Field->new(
74 $temp[0],
75 $temp[0] < 10
76 ? ()
77 : ( ( $inds->{$temp[0]} ? $inds->{$temp[0]}->{ind1} // '' : '', $inds->{$temp[0]} ? $inds->{$temp[0]}->{ind2} // '' : ''), substr( $temp[1], 0, 1 ) ),
78 $value,
79 ));
81 $retval .= $marc->as_usmarc if $fldcount;
83 return $retval;