Merge branch 'develop' of https://github.com/tainacan/tainacan into develop

This commit is contained in:
vnmedeiros 2019-07-11 10:24:24 -03:00
commit 2050a63d1f
4 changed files with 26 additions and 16 deletions

View File

@ -96,7 +96,7 @@ class REST_Item_Metadata_Controller extends REST_Controller {
* @return array|\WP_Error|\WP_REST_Response
*/
public function prepare_item_for_response( $item, $request ) {
$item_arr = $item->_toArray();
$item_arr = $item->_toArray(true, true);
if($request['context'] === 'edit'){
$item_arr['current_user_can_edit'] = $item->can_edit();
@ -122,7 +122,7 @@ class REST_Item_Metadata_Controller extends REST_Controller {
foreach ($items_metadata as $item_metadata){
$index = array_push($prepared_item, $this->prepare_item_for_response($item_metadata, $request));
$prepared_item[$index-1]['metadatum']['metadata_type_object'] = $this->prepare_item_for_response( $item_metadata->get_metadatum()->get_metadata_type_object(), $request);
$prepared_item[$index-1]['metadatum']['metadata_type_object'] = $item_metadata->get_metadatum()->get_metadata_type_object()->_toArray();
}
return new \WP_REST_Response(apply_filters('tainacan-rest-response', $prepared_item, $request), 200);
@ -147,7 +147,7 @@ class REST_Item_Metadata_Controller extends REST_Controller {
$metadatum = $item_metadata->get_metadatum();
if($metadatum->get_id() == $metadatum_id) {
$prepared_item = $this->prepare_item_for_response($item_metadata, $request);
$prepared_item['metadatum']['metadata_type_object'] = $this->prepare_item_for_response( $metadatum->get_metadata_type_object(), $request);
$prepared_item['metadatum']['metadata_type_object'] = $metadatum->get_metadata_type_object()->_toArray();
}
}
@ -203,10 +203,10 @@ class REST_Item_Metadata_Controller extends REST_Controller {
if ( $item_metadata->validate() ) {
if($item->can_edit()) {
$metadatum_updated = $this->item_metadata_repository->update( $item_metadata );
$updated_item_metadata = $this->item_metadata_repository->update( $item_metadata );
$prepared_item = $this->prepare_item_for_response($metadatum_updated, $request);
$prepared_item['metadatum']['metadata_type_object'] = $this->prepare_item_for_response($metadatum_updated->get_metadatum()->get_metadata_type_object(), $request);
$prepared_item = $this->prepare_item_for_response($updated_item_metadata, $request);
$prepared_item['metadatum']['metadata_type_object'] = $updated_item_metadata->get_metadatum()->get_metadata_type_object()->_toArray();
}
elseif($metadatum->get_accept_suggestion()) {
$log = $this->item_metadata_repository->suggest( $item_metadata );

View File

@ -117,13 +117,14 @@ class REST_Items_Controller extends REST_Controller {
$item_metadata_array = $me->_toArray();
$item_array['metadata'][ $slug ]['name'] = $metadatum->get_name();
$item_array['metadata'][ $slug ]['id'] = $metadatum->get_id();
if($metadatum->get_metadata_type_object()->get_primitive_type() === 'date') {
$item_array['metadata'][ $slug ]['date_i18n'] = $item_metadata_array['date_i18n'];
}
$item_array['metadata'][ $slug ]['value'] = $item_metadata_array['value'];
$item_array['metadata'][ $slug ]['value_as_html'] = $item_metadata_array['value_as_html'];
$item_array['metadata'][ $slug ]['value_as_string'] = $item_metadata_array['value_as_string'];
$item_array['metadata'][ $slug ]['semantic_uri'] = $item_metadata_array['metadatum']['semantic_uri'];
$item_array['metadata'][ $slug ]['semantic_uri'] = $metadatum->get_semantic_uri();
$item_array['metadata'][ $slug ]['multiple'] = $metadatum->get_multiple();
$item_array['metadata'][ $slug ]['mapping'] = $metadatum->get_exposer_mapping();

View File

@ -208,21 +208,31 @@ class Item_Metadata_Entity extends Entity {
/**
* Convert the object to an Array
*
* @param bool $formatted_values Whether to add or not values formatted as html and string to the response
* @param bool $cascade Whether to add or not Item and Metadatum Entities as arrays to the response
*
* @return array the representation of this object as an array
*/
public function _toArray(){
public function _toArray( $formatted_values = true, $cascade = false ){
$as_array = [];
$as_array['value'] = $this->get_value_as_array();
$as_array['value_as_html'] = $this->get_value_as_html();
$as_array['value_as_string'] = $this->get_value_as_string();
if ( $formatted_values ) {
$as_array['value_as_html'] = $this->get_value_as_html();
$as_array['value_as_string'] = $this->get_value_as_string();
if($this->get_metadatum()->get_metadata_type_object()->get_primitive_type() === 'date'){
$as_array['date_i18n'] = $this->get_date_i18n($this->get_value_as_string());
if($this->get_metadatum()->get_metadata_type_object()->get_primitive_type() === 'date'){
$as_array['date_i18n'] = $this->get_date_i18n($this->get_value_as_string());
}
}
$as_array['item'] = $this->get_item()->_toArray();
$as_array['metadatum'] = $this->get_metadatum()->_toArray();
if ( $cascade ) {
$as_array['item'] = $this->get_item()->_toArray();
$as_array['metadatum'] = $this->get_metadatum()->_toArray();
}
return apply_filters('tainacan-item-metadata-to-array', $as_array, $this);
}

View File

@ -52,7 +52,6 @@ class Item extends Entity {
public function _toArray() {
$array_item = parent::_toArray();
$array_item['thumbnail'] = $this->get_thumbnail();
$array_item['_thumbnail_id'] = $this->get__thumbnail_id();
$array_item['author_name'] = $this->get_author_name();
$array_item['url'] = get_permalink( $this->get_id() );