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

(-)a/Koha/Patron/Attribute.pm (-28 lines)
Lines 51-84 sub store { Link Here
51
    return $self->SUPER::store();
51
    return $self->SUPER::store();
52
}
52
}
53
53
54
=head3 opac_display
55
56
    my $attribute = Koha::Patron::Attribute->new({ code => 'a_code', ... });
57
    if ( $attribute->opac_display ) { ... }
58
59
=cut
60
61
sub opac_display {
62
63
    my $self = shift;
64
65
    return Koha::Patron::Attribute::Types->find( $self->code )->opac_display;
66
}
67
68
=head3 opac_editable
69
70
    my $attribute = Koha::Patron::Attribute->new({ code => 'a_code', ... });
71
    if ( $attribute->is_opac_editable ) { ... }
72
73
=cut
74
75
sub opac_editable {
76
77
    my $self = shift;
78
79
    return Koha::Patron::Attribute::Types->find( $self->code )->opac_editable;
80
}
81
82
=head3 type
54
=head3 type
83
55
84
    my $attribute_type = $attribute->type;
56
    my $attribute_type = $attribute->type;
(-)a/Koha/Patron/Attributes.pm (+38 lines)
Lines 31-36 Koha::Patron::Attributes - Koha Patron Attributes Object set class Link Here
31
31
32
=cut
32
=cut
33
33
34
=head3 search_opac_editable
35
36
search_opac_editable will accept the same parameters as the search method, but will only
37
return attributes that are editable by patrons via the opac.
38
39
=cut
40
41
sub search_opac_editable {
42
    my ( $self, $params, $attributes ) = @_;
43
44
    my $join = { join => 'code' };
45
    $attributes //= {};
46
    $attributes = { %$attributes, %$join };
47
48
    my $opac_editable = { 'code.opac_editable' => 1 };
49
50
    return $self->SUPER::search( { %$params, %$opac_editable }, $attributes );
51
}
52
53
=head3 search_opac_displayable
54
55
search_opac_displayable will accept the same parameters as the search method, but will only
56
return attributes that are displayable for patrons via the opac.
57
58
=cut
59
60
sub search_opac_displayable {
61
    my ( $self, $params, $attributes ) = @_;
62
63
    my $join = { join => 'code' };
64
    $attributes //= {};
65
    $attributes = { %$attributes, %$join };
66
67
    my $opac_displayable = { 'code.opac_display' => 1 };
68
69
    return $self->SUPER::search( { %$params, %$opac_displayable }, $attributes );
70
}
71
34
=head3 _type
72
=head3 _type
35
73
36
=cut
74
=cut
(-)a/opac/opac-memberentry.pl (-7 / +16 lines)
Lines 473-479 sub DelEmptyFields { Link Here
473
sub FilterUnchangedAttributes {
473
sub FilterUnchangedAttributes {
474
    my ( $borrowernumber, $entered_attributes ) = @_;
474
    my ( $borrowernumber, $entered_attributes ) = @_;
475
475
476
    my @patron_attributes = grep {$_->opac_editable} Koha::Patron::Attributes->search({ borrowernumber => $borrowernumber })->as_list;
476
    my @patron_attributes = Koha::Patron::Attributes->search_opac_editable(
477
        { borrowernumber => $borrowernumber } );
477
478
478
    my $patron_attribute_types;
479
    my $patron_attribute_types;
479
    foreach my $attr (@patron_attributes) {
480
    foreach my $attr (@patron_attributes) {
Lines 530-543 sub GeneratePatronAttributesForm { Link Here
530
    my ( $borrowernumber, $entered_attributes ) = @_;
531
    my ( $borrowernumber, $entered_attributes ) = @_;
531
532
532
    # Get all attribute types and the values for this patron (if applicable)
533
    # Get all attribute types and the values for this patron (if applicable)
533
    my @types = grep { $_->opac_editable() or $_->opac_display }
534
    my @types = Koha::Patron::Attribute::Types->search(
534
        Koha::Patron::Attribute::Types->search()->as_list();
535
        {   -or => [
536
                opac_editable => 1,
537
                opac_display  => 1
538
                ]
539
        }
540
    );
535
    if ( scalar(@types) == 0 ) {
541
    if ( scalar(@types) == 0 ) {
536
        return [];
542
        return [];
537
    }
543
    }
538
544
539
    my @displayable_attributes = grep { $_->opac_display }
545
    my @displayable_attributes
540
        Koha::Patron::Attributes->search({ borrowernumber => $borrowernumber })->as_list;
546
        = Koha::Patron::Attributes->search_opac_displayable(
547
        { borrowernumber => $borrowernumber } );
541
548
542
    my %attr_values = ();
549
    my %attr_values = ();
543
550
Lines 549-562 sub GeneratePatronAttributesForm { Link Here
549
        }
556
        }
550
    }
557
    }
551
    elsif ( defined $borrowernumber ) {
558
    elsif ( defined $borrowernumber ) {
552
        my @editable_attributes = grep { $_->opac_editable } @displayable_attributes;
559
        my @editable_attributes
560
            = Koha::Patron::Attributes->search_opac_editable(
561
            { borrowernumber => $borrowernumber } );
553
        foreach my $attr (@editable_attributes) {
562
        foreach my $attr (@editable_attributes) {
554
            push @{ $attr_values{ $attr->code } }, $attr->attribute;
563
            push @{ $attr_values{ $attr->code } }, $attr->attribute;
555
        }
564
        }
556
    }
565
    }
557
566
558
    # Add the non-editable attributes (that don't come from the form)
567
    # Add the non-editable attributes (that don't come from the form)
559
    foreach my $attr ( grep { !$_->opac_editable } @displayable_attributes ) {
568
    foreach my $attr ( grep { !$_->type->opac_editable } @displayable_attributes ) {
560
        push @{ $attr_values{ $attr->code } }, $attr->attribute;
569
        push @{ $attr_values{ $attr->code } }, $attr->attribute;
561
    }
570
    }
562
571
(-)a/t/db_dependent/Koha/Patron/Attributes.t (-80 / +66 lines)
Lines 19-31 Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::More tests => 5;
22
use Test::More tests => 4;
23
23
24
use t::lib::TestBuilder;
24
use t::lib::TestBuilder;
25
use Test::Exception;
25
use Test::Exception;
26
26
27
use Koha::Database;
27
use Koha::Database;
28
use Koha::Patron::Attribute;
29
use Koha::Patron::Attributes;
28
use Koha::Patron::Attributes;
30
29
31
my $schema  = Koha::Database->new->schema;
30
my $schema  = Koha::Database->new->schema;
Lines 157-276 subtest 'store() unique_id attributes tests' => sub { Link Here
157
    $schema->storage->txn_rollback;
156
    $schema->storage->txn_rollback;
158
};
157
};
159
158
160
subtest 'opac_display() tests' => sub {
159
subtest 'type() tests' => sub {
161
160
162
    plan tests => 2;
161
    plan tests => 4;
163
162
164
    $schema->storage->txn_begin;
163
    $schema->storage->txn_begin;
165
164
166
    my $patron
165
    my $patron
167
        = $builder->build( { source => 'Borrower' } )->{borrowernumber};
166
        = $builder->build( { source => 'Borrower' } )->{borrowernumber};
168
    my $attribute_type_1 = $builder->build(
167
    my $attr_type = $builder->build( { source => 'BorrowerAttributeType' } );
169
        {   source => 'BorrowerAttributeType',
168
    my $attribute = Koha::Patron::Attribute->new(
170
            value  => { opac_display => 1 }
171
        }
172
    );
173
174
    my $attribute_1 = Koha::Patron::Attribute->new(
175
        {   borrowernumber => $patron,
169
        {   borrowernumber => $patron,
176
            code           => $attribute_type_1->{code},
170
            code           => $attr_type->{code},
177
            attribute      => $patron
171
            attribute      => $patron
178
        }
172
        }
179
    );
173
    );
180
    is( $attribute_1->opac_display, 1, '->opac_display returns 1' );
181
174
182
    my $attribute_type_2 = $builder->build(
175
    my $attribute_type = $attribute->type;
183
        {   source => 'BorrowerAttributeType',
176
184
            value  => { opac_display => 0 }
177
    is( ref($attribute_type),
185
        }
178
        'Koha::Patron::Attribute::Type',
179
        '->type returns a Koha::Patron::Attribute::Type object'
186
    );
180
    );
187
181
188
    my $attribute_2 = Koha::Patron::Attribute->new(
182
    is( $attribute_type->code,
189
        {   borrowernumber => $patron,
183
        $attr_type->{code},
190
            code           => $attribute_type_2->{code},
184
        '->type returns the right Koha::Patron::Attribute::Type object' );
191
            attribute      => $patron
185
192
        }
186
    is( $attribute_type->opac_editable,
187
        $attr_type->{opac_editable},
188
        '->type returns the right Koha::Patron::Attribute::Type object'
189
    );
190
191
    is( $attribute_type->opac_display,
192
        $attr_type->{opac_display},
193
        '->type returns the right Koha::Patron::Attribute::Type object'
193
    );
194
    );
194
    is( $attribute_2->opac_display, 0, '->opac_display returns 0' );
195
195
196
    $schema->storage->txn_rollback;
196
    $schema->storage->txn_rollback;
197
};
197
};
198
198
199
subtest 'opac_editable() tests' => sub {
199
subtest 'search_opac_editable() and search_opac_displayable() tests' => sub {
200
200
201
    plan tests => 2;
201
    plan tests => 3;
202
202
203
    $schema->storage->txn_begin;
203
    $schema->storage->txn_begin;
204
204
205
    my $patron
205
    my $editable_attr_type = $builder->build_object(
206
        = $builder->build( { source => 'Borrower' } )->{borrowernumber};
206
        {   class => 'Koha::Patron::Attribute::Types',
207
    my $attribute_type_1 = $builder->build(
207
            value => { opac_editable => 1, opac_display => 1 }
208
        {   source => 'BorrowerAttributeType',
209
            value  => { opac_editable => 1 }
210
        }
208
        }
211
    );
209
    );
212
210
    my $displayable_attr_type = $builder->build_object(
213
    my $attribute_1 = Koha::Patron::Attribute->new(
211
        {   class => 'Koha::Patron::Attribute::Types',
214
        {   borrowernumber => $patron,
212
            value => { opac_editable => 0, opac_display => 1 }
215
            code           => $attribute_type_1->{code},
216
            attribute      => $patron
217
        }
213
        }
218
    );
214
    );
219
    is( $attribute_1->opac_editable, 1, '->opac_editable returns 1' );
215
    my $staff_only_attr_type = $builder->build_object(
220
216
        {   class => 'Koha::Patron::Attribute::Types',
221
    my $attribute_type_2 = $builder->build(
217
            value => { opac_editable => 0, opac_display => 0 }
222
        {   source => 'BorrowerAttributeType',
223
            value  => { opac_editable => 0 }
224
        }
218
        }
225
    );
219
    );
226
220
    my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
227
    my $attribute_2 = Koha::Patron::Attribute->new(
221
    my $editable_attr = $builder->build_object(
228
        {   borrowernumber => $patron,
222
        {   class => 'Koha::Patron::Attributes',
229
            code           => $attribute_type_2->{code},
223
            value => {
230
            attribute      => $patron
224
                borrowernumber => $patron->borrowernumber,
225
                code           => $editable_attr_type->code
226
            }
231
        }
227
        }
232
    );
228
    );
233
    is( $attribute_2->opac_editable, 0, '->opac_editable returns 0' );
229
    my $displayable_attr = $builder->build_object(
234
230
        {   class => 'Koha::Patron::Attributes',
235
    $schema->storage->txn_rollback;
231
            value => {
236
};
232
                borrowernumber => $patron->borrowernumber,
237
233
                code           => $displayable_attr_type->code
238
subtest 'type() tests' => sub {
234
            }
239
235
        }
240
    plan tests => 4;
236
    );
241
237
    my $staff_only_attr = $builder->build_object(
242
    $schema->storage->txn_begin;
238
        {   class => 'Koha::Patron::Attributes',
243
239
            value => {
244
    my $patron
240
                borrowernumber => $patron->borrowernumber,
245
        = $builder->build( { source => 'Borrower' } )->{borrowernumber};
241
                code           => $staff_only_attr_type->code
246
    my $attr_type = $builder->build( { source => 'BorrowerAttributeType' } );
242
            }
247
    my $attribute = Koha::Patron::Attribute->new(
248
        {   borrowernumber => $patron,
249
            code           => $attr_type->{code},
250
            attribute      => $patron
251
        }
243
        }
252
    );
244
    );
253
245
254
    my $attribute_type = $attribute->type;
246
    my $search_results = Koha::Patron::Attributes->search(
247
        { borrowernumber => $patron->borrowernumber } );
248
    is( $search_results->count, 3, '3 attributes present for patron' );
255
249
256
    is( ref($attribute_type),
250
    $search_results = Koha::Patron::Attributes->search_opac_displayable(
257
        'Koha::Patron::Attribute::Type',
251
        { borrowernumber => $patron->borrowernumber } );
258
        '->type returns a Koha::Patron::Attribute::Type object'
259
    );
260
252
261
    is( $attribute_type->code,
253
    is( $search_results->count, 2,
262
        $attr_type->{code},
254
        '2 attributes are opac displayable for patron' );
263
        '->type returns the right Koha::Patron::Attribute::Type object' );
264
255
265
    is( $attribute_type->opac_editable,
256
    $search_results = Koha::Patron::Attributes->search_opac_editable(
266
        $attr_type->{opac_editable},
257
        { borrowernumber => $patron->borrowernumber } );
267
        '->type returns the right Koha::Patron::Attribute::Type object'
268
    );
269
258
270
    is( $attribute_type->opac_display,
259
    is( $search_results->count, 1,
271
        $attr_type->{opac_display},
260
        '1 attribute is opac editable for patron' );
272
        '->type returns the right Koha::Patron::Attribute::Type object'
273
    );
274
261
275
    $schema->storage->txn_rollback;
262
    $schema->storage->txn_rollback;
276
};
263
};
277
- 

Return to bug 18339