Resolve conflitos na class-tainacan-item-metadata.

This commit is contained in:
mateuswetah 2018-02-15 11:30:08 -02:00
commit ec76b51217
4 changed files with 35 additions and 54 deletions

View File

@ -85,13 +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 $field ) {
$field_as[] = $field->__toArray();
}
return $field_as;
return $item->__toArray();
}
/**
@ -102,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);
}
@ -182,18 +181,18 @@ class TAINACAN_REST_Item_Metadata_Controller extends TAINACAN_REST_Controller {
if($body) {
$item_id = $request['item_id'];
$item_id = $request['item_id'];
$field_id = $request['metadata_id'];
$value = $body['values'];
$value = $body['values'];
$item = $this->item_repository->fetch( $item_id );
$item = $this->item_repository->fetch( $item_id );
$field = $this->field_repository->fetch( $field_id );
$item_metadata = new Entities\Item_Metadata_Entity( $item, $field );
$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

@ -82,5 +82,17 @@ abstract class Field_Type {
public function form(){
}
public function __toArray(){
$attributes = [];
$attributes['className'] = get_class($this);
$attributes['core'] = $this->core;
$attributes['component'] = $this->get_component();
$attributes['primitive_type'] = $this->get_primitive_type();
return $attributes;
}
}

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,42 +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);
$value = get_post_meta($item_metadata->item->get_id(), $item_metadata->field->get_id());
$item_metadata = new Entities\Item_Metadata_Entity($item_metadata->get_item(), $item_metadata->get_field());
$item_metadata->set_value($value);
return $item_metadata;
}
/**
* Delete Item Field
*
@ -168,5 +132,11 @@ 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',
)
)
);