View | Details | Raw Unified | Return to bug 17828
Collapse All | Expand All

(-)a/Koha/Exceptions/Patron/Attribute.pm (+20 lines)
Line 0 Link Here
1
package Koha::Exceptions::Patron::Attribute;
2
3
use Modern::Perl;
4
5
use Exception::Class (
6
7
    'Koha::Exceptions::Patron::Attribute' => {
8
        description => 'Something went wrong'
9
    },
10
    'Koha::Exceptions::Patron::Attribute::NonRepeatable' => {
11
        isa => 'Koha::Exceptions::Patron::Attribute',
12
        description => "repeatable not set for attribute type and tried to add a new attribute for the same code"
13
    },
14
    'Koha::Exceptions::Patron::Attribute::UniqueIDConstraint' => {
15
        isa => 'Koha::Exceptions::Patron::Attribute',
16
        description => "unique_id set for attribute type and tried to add a new with the same code and value"
17
    }
18
);
19
20
1;
(-)a/Koha/Patron/Attribute.pm (-1 / +70 lines)
Lines 17-22 package Koha::Patron::Attribute; Link Here
17
17
18
use Modern::Perl;
18
use Modern::Perl;
19
19
20
use Koha::Database;
21
use Koha::Exceptions::Patron::Attribute;
20
use Koha::Patron::Attribute::Types;
22
use Koha::Patron::Attribute::Types;
21
23
22
use base qw(Koha::Object);
24
use base qw(Koha::Object);
Lines 31-36 Koha::Patron::Attribute - Koha Patron Attribute Object class Link Here
31
33
32
=cut
34
=cut
33
35
36
=head3 store
37
38
    my $attribute = Koha::Patron::Attribute->new({ code => 'a_code', ... });
39
    try { $attribute->store }
40
    catch { handle_exception };
41
42
=cut
43
44
sub store {
45
46
    my $self = shift;
47
48
    $self->_check_repeatable;
49
    $self->_check_unique_id;
50
51
    return $self->SUPER::store();
52
}
53
34
=head3 opac_display
54
=head3 opac_display
35
55
36
    my $attribute = Koha::Patron::Attribute->new({ code => 'a_code', ... });
56
    my $attribute = Koha::Patron::Attribute->new({ code => 'a_code', ... });
Lines 59-64 sub opac_editable { Link Here
59
    return Koha::Patron::Attribute::Types->find( $self->code )->opac_editable;
79
    return Koha::Patron::Attribute::Types->find( $self->code )->opac_editable;
60
}
80
}
61
81
82
=head2 Internal methods
83
84
=head3 _check_repeatable
85
86
_check_repeatable checks if the attribute type is repeatable and throws and exception
87
if the attribute type isn't repeatable and there's already an attribute with the same
88
code for the given patron.
89
90
=cut
91
92
sub _check_repeatable {
93
94
    my $self = shift;
95
96
    if ( !Koha::Patron::Attribute::Types->find( $self->code )->repeatable ) {
97
        my $attr_count
98
            = Koha::Database->new->schema->resultset( $self->_type )->search(
99
            {   borrowernumber => $self->borrowernumber,
100
                code           => $self->code
101
            }
102
            )->count;
103
        Koha::Exceptions::Patron::Attribute::NonRepeatable->throw()
104
            if $attr_count > 0;
105
    }
106
107
    return $self;
108
}
109
110
=head3 _check_unique_id
111
112
_check_unique_id checks if the attribute type is marked as unique id and throws and exception
113
if the attribute type is a unique id and there's already an attribute with the same
114
code and value on the database.
115
116
=cut
117
118
sub _check_unique_id {
119
120
    my $self = shift;
121
122
    if ( Koha::Patron::Attribute::Types->find( $self->code )->unique_id ) {
123
        my $unique_count
124
            = Koha::Database->new->schema->resultset( $self->_type )
125
            ->search( { code => $self->code, attribute => $self->attribute } )
126
            ->count;
127
        Koha::Exceptions::Patron::Attribute::UniqueIDConstraint->throw()
128
            if $unique_count > 0;
129
    }
130
}
131
62
=head3 _type
132
=head3 _type
63
133
64
=cut
134
=cut
65
- 

Return to bug 17828