Tainacan csv importer - add hierarchical terms in taxonomy metadata (ref. #138)

This commit is contained in:
eduardohumberto 2018-10-16 21:47:44 -03:00
parent 3582700e75
commit b75109315d
2 changed files with 50 additions and 11 deletions

View File

@ -602,7 +602,7 @@ class CSV extends Importer {
$Tainacan_Metadata = \Tainacan\Repositories\Metadata::get_instance();
$Tainacan_Item_Metadata = \Tainacan\Repositories\Item_Metadata::get_instance();
$Tainacan_Items = \Tainacan\Repositories\Items::get_instance();
$Tainacan_Items = \Tainacan\Repositories\Items::get_instance();
$Tainacan_Items->disable_logs();
$Tainacan_Metadata->disable_logs();
@ -620,10 +620,9 @@ class CSV extends Importer {
$singleItemMetadata = new Entities\Item_Metadata_Entity( $item, $metadatum); // *empty item will be replaced by inserted in the next foreach
if( $metadatum->get_metadata_type() == 'Tainacan\Metadata_Types\Taxonomy' ){
// TODO: explode hierarchy values
$singleItemMetadata->set_value( $values );
$ids = $this->insert_hierarchy( $metadatum, $values );
$singleItemMetadata->set_value( $ids );
} else {
$singleItemMetadata->set_value( $values );
}
@ -686,5 +685,44 @@ class CSV extends Importer {
return false;
}
}
}
/**
* @param $metadatum the metadata
* @param $values the categories names
*
* @return array empty with no category or array with IDs
*/
private function insert_hierarchy( $metadatum, $values ){
$Tainacan_Terms = \Tainacan\Repositories\Terms::get_instance();
$taxonomy = new Entities\Taxonomy( $metadatum->get_metadata_type_options()['taxonomy_id']);
$exploded_values = explode(">>",$values);
if( is_array($exploded_values) ){
$parent = 0;
foreach ( $exploded_values as $key => $value) {
$value = trim($value);
$exists = term_exists( $value ,$taxonomy->get_db_identifier(), $parent );
if( 0 !== $exists && null !== $exists && isset($exists['term_id']) ){
$exists = new Entities\Term($exists['term_id']);
$parent = $term->get_id();
} else {
$term = new Entities\Term();
$term->set_name( $value );
$term->set_parent( $parent );
$term->set_taxonomy( $taxonomy->get_db_identifier() );
$term = $Tainacan_Terms->insert( $term );
$parent = $term->get_id();
}
}
return $parent !== 0 ? $parent : false;
} else {
return false;
}
}
}

View File

@ -2,6 +2,7 @@
namespace Tainacan\Tests;
use Tainacan\Importer;
use Tainacan\Entities;
/**
* Class Importer
*
@ -261,7 +262,7 @@ class ImporterTests extends TAINACAN_UnitTestCase {
// Sample data
$data = array(
array('Data 11', 'Data 12', 'Data 13||TESTE', 'Data 14', 'Data 15'),
array('Data 11', 'Data 12', 'Data 13||TESTE', 'Data 14', 'Data 15>>DATA 151'),
array('Data 21', 'Data 22', 'this
is
having
@ -269,7 +270,7 @@ class ImporterTests extends TAINACAN_UnitTestCase {
lines', 'Data 24', 'Data 25'),
array('Data 31', 'Data 32', utf8_decode( 'Data 33||Rééço' ), 'Data 34', 'Data 35'),
array('Data 41', 'Data 42', 'Data 43||limbbo', 'Data 44', 'Data 45'),
array('Data 51', 'Data 52', 'Data 53', 'Data 54', 'Data 55')
array('Data 51', 'Data 52', 'Data 53', 'Data 54', 'Data 55>>DATA551')
);
// save each row of the data
@ -338,10 +339,10 @@ class ImporterTests extends TAINACAN_UnitTestCase {
true
);
$this->tainacan_entity_factory->create_entity(
$metadata_taxonomy =$this->tainacan_entity_factory->create_entity(
'metadatum',
array(
'name' => 'metadatum son',
'name' => 'taxonomia',
'collection_id' => $collection->get_id(),
'metadata_type_options' => ['taxonomy_id' => $taxonomy->get_id(), 'allow_new_terms' => true ],
'metadata_type' => 'Tainacan\Metadata_Types\Taxonomy',
@ -364,7 +365,7 @@ class ImporterTests extends TAINACAN_UnitTestCase {
foreach ( $metadata as $index => $metadatum ){
$map[$metadatum->get_id()] = $headers[$index];
}
$collection_definition['mapping'] = $map;
// add the collection