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

This commit is contained in:
mateuswetah 2020-09-22 11:45:29 -03:00
commit 946bc94a99
2 changed files with 286 additions and 42 deletions

View File

@ -841,7 +841,32 @@ class REST_Items_Controller extends REST_Controller {
}
public function submission_item ( $request ) {
private function submission_item_metadada ( \Tainacan\Entities\Item_Metadata_Entity &$item_metadata) {
$collection_id = $item_metadata->get_item()->get_collection_id();
$item = $item_metadata->get_item();
$collection = $this->collections_repository->fetch($collection_id);
if ( $item_metadata->validate() ) {
if($item->can_edit() || $collection->get_submission_anonymous_user()) {
return $this->item_metadata->update( $item_metadata );
}
elseif($metadatum->get_accept_suggestion()) {
return $this->item_metadata->suggest( $item_metadata );
}
else {
return new \WP_REST_Response( [
'error_message' => __( 'The metadatum does not accept suggestions', 'tainacan' ),
], 400 );
}
} else {
return new \WP_REST_Response( [
'error_message' => __( 'Please verify, invalid value(s)', 'tainacan' ),
'errors' => $item_metadata->get_errors(),
'item_metadata' => $this->prepare_item_for_response($item_metadata, $request),
], 400 );
}
}
public function submission_item ( $request) {
$collection_id = $request['collection_id'];
$item = json_decode($request->get_body(), true);
$metadata = $item['metadata'];
@ -865,33 +890,47 @@ class REST_Items_Controller extends REST_Controller {
foreach ( $metadata as $m ) {
$value = $m['value'];
$metadatum_id = $m['metadatum_id'];
$parent_meta_id = null;
$metadatum = $this->metadatum_repository->fetch( $metadatum_id );
$item_metadata = new Entities\Item_Metadata_Entity($item, $metadatum, null, $parent_meta_id);
$item_metadata = new Entities\Item_Metadata_Entity($item, $metadatum);
if ($item_metadata->is_multiple()) {
$item_metadata->set_value( is_array($value) ? $value : [$value] );
} else {
$item_metadata->set_value( is_array($value) ? implode(' ', $value) : $value);
}
if ( $item_metadata->validate() ) {
if($item->can_edit() || $collection->get_submission_anonymous_user()) {
$this->item_metadata->update( $item_metadata );
}
elseif($metadatum->get_accept_suggestion()) {
$this->item_metadata->suggest( $item_metadata );
}
else {
return new \WP_REST_Response( [
'error_message' => __( 'The metadatum does not accept suggestions', 'tainacan' ),
], 400 );
if($metadatum->get_metadata_type() == 'Tainacan\Metadata_Types\Compound') {
if ($item_metadata->is_multiple()) {
foreach($value as $row) {
$parent_meta_id = null;
foreach($row as $child) {
$metadatum_child = $this->metadatum_repository->fetch( $child['metadatum_id'] );
$item_metadata_child = new Entities\Item_Metadata_Entity($item, $metadatum_child, null, $parent_meta_id);
$item_metadata_child->set_value(is_array($child['value']) ? implode(' ', $child['value']) : $child['value']);
$item_metadata_child = $this->submission_item_metadada($item_metadata_child);
if ($item_metadata_child instanceof \WP_REST_Response) {
return $item_metadata_child;
}
$parent_meta_id = $item_metadata_child->get_parent_meta_id();
}
}
} else {
$parent_meta_id = null;
foreach($value as $child) {
$metadatum_child = $this->metadatum_repository->fetch( $child['metadatum_id'] );
$item_metadata_child = new Entities\Item_Metadata_Entity($item, $metadatum_child, null, $parent_meta_id);
$item_metadata_child->set_value(is_array($child['value']) ? implode(' ', $child['value']) : $child['value']);
$item_metadata_child = $this->submission_item_metadada($item_metadata_child);
if ($item_metadata_child instanceof \WP_REST_Response) {
return $item_metadata_child;
}
$parent_meta_id = $item_metadata_child->get_parent_meta_id();
}
}
} else {
return new \WP_REST_Response( [
'error_message' => __( 'Please verify, invalid value(s)', 'tainacan' ),
'errors' => $item_metadata->get_errors(),
'item_metadata' => $this->prepare_item_for_response($item_metadata, $request),
], 400 );
if ($item_metadata->is_multiple()) {
$item_metadata->set_value( is_array($value) ? $value : [$value] );
} else {
$item_metadata->set_value( is_array($value) ? implode(' ', $value) : $value);
}
$item_metadata = $this->submission_item_metadada($item_metadata);
if ($item_metadata instanceof \WP_REST_Response) {
return $item_metadata;
}
}
}

View File

@ -40,29 +40,197 @@ class TAINACAN_REST_Items_Submission extends TAINACAN_UnitApiTestCase {
}
private function create_metadatum(&$collection) {
$tax = $this->tainacan_entity_factory->create_entity(
'taxonomy',
array(
'name' => 'tax_test',
'collections' => [$collection],
'status' => 'publish'
),
true
);
$this->tainacan_entity_factory->create_entity(
'term',
array(
'taxonomy' => $tax->get_db_identifier(),
'name' => 'term-1',
),
true
);
$this->tainacan_entity_factory->create_entity(
'term',
array(
'taxonomy' => $tax->get_db_identifier(),
'name' => 'term-2',
),
true
);
$metadatum_compound = $this->tainacan_entity_factory->create_entity(
'metadatum',
array(
'name' => 'metadatum_compound',
'status' => 'publish',
'collection' => $collection,
'metadata_type' => 'Tainacan\Metadata_Types\Compound',
),
true
);
$metadatum_child1 = $this->tainacan_entity_factory->create_entity(
'metadatum',
array(
'name' => 'metadatum_child1',
'status' => 'publish',
'collection' => $collection,
'parent' => $metadatum_compound->get_id(),
'metadata_type' => 'Tainacan\Metadata_Types\Text',
),
true
);
$metadatum_child2 = $this->tainacan_entity_factory->create_entity(
'metadatum',
array(
'name' => 'metadatum_child2',
'status' => 'publish',
'collection' => $collection,
'parent' => $metadatum_compound->get_id(),
'metadata_type' => 'Tainacan\Metadata_Types\Text',
),
true
);
$metadatum_compound_multiple = $this->tainacan_entity_factory->create_entity(
'metadatum',
array(
'name' => 'metadatum_compound_multiple',
'status' => 'publish',
'collection' => $collection,
'metadata_type' => 'Tainacan\Metadata_Types\Compound',
'multiple' => 'yes'
),
true
);
$metadatum_child1_multiple = $this->tainacan_entity_factory->create_entity(
'metadatum',
array(
'name' => 'metadatum_child1_multiple',
'status' => 'publish',
'collection' => $collection,
'parent' => $metadatum_compound_multiple->get_id(),
'metadata_type' => 'Tainacan\Metadata_Types\Text',
),
true
);
$metadatum_child2_multiple = $this->tainacan_entity_factory->create_entity(
'metadatum',
array(
'name' => 'metadatum_child2_multiple',
'status' => 'publish',
'collection' => $collection,
'parent' => $metadatum_compound_multiple->get_id(),
'metadata_type' => 'Tainacan\Metadata_Types\Text',
),
true
);
$metadatum = array(
'text' => $this->tainacan_entity_factory->create_entity(
'metadatum',
array(
'name' => 'text',
'status' => 'publish',
'collection' => $collection,
'metadata_type' => 'Tainacan\Metadata_Types\Text',
),
true
)->get_id(),
'metadatum',
array(
'name' => 'text',
'status' => 'publish',
'collection' => $collection,
'metadata_type' => 'Tainacan\Metadata_Types\Text',
),
true
)->get_id(),
'textarea' => $this->tainacan_entity_factory->create_entity(
'metadatum',
array(
'name' => 'textarea',
'status' => 'publish',
'collection' => $collection,
'metadata_type' => 'Tainacan\Metadata_Types\Textarea',
),
true
)->get_id(),
'numeric' => $this->tainacan_entity_factory->create_entity(
'metadatum',
array(
'name' => 'numeric',
'status' => 'publish',
'collection' => $collection,
'metadata_type' => 'Tainacan\Metadata_Types\Numeric',
),
true
)->get_id()
'metadatum',
array(
'name' => 'numeric',
'status' => 'publish',
'collection' => $collection,
'metadata_type' => 'Tainacan\Metadata_Types\Numeric',
),
true
)->get_id(),
'date' => $this->tainacan_entity_factory->create_entity(
'metadatum',
array(
'name' => 'date',
'status' => 'publish',
'collection' => $collection,
'metadata_type' => 'Tainacan\Metadata_Types\Date',
),
true
)->get_id(),
'selectbox' => $this->tainacan_entity_factory->create_entity(
'metadatum',
array(
'name' => 'selectbox',
'status' => 'publish',
'collection' => $collection,
'metadata_type' => 'Tainacan\Metadata_Types\Selectbox',
'metadata_type_options' => [
'options' => ['op1', 'op2', 'op3']
]
),
true
)->get_id(),
'taxonomy' => $this->tainacan_entity_factory->create_entity(
'metadatum',
array(
'name' => 'taxonomy',
'status' => 'publish',
'collection' => $collection,
'metadata_type' => 'Tainacan\Metadata_Types\Taxonomy',
'metadata_type_options' => [
'taxonomy_id' => $tax->get_id(),
'allow_new_terms' => 'no'
],
),
true
)->get_id(),
'compound' => [
'parent' => $metadatum_compound->get_id(),
'childrens' => [
$metadatum_child1->get_id(),
$metadatum_child2->get_id()
]
],
'compound_multiple' => [
'parent' => $metadatum_compound_multiple->get_id(),
'childrens' => [
$metadatum_child1_multiple->get_id(),
$metadatum_child2_multiple->get_id()
]
]
);
//relationship?
//user?
return $metadatum;
}
@ -81,9 +249,46 @@ class TAINACAN_REST_Items_Submission extends TAINACAN_UnitApiTestCase {
'metadatum_id' => $metadatums['text'],
'value' => 'Text submission'
],
[
'metadatum_id' => $metadatums['textarea'],
'value' => 'textarea submission'
],
[
'metadatum_id' => $metadatums['date'],
'value' => '2020-12-31'
],
[
'metadatum_id' => $metadatums['numeric'],
'value' => 10
],
[
'metadatum_id' => $metadatums['selectbox'],
'value' => 'op2'
],
[
'metadatum_id' => $metadatums['taxonomy'],
'value' => 'term-1'
],
[
'metadatum_id' => $metadatums['compound']['parent'],
'value' => [
['metadatum_id' => $metadatums['compound']['childrens'][0], 'value' => 'metadatum_child1'],
['metadatum_id' => $metadatums['compound']['childrens'][1], 'value' => 'metadatum_child2']
]
],
[
'metadatum_id' => $metadatums['compound_multiple']['parent'],
'value' => [
[
['metadatum_id' => $metadatums['compound_multiple']['childrens'][0], 'value' => 'metadatum_child1_multiple_row_1'],
['metadatum_id' => $metadatums['compound_multiple']['childrens'][1], 'value' => 'metadatum_child2_multiple_row_1']
],
[
['metadatum_id' => $metadatums['compound_multiple']['childrens'][0], 'value' => 'metadatum_child1_multiple_row_2'],
['metadatum_id' => $metadatums['compound_multiple']['childrens'][1], 'value' => 'metadatum_child2_multiple_row_2']
]
]
]
)
]);