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

(-)a/Koha/Object.pm (-8 / +23 lines)
Lines 26-31 use Scalar::Util qw( looks_like_number ); Link Here
26
use Try::Tiny;
26
use Try::Tiny;
27
27
28
use Koha::Database;
28
use Koha::Database;
29
use Koha::Exceptions;
29
use Koha::Exceptions::Object;
30
use Koha::Exceptions::Object;
30
use Koha::DateUtils;
31
use Koha::DateUtils;
31
32
Lines 429-449 sub to_api { Link Here
429
            my $next = $embeds->{$curr}->{children};
430
            my $next = $embeds->{$curr}->{children};
430
431
431
            my $children = $self->$curr;
432
            my $children = $self->$curr;
433
            
434
            my $res;
432
            if ( ref $children eq 'ARRAY' ) {
435
            if ( ref $children eq 'ARRAY' ) {
436
                #when $self->$curr returns an array of Koha::Object
433
                my @list;
437
                my @list;
434
                my $pos = 0;
435
                foreach my $child (@$children) {
438
                foreach my $child (@$children) {
436
                    my $res = $child->to_api({ embed => $next });
439
                    Koha::Exceptions::BadParameter->throw($curr." is not a valid embeddable option.") unless $child->can('to_api');
437
                    $res = { $json_object->{$curr}->[$pos], $res }
440
                    my $r = $child->to_api({ embed => $next });
441
                    push @list, $r;
442
                }
443
                $res = \@list;
444
            }
445
            else {
446
                #when $self->$curr returns a single Koha::Object or Koha::Objects
447
                Koha::Exceptions::BadParameter->throw($curr." is not a valid embeddable option.") unless $children->can('to_api');
448
                $res = $children->to_api({ embed => $next });
449
            }
450
451
            if(ref $res eq 'ARRAY') {
452
                my @list;
453
                my $pos = 0;
454
                foreach my $r (@$res) {
455
                    $r = { $json_object->{$curr}->[$pos], $r }
438
                      if defined $json_object->{$curr}
456
                      if defined $json_object->{$curr}
439
                      && defined $json_object->{$curr}->[$pos];
457
                      && defined $json_object->{$curr}->[$pos];
440
                    push @list, $res;
458
                    push @list, $r;
441
                    $pos++;
459
                    $pos++;
442
                }
460
                }
443
                $json_object->{$curr} = \@list;
461
                $json_object->{$curr} = \@list;
444
            }
462
            } else {
445
            else {
446
                my $res = $children->to_api({ embed => $next });
447
                $res = { $json_object->{$curr}, $res }
463
                $res = { $json_object->{$curr}, $res }
448
                  if defined $json_object->{$curr};
464
                  if defined $json_object->{$curr};
449
                $json_object->{$curr} = $res;
465
                $json_object->{$curr} = $res;
450
- 

Return to bug 24228