|
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; |