tainacan csv importer - starts handle hierarchy in taxonomy metadata (ref. #138)

This commit is contained in:
eduardohumberto 2018-10-15 22:47:20 -03:00
parent f2b73f4eb1
commit e39cae846d
2 changed files with 132 additions and 1 deletions

View File

@ -2,6 +2,7 @@
namespace Tainacan\Importer; namespace Tainacan\Importer;
use Tainacan; use Tainacan;
use Tainacan\Entities;
class CSV extends Importer { class CSV extends Importer {
@ -577,4 +578,113 @@ class CSV extends Importer {
$this->add_transient( 'item_action',$this->get_option('repeated_item') ); $this->add_transient( 'item_action',$this->get_option('repeated_item') );
} }
} }
/**
* insert processed item from source to Tainacan
*
* @param array $processed_item Associative array with metadatum source's as index with
* its value or values
* @param integet $collection_index The index in the $this->collections array of the collection the item is beeing inserted into
*
* @return Tainacan\Entities\Item Item inserted
*/
public function insert( $processed_item, $collection_index ) {
remove_action( 'post_updated', 'wp_save_post_revision' );
$collections = $this->get_collections();
$collection_definition = isset($collections[$collection_index]) ? $collections[$collection_index] : false;
if ( !$collection_definition || !is_array($collection_definition) || !isset($collection_definition['id']) || !isset($collection_definition['mapping']) ) {
$this->add_error_log('Collection misconfigured');
return false;
}
$collection = \Tainacan\Repositories\Collections::get_instance()->fetch($collection_definition['id']);
$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->disable_logs();
$Tainacan_Metadata->disable_logs();
$Tainacan_Item_Metadata->disable_logs();
$item = new Entities\Item( ( $this->get_transient('item_id') ) ? $this->get_transient('item_id') : 0 );
$itemMetadataArray = [];
if( is_array( $processed_item ) ){
foreach ( $processed_item as $metadatum_source => $values ){
$tainacan_metadatum_id = array_search( $metadatum_source, $collection_definition['mapping'] );
$metadatum = $Tainacan_Metadata->fetch( $tainacan_metadatum_id );
if( $metadatum instanceof Entities\Metadatum ){
$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 );
} else {
$singleItemMetadata->set_value( $values );
}
$itemMetadataArray[] = $singleItemMetadata;
} else {
$this->add_error_log('Metadata ' . $metadatum_source . ' not found');
}
}
}
if( !empty( $itemMetadataArray ) && $collection instanceof Entities\Collection ){
$item->set_collection( $collection );
if( $item->validate() ){
$insertedItem = $Tainacan_Items->insert( $item );
} else {
$this->add_error_log( 'Error inserting item' );
$this->add_error_log( $item->get_errors() );
return false;
}
foreach ( $itemMetadataArray as $itemMetadata ) {
$itemMetadata->set_item( $insertedItem ); // *I told you
if( $itemMetadata->validate() ){
$result = $Tainacan_Item_Metadata->insert( $itemMetadata );
} else {
$this->add_error_log('Error saving value for ' . $itemMetadata->get_metadatum()->get_name());
$this->add_error_log($itemMetadata->get_errors());
continue;
}
//if( $result ){
// $values = ( is_array( $itemMetadata->get_value() ) ) ? implode( PHP_EOL, $itemMetadata->get_value() ) : $itemMetadata->get_value();
// $this->add_log( 'Item ' . $insertedItem->get_id() .
// ' has inserted the values: ' . $values . ' on metadata: ' . $itemMetadata->get_metadatum()->get_name() );
//} else {
// $this->add_error_log( 'Item ' . $insertedItem->get_id() . ' has an error' );
//}
}
$insertedItem->set_status('publish' );
if($insertedItem->validate()) {
$insertedItem = $Tainacan_Items->update( $insertedItem );
$this->after_inserted_item( $insertedItem, $collection_index );
} else {
$this->add_error_log( 'Error publishing Item' );
$this->add_error_log( $insertedItem->get_errors() );
return false;
}
return $insertedItem;
} else {
$this->add_error_log( 'Collection not set');
return false;
}
}
} }

View File

@ -330,6 +330,27 @@ class ImporterTests extends TAINACAN_UnitTestCase {
true true
); );
$taxonomy = $this->tainacan_entity_factory->create_entity(
'taxonomy',
array(
'name' => 'genero',
),
true
);
$this->tainacan_entity_factory->create_entity(
'metadatum',
array(
'name' => 'metadatum son',
'collection_id' => $collection->get_id(),
'metadata_type_options' => ['taxonomy_id' => $taxonomy->get_id(), 'allow_new_terms' => true ],
'metadata_type' => 'Tainacan\Metadata_Types\Taxonomy',
'status' => 'publish'
),
true
);
$collection_definition = [ $collection_definition = [
'id' => $collection->get_id(), 'id' => $collection->get_id(),
'total_items' => $_SESSION['tainacan_importer'][$id]->get_source_number_of_items(), 'total_items' => $_SESSION['tainacan_importer'][$id]->get_source_number_of_items(),