tainacan csv importer - fix tests and start metadata creation
This commit is contained in:
parent
321a40aa56
commit
e06eef44ab
|
@ -146,7 +146,7 @@ class CSV extends Importer {
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach ( $collection_definition['mapping'] as $metadatum_id => $header) {
|
foreach ( $collection_definition['mapping'] as $metadatum_id => $header) {
|
||||||
$metadatum = new \Tainacan\Entities\Metadatum($metadatum_id);
|
|
||||||
|
|
||||||
foreach ( $headers as $indexRaw => $headerRaw ) {
|
foreach ( $headers as $indexRaw => $headerRaw ) {
|
||||||
if( $headerRaw === $header ){
|
if( $headerRaw === $header ){
|
||||||
|
@ -159,6 +159,12 @@ class CSV extends Importer {
|
||||||
|
|
||||||
$valueToInsert = $this->handle_encoding( $values[ $column ] );
|
$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() ) ?
|
$processedItem[ $header ] = ( $metadatum->is_multiple() ) ?
|
||||||
explode( $this->get_option('multivalued_delimiter'), $valueToInsert) : $valueToInsert;
|
explode( $this->get_option('multivalued_delimiter'), $valueToInsert) : $valueToInsert;
|
||||||
}
|
}
|
||||||
|
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -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(), '5 items and return false because its finished');
|
||||||
$this->assertEquals(false, $_SESSION['tainacan_importer'][$id]->run(), 'if call run again after finish, do nothing');
|
$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 ) {
|
foreach ( $items as $index => $item ) {
|
||||||
$singleItemMetadata = new Entities\Item_Metadata_Entity( $item, $metadata_taxonomy );
|
$singleItemMetadata = new Entities\Item_Metadata_Entity( $item, $metadata_taxonomy );
|
||||||
|
@ -525,7 +525,7 @@ class ImporterTests extends TAINACAN_UnitTestCase {
|
||||||
continue;
|
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 ) );
|
$this->assertEquals( $_SESSION['tainacan_importer'][$id]->get_source_number_of_items(), count( $items ) );
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue