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

(-)a/Koha/Object.pm (-23 / +46 lines)
Lines 551-578 sub to_api { Link Here
551
    my ( $self, $params ) = @_;
551
    my ( $self, $params ) = @_;
552
    my $json_object = $self->TO_JSON;
552
    my $json_object = $self->TO_JSON;
553
553
554
    my $to_api_mapping = $self->to_api_mapping;
554
    $json_object = $self->_do_api_mapping($json_object);
555
556
    # Rename attributes if there's a mapping
557
    if ( $self->can('to_api_mapping') ) {
558
        foreach my $column ( keys %{ $self->to_api_mapping } ) {
559
            my $mapped_column = $self->to_api_mapping->{$column};
560
            if ( exists $json_object->{$column}
561
                && defined $mapped_column )
562
            {
563
                # key != undef
564
                $json_object->{$mapped_column} = delete $json_object->{$column};
565
            }
566
            elsif ( exists $json_object->{$column}
567
                && !defined $mapped_column )
568
            {
569
                # key == undef
570
                delete $json_object->{$column};
571
            }
572
        }
573
    }
574
555
575
    my $embeds = $params->{embed};
556
    my $embeds = $params->{embed};
557
    my $av_expand = $params->{av_expand};
576
558
577
    if ($embeds) {
559
    if ($embeds) {
578
        foreach my $embed ( keys %{$embeds} ) {
560
        foreach my $embed ( keys %{$embeds} ) {
Lines 591-610 sub to_api { Link Here
591
                if ( defined $children and ref($children) eq 'ARRAY' ) {
573
                if ( defined $children and ref($children) eq 'ARRAY' ) {
592
                    my @list = map {
574
                    my @list = map {
593
                        $self->_handle_to_api_child(
575
                        $self->_handle_to_api_child(
594
                            { child => $_, next => $next, curr => $curr } )
576
                            { child => $_, next => $next, curr => $curr, av_expand => $av_expand } )
595
                    } @{$children};
577
                    } @{$children};
596
                    $json_object->{$curr} = \@list;
578
                    $json_object->{$curr} = \@list;
597
                }
579
                }
598
                else {
580
                else {
599
                    $json_object->{$curr} = $self->_handle_to_api_child(
581
                    $json_object->{$curr} = $self->_handle_to_api_child(
600
                        { child => $children, next => $next, curr => $curr } );
582
                        { child => $children, next => $next, curr => $curr, av_expand => $av_expand } );
601
                }
583
                }
602
            }
584
            }
603
        }
585
        }
604
    }
586
    }
605
587
588
    if($av_expand && $self->can('_fetch_authorised_values')) {
589
        # _fetch_authorised_values should return a hash as the following
590
        # {
591
        #  column_name => <authorised_value>->unblessed
592
        #  ...
593
        # }
594
        my $avs = $self->_fetch_authorised_values($av_expand);
595
596
        # Language selection will be implemented when lang overlay for av is ready
597
        # Now we will just fetch plain authorised values from the Koha::AuthorisedValues
598
        $avs = $self->_do_api_mapping($avs);
599
600
        $json_object->{_authorised_values} = $avs || {};
601
    }
602
603
    return $json_object;
604
}
605
606
=head3 _do_api_mapping
606
607
608
=cut
607
609
610
sub _do_api_mapping {
611
    my ($self, $json_object) = @_;
612
    # Rename attributes if there's a mapping
613
    if ( $self->can('to_api_mapping') ) {
614
        foreach my $column ( keys %{ $self->to_api_mapping } ) {
615
            my $mapped_column = $self->to_api_mapping->{$column};
616
            if ( exists $json_object->{$column}
617
                && defined $mapped_column )
618
            {
619
                # key != undef
620
                $json_object->{$mapped_column} = delete $json_object->{$column};
621
            }
622
            elsif ( exists $json_object->{$column}
623
                && !defined $mapped_column )
624
            {
625
                # key == undef
626
                delete $json_object->{$column};
627
            }
628
        }
629
    }
608
    return $json_object;
630
    return $json_object;
609
}
631
}
610
632
Lines 852-857 sub _handle_to_api_child { Link Here
852
    my $child = $args->{child};
874
    my $child = $args->{child};
853
    my $next  = $args->{next};
875
    my $next  = $args->{next};
854
    my $curr  = $args->{curr};
876
    my $curr  = $args->{curr};
877
    my $av_expand = $args->{av_expand};
855
878
856
    my $res;
879
    my $res;
857
880
Lines 861-867 sub _handle_to_api_child { Link Here
861
            if defined $next and blessed $child and !$child->can('to_api');
884
            if defined $next and blessed $child and !$child->can('to_api');
862
885
863
        if ( blessed $child ) {
886
        if ( blessed $child ) {
864
            $res = $child->to_api({ embed => $next });
887
            $res = $child->to_api({ embed => $next, av_expand => $av_expand });
865
        }
888
        }
866
        else {
889
        else {
867
            $res = $child;
890
            $res = $child;
(-)a/Koha/REST/Plugin/Objects.pm (-5 / +7 lines)
Lines 53-59 the requested object. It passes through any embeds if specified. Link Here
53
            my $attributes = {};
53
            my $attributes = {};
54
54
55
            # Look for embeds
55
            # Look for embeds
56
            my $embed = $c->stash('koha.embed');
56
            my $embed     = $c->stash('koha.embed');
57
            my $av_expand = $c->req->headers->header('x-koha-av-expand');
58
57
            # Generate prefetches for embedded stuff
59
            # Generate prefetches for embedded stuff
58
            $c->dbic_merge_prefetch(
60
            $c->dbic_merge_prefetch(
59
                {
61
                {
Lines 66-72 the requested object. It passes through any embeds if specified. Link Here
66
68
67
            return unless $object;
69
            return unless $object;
68
70
69
            return $object->to_api({ embed => $embed });
71
            return $object->to_api({ embed => $embed, av_expand => $av_expand });
70
        }
72
        }
71
    );
73
    );
72
74
Lines 92-98 for API rendering. Link Here
92
            # Extract reserved params
94
            # Extract reserved params
93
            my ( $filtered_params, $reserved_params, $path_params ) = $c->extract_reserved_params($args);
95
            my ( $filtered_params, $reserved_params, $path_params ) = $c->extract_reserved_params($args);
94
            # Look for embeds
96
            # Look for embeds
95
            my $embed = $c->stash('koha.embed');
97
            my $embed     = $c->stash('koha.embed');
98
            my $av_expand = $c->req->headers->header('x-koha-av-expand');
96
99
97
            # Merge sorting into query attributes
100
            # Merge sorting into query attributes
98
            $c->dbic_merge_sorting(
101
            $c->dbic_merge_sorting(
Lines 162-168 for API rendering. Link Here
162
                }
165
                }
163
            );
166
            );
164
167
165
            return $objects->to_api({ embed => $embed });
168
            return $objects->to_api({ embed => $embed, av_expand => $av_expand });
166
        }
169
        }
167
    );
170
    );
168
}
171
}
169
- 

Return to bug 26635