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

(-)a/Koha/Patron.pm (-39 / +43 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-2338 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} //= [];
2333
                        $change->{after}  //= [];
2332
                        $change->{after}  //= [];
2334
2333
2335
                        if ( $is_different->( $change->{before}, $change->{after} ) ) {
2334
                        if ( $is_different->( $change->{before}, $change->{after} ) ) {
2335
                            $changed_attributes_codes{$code} = 1;
2336
2336
                            unless ($repeatable) {
2337
                            unless ($repeatable) {
2337
                                $change->{before} = @{ $change->{before} } ? $change->{before}->[0] : '';
2338
                                $change->{before} = @{ $change->{before} } ? $change->{before}->[0] : '';
2338
                                $change->{after}  = @{ $change->{after} }  ? $change->{after}->[0]  : '';
2339
                                $change->{after}  = @{ $change->{after} }  ? $change->{after}->[0]  : '';
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;
2357
                    # Store changed attributes in the order they where passed in as some tests
2365
                my $params    = {
2358
                    # relies on ids being assigned in that order
2366
                    mandatory                                        => 1,
2359
                    my $new_types = {};
2367
                    category_code                                    => [ undef, $self->categorycode ],
2360
                    for my $new_attribute (@new_attributes) {
2368
                    'borrower_attribute_types_branches.b_branchcode' => undef,
2361
                        $new_attribute->store() if $changed_attributes_codes{$new_attribute->code};
2369
                };
2362
                        $new_types->{ $new_attribute->code } = 1;
2363
                    }
2370
2364
2371
                if ( $interface eq 'opac' ) {
2365
                    # Check globally mandatory types
2372
                    $params->{opac_editable} = 1;
2366
                    my $interface = C4::Context->interface;
2373
                }
2367
                    my $params    = {
2368
                        mandatory                                        => 1,
2369
                        category_code                                    => [ undef, $self->categorycode ],
2370
                        'borrower_attribute_types_branches.b_branchcode' => undef,
2371
                    };
2374
2372
2375
                my @required_attribute_types = Koha::Patron::Attribute::Types->search(
2373
                    if ( $interface eq 'opac' ) {
2376
                    $params,
2374
                        $params->{opac_editable} = 1;
2377
                    { join => 'borrower_attribute_types_branches' }
2375
                    }
2378
                )->get_column('code');
2376
2379
                for my $type (@required_attribute_types) {
2377
                    my @required_attribute_types = Koha::Patron::Attribute::Types->search(
2380
                    Koha::Exceptions::Patron::MissingMandatoryExtendedAttribute->throw(
2378
                        $params,
2381
                        type => $type,
2379
                        { join => 'borrower_attribute_types_branches' }
2382
                    ) if !$new_types->{$type};
2380
                    )->get_column('code');
2383
                }
2381
2384
                if ( %{$all_changes} ) {
2382
                    for my $type (@required_attribute_types) {
2385
                    logaction(
2383
                        Koha::Exceptions::Patron::MissingMandatoryExtendedAttribute->throw(
2386
                        "MEMBERS",
2384
                            type => $type,
2387
                        "MODIFY",
2385
                        ) if !$new_types->{$type};
2388
                        $self->borrowernumber,
2386
                    }
2389
                        to_json( $all_changes, { pretty => 1, canonical => 1 } )
2387
2390
                    );
2388
                    if ( %{$all_changes} ) {
2389
                        logaction(
2390
                            "MEMBERS",
2391
                            "MODIFY",
2392
                            $self->borrowernumber,
2393
                            to_json( $all_changes, { pretty => 1, canonical => 1 } )
2394
                        );
2395
                    }
2391
                }
2396
                }
2392
            }
2397
            }
2393
        );
2398
        );
2394
- 

Return to bug 26744