csv import - adapt process item method to skip special columns
This commit is contained in:
parent
6d41c1a90a
commit
ded79f7145
|
@ -35,14 +35,14 @@ class CSV extends Importer {
|
|||
$rawColumns = $file->fgetcsv( $this->get_option('delimiter') );
|
||||
|
||||
if( $rawColumns ){
|
||||
foreach( $rawColumns as $rawColumn ){
|
||||
foreach( $rawColumns as $index => $rawColumn ){
|
||||
|
||||
if( strpos($rawColumn,'special_') === 0 ){
|
||||
|
||||
if( $rawColumn === 'special_document' ){
|
||||
//TODO: save the index for column document
|
||||
$this->set_option('document_index', $index);
|
||||
} else if( $rawColumn === 'special_attachments' ){
|
||||
//TODO: save the index for column attachment
|
||||
$this->set_option('attachment_index', $index);
|
||||
}
|
||||
|
||||
} else {
|
||||
|
@ -56,13 +56,22 @@ class CSV extends Importer {
|
|||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* returns all header including special
|
||||
*/
|
||||
public function raw_source_metadata(){
|
||||
$file = new \SplFileObject( $this->tmp_file, 'r' );
|
||||
$file->seek(0);
|
||||
return $file->fgetcsv( $this->get_option('delimiter') );
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function process_item( $index, $collection_definition ){
|
||||
$processedItem = [];
|
||||
$headers = $this->get_source_metadata();
|
||||
$headers = $this->raw_source_metadata();
|
||||
|
||||
$this->add_log('Proccessing item index ' . $index . ' in collection ' . $collection_definition['id'] );
|
||||
// search the index in the file and get values
|
||||
|
@ -91,16 +100,22 @@ class CSV extends Importer {
|
|||
return false;
|
||||
}
|
||||
|
||||
$cont = 0;
|
||||
foreach ( $collection_definition['mapping'] as $metadatum_id => $header) {
|
||||
$metadatum = new \Tainacan\Entities\Metadatum($metadatum_id);
|
||||
|
||||
$valueToInsert = $this->handle_encoding( $values[ $cont ] );
|
||||
foreach ( $headers as $indexRaw => $headerRaw ) {
|
||||
if( $headerRaw === $header ){
|
||||
$index = $indexRaw;
|
||||
}
|
||||
}
|
||||
|
||||
if(!isset($index))
|
||||
continue;
|
||||
|
||||
$valueToInsert = $this->handle_encoding( $values[ $index ] );
|
||||
|
||||
$processedItem[ $header ] = ( $metadatum->is_multiple() ) ?
|
||||
explode( $this->get_option('multivalued_delimiter'), $valueToInsert) : $valueToInsert;
|
||||
|
||||
$cont++;
|
||||
}
|
||||
|
||||
$this->add_log('Success to proccess index: ' . $index );
|
||||
|
|
|
@ -420,5 +420,69 @@ class ImporterTests extends TAINACAN_UnitTestCase {
|
|||
// get metadata to mapping AVOIDING special fields
|
||||
$headers = $_SESSION['tainacan_importer'][$id]->get_source_metadata();
|
||||
$this->assertEquals( $headers[1], 'Column 3' );
|
||||
|
||||
$this->assertEquals( $_SESSION['tainacan_importer'][$id]->get_option('attachment_index'), 3 );
|
||||
$this->assertEquals( $_SESSION['tainacan_importer'][$id]->get_option('document_index'), 4 );
|
||||
|
||||
// inserting the collection
|
||||
$collection = $this->tainacan_entity_factory->create_entity(
|
||||
'collection',
|
||||
array(
|
||||
'name' => 'Other',
|
||||
'description' => 'adasdasdsa',
|
||||
'default_order' => 'DESC',
|
||||
'status' => 'publish'
|
||||
),
|
||||
true
|
||||
);
|
||||
|
||||
$metadatum = $this->tainacan_entity_factory->create_entity(
|
||||
'metadatum',
|
||||
array(
|
||||
'name' => 'Data multiplo',
|
||||
'description' => 'Descreve o dado do campo data.',
|
||||
'collection' => $collection,
|
||||
'status' => 'publish',
|
||||
'metadata_type' => 'Tainacan\Metadata_Types\Text',
|
||||
'multiple' => 'yes'
|
||||
),
|
||||
true
|
||||
);
|
||||
|
||||
$metadatum = $this->tainacan_entity_factory->create_entity(
|
||||
'metadatum',
|
||||
array(
|
||||
'name' => 'Texto simples',
|
||||
'description' => 'Descreve o dado do campo data.',
|
||||
'collection' => $collection,
|
||||
'status' => 'publish',
|
||||
'metadata_type' => 'Tainacan\Metadata_Types\Text',
|
||||
'multiple' => 'no'
|
||||
),
|
||||
true
|
||||
);
|
||||
|
||||
$collection_definition = [
|
||||
'id' => $collection->get_id(),
|
||||
'total_items' => $_SESSION['tainacan_importer'][$id]->get_source_number_of_items(),
|
||||
];
|
||||
|
||||
// get collection metadata to map
|
||||
$metadata = $Tainacan_Metadata->fetch_by_collection( $collection, [], 'OBJECT' ) ;
|
||||
|
||||
//create a random mapping
|
||||
$map = [];
|
||||
foreach ( $metadata as $index => $metadatum ){
|
||||
if(isset($headers[$index]))
|
||||
$map[$metadatum->get_id()] = $headers[$index];
|
||||
}
|
||||
|
||||
$collection_definition['mapping'] = $map;
|
||||
|
||||
// add the collection
|
||||
$_SESSION['tainacan_importer'][$id]->add_collection( $collection_definition );
|
||||
$_SESSION['tainacan_importer'][$id]->set_option('encode','iso88591');
|
||||
|
||||
$this->assertEquals(1, $_SESSION['tainacan_importer'][$id]->run(), 'first step should import 1 item');
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue