From 9be7d8521a11f6a447989a5be08782a519e4bdea Mon Sep 17 00:00:00 2001 From: eduardohumberto Date: Thu, 7 Jun 2018 22:52:46 -0300 Subject: [PATCH] create step for tainacan old structure --- src/importer/class-tainacan-old-tainacan.php | 72 +++++++++++++------- tests/test-importer.php | 3 +- 2 files changed, 48 insertions(+), 27 deletions(-) diff --git a/src/importer/class-tainacan-old-tainacan.php b/src/importer/class-tainacan-old-tainacan.php index 0011bdd52..3c8329586 100644 --- a/src/importer/class-tainacan-old-tainacan.php +++ b/src/importer/class-tainacan-old-tainacan.php @@ -34,14 +34,9 @@ class Old_Tainacan extends Importer{ 'socialdb_property_fixed_attachments' ], $steps = [ - //'Creating all categories' => 'create_categories', - //'Create empty collections' => 'create_collections', - //'Creating relationships metadata' => 'create_relationships_meta', - //'Create repository metadata' => 'treat_repo_meta', - //'Create collections metadata' => 'treat_collection_metas', [ 'name' => 'Create categories, collections and metadata', - 'callback' => 'process_collections' + 'callback' => 'create_structure' ], [ 'name' => 'Import Items', @@ -51,8 +46,27 @@ class Old_Tainacan extends Importer{ 'name' => 'Finishing', 'callback' => 'clear' ] - ], $tainacan_api_address, $wordpress_api_address; + ], $tainacan_api_address, $wordpress_api_address, $actual_collection; + /** + * create structure tainacan + */ + public function create_structure(){ + $this->create_categories(); + $this->create_collections(); + $this->create_relationships_meta(); + $this->treat_repo_meta(); + $this->treat_collection_metas(); + + return false; + } + + /** + * helper method to set the actual collection in loop + */ + public function set_actual_collection($collection){ + $this->actual_collection = $collection; + } public function create_categories() { @@ -67,8 +81,10 @@ class Old_Tainacan extends Importer{ $categories_array = $this->remove_same_name($categories_array); - list($inside_step_pointer, $end) = $this->get_begin_end($categories_array); - if($inside_step_pointer === false) return false; + // list($inside_step_pointer, $end) = $this->get_begin_end($categories_array); + // if($inside_step_pointer === false) return false; + $inside_step_pointer = 0; + $end = ( $categories_array ) ? count( $categories_array) : 0; $created_categories = []; while($inside_step_pointer < $end) @@ -110,8 +126,10 @@ class Old_Tainacan extends Importer{ $created_collections = []; if($collections_array) { - list($inside_step_pointer, $end) = $this->get_begin_end($collections_array); - if($inside_step_pointer === false) return false; + // list($inside_step_pointer, $end) = $this->get_begin_end($collections_array); + // if($inside_step_pointer === false) return false; + $inside_step_pointer = 0; + $end = ( $collections_array ) ? count( $collections_array) : 0; while($inside_step_pointer < $end) { @@ -206,17 +224,19 @@ class Old_Tainacan extends Importer{ $created_categories = $this->read_from_file("categories"); $relationships = $this->read_from_file("relationships"); - list($inside_step_pointer, $end) = $this->get_begin_end($created_collections); - if($inside_step_pointer === false) return false; + // list($inside_step_pointer, $end) = $this->get_begin_end($created_collections); + // if($inside_step_pointer === false) return false; + $inside_step_pointer = 0; + $end = ( $created_collections ) ? count( $created_collections) : 0; $Tainacan_Fields = \Tainacan\Repositories\Fields::get_instance(); $Fields_Repository = \Tainacan\Repositories\Fields::get_instance(); $Repository_Collections = \Tainacan\Repositories\Collections::get_instance(); - for($i = 0; $i < $inside_step_pointer; $i++) - { - next($created_collections); - } + //for($i = 0; $i < $inside_step_pointer; $i++) + // { + // next($created_collections); + // } while($inside_step_pointer < $end) { @@ -224,13 +244,15 @@ class Old_Tainacan extends Importer{ $new_collection_id = $collection_info['new_id']; $old_collection_id = key($created_collections); $collection = $Repository_Collections->fetch($new_collection_id); - $this->set_collection($collection); + $this->set_actual_collection($collection); $file_fields = $this->get_collection_fields($old_collection_id); $mapping = $this->create_collection_meta($file_fields, $Fields_Repository, $Tainacan_Fields, $created_repository_fields, $created_categories, $relationships); - $this->set_repository_mapping($mapping, $old_collection_id); + //TODO: add collection definition + + // $this->set_repository_mapping($mapping, $old_collection_id); next($created_collections); $inside_step_pointer++; } @@ -280,7 +302,7 @@ class Old_Tainacan extends Importer{ $mapping[$new_id] = $created_repository_fields[$old_field_id]['name']; }else { - $fields = $Tainacan_Fields->fetch_by_collection( $this->collection, [], 'OBJECT' ); + $fields = $Tainacan_Fields->fetch_by_collection( $this->actual_collection, [], 'OBJECT' ); foreach ($fields as $field) { @@ -367,7 +389,7 @@ class Old_Tainacan extends Importer{ $newField->set_collection_id('default'); }else { - $newField->set_collection($this->collection); + $newField->set_collection($this->actual_collection); } }else //Set compound as field parent { @@ -413,7 +435,7 @@ class Old_Tainacan extends Importer{ $new_collection_id = $collection_info['new_id']; $old_collection_id = key($created_collections); $collection = $Repository_Collections->fetch($new_collection_id); - $this->set_collection($collection); + $this->set_actual_collection($collection); $mapping = $this->get_repository_mapping($old_collection_id); $this->set_mapping($mapping); @@ -679,7 +701,7 @@ class Old_Tainacan extends Importer{ $headers = $this->get_fields(); // search the index in the file and get values - $file = new \SplFileObject( $this->tmp_file, 'r' ); + /*$file = new \SplFileObject( $this->tmp_file, 'r' ); $file_content = unserialize($file->fread($file->getSize())); $values = $file_content->items[$index]; foreach ($headers as $header) @@ -702,7 +724,7 @@ class Old_Tainacan extends Importer{ $processedItem[$header] = $values->item->{$header}; } } - } + }*/ return $processedItem; } @@ -748,7 +770,7 @@ class Old_Tainacan extends Importer{ $newField->set_field_type('Tainacan\Field_Types\\'.$type); - $newField->set_collection($this->collection); + $newField->set_collection($this->actual_collection); $newField->validate(); // there is no user input here, so we can be sure it will validate. $newField = $fields_repository->insert($newField); diff --git a/tests/test-importer.php b/tests/test-importer.php index 1d1706ac4..0fa8445bd 100644 --- a/tests/test-importer.php +++ b/tests/test-importer.php @@ -47,8 +47,7 @@ class ImporterTests extends TAINACAN_UnitTestCase { $url_repository = 'http://localhost/wordpress_tainacan/'; $url_repository = ''; if( $url_repository !== '' ){ - $_SESSION['tainacan_importer'][$id]->set_url($url); - $_SESSION['tainacan_importer'][$id]->set_repository(); + $_SESSION['tainacan_importer'][$id]->set_url($url_repository); while (!$_SESSION['tainacan_importer'][$id]->is_finished()) {