1 package Koha::Item::Attributes;
3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
21 use List::MoreUtils qw( uniq );
24 use C4::Charset qw( StripNonXmlChars );
28 Koha::Item::Attributes - Class to represent the additional attributes of items.
30 Additional attributes are 'more subfields xml'
38 =head3 new_from_marcxml
40 my $attributes = Koha::Item::Attributes->new_from_marcxml( $item->more_subfield_xml );
42 Constructor that takes a MARCXML.
46 # FIXME maybe this needs to care about repeatable but don't from batchMod - To implement later?
47 sub new_from_marcxml {
48 my ( $class, $more_subfields_xml ) = @_;
51 if ($more_subfields_xml) {
52 # FIXME MARC::Record->new_from_xml (vs MARC::Record::new_from_xml) does not return the correctly encoded subfield code (??)
54 MARC::Record::new_from_xml(
55 C4::Charset::StripNonXmlChars($more_subfields_xml), 'UTF-8' );
57 # use of tag 999 is arbitrary, and doesn't need to match the item tag
58 # used in the framework
59 my $field = $marc_more->field('999');
60 my $more_subfields = [ uniq map { $_->[0] } $field->subfields ];
61 for my $more_subfield (@$more_subfields) {
62 my @s = $field->subfield($more_subfield);
63 $self->{$more_subfield} = join ' | ', @s;
66 return bless $self, $class;
75 # FIXME maybe this needs to care about repeatable but don't from batchMod - To implement later?
77 my ( $class, $attributes ) = @_;
79 my $self = $attributes;
80 return bless $self, $class;
85 $attributes->to_marcxml;
87 $item->more_subfields_xml( $attributes->to_marcxml );
89 Return the MARCXML representation of the attributes.
94 my ( $self, $frameworkcode ) = @_;
96 return unless keys %$self;
99 C4::Biblio::GetMarcStructure( 1, $frameworkcode, { unsafe => 1 } );
101 my ( $itemtag, $itemtagsubfield ) =
102 C4::Biblio::GetMarcFromKohaField("items.itemnumber");
104 for my $tagsubfield (
106 $tagslib->{$itemtag}->{$a}->{display_order} <=> $tagslib->{$itemtag}->{$b}->{display_order}
107 || $tagslib->{$itemtag}->{$a}->{subfield} cmp $tagslib->{$itemtag}->{$b}->{subfield}
112 if not defined $self->{$tagsubfield}
113 or $self->{$tagsubfield} eq "";
115 if ( $tagslib->{$itemtag}->{$tagsubfield}->{repeatable} ) {
116 my @values = split ' \| ', $self->{$tagsubfield};
117 push @subfields, ( $tagsubfield => $_ ) for @values;
120 push @subfields, ( $tagsubfield => $self->{$tagsubfield} );
124 return unless @subfields;
126 my $marc_more = MARC::Record->new();
128 # use of tag 999 is arbitrary, and doesn't need to match the item tag
129 # used in the framework
130 $marc_more->append_fields(
131 MARC::Field->new( '999', ' ', ' ', @subfields ) );
132 $marc_more->encoding("UTF-8");
133 return $marc_more->as_xml("USMARC");
138 $attributes->to_hashref;
140 Returns the hashref representation of the attributes.
146 return { map { $_ => $self->{$_} } keys %$self };