From 0333af56f76a945a444fc48085a84f9d923ec980 Mon Sep 17 00:00:00 2001 From: vnmedeiros Date: Wed, 18 Mar 2020 14:56:28 -0300 Subject: [PATCH] add order in `children_objects` of metadata #17 --- .../class-tainacan-rest-items-controller.php | 4 ---- .../class-tainacan-item-metadata-entity.php | 6 +++--- .../compound/class-tainacan-compound.php | 13 +++++++++++-- 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/src/classes/api/endpoints/class-tainacan-rest-items-controller.php b/src/classes/api/endpoints/class-tainacan-rest-items-controller.php index 676edbdc4..ccc5adcb4 100644 --- a/src/classes/api/endpoints/class-tainacan-rest-items-controller.php +++ b/src/classes/api/endpoints/class-tainacan-rest-items-controller.php @@ -255,11 +255,7 @@ class REST_Items_Controller extends REST_Controller { } - // \error_log("DEBUG-1:"); - // \error_log(\json_encode($item_arr)); $item_arr = apply_filters('tainacan-api-items-prepare-for-response', $item_arr, $item, $request); - // \error_log("DEBUG-2:"); - // \error_log(\json_encode($item_arr)); return $item_arr; } diff --git a/src/classes/entities/class-tainacan-item-metadata-entity.php b/src/classes/entities/class-tainacan-item-metadata-entity.php index 6038cbfd9..f5a284b7d 100644 --- a/src/classes/entities/class-tainacan-item-metadata-entity.php +++ b/src/classes/entities/class-tainacan-item-metadata-entity.php @@ -186,12 +186,12 @@ class Item_Metadata_Entity extends Entity { if( is_array($v) ) { $compounds = []; foreach ($v as $itemMetadata) { - if ( $itemMetadata instanceof ItemMetadataEntity ) { + if ( $itemMetadata instanceof Item_Metadata_Entity ) { $compounds[] = $itemMetadata->_toArray(); } } $return[] = $compounds; - } else if ( $v instanceof Term || $v instanceof ItemMetadataEntity ) { + } else if ( $v instanceof Term || $v instanceof Item_Metadata_Entity ) { $return[] = $v->_toArray(); } else { $return[] = $v; @@ -236,7 +236,7 @@ class Item_Metadata_Entity extends Entity { } ksort( $compounds ); $return = array_merge($compounds, $compounds_not_ordinate); - } else if ( $value instanceof Term || $value instanceof ItemMetadataEntity ) { + } else if ( $value instanceof Term || $value instanceof Item_Metadata_Entity ) { $return = $value->_toArray(); } else { $return = $value; diff --git a/src/views/admin/components/metadata-types/compound/class-tainacan-compound.php b/src/views/admin/components/metadata-types/compound/class-tainacan-compound.php index 56957aa3f..c2757a085 100644 --- a/src/views/admin/components/metadata-types/compound/class-tainacan-compound.php +++ b/src/views/admin/components/metadata-types/compound/class-tainacan-compound.php @@ -90,6 +90,7 @@ class Compound extends Metadata_Type { $options = parent::get_options(); $options['children_order'] = isset($options['children_order']) ? $options['children_order'] : []; $options['children_objects'] = []; + $children_not_ordinate = []; if( isset( $options['parent'] ) ) { $childrens = $Tainacan_Metadata->fetch( ['parent' => $options['parent'] ], "OBJECT" ); @@ -100,9 +101,17 @@ class Compound extends Metadata_Type { ob_start(); $child->get_metadata_type_object()->form(); $child = ob_get_clean(); - $item_arr['edit_form'] = $form; - $options['children_objects'][] = $item_arr; + $item_arr['edit_form'] = $form; + + $index = array_search( $item_arr['id'], array_column( $options['children_order'], 'id' ) ); + if ( $index !== false ) { + $options['children_objects'][$index] = $item_arr; + } else { + $children_not_ordinate['children_objects'][] = $item_arr; + } } + ksort( $options['children_objects'] ); + $options['children_objects'] = array_merge($options['children_objects'], $children_not_ordinate); } return $options; }