From e06eef44abad7caef6410b14375dd096f638d653 Mon Sep 17 00:00:00 2001 From: Eduardo Humberto Date: Mon, 22 Oct 2018 21:48:43 -0300 Subject: [PATCH] tainacan csv importer - fix tests and start metadata creation --- src/importer/class-tainacan-csv.php | 8 +++- src/importer/class-tainacan-importer.php | 61 ++++++++++++++++++++++++ tests/test-importer.php | 4 +- 3 files changed, 70 insertions(+), 3 deletions(-) diff --git a/src/importer/class-tainacan-csv.php b/src/importer/class-tainacan-csv.php index 88ee4268f..95fce4694 100644 --- a/src/importer/class-tainacan-csv.php +++ b/src/importer/class-tainacan-csv.php @@ -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; } diff --git a/src/importer/class-tainacan-importer.php b/src/importer/class-tainacan-importer.php index bd39a9e07..7f9183cce 100644 --- a/src/importer/class-tainacan-importer.php +++ b/src/importer/class-tainacan-importer.php @@ -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; + } + } } \ No newline at end of file diff --git a/tests/test-importer.php b/tests/test-importer.php index 6eb3d1485..db4895dec 100644 --- a/tests/test-importer.php +++ b/tests/test-importer.php @@ -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 ) );