Bug 15774: (follow-up) Address QA issues
[koha.git] / Koha / Objects / Mixin / AdditionalFields.pm
1 package Koha::Objects::Mixin::AdditionalFields;
2
3 use Modern::Perl;
4
5 =head1 NAME
6
7 Koha::Objects::Mixin::AdditionalFields
8
9 =head1 SYNOPSIS
10
11     package Koha::Foos;
12
13     use parent qw( Koha::Objects Koha::Objects::Mixin::AdditionalFields );
14
15     sub _type { 'Foo' }
16     sub object_class { 'Koha::Foo' }
17
18
19     package main;
20
21     use Koha::Foos;
22
23     Koha::Foos->filter_by_additional_fields(...)
24
25 =head1 API
26
27 =head2 Public methods
28
29 =head3 filter_by_additional_fields
30
31     my @objects = Koha::Foos->filter_by_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 filter_by_additional_fields {
45     my ($class, $additional_fields) = @_;
46
47     my %conditions;
48     my $idx = 0;
49     foreach my $additional_field (@$additional_fields) {
50         ++$idx;
51         my $alias = $idx > 1 ? "additional_field_values_$idx" : "additional_field_values";
52         $conditions{"$alias.field_id"} = $additional_field->{id};
53         $conditions{"$alias.value"} = { -like => '%' . $additional_field->{value} . '%'};
54     }
55
56     return $class->search(\%conditions, { join => [ ('additional_field_values') x $idx ] });
57 }
58
59 =head1 AUTHOR
60
61 Koha Development Team <http://koha-community.org/>
62
63 =head1 COPYRIGHT AND LICENSE
64
65 Copyright 2018 BibLibre
66
67 This file is part of Koha.
68
69 Koha is free software; you can redistribute it and/or modify it under the
70 terms of the GNU General Public License as published by the Free Software
71 Foundation; either version 3 of the License, or (at your option) any later
72 version.
73
74 Koha is distributed in the hope that it will be useful, but WITHOUT ANY
75 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
76 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
77
78 You should have received a copy of the GNU General Public License along
79 with Koha; if not, see <http://www.gnu.org/licenses>.
80
81 =cut
82
83 1;