tainacan csv importer - fix tests and start metadata creation

This commit is contained in:
Eduardo Humberto 2018-10-22 21:48:43 -03:00 committed by Mateus Machado Luna
parent 321a40aa56
commit e06eef44ab
3 changed files with 70 additions and 3 deletions

View File

@ -146,7 +146,7 @@ class CSV extends Importer {
}
foreach ( $collection_definition['mapping'] as $metadatum_id => $header) {
$metadatum = new \Tainacan\Entities\Metadatum($metadatum_id);
foreach ( $headers as $indexRaw => $headerRaw ) {
if( $headerRaw === $header ){
@ -159,6 +159,12 @@ class CSV extends Importer {
$valueToInsert = $this->handle_encoding( $values[ $column ] );
if( !is_numeric($metadatum_id) ){
$metadatum = $this->create_metadata( $metadatum_id, $collection_definition['id']);
} else {
$metadatum = new \Tainacan\Entities\Metadatum($metadatum_id);
}
$processedItem[ $header ] = ( $metadatum->is_multiple() ) ?
explode( $this->get_option('multivalued_delimiter'), $valueToInsert) : $valueToInsert;
}

View File

@ -900,4 +900,65 @@ abstract class Importer {
}
/**
* @param $metadata_description
* @param $collection_id
* @return bool
* @throws \Exception
*/
protected function create_metadata( $metadata_description, $collection_id){
$taxonomy_repo = \Repositories\Taxonomy::get_instance();
$metadata_repo = \Repositories\Metadata::get_instance();
$properties = array_filter( explode('|', $metadata_description) );
if( !$properties || count($properties) < 2 ){
return false;
}
$name = $properties[0];
$type = $properties[1];
$newMetadatum = new Entities\Metadatum();
$newMetadatum->set_name($name);
$type = ucfirst($type);
$newMetadatum->set_metadata_type('Tainacan\Metadata_Types\\'.$type);
$newMetadatum->set_collection_id( (isset($collection_id)) ? $collection_id : 'default');
$newMetadatum->set_status('publish');
if( strcmp($type, "Taxonomy") === 0 ){
$taxonomy = new Entities\Taxonomy();
$taxonomy->set_name($name);
$taxonomy->set_allow_insert('yes');
if($taxonomy->validate()){
$inserted_tax = $taxonomy_repo->insert( $taxonomy );
$newMetadatum->set_metadata_type_options(['taxonomy_id' => $inserted_tax->get_id()]);
}
}
/*Properties of metadatum*/
if( isset($properties[2]) && $properties[2] === 'required'){
$newMetadatum->set_required(true);
}
if( isset($properties[3]) && $properties[3] === 'multiple' ){
$newMetadatum->set_multiple('yes');
}
if($newMetadatum->validate()){
$inserted_metadata = $metadata_repo->insert( $newMetadatum );
$this->add_log('Metadata created: ' . $inserted_metadata->get_name());
return $inserted_metadata;
} else{
$this->add_log('Error creating metadata ' . $name . ' in collection ' . $collection_id);
$this->add_log($newMetadatum->get_errors());
return false;
}
}
}

View File

@ -381,7 +381,7 @@ class ImporterTests extends TAINACAN_UnitTestCase {
$this->assertEquals(false, $_SESSION['tainacan_importer'][$id]->run(), '5 items and return false because its finished');
$this->assertEquals(false, $_SESSION['tainacan_importer'][$id]->run(), 'if call run again after finish, do nothing');
$items = $Tainacan_Items->fetch( ['orderby' => 'date'], $collection, 'OBJECT' );
$items = $Tainacan_Items->fetch( ['order'=> 'ASC','orderby' => 'date'], $collection, 'OBJECT' );
foreach ( $items as $index => $item ) {
$singleItemMetadata = new Entities\Item_Metadata_Entity( $item, $metadata_taxonomy );
@ -525,7 +525,7 @@ class ImporterTests extends TAINACAN_UnitTestCase {
continue;
}
$items = $Tainacan_Items->fetch( ['order'=> 'DESC', 'orderby' => 'ID'], $collection, 'OBJECT' );
$items = $Tainacan_Items->fetch( ['order'=> 'ASC', 'orderby' => 'ID'], $collection, 'OBJECT' );
$this->assertEquals( $_SESSION['tainacan_importer'][$id]->get_source_number_of_items(), count( $items ) );