Bug 31183: Use filter_by_current in Koha::Holds->get_items_that_can_fill
[koha.git] / Koha / Patron / Attributes.pm
1 package Koha::Patron::Attributes;
2
3 # This file is part of Koha.
4 #
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.
9 #
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.
14 #
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>.
17
18 use Modern::Perl;
19
20 use Koha::Patron::Attribute;
21 use Koha::Patron::Attribute::Types;
22
23 use base qw(Koha::Objects);
24
25 =head1 NAME
26
27 Koha::Patron::Attributes - Koha Patron Attributes Object set class
28
29 =head1 API
30
31 =head2 Class Methods
32
33 =cut
34
35 =head3 search
36
37 my $attributes = Koha::Patron::Attributes->search( $params );
38
39 =cut
40
41 sub search {
42     my ( $self, $params, $attributes ) = @_;
43
44     unless ( exists $attributes->{order_by} ) { $attributes->{order_by} = ['me.code', 'attribute'] }
45
46     return $self->SUPER::search( $params, $attributes );
47 }
48
49 =head3 filter_by_branch_limitations
50
51 my $attributes = Koha::Patron::Attributes->filter_by_branch_limitations([$branchcode]);
52
53 Search patron attributes filtered by a library
54
55 If $branchcode exists it will be used to filter the result set.
56
57 Otherwise it will be the library of the logged in user.
58
59 =cut
60
61 sub filter_by_branch_limitations {
62     my ( $self, $branchcode ) = @_;
63
64     # Maybe we should not limit if logged in user is superlibrarian?
65     my $branch_limit =
66         $branchcode          ? $branchcode
67         # Do we raise an exception if no userenv defined?
68       : C4::Context->userenv ? C4::Context->userenv->{"branch"}
69       :                        undef;
70
71     my $or = $branch_limit
72       ? {
73         '-or' => [
74             'borrower_attribute_types_branches.b_branchcode' => undef,
75             'borrower_attribute_types_branches.b_branchcode' => $branch_limit,
76         ]
77       }
78       : {};
79
80     my $join = $branch_limit
81       ? {
82         join => {
83             code => 'borrower_attribute_types_branches'
84         },
85       }
86       : {};
87     return $self->search( $or, $join );
88 }
89
90 =head3 merge_and_replace_with
91
92 $new_attributes is an arrayref of hashrefs
93
94 =cut
95
96 sub merge_and_replace_with {
97     my ( $self, $new_attributes ) = @_;
98
99     my @existing_attributes = @{$self->unblessed};
100     my $attribute_types = { map { $_->code => $_->unblessed } Koha::Patron::Attribute::Types->search->as_list };
101     my @new_attributes;
102     for my $attr ( @$new_attributes ) {
103
104         my $attribute_type = $attribute_types->{$attr->{code}};
105
106         Koha::Exceptions::Patron::Attribute::InvalidType->throw( type => $attr->{code} )
107             unless $attribute_types->{$attr->{code}};
108
109         unless ( $attribute_type->{repeatable} ) {
110             # filter out any existing attributes of the same code
111             @existing_attributes = grep {$attr->{code} ne $_->{code}} @existing_attributes;
112         }
113
114         push @new_attributes, $attr;
115     }
116
117     my @merged = map { { code => $_->{code}, attribute => $_->{attribute} } } ( @existing_attributes, @new_attributes );
118
119     # WARNING - we would like to return a set, but $new_attributes is not in storage yet
120     # Maybe there is something obvious I (JD) am missing
121     return [ sort { $a->{code} cmp $b->{code} || $a->{attribute} cmp $b->{attribute} } @merged ];
122 }
123
124 =head3 _type
125
126 =cut
127
128 sub _type {
129     return 'BorrowerAttribute';
130 }
131
132 sub object_class {
133     return 'Koha::Patron::Attribute';
134 }
135
136 1;