772cad3c4376c3647305fd1caf01871abe610585
[koha.git] / Patron / Attribute / Type.pm
1 package Koha::Patron::Attribute::Type;
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::Database;
21 use Koha::Exceptions::Patron::Attribute::Type;
22
23 use base qw(Koha::Object Koha::Object::Limit::Library);
24
25 =head1 NAME
26
27 Koha::Patron::Attribute::Type - Koha::Patron::Attribute::Type Object class
28
29 =head1 API
30
31 =head2 Class Methods
32
33 =cut
34
35 =head3 store
36
37     my $attribute = Koha::Patron::Attribute->new({ code => 'a_code', ... });
38     try { $attribute->store }
39     catch { handle_exception };
40
41 =cut
42
43 sub store {
44
45     my $self = shift;
46
47     $self->check_repeatables;
48     $self->check_unique_ids;
49
50     return $self->SUPER::store();
51 }
52
53 =head3 attributes
54
55 =cut
56
57 sub attributes {
58     my ($self) = @_;
59     my $attributes_rs = $self->_result->borrower_attributes;
60     Koha::Patron::Attributes->_new_from_dbic($attributes_rs);
61 }
62
63 =head2 Internal Methods
64
65 =cut
66
67 =head3 check_repeatables
68
69 =cut
70
71 sub check_repeatables {
72     my ($self) = @_;
73
74     return $self if $self->repeatable;
75
76     my $count = $self->attributes->search(
77         {},
78         {
79             select   => [ { count => 'id', '-as' => 'c' } ],
80             group_by => 'borrowernumber',
81             having   => { c => { '>' => 1 } }
82         }
83     )->count;
84
85     Koha::Exceptions::Patron::Attribute::Type::CannotChangeProperty->throw(
86         property => 'repeatable' )
87       if $count;
88
89     return $self;
90 }
91
92 =head3 check_unique_ids
93
94 =cut
95
96 sub check_unique_ids {
97     my ($self) = @_;
98
99     return $self unless $self->unique_id;
100
101     my $count = $self->attributes->search(
102         {},
103         {
104             select   => [ { count => 'id', '-as' => 'c' } ],
105             group_by => 'attribute',
106             having   => { c => { '>' => 1 } }
107         }
108     )->count;
109
110     Koha::Exceptions::Patron::Attribute::Type::CannotChangeProperty->throw(
111         property => 'unique_id' )
112       if $count;
113
114     return $self;
115 }
116
117
118
119 =head3 _type
120
121 =cut
122
123 sub _type {
124     return 'BorrowerAttributeType';
125 }
126
127 =head3 _library_limits
128
129 =cut
130
131 sub _library_limits {
132     return {
133         class   => 'BorrowerAttributeTypesBranch',
134         id      => 'bat_code',
135         library => 'b_branchcode'
136     };
137 }
138
139 1;