Field type object is now returned with item metadata

This commit is contained in:
weryques 2018-02-15 11:27:45 -02:00
parent 30c51901f9
commit 01a7c41d71
3 changed files with 23 additions and 55 deletions

View File

@ -85,16 +85,7 @@ class TAINACAN_REST_Item_Metadata_Controller extends TAINACAN_REST_Controller {
* @return array|WP_Error|WP_REST_Response
*/
public function prepare_item_for_response( $item, $request ) {
$field_as = [];
foreach ( $item as $item_metadata ) {
$field_as[] = $item_metadata->__toArray();
//$field_as['field']['field_type_object'] = $item_metadata->get_field()->get_field_type_object()->__toArray();
}
return $field_as;
return $item->__toArray();
}
/**
@ -105,11 +96,16 @@ class TAINACAN_REST_Item_Metadata_Controller extends TAINACAN_REST_Controller {
public function get_items( $request ) {
$item_id = $request['item_id'];
$item = new Entities\Item($item_id);
$item = $this->item_repository->fetch($item_id);
$item_metadata = $item->get_fields();
$items_metadata = $item->get_fields();
$prepared_item = $this->prepare_item_for_response($item_metadata, $request);
$prepared_item = [];
foreach ($items_metadata as $item_metadata){
$index = array_push($prepared_item, $this->prepare_item_for_response($item_metadata, $request));
$prepared_item[$index-1]['field']['field_type_object'] = $item_metadata->get_field()->get_field_type_object()->__toArray();
}
return new WP_REST_Response($prepared_item, 200);
}
@ -196,7 +192,7 @@ class TAINACAN_REST_Item_Metadata_Controller extends TAINACAN_REST_Controller {
$item_metadata->set_value( $value );
if ( $item_metadata->validate() ) {
$field_updated = $this->item_metadata_repository->update( $item_metadata );
$field_updated = $this->item_metadata_repository->insert( $item_metadata );
return new WP_REST_Response( $field_updated->__toArray(), 200 );
} else {

View File

@ -17,7 +17,7 @@ class Item_Metadata extends Repository {
if ($field_type->core) {
$this->save_core_field_value($item_metadata);
} else {
add_post_meta($item_metadata->item->get_id(), $item_metadata->field->get_id(), wp_slash( $item_metadata->get_value() ) );
update_post_meta($item_metadata->item->get_id(), $item_metadata->field->get_id(), wp_slash( $item_metadata->get_value() ) );
}
} else {
@ -38,40 +38,6 @@ class Item_Metadata extends Repository {
return new Entities\Item_Metadata_Entity($item_metadata->get_item(), $item_metadata->get_field());
}
public function update($item_metadata, $new_values = null){
$unique = !$item_metadata->is_multiple();
if ($unique) {
$field_type = $item_metadata->get_field()->get_field_type_object();
if ($field_type->core) {
$this->save_core_field_value($item_metadata);
} else {
update_post_meta($item_metadata->item->get_id(), $item_metadata->field->get_id(), wp_slash( $item_metadata->get_value() ) );
}
} else {
delete_post_meta($item_metadata->item->get_id(), $item_metadata->field->get_id());
if (is_array($item_metadata->get_value())){
$values = $item_metadata->get_value();
foreach ($values as $value){
if(array_key_exists('prev', $value) && !empty($value['prev'])) {
update_post_meta( $item_metadata->item->get_id(), $item_metadata->field->get_id(), wp_slash( $value['new'] ), wp_slash($value['prev']) );
} else {
add_post_meta($item_metadata->item->get_id(), $item_metadata->field->get_id(), wp_slash( $value['new'] ));
}
}
}
}
do_action('tainacan-update', $item_metadata);
do_action('tainacan-update-Item_Metadata_Entity', $item_metadata);
$item_metadata = new Entities\Item_Metadata_Entity($item_metadata->get_item(), $item_metadata->get_field());
return $item_metadata;
}
/**
* Delete Item Field
*
@ -167,4 +133,10 @@ class Item_Metadata extends Repository {
public function get_map() { return []; }
public function get_default_properties($map) { return []; }
/**
* @param $object
*
* @return mixed
*/
public function update( $object, $new_values = null ) {}
}

View File

@ -156,8 +156,8 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_UnitApiTestCase {
$meta_values = json_encode(
array(
'values' => array(
array('new' => '19/01/2018', 'prev' => ''),
array('new' => '19/02/2018', 'prev' => '')
'19/01/2018',
'19/02/2018',
)
)
);