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

(-)a/Koha/Exceptions/Patron/Attribute/Type.pm (+31 lines)
Line 0 Link Here
1
package Koha::Exceptions::Patron::Attribute::Type;
2
3
use Modern::Perl;
4
5
use Exception::Class (
6
7
    'Koha::Exceptions::Patron::Attribute::Type' => {
8
        description => 'Something went wrong'
9
    },
10
    'Koha::Exceptions::Patron::Attribute::Type::CannotChangeProperty' => {
11
        isa => 'Koha::Exceptions::Patron::Attribute::Type',
12
        description => "Cannot change property",
13
        fields => ['property'],
14
    },
15
);
16
17
sub full_message {
18
    my $self = shift;
19
20
    my $msg = $self->message;
21
22
    unless ( $msg) {
23
        if ( $self->isa('Koha::Exceptions::Patron::Attribute::Type::CannotChangeProperty') ) {
24
            $msg = sprintf("The property '%s' cannot be changed, some patron attributes are using it that way.", $self->property );
25
        }
26
    }
27
28
    return $msg;
29
}
30
31
1;
(-)a/Koha/Patron/Attribute/Type.pm (+85 lines)
Lines 18-23 package Koha::Patron::Attribute::Type; Link Here
18
use Modern::Perl;
18
use Modern::Perl;
19
19
20
use Koha::Database;
20
use Koha::Database;
21
use Koha::Exceptions::Patron::Attribute::Type;
21
22
22
use base qw(Koha::Object Koha::Object::Limit::Library);
23
use base qw(Koha::Object Koha::Object::Limit::Library);
23
24
Lines 31-36 Koha::Patron::Attribute::Type - Koha::Patron::Attribute::Type Object class Link Here
31
32
32
=cut
33
=cut
33
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
34
=head3 _type
119
=head3 _type
35
120
36
=cut
121
=cut
(-)a/admin/patron-attr-types.pl (-3 / +21 lines)
Lines 142-154 sub add_update_attribute_type { Link Here
142
            {
142
            {
143
                code        => $code,
143
                code        => $code,
144
                description => $description,
144
                description => $description,
145
                repeatable  => $repeatable,
146
                unique_id   => $unique_id,
147
            }
145
            }
148
        );
146
        );
149
    }
147
    }
148
150
    $attr_type->set(
149
    $attr_type->set(
151
        {
150
        {
151
            repeatable                => $repeatable,
152
            unique_id                 => $unique_id,
152
            opac_display              => $opac_display,
153
            opac_display              => $opac_display,
153
            opac_editable             => $opac_editable,
154
            opac_editable             => $opac_editable,
154
            staff_searchable          => $staff_searchable,
155
            staff_searchable          => $staff_searchable,
Lines 219-230 sub edit_attribute_type_form { Link Here
219
    my $code = shift;
220
    my $code = shift;
220
221
221
    my $attr_type = Koha::Patron::Attribute::Types->find($code);
222
    my $attr_type = Koha::Patron::Attribute::Types->find($code);
222
    $template->param(attribute_type => $attr_type);
223
223
224
    my $patron_categories = Koha::Patron::Categories->search({}, {order_by => ['description']});
224
    my $patron_categories = Koha::Patron::Categories->search({}, {order_by => ['description']});
225
226
    my $can_be_set_to_nonrepeatable = 1;
227
    if ( $attr_type->repeatable == 1 ) {
228
        $attr_type->repeatable(0);
229
        eval {$attr_type->check_repeatables};
230
        $can_be_set_to_nonrepeatable = 0 if $@;
231
        $attr_type->repeatable(1);
232
    }
233
    my $can_be_set_to_unique = 1;
234
    if ( $attr_type->unique_id == 0 ) {
235
        $attr_type->unique_id(1);
236
        eval {$attr_type->check_unique_ids};
237
        $can_be_set_to_unique = 0 if $@;
238
        $attr_type->unique_id(0);
239
    }
225
    $template->param(
240
    $template->param(
241
        attribute_type => $attr_type,
226
        attribute_type_form => 1,
242
        attribute_type_form => 1,
227
        edit_attribute_type => 1,
243
        edit_attribute_type => 1,
244
        can_be_set_to_nonrepeatable => $can_be_set_to_nonrepeatable,
245
        can_be_set_to_unique => $can_be_set_to_unique,
228
        confirm_op => 'edit_attribute_type_confirmed',
246
        confirm_op => 'edit_attribute_type_confirmed',
229
        categories => $patron_categories,
247
        categories => $patron_categories,
230
    );
248
    );
(-)a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/patron-attr-types.tt (-6 / +11 lines)
Lines 79-107 Link Here
79
       </li>
79
       </li>
80
       <li><label for="repeatable">Repeatable: </label>
80
       <li><label for="repeatable">Repeatable: </label>
81
            [% IF attribute_type %]
81
            [% IF attribute_type %]
82
                [% IF attribute_type.repeatable %]
82
                [% IF attribute_type.repeatable AND NOT can_be_set_to_nonrepeatable %]
83
                    <input type="checkbox" id="repeatable" name="repeatable" checked="checked" disabled="disabled" />
83
                    <input type="checkbox" id="repeatable" name="repeatable" checked="checked" disabled="disabled" />
84
                    <input type="hidden" name="repeatable" value="1" />
85
                [% ELSIF attribute_type.repeatable %]
86
                    <input type="checkbox" id="repeatable" name="repeatable" checked="checked" />
84
                [% ELSE %]
87
                [% ELSE %]
85
                    <input type="checkbox" id="repeatable" name="repeatable" disabled="disabled" />
88
                    <input type="checkbox" id="repeatable" name="repeatable" />
86
                [% END %]
89
                [% END %]
87
            [% ELSE %]
90
            [% ELSE %]
88
                <input type="checkbox" id="repeatable" name="repeatable" />
91
                <input type="checkbox" id="repeatable" name="repeatable" />
89
            [% END %]
92
            [% END %]
90
            <span>Check to let a patron record have multiple values of this attribute.
93
            <span>Check to let a patron record have multiple values of this attribute.</span>
91
                  This setting cannot be changed after an attribute is defined.</span>
92
       </li>
94
       </li>
93
       <li><label for="unique_id">Unique identifier: </label>
95
       <li><label for="unique_id">Unique identifier: </label>
94
            [% IF attribute_type %]
96
            [% IF attribute_type %]
95
                [% IF attribute_type.unique_id %]
97
                [% IF attribute_type.unique_id %]
96
                    <input type="checkbox" id="unique_id" name="unique_id" checked="checked" disabled="disabled" />
98
                    <input type="checkbox" id="unique_id" name="unique_id" checked="checked" />
99
                [% ELSIF can_be_set_to_unique %]
100
                    <input type="checkbox" id="unique_id" name="unique_id" />
97
                [% ELSE %]
101
                [% ELSE %]
98
                    <input type="checkbox" id="unique_id" name="unique_id" disabled="disabled" />
102
                    <input type="checkbox" id="unique_id" name="unique_id" disabled="disabled" />
103
                    <input type="hidden" name="unique_id" value="0" />
99
                [% END %]
104
                [% END %]
100
            [% ELSE %]
105
            [% ELSE %]
101
                <input type="checkbox" id="unique_id" name="unique_id" />
106
                <input type="checkbox" id="unique_id" name="unique_id" />
102
            [% END %]
107
            [% END %]
103
            <span>If checked, attribute will be a unique identifier &mdash; if a value is given to a patron record, the same value
108
            <span>If checked, attribute will be a unique identifier &mdash; if a value is given to a patron record, the same value
104
                  cannot be given to a different record.  This setting cannot be changed after an attribute is defined.</span>
109
                  cannot be given to a different record.</span>
105
       </li>
110
       </li>
106
       <li><label for="opac_display">Display in OPAC: </label>
111
       <li><label for="opac_display">Display in OPAC: </label>
107
          [% IF attribute_type AND attribute_type.opac_display %]
112
          [% IF attribute_type AND attribute_type.opac_display %]
(-)a/t/db_dependent/Koha/Patron/Attribute/Types.t (-2 / +101 lines)
Lines 19-25 Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::More tests => 6;
22
use Test::More tests => 7;
23
23
24
use t::lib::TestBuilder;
24
use t::lib::TestBuilder;
25
use t::lib::Mocks;
25
use t::lib::Mocks;
Lines 69-74 subtest 'new() tests' => sub { Link Here
69
    $schema->storage->txn_rollback;
69
    $schema->storage->txn_rollback;
70
};
70
};
71
71
72
subtest 'store' => sub {
73
74
    plan tests => 4;
75
76
    $schema->storage->txn_begin;
77
78
    # Create 2 attribute types without restrictions:
79
    # Repeatable and can have the same values
80
    my $attr_type_1 = $builder->build_object(
81
        {
82
            class => 'Koha::Patron::Attribute::Types',
83
            value => { repeatable => 1, unique_id => 0 }
84
        }
85
    );
86
    my $attr_type_2 = $builder->build_object(
87
        {
88
            class => 'Koha::Patron::Attribute::Types',
89
            value => { repeatable => 1, unique_id => 0 }
90
        }
91
    );
92
93
    # Patron 1 has twice the attribute 1 and attribute 2
94
    # Patron 2 has attribute 1 and attribute 2="42"
95
    # Patron 3 has attribute 2="42"
96
    # Attribute 1 cannot remove repeatable
97
    # Attribute 2 cannot set unique_id
98
    my $patron_1 = $builder->build_object( { class => 'Koha::Patrons' } );
99
    my $patron_2 = $builder->build_object( { class => 'Koha::Patrons' } );
100
    my $patron_3 = $builder->build_object( { class => 'Koha::Patrons' } );
101
102
    my $attribute_111 = $builder->build_object(
103
        {
104
            class => 'Koha::Patron::Attributes',
105
            value => {
106
                borrowernumber => $patron_1->borrowernumber,
107
                code           => $attr_type_1->code
108
            }
109
        }
110
    );
111
    my $attribute_112 = $builder->build_object(
112
        {
113
            class => 'Koha::Patron::Attributes',
114
            value => {
115
                borrowernumber => $patron_1->borrowernumber,
116
                code           => $attr_type_1->code
117
            }
118
        }
119
    );
120
121
    my $attribute_211 = $builder->build_object(
122
        {
123
            class => 'Koha::Patron::Attributes',
124
            value => {
125
                borrowernumber => $patron_2->borrowernumber,
126
                code           => $attr_type_1->code
127
            }
128
        }
129
    );
130
    my $attribute_221 = $builder->build_object(
131
        {
132
            class => 'Koha::Patron::Attributes',
133
            value => {
134
                borrowernumber => $patron_2->borrowernumber,
135
                code           => $attr_type_2->code,
136
                attribute      => '42',
137
            }
138
        }
139
    );
140
141
    my $attribute_321 = $builder->build_object(
142
        {
143
            class => 'Koha::Patron::Attributes',
144
            value => {
145
                borrowernumber => $patron_3->borrowernumber,
146
                code           => $attr_type_2->code,
147
                attribute      => '42',
148
            }
149
        }
150
    );
151
152
    throws_ok {
153
        $attr_type_1->repeatable(0)->store;
154
    }
155
    'Koha::Exceptions::Patron::Attribute::Type::CannotChangeProperty', "";
156
157
    $attribute_112->delete;
158
    ok($attr_type_1->set({ unique_id => 1, repeatable => 0 })->store);
159
160
    throws_ok {
161
        $attr_type_2->unique_id(1)->store;
162
    }
163
    'Koha::Exceptions::Patron::Attribute::Type::CannotChangeProperty', "";
164
165
    $attribute_321->attribute(43)->store;
166
    ok($attr_type_2->set({ unique_id => 1, repeatable => 0 })->store);
167
168
    $schema->storage->txn_rollback;
169
170
};
171
72
subtest 'library_limits() tests' => sub {
172
subtest 'library_limits() tests' => sub {
73
173
74
    plan tests => 13;
174
    plan tests => 13;
75
- 

Return to bug 8326