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

(-)a/Koha/Object.pm (-23 / +46 lines)
Lines 552-579 sub to_api { Link Here
552
    my ( $self, $params ) = @_;
552
    my ( $self, $params ) = @_;
553
    my $json_object = $self->TO_JSON;
553
    my $json_object = $self->TO_JSON;
554
554
555
    my $to_api_mapping = $self->to_api_mapping;
555
    $json_object = $self->_do_api_mapping($json_object);
556
557
    # Rename attributes if there's a mapping
558
    if ( $self->can('to_api_mapping') ) {
559
        foreach my $column ( keys %{ $self->to_api_mapping } ) {
560
            my $mapped_column = $self->to_api_mapping->{$column};
561
            if ( exists $json_object->{$column}
562
                && defined $mapped_column )
563
            {
564
                # key != undef
565
                $json_object->{$mapped_column} = delete $json_object->{$column};
566
            }
567
            elsif ( exists $json_object->{$column}
568
                && !defined $mapped_column )
569
            {
570
                # key == undef
571
                delete $json_object->{$column};
572
            }
573
        }
574
    }
575
556
576
    my $embeds = $params->{embed};
557
    my $embeds = $params->{embed};
558
    my $av_expand = $params->{av_expand};
577
559
578
    if ($embeds) {
560
    if ($embeds) {
579
        foreach my $embed ( keys %{$embeds} ) {
561
        foreach my $embed ( keys %{$embeds} ) {
Lines 592-611 sub to_api { Link Here
592
                if ( defined $children and ref($children) eq 'ARRAY' ) {
574
                if ( defined $children and ref($children) eq 'ARRAY' ) {
593
                    my @list = map {
575
                    my @list = map {
594
                        $self->_handle_to_api_child(
576
                        $self->_handle_to_api_child(
595
                            { child => $_, next => $next, curr => $curr } )
577
                            { child => $_, next => $next, curr => $curr, av_expand => $av_expand } )
596
                    } @{$children};
578
                    } @{$children};
597
                    $json_object->{$curr} = \@list;
579
                    $json_object->{$curr} = \@list;
598
                }
580
                }
599
                else {
581
                else {
600
                    $json_object->{$curr} = $self->_handle_to_api_child(
582
                    $json_object->{$curr} = $self->_handle_to_api_child(
601
                        { child => $children, next => $next, curr => $curr } );
583
                        { child => $children, next => $next, curr => $curr, av_expand => $av_expand } );
602
                }
584
                }
603
            }
585
            }
604
        }
586
        }
605
    }
587
    }
606
588
589
    if($av_expand && $self->can('_fetch_authorised_values')) {
590
        # _fetch_authorised_values should return a hash as the following
591
        # {
592
        #  column_name => <authorised_value>->unblessed
593
        #  ...
594
        # }
595
        my $avs = $self->_fetch_authorised_values($av_expand);
596
597
        # Language selection will be implemented when lang overlay for av is ready
598
        # Now we will just fetch plain authorised values from the Koha::AuthorisedValues
599
        $avs = $self->_do_api_mapping($avs);
600
601
        $json_object->{_authorised_values} = $avs || {};
602
    }
603
604
    return $json_object;
605
}
606
607
=head3 _do_api_mapping
607
608
609
=cut
608
610
611
sub _do_api_mapping {
612
    my ($self, $json_object) = @_;
613
    # Rename attributes if there's a mapping
614
    if ( $self->can('to_api_mapping') ) {
615
        foreach my $column ( keys %{ $self->to_api_mapping } ) {
616
            my $mapped_column = $self->to_api_mapping->{$column};
617
            if ( exists $json_object->{$column}
618
                && defined $mapped_column )
619
            {
620
                # key != undef
621
                $json_object->{$mapped_column} = delete $json_object->{$column};
622
            }
623
            elsif ( exists $json_object->{$column}
624
                && !defined $mapped_column )
625
            {
626
                # key == undef
627
                delete $json_object->{$column};
628
            }
629
        }
630
    }
609
    return $json_object;
631
    return $json_object;
610
}
632
}
611
633
Lines 853-858 sub _handle_to_api_child { Link Here
853
    my $child = $args->{child};
875
    my $child = $args->{child};
854
    my $next  = $args->{next};
876
    my $next  = $args->{next};
855
    my $curr  = $args->{curr};
877
    my $curr  = $args->{curr};
878
    my $av_expand = $args->{av_expand};
856
879
857
    my $res;
880
    my $res;
858
881
Lines 862-868 sub _handle_to_api_child { Link Here
862
            if defined $next and blessed $child and !$child->can('to_api');
885
            if defined $next and blessed $child and !$child->can('to_api');
863
886
864
        if ( blessed $child ) {
887
        if ( blessed $child ) {
865
            $res = $child->to_api({ embed => $next });
888
            $res = $child->to_api({ embed => $next, av_expand => $av_expand });
866
        }
889
        }
867
        else {
890
        else {
868
            $res = $child;
891
            $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