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

(-)a/Koha/Object.pm (-23 / +46 lines)
Lines 505-532 sub to_api { Link Here
505
    my ( $self, $params ) = @_;
505
    my ( $self, $params ) = @_;
506
    my $json_object = $self->TO_JSON;
506
    my $json_object = $self->TO_JSON;
507
507
508
    my $to_api_mapping = $self->to_api_mapping;
508
    $json_object = $self->_do_api_mapping($json_object);
509
510
    # Rename attributes if there's a mapping
511
    if ( $self->can('to_api_mapping') ) {
512
        foreach my $column ( keys %{ $self->to_api_mapping } ) {
513
            my $mapped_column = $self->to_api_mapping->{$column};
514
            if ( exists $json_object->{$column}
515
                && defined $mapped_column )
516
            {
517
                # key != undef
518
                $json_object->{$mapped_column} = delete $json_object->{$column};
519
            }
520
            elsif ( exists $json_object->{$column}
521
                && !defined $mapped_column )
522
            {
523
                # key == undef
524
                delete $json_object->{$column};
525
            }
526
        }
527
    }
528
509
529
    my $embeds = $params->{embed};
510
    my $embeds = $params->{embed};
511
    my $av_expand = $params->{av_expand};
530
512
531
    if ($embeds) {
513
    if ($embeds) {
532
        foreach my $embed ( keys %{$embeds} ) {
514
        foreach my $embed ( keys %{$embeds} ) {
Lines 545-564 sub to_api { Link Here
545
                if ( defined $children and ref($children) eq 'ARRAY' ) {
527
                if ( defined $children and ref($children) eq 'ARRAY' ) {
546
                    my @list = map {
528
                    my @list = map {
547
                        $self->_handle_to_api_child(
529
                        $self->_handle_to_api_child(
548
                            { child => $_, next => $next, curr => $curr } )
530
                            { child => $_, next => $next, curr => $curr, av_expand => $av_expand } )
549
                    } @{$children};
531
                    } @{$children};
550
                    $json_object->{$curr} = \@list;
532
                    $json_object->{$curr} = \@list;
551
                }
533
                }
552
                else {
534
                else {
553
                    $json_object->{$curr} = $self->_handle_to_api_child(
535
                    $json_object->{$curr} = $self->_handle_to_api_child(
554
                        { child => $children, next => $next, curr => $curr } );
536
                        { child => $children, next => $next, curr => $curr, av_expand => $av_expand } );
555
                }
537
                }
556
            }
538
            }
557
        }
539
        }
558
    }
540
    }
559
541
542
    if($av_expand && $self->can('_fetch_authorised_values')) {
543
        # _fetch_authorised_values should return a hash as the following
544
        # {
545
        #  column_name => <authorised_value>->unblessed
546
        #  ...
547
        # }
548
        my $avs = $self->_fetch_authorised_values($av_expand);
549
550
        # Language selection will be implemented when lang overlay for av is ready
551
        # Now we will just fetch plain authorised values from the Koha::AuthorisedValues
552
        $avs = $self->_do_api_mapping($avs);
553
554
        $json_object->{_authorised_values} = $avs || {};
555
    }
556
557
    return $json_object;
558
}
559
560
=head3 _do_api_mapping
560
561
562
=cut
561
563
564
sub _do_api_mapping {
565
    my ($self, $json_object) = @_;
566
    # Rename attributes if there's a mapping
567
    if ( $self->can('to_api_mapping') ) {
568
        foreach my $column ( keys %{ $self->to_api_mapping } ) {
569
            my $mapped_column = $self->to_api_mapping->{$column};
570
            if ( exists $json_object->{$column}
571
                && defined $mapped_column )
572
            {
573
                # key != undef
574
                $json_object->{$mapped_column} = delete $json_object->{$column};
575
            }
576
            elsif ( exists $json_object->{$column}
577
                && !defined $mapped_column )
578
            {
579
                # key == undef
580
                delete $json_object->{$column};
581
            }
582
        }
583
    }
562
    return $json_object;
584
    return $json_object;
563
}
585
}
564
586
Lines 806-811 sub _handle_to_api_child { Link Here
806
    my $child = $args->{child};
828
    my $child = $args->{child};
807
    my $next  = $args->{next};
829
    my $next  = $args->{next};
808
    my $curr  = $args->{curr};
830
    my $curr  = $args->{curr};
831
    my $av_expand = $args->{av_expand};
809
832
810
    my $res;
833
    my $res;
811
834
Lines 815-821 sub _handle_to_api_child { Link Here
815
            if defined $next and blessed $child and !$child->can('to_api');
838
            if defined $next and blessed $child and !$child->can('to_api');
816
839
817
        if ( blessed $child ) {
840
        if ( blessed $child ) {
818
            $res = $child->to_api({ embed => $next });
841
            $res = $child->to_api({ embed => $next, av_expand => $av_expand });
819
        }
842
        }
820
        else {
843
        else {
821
            $res = $child;
844
            $res = $child;
(-)a/Koha/REST/Plugin/Objects.pm (-2 / +1 lines)
Lines 138-144 sub register { Link Here
138
                });
138
                });
139
            }
139
            }
140
140
141
            return $objects->to_api({ embed => $embed });
141
            return $objects->to_api({ embed => $embed, av_expand => $c->req->headers->header('x-koha-av') });
142
        }
142
        }
143
    );
143
    );
144
}
144
}
145
- 

Return to bug 26635