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