From 539a3721e46cbde4b8abbc5e56d802624e50e01a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rodrigo=20Guimara=CC=83es?= Date: Wed, 23 Dec 2020 17:56:08 -0300 Subject: [PATCH] Fixes setting value for multiple config --- compile-sass.sh | 2 +- ...ss-tainacan-rest-item-metadata-controller.php | 15 ++++++++------- .../class-tainacan-item-metadata-entity.php | 16 +++++----------- 3 files changed, 14 insertions(+), 19 deletions(-) diff --git a/compile-sass.sh b/compile-sass.sh index cdfdc2787..36a3e9dcb 100644 --- a/compile-sass.sh +++ b/compile-sass.sh @@ -2,7 +2,7 @@ # Executa o comando 'sass' para verificar se existe (veja http://stackoverflow.com/a/677212/329911) command -v sass >/dev/null 2>&1 || { - echo >&2 "SASS parece não está disponivel."; + echo >&2 "Sass parece não estar disponivel."; exit 1; } diff --git a/src/classes/api/endpoints/class-tainacan-rest-item-metadata-controller.php b/src/classes/api/endpoints/class-tainacan-rest-item-metadata-controller.php index b8d467d16..6aea153fc 100644 --- a/src/classes/api/endpoints/class-tainacan-rest-item-metadata-controller.php +++ b/src/classes/api/endpoints/class-tainacan-rest-item-metadata-controller.php @@ -188,13 +188,9 @@ class REST_Item_Metadata_Controller extends REST_Controller { $body = json_decode( $request->get_body(), true ); if ($body) { - $item_id = $request['item_id']; - $metadatum_id = $request['metadatum_id']; - $value = $body['values']; - if (is_array($value)) { - $value = implode(' ', $value); - } - $value = $this->filter_value($value); + $item_id = $request['item_id']; + $value = $body['values']; + $metadatum_id = $request['metadatum_id']; $parent_meta_id = isset( $body['parent_meta_id'] ) && $body['parent_meta_id'] > 0 ? $body['parent_meta_id'] : null; $item = $this->item_repository->fetch($item_id); @@ -202,6 +198,11 @@ class REST_Item_Metadata_Controller extends REST_Controller { $item_metadata = new Entities\Item_Metadata_Entity( $item, $metadatum, null, $parent_meta_id); + if (is_array($value) && !$item_metadata->is_multiple()) { + $value = implode(' ', $value); + } + $value = $this->filter_value($value); + $item_metadata->set_value($value); if ($item_metadata->validate()) { diff --git a/src/classes/entities/class-tainacan-item-metadata-entity.php b/src/classes/entities/class-tainacan-item-metadata-entity.php index 85ff9b2f2..374af764d 100644 --- a/src/classes/entities/class-tainacan-item-metadata-entity.php +++ b/src/classes/entities/class-tainacan-item-metadata-entity.php @@ -199,10 +199,9 @@ class Item_Metadata_Entity extends Entity { public function get_value_as_array() { $value = $this->get_value(); $primitive_type = $this->get_metadatum()->get_metadata_type_object()->get_primitive_type(); - - if ( $this->is_multiple() ) { - $return = []; - + $return = []; + + if ($this->is_multiple()) { foreach ($value as $v) { if( is_array($v) ) { $options = $this->get_metadatum()->get_metadata_type_object()->get_options(); @@ -227,12 +226,8 @@ class Item_Metadata_Entity extends Entity { $return[] = $v; } } - } else { - - $return = ''; - - if( $primitive_type === 'compound' ) { + if ($primitive_type === 'compound') { $compounds = []; $compounds_not_ordinate = []; $options = $this->get_metadatum()->get_metadata_type_object()->get_options(); @@ -274,7 +269,6 @@ class Item_Metadata_Entity extends Entity { } return $return; - } /** @@ -285,7 +279,7 @@ class Item_Metadata_Entity extends Entity { * * @return array the representation of this object as an array */ - public function _toArray( $formatted_values = true, $cascade = false ){ + public function _toArray( $formatted_values = true, $cascade = false ){ $as_array = []; $as_array['value'] = $this->get_value_as_array();