Bug 15774: (follow-up) Address QA issues
[koha.git] / Koha / Object / Mixin / AdditionalFields.pm
1 package Koha::Object::Mixin::AdditionalFields;
2
3 use Modern::Perl;
4 use Koha::AdditionalFieldValues;
5
6 =head1 NAME
7
8 Koha::Object::Mixin::AdditionalFields
9
10 =head1 SYNOPSIS
11
12     package Koha::Foo;
13
14     use parent qw( Koha::Object Koha::Object::Mixin::AdditionalFields );
15
16     sub _type { 'Foo' }
17
18
19     package main;
20
21     use Koha::Foo;
22
23     Koha::Foos->find($id)->set_additional_fields(...);
24
25 =head1 API
26
27 =head2 Public methods
28
29 =head3 set_additional_fields
30
31     $foo->set_additional_fields([
32         {
33             id => 1,
34             value => 'foo',
35         },
36         {
37             id => 2,
38             value => 'bar',
39         }
40     ]);
41
42 =cut
43
44 sub set_additional_fields {
45     my ($self, $additional_fields) = @_;
46
47     my @additional_field_values = $self->additional_field_values->delete;
48
49     foreach my $additional_field (@$additional_fields) {
50         my $value = $additional_field->{value};
51         if (defined $value) {
52             my $field_value = Koha::AdditionalFieldValue->new({
53                 field_id => $additional_field->{id},
54                 record_id => $self->id,
55                 value => $value,
56             })->store;
57         }
58     }
59 }
60
61 =head3 additional_field_values
62
63 Returns additional field values
64
65     my @values = $foo->additional_field_values;
66
67 =cut
68
69 sub additional_field_values {
70     my ($self) = @_;
71
72     my $afv_rs = $self->_result->additional_field_values;
73     return Koha::AdditionalFieldValues->_new_from_dbic( $afv_rs );
74 }
75
76 =head1 AUTHOR
77
78 Koha Development Team <http://koha-community.org/>
79
80 =head1 COPYRIGHT AND LICENSE
81
82 Copyright 2018 BibLibre
83
84 This file is part of Koha.
85
86 Koha is free software; you can redistribute it and/or modify it under the
87 terms of the GNU General Public License as published by the Free Software
88 Foundation; either version 3 of the License, or (at your option) any later
89 version.
90
91 Koha is distributed in the hope that it will be useful, but WITHOUT ANY
92 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
93 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
94
95 You should have received a copy of the GNU General Public License along
96 with Koha; if not, see <http://www.gnu.org/licenses>.
97
98 =cut
99
100 1;