Bug 26507: Elasticsearch - Index records after storing new item
[koha.git] / Koha / Patron / Attribute.pm
blob5afd6741496e207c47a904ec1a6929218627a429
1 package Koha::Patron::Attribute;
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>.
18 use Modern::Perl;
20 use Koha::Database;
21 use Koha::Exceptions::Patron::Attribute;
22 use Koha::Patron::Attribute::Types;
23 use Koha::AuthorisedValues;
25 use base qw(Koha::Object);
27 =head1 NAME
29 Koha::Patron::Attribute - Koha Patron Attribute Object class
31 =head1 API
33 =head2 Class Methods
35 =cut
37 =head3 store
39 my $attribute = Koha::Patron::Attribute->new({ code => 'a_code', ... });
40 try { $attribute->store }
41 catch { handle_exception };
43 =cut
45 sub store {
47 my $self = shift;
49 $self->_check_repeatable;
50 $self->check_unique_id;
52 return $self->SUPER::store();
55 =head3 type
57 my $attribute_type = $attribute->type;
59 Returns a C<Koha::Patron::Attribute::Type> object corresponding to the current patron attribute
61 =cut
63 sub type {
65 my $self = shift;
67 return scalar Koha::Patron::Attribute::Types->find( $self->code );
70 =head3 authorised_value
72 my $authorised_value = $attribute->authorised_value;
74 Return the Koha::AuthorisedValue object of this attribute when one is attached.
76 Return undef if this attribute is not attached to an authorised value
78 =cut
80 sub authorised_value {
81 my ($self) = @_;
83 return unless $self->type->authorised_value_category;
85 my $av = Koha::AuthorisedValues->search(
87 category => $self->type->authorised_value_category,
88 authorised_value => $self->attribute,
91 return unless $av->count; # Data inconsistency
92 return $av->next;
95 =head3 description
97 my $description = $patron_attribute->description;
99 Return the value of this attribute or the description of the authorised value (when attached).
101 This method must be called when the authorised value's description must be
102 displayed instead of the code.
104 =cut
106 sub description {
107 my ( $self) = @_;
108 if ( $self->type->authorised_value_category ) {
109 my $av = $self->authorised_value;
110 return $av ? $av->lib : "";
112 return $self->attribute;
116 =head2 Internal methods
118 =head3 _check_repeatable
120 _check_repeatable checks if the attribute type is repeatable and throws and exception
121 if the attribute type isn't repeatable and there's already an attribute with the same
122 code for the given patron.
124 =cut
126 sub _check_repeatable {
128 my $self = shift;
130 if ( !$self->type->repeatable ) {
131 my $attr_count = Koha::Patron::Attributes->search(
132 { borrowernumber => $self->borrowernumber,
133 code => $self->code
135 )->count;
136 Koha::Exceptions::Patron::Attribute::NonRepeatable->throw()
137 if $attr_count > 0;
140 return $self;
143 =head3 check_unique_id
145 check_unique_id checks if the attribute type is marked as unique id and throws and exception
146 if the attribute type is a unique id and there's already an attribute with the same
147 code and value on the database.
149 =cut
151 sub check_unique_id {
153 my $self = shift;
155 if ( $self->type->unique_id ) {
156 my $params = { code => $self->code, attribute => $self->attribute };
158 $params->{borrowernumber} = { '!=' => $self->borrowernumber } if $self->borrowernumber;
159 $params->{id} = { '!=' => $self->id } if $self->in_storage;
161 my $unique_count = Koha::Patron::Attributes
162 ->search( $params )
163 ->count;
164 Koha::Exceptions::Patron::Attribute::UniqueIDConstraint->throw()
165 if $unique_count > 0;
168 return $self;
171 =head3 _type
173 =cut
175 sub _type {
176 return 'BorrowerAttribute';