|
Lines 553-587
sub to_api {
Link Here
|
| 553 |
my ( $self, $params ) = @_; |
553 |
my ( $self, $params ) = @_; |
| 554 |
my $json_object = $self->TO_JSON; |
554 |
my $json_object = $self->TO_JSON; |
| 555 |
|
555 |
|
| 556 |
# Remove forbidden attributes if required |
556 |
# coded values handling |
|
|
557 |
my $avs = {}; |
| 558 |
if ( $params->{av_expand} and $self->can('_fetch_authorised_values') ) { |
| 559 |
$avs = $self->_fetch_authorised_values; |
| 560 |
} |
| 561 |
|
| 562 |
# Remove forbidden attributes if required (including their coded values) |
| 557 |
if ( $params->{public} ) { |
563 |
if ( $params->{public} ) { |
| 558 |
for my $field ( keys %{$json_object} ) { |
564 |
for my $field ( keys %{$json_object} ) { |
| 559 |
delete $json_object->{$field} unless any { $_ eq $field } @{ $self->public_read_list }; |
565 |
delete $json_object->{$field} unless any { $_ eq $field } @{ $self->public_read_list }; |
| 560 |
} |
566 |
} |
|
|
567 |
|
| 568 |
if ( $params->{av_expand} ) { |
| 569 |
foreach my $field (keys %{$avs}) { |
| 570 |
delete $avs->{$field} |
| 571 |
unless any { $_ eq $field } @{ $self->public_read_list }; |
| 572 |
} |
| 573 |
} |
| 561 |
} |
574 |
} |
| 562 |
|
575 |
|
| 563 |
my $to_api_mapping = $self->to_api_mapping; |
576 |
my $to_api_mapping = $self->to_api_mapping; |
| 564 |
|
577 |
|
| 565 |
# Rename attributes if there's a mapping |
578 |
# Rename attributes and coded values if there's a mapping |
| 566 |
if ( $self->can('to_api_mapping') ) { |
579 |
if ( $self->can('to_api_mapping') ) { |
| 567 |
foreach my $column ( keys %{ $self->to_api_mapping } ) { |
580 |
foreach my $column ( keys %{ $self->to_api_mapping } ) { |
| 568 |
my $mapped_column = $self->to_api_mapping->{$column}; |
581 |
my $mapped_column = $self->to_api_mapping->{$column}; |
| 569 |
if ( exists $json_object->{$column} |
582 |
if ( exists $json_object->{$column} |
| 570 |
&& defined $mapped_column ) |
583 |
&& defined $mapped_column ) { |
| 571 |
{ |
584 |
|
| 572 |
# key != undef |
585 |
# key != undef |
| 573 |
$json_object->{$mapped_column} = delete $json_object->{$column}; |
586 |
$json_object->{$mapped_column} = delete $json_object->{$column}; |
| 574 |
} |
587 |
$avs->{$mapped_column} = delete $avs->{$column} |
| 575 |
elsif ( exists $json_object->{$column} |
588 |
if exists $avs->{$column}; |
| 576 |
&& !defined $mapped_column ) |
589 |
|
| 577 |
{ |
590 |
} elsif ( exists $json_object->{$column} |
|
|
591 |
&& !defined $mapped_column ) { |
| 592 |
|
| 578 |
# key == undef |
593 |
# key == undef |
| 579 |
delete $json_object->{$column}; |
594 |
delete $json_object->{$column}; |
|
|
595 |
delete $avs->{$column}; |
| 580 |
} |
596 |
} |
| 581 |
} |
597 |
} |
| 582 |
} |
598 |
} |
| 583 |
|
599 |
|
| 584 |
my $av_expand = $params->{av_expand}; |
600 |
$json_object->{_authorised_values} = $avs |
|
|
601 |
if $params->{av_expand}; |
| 585 |
|
602 |
|
| 586 |
# Make sure we duplicate the $params variable to avoid |
603 |
# Make sure we duplicate the $params variable to avoid |
| 587 |
# breaking calls in a loop (Koha::Objects->to_api) |
604 |
# breaking calls in a loop (Koha::Objects->to_api) |
|
Lines 629-677
sub to_api {
Link Here
|
| 629 |
} |
646 |
} |
| 630 |
} |
647 |
} |
| 631 |
|
648 |
|
| 632 |
if ( $av_expand && $self->can('_fetch_authorised_values') ) { |
|
|
| 633 |
|
| 634 |
# _fetch_authorised_values should return a hash as the following |
| 635 |
# { |
| 636 |
# column_name => <authorised_value>->unblessed |
| 637 |
# ... |
| 638 |
# } |
| 639 |
my $avs = $self->_fetch_authorised_values($av_expand); |
| 640 |
|
| 641 |
# Language selection will be implemented when lang overlay for av is ready |
| 642 |
# Now we will just fetch plain authorised values from the Koha::AuthorisedValues |
| 643 |
$avs = $self->_do_api_mapping($avs); |
| 644 |
|
| 645 |
$json_object->{_authorised_values} = $avs || {}; |
| 646 |
} |
| 647 |
|
| 648 |
return $json_object; |
| 649 |
} |
| 650 |
|
| 651 |
=head3 _do_api_mapping |
| 652 |
|
| 653 |
=cut |
| 654 |
|
| 655 |
sub _do_api_mapping { |
| 656 |
my ($self, $json_object) = @_; |
| 657 |
# Rename attributes if there's a mapping |
| 658 |
if ( $self->can('to_api_mapping') ) { |
| 659 |
foreach my $column ( keys %{ $self->to_api_mapping } ) { |
| 660 |
my $mapped_column = $self->to_api_mapping->{$column}; |
| 661 |
if ( exists $json_object->{$column} |
| 662 |
&& defined $mapped_column ) |
| 663 |
{ |
| 664 |
# key != undef |
| 665 |
$json_object->{$mapped_column} = delete $json_object->{$column}; |
| 666 |
} |
| 667 |
elsif ( exists $json_object->{$column} |
| 668 |
&& !defined $mapped_column ) |
| 669 |
{ |
| 670 |
# key == undef |
| 671 |
delete $json_object->{$column}; |
| 672 |
} |
| 673 |
} |
| 674 |
} |
| 675 |
return $json_object; |
649 |
return $json_object; |
| 676 |
} |
650 |
} |
| 677 |
|
651 |
|
| 678 |
- |
|
|