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 217-228 sub edit_attribute_type_form { Link Here
217
    my $code = shift;
218
    my $code = shift;
218
219
219
    my $attr_type = Koha::Patron::Attribute::Types->find($code);
220
    my $attr_type = Koha::Patron::Attribute::Types->find($code);
220
    $template->param(attribute_type => $attr_type);
221
221
222
    my $patron_categories = Koha::Patron::Categories->search({}, {order_by => ['description']});
222
    my $patron_categories = Koha::Patron::Categories->search({}, {order_by => ['description']});
223
224
    my $can_be_set_to_nonrepeatable = 1;
225
    if ( $attr_type->repeatable == 0 ) {
226
        $attr_type->repeatable(1);
227
        eval {$attr_type->check_repeatables};
228
        $can_be_set_to_nonrepeatable = 0 if $@;
229
        $attr_type->repeatable(0);
230
    }
231
    my $can_be_set_to_unique = 1;
232
    if ( $attr_type->unique_id == 0 ) {
233
        $attr_type->unique_id(1);
234
        eval {$attr_type->check_unique_ids};
235
        $can_be_set_to_unique = 0 if $@;
236
        $attr_type->unique_id(0);
237
    }
223
    $template->param(
238
    $template->param(
239
        attribute_type => $attr_type,
224
        attribute_type_form => 1,
240
        attribute_type_form => 1,
225
        edit_attribute_type => 1,
241
        edit_attribute_type => 1,
242
        can_be_set_to_nonrepeatable => $can_be_set_to_nonrepeatable,
243
        can_be_set_to_unique => $can_be_set_to_unique,
226
        confirm_op => 'edit_attribute_type_confirmed',
244
        confirm_op => 'edit_attribute_type_confirmed',
227
        categories => $patron_categories,
245
        categories => $patron_categories,
228
    );
246
    );
(-)a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/patron-attr-types.tt (-5 / +10 lines)
Lines 79-106 Link Here
79
       <li><label for="repeatable">Repeatable: </label>
79
       <li><label for="repeatable">Repeatable: </label>
80
            [% IF attribute_type %]
80
            [% IF attribute_type %]
81
                [% IF attribute_type.repeatable %]
81
                [% IF attribute_type.repeatable %]
82
                    <input type="checkbox" id="repeatable" name="repeatable" checked="checked" disabled="disabled" />
82
                    <input type="checkbox" id="repeatable" name="repeatable" checked="checked" />
83
                [% ELSIF can_be_set_to_nonrepeatable %]
84
                    <input type="checkbox" id="repeatable" name="repeatable" />
83
                [% ELSE %]
85
                [% ELSE %]
84
                    <input type="checkbox" id="repeatable" name="repeatable" disabled="disabled" />
86
                    <input type="checkbox" id="repeatable" name="repeatable" disabled="disabled" />
87
                    <input type="hidden" name="repeatable" value="0" />
85
                [% END %]
88
                [% END %]
86
            [% ELSE %]
89
            [% ELSE %]
87
                <input type="checkbox" id="repeatable" name="repeatable" />
90
                <input type="checkbox" id="repeatable" name="repeatable" />
88
            [% END %]
91
            [% END %]
89
            <span>Check to let a patron record have multiple values of this attribute.
92
            <span>Check to let a patron record have multiple values of this attribute.</span>
90
                  This setting cannot be changed after an attribute is defined.</span>
91
       </li>
93
       </li>
92
       <li><label for="unique_id">Unique identifier: </label>
94
       <li><label for="unique_id">Unique identifier: </label>
93
            [% IF attribute_type %]
95
            [% IF attribute_type %]
94
                [% IF attribute_type.unique_id %]
96
                [% IF attribute_type.unique_id %]
95
                    <input type="checkbox" id="unique_id" name="unique_id" checked="checked" disabled="disabled" />
97
                    <input type="checkbox" id="unique_id" name="unique_id" checked="checked" />
98
                [% ELSIF can_be_set_to_unique %]
99
                    <input type="checkbox" id="unique_id" name="unique_id" />
96
                [% ELSE %]
100
                [% ELSE %]
97
                    <input type="checkbox" id="unique_id" name="unique_id" disabled="disabled" />
101
                    <input type="checkbox" id="unique_id" name="unique_id" disabled="disabled" />
102
                    <input type="hidden" name="unique_id" value="0" />
98
                [% END %]
103
                [% END %]
99
            [% ELSE %]
104
            [% ELSE %]
100
                <input type="checkbox" id="unique_id" name="unique_id" />
105
                <input type="checkbox" id="unique_id" name="unique_id" />
101
            [% END %]
106
            [% END %]
102
            <span>If checked, attribute will be a unique identifier &mdash; if a value is given to a patron record, the same value
107
            <span>If checked, attribute will be a unique identifier &mdash; if a value is given to a patron record, the same value
103
                  cannot be given to a different record.  This setting cannot be changed after an attribute is defined.</span>
108
                  cannot be given to a different record.</span>
104
       </li>
109
       </li>
105
       <li><label for="opac_display">Display in OPAC: </label>
110
       <li><label for="opac_display">Display in OPAC: </label>
106
          [% IF attribute_type AND attribute_type.opac_display %]
111
          [% IF attribute_type AND attribute_type.opac_display %]
(-)a/members/memberentry.pl (-1 / +1 lines)
Lines 406-412 if ($op eq 'save' || $op eq 'insert'){ Link Here
406
          eval {$attribute->check_unique_id};
406
          eval {$attribute->check_unique_id};
407
          if ( $@ ) {
407
          if ( $@ ) {
408
              push @errors, "ERROR_extended_unique_id_failed";
408
              push @errors, "ERROR_extended_unique_id_failed";
409
              my $attr_type = Koha::Patron::Attribute::Types->find($attr->code);
409
              my $attr_type = Koha::Patron::Attribute::Types->find($attr->{code});
410
              $template->param(
410
              $template->param(
411
                  ERROR_extended_unique_id_failed_code => $attr->{code},
411
                  ERROR_extended_unique_id_failed_code => $attr->{code},
412
                  ERROR_extended_unique_id_failed_value => $attr->{attribute},
412
                  ERROR_extended_unique_id_failed_value => $attr->{attribute},
(-)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