feat: update metadata-section on metadatum edit #184

This commit is contained in:
vnmedeiros 2022-04-11 09:42:27 -03:00
parent 223059b179
commit dad16bdbd2
4 changed files with 96 additions and 7 deletions

View File

@ -24,7 +24,8 @@ class Metadatum extends Entity {
$mask,
$default_value,
$metadata_type,
$metadata_type_options;
$metadata_type_options,
$metadatum_section_id;
// Collection getter and setter declared here
use \Tainacan\Traits\Entity_Collection_Relation;
@ -252,6 +253,15 @@ class Metadatum extends Entity {
return $this->get_mapped_property('semantic_uri');
}
/**
* Return the metadatum_section_id
*
* @return string
*/
function get_metadatum_section_id(){
return $this->get_mapped_property('metadatum_section_id');
}
/**
* Set the metadatum name
*
@ -433,6 +443,17 @@ class Metadatum extends Entity {
$this->set_mapped_property('semantic_uri', $value);
}
/**
* Set metadatum section ID for the metadatum
*
* @param [string] $value
* @return void
*/
function set_metadatum_section_id( $value) {
return $this->set_mapped_property('metadatum_section_id', $value);
}
/**
* Transient property used to store the status of the metadatum for a particular collection
*

View File

@ -295,6 +295,7 @@ class Metadata_Section extends Repository {
public function add_metadatum($metadata_section_id, $metadatum_list) {
$metadata_section = $this->fetch($metadata_section_id);
if ($metadata_section) {
$list = $metadata_section->get_metadatum_list();
$metadatum_list = array_merge($list, $metadatum_list);
$metadata_section->set_metadatum_list($metadatum_list);
@ -302,6 +303,7 @@ class Metadata_Section extends Repository {
$metadata_section = $this->update($metadata_section);
return $metadata_section;
}
}
return false;
}

View File

@ -225,6 +225,12 @@ class Metadata extends Repository {
// yes or no. It cant be multiple if its collection_key
'default' => 'no'
],
'metadatum_section_id' => [
'map' => 'meta',
'title' => __( 'Metadatum section', 'tainacan' ),
'type' => ['integer', 'string', 'array'],
'description' => __( 'The metadatum section ID', 'tainacan' ),
],
] );
}
@ -651,6 +657,7 @@ class Metadata extends Repository {
$this->update_taxonomy_metadatum( $new_metadatum );
$this->update_metadata_type_index( $new_metadatum );
$this->update_metadatum_section( $new_metadatum );
return $new_metadatum;
}
@ -1585,6 +1592,7 @@ class Metadata extends Repository {
*/
public function delete( Entities\Entity $entity, $permanent = true ) {
$this->delete_taxonomy_metadatum($entity);
$this->update_metadatum_section($entity, true);
return parent::delete($entity, $permanent);
}
@ -1715,4 +1723,14 @@ class Metadata extends Repository {
return false;
}
public function update_metadatum_section( Entities\Metadatum $metadatum, $remove = false ) {
$metadatum_section_repository = Metadata_Section::get_instance();
if (!$remove) {
$metadatum_section_repository->add_metadatum($metadatum->get_metadatum_section_id(), [$metadatum->get_id()]);
} else {
$metadatum_section_repository->delete_metadatum($metadatum->get_metadatum_section_id(), [$metadatum->get_id()]);
}
}
}

View File

@ -89,6 +89,7 @@ class TAINACAN_REST_Metadata_Section_Controller extends TAINACAN_UnitApiTestCase
'name' => 'name-1',
'description' => 'description-1',
'collection' => $collection,
'status' => 'publish',
'metadata_type' => 'Tainacan\Metadata_Types\Text',
),
true
@ -100,6 +101,7 @@ class TAINACAN_REST_Metadata_Section_Controller extends TAINACAN_UnitApiTestCase
'name' => 'name-2',
'description' => 'description-2',
'collection' => $collection,
'status' => 'publish',
'metadata_type' => 'Tainacan\Metadata_Types\Text',
),
true
@ -111,6 +113,7 @@ class TAINACAN_REST_Metadata_Section_Controller extends TAINACAN_UnitApiTestCase
'name' => 'name-3',
'description' => 'description-3',
'collection' => $collection,
'status' => 'publish',
'metadata_type' => 'Tainacan\Metadata_Types\Text',
),
true
@ -122,6 +125,7 @@ class TAINACAN_REST_Metadata_Section_Controller extends TAINACAN_UnitApiTestCase
'name' => 'Section',
'description' => 'Section Description',
'collection' => $collection,
'status' => 'publish',
'metadatum_list' => [$metadatum_1->get_id(), $metadatum_1->get_id()]
),
true
@ -147,6 +151,50 @@ class TAINACAN_REST_Metadata_Section_Controller extends TAINACAN_UnitApiTestCase
$this->assertContains($metadatum_1->get_id(), $metadatum_section_added['metadatum_list']);
$this->assertContains($metadatum_2->get_id(), $metadatum_section_added['metadatum_list']);
$this->assertContains($metadatum_3->get_id(), $metadatum_section_added['metadatum_list']);
$metadatum = $this->tainacan_entity_factory->create_entity(
'metadatum',
array(
'name' => 'Data',
'description' => 'Descreve o dado do campo data.',
'collection' => $collection,
'status' => 'publish',
'metadata_type' => 'Tainacan\Metadata_Types\Text',
'multiple' => 'yes'
),
true
);
$values = json_encode([
'name' => 'Dia/Mês/Ano',
'description' => 'Continua descrevendo o dado do campo.',
'metadatum_section_id' => $metadatum_section->get_id()
]);
$request = new \WP_REST_Request(
'PATCH',
$this->namespace . '/collection/' . $collection->get_id() . '/metadata/' . $metadatum->get_id()
);
$request->set_body($values);
$response = $this->server->dispatch($request);
$data = $response->get_data();
$this->assertEquals($metadatum->get_id(), $data['id']);
$this->assertEquals('Dia/Mês/Ano', $data['name']);
$this->assertEquals($metadatum_section->get_id(), $data['metadatum_section_id']);
$request = new \WP_REST_Request(
'GET',
$this->namespace . '/collection/' . $collection->get_id() . '/metadata-sections/' . $metadatum_section->get_id() . '/metadatum'
);
$response = $this->server->dispatch($request);
$metadata_list = $response->get_data();
$this->assertEquals(4, count($metadata_list));
$this->assertNotNull(array_search($metadatum_1->get_id(), array_column($metadata_list, "id")));
$this->assertNotNull(array_search($metadatum_2->get_id(), array_column($metadata_list, "id")));
$this->assertNotNull(array_search($metadatum_3->get_id(), array_column($metadata_list, "id")));
$this->assertNotNull(array_search($metadatum->get_id(), array_column($metadata_list, "id")));
}
public function test_delete_metadata_metadatum_section() {