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

(-)a/Koha/Patron.pm (-39 / +46 lines)
Lines 2305-2315 sub extended_attributes { Link Here
2305
            $attribute->validate_type();
2305
            $attribute->validate_type();
2306
        }
2306
        }
2307
2307
2308
        # Sort new attributes by code
2309
        @new_attributes = sort { $a->attribute cmp $b->attribute } @new_attributes;
2310
2311
        # Stash changes after
2308
        # Stash changes after
2312
        for my $attribute ( values @new_attributes ) {
2309
        # Sort attributes to ensure order is the same as for current attributes retrieved above
2310
        for my $attribute ( sort { $a->attribute cmp $b->attribute } @new_attributes ) {
2313
            my $repeatable = $attribute->type->repeatable ? 1 : 0;
2311
            my $repeatable = $attribute->type->repeatable ? 1 : 0;
2314
            $attribute_changes{$repeatable}->{ $attribute->code }->{after} //= [];
2312
            $attribute_changes{$repeatable}->{ $attribute->code }->{after} //= [];
2315
            push(
2313
            push(
Lines 2327-2332 sub extended_attributes { Link Here
2327
        $schema->txn_do(
2325
        $schema->txn_do(
2328
            sub {
2326
            sub {
2329
                my $all_changes = {};
2327
                my $all_changes = {};
2328
                my @changed_attributes_codes;
2330
                while ( my ( $repeatable, $changes ) = each %attribute_changes ) {
2329
                while ( my ( $repeatable, $changes ) = each %attribute_changes ) {
2331
                    while ( my ( $code, $change ) = each %{$changes} ) {
2330
                    while ( my ( $code, $change ) = each %{$changes} ) {
2332
                        $change->{before} //= [];
2331
                        $change->{before} //= [];
Lines 2338-2343 sub extended_attributes { Link Here
2338
                                $change->{after}  = @{ $change->{after} }  ? $change->{after}->[0]  : '';
2337
                                $change->{after}  = @{ $change->{after} }  ? $change->{after}->[0]  : '';
2339
                            }
2338
                            }
2340
2339
2340
                            push @changed_attributes_codes, $code;
2341
2341
                            # Remove existing
2342
                            # Remove existing
2342
                            $self->extended_attributes->filter_by_branch_limitations->search(
2343
                            $self->extended_attributes->filter_by_branch_limitations->search(
2343
                                {
2344
                                {
Lines 2345-2393 sub extended_attributes { Link Here
2345
                                }
2346
                                }
2346
                            )->delete;
2347
                            )->delete;
2347
2348
2348
                            # Add possible new attribute values
2349
                            for my $attribute (@new_attributes) {
2350
                                $attribute->store() if ( $attribute->code eq $code );
2351
                            }
2352
                            if ( C4::Context->preference("BorrowersLog") ) {
2349
                            if ( C4::Context->preference("BorrowersLog") ) {
2353
                                $all_changes->{"attribute.$code"} = $change;
2350
                                $all_changes->{"attribute.$code"} = $change;
2354
                            }
2351
                            }
2355
                        }
2352
                        }
2356
                    }
2353
                    }
2357
                }
2354
                }
2358
                my $new_types = {};
2359
                for my $attribute ( @{$attributes} ) {
2360
                    $new_types->{ $attribute->{code} } = 1;
2361
                }
2362
2355
2363
                # Check globally mandatory types
2356
                if ( @changed_attributes_codes ) {
2364
                my $interface = C4::Context->interface;
2365
                my $params    = {
2366
                    mandatory                                        => 1,
2367
                    category_code                                    => [ undef, $self->categorycode ],
2368
                    'borrower_attribute_types_branches.b_branchcode' => undef,
2369
                };
2370
2357
2371
                if ( $interface eq 'opac' ) {
2358
                    # Store changed attributes in the order they where passed in as some tests
2372
                    $params->{opac_editable} = 1;
2359
                    # relies on ids being assigned in that order
2373
                }
2360
                    my $new_types = {};
2361
                    for my $new_attribute (@new_attributes) {
2362
                        if ( grep { $_ eq $new_attribute->code } @changed_attributes_codes ) {
2363
                            $new_attribute->store();
2364
                        }
2365
                        $new_types->{ $new_attribute->code } = 1;
2366
                    }
2374
2367
2375
                my @required_attribute_types = Koha::Patron::Attribute::Types->search(
2368
                    # Check globally mandatory types
2376
                    $params,
2369
                    my $interface = C4::Context->interface;
2377
                    { join => 'borrower_attribute_types_branches' }
2370
                    my $params    = {
2378
                )->get_column('code');
2371
                        mandatory                                        => 1,
2379
                for my $type (@required_attribute_types) {
2372
                        category_code                                    => [ undef, $self->categorycode ],
2380
                    Koha::Exceptions::Patron::MissingMandatoryExtendedAttribute->throw(
2373
                        'borrower_attribute_types_branches.b_branchcode' => undef,
2381
                        type => $type,
2374
                    };
2382
                    ) if !$new_types->{$type};
2375
2383
                }
2376
                    if ( $interface eq 'opac' ) {
2384
                if ( %{$all_changes} ) {
2377
                        $params->{opac_editable} = 1;
2385
                    logaction(
2378
                    }
2386
                        "MEMBERS",
2379
2387
                        "MODIFY",
2380
                    my @required_attribute_types = Koha::Patron::Attribute::Types->search(
2388
                        $self->borrowernumber,
2381
                        $params,
2389
                        to_json( $all_changes, { pretty => 1, canonical => 1 } )
2382
                        { join => 'borrower_attribute_types_branches' }
2390
                    );
2383
                    )->get_column('code');
2384
2385
                    for my $type (@required_attribute_types) {
2386
                        Koha::Exceptions::Patron::MissingMandatoryExtendedAttribute->throw(
2387
                            type => $type,
2388
                        ) if !$new_types->{$type};
2389
                    }
2390
2391
                    if ( %{$all_changes} ) {
2392
                        logaction(
2393
                            "MEMBERS",
2394
                            "MODIFY",
2395
                            $self->borrowernumber,
2396
                            to_json( $all_changes, { pretty => 1, canonical => 1 } )
2397
                        );
2398
                    }
2391
                }
2399
                }
2392
            }
2400
            }
2393
        );
2401
        );
2394
- 

Return to bug 26744