From 2c450a6b477ab1b447a4c7a535c796c5dd90f339 Mon Sep 17 00:00:00 2001 From: andre2ar Date: Tue, 24 Apr 2018 13:15:19 -0300 Subject: [PATCH] Old Tainacan Importer: corrections --- src/importer/class-tainacan-importer.php | 4 +- src/importer/class-tainacan-old-tainacan.php | 77 +++++++++++++------- tests/test-importer.php | 2 +- 3 files changed, 52 insertions(+), 31 deletions(-) diff --git a/src/importer/class-tainacan-importer.php b/src/importer/class-tainacan-importer.php index 64cea57e9..652cd4b09 100644 --- a/src/importer/class-tainacan-importer.php +++ b/src/importer/class-tainacan-importer.php @@ -406,9 +406,9 @@ abstract class Importer { // inserted the id on processed item with its index as array index $this->processed_items[ $index ] = $insertedItem->get_id(); if($insertedItem->validate()) { - $Tainacan_Items->update( $insertedItem ); + $insertedItem = $Tainacan_Items->update( $insertedItem ); } else { - $this->add_log( 'error', 'Item ' . $index . ': ' . $insertedItem->get_errors()[0] ); // TODO add the $item->get_errors() array + $this->add_log( 'error', 'Item ' . $index . ': ' . $insertedItem->get_errors()[0]['title'] ); // TODO add the $item->get_errors() array return false; } diff --git a/src/importer/class-tainacan-old-tainacan.php b/src/importer/class-tainacan-old-tainacan.php index 657a6267b..d61237742 100644 --- a/src/importer/class-tainacan-old-tainacan.php +++ b/src/importer/class-tainacan-old-tainacan.php @@ -11,7 +11,31 @@ namespace Tainacan\Importer; class Old_Tainacan extends Importer { - public function __construct($import_structure_and_mapping = false) { + public $avoid = [ + 'ID', + 'post_author', + 'post_date', + 'post_date_gmt', + /*'post_content', + 'post_title',*/ + 'post_excerpt', + 'post_status', + 'comment_status', + 'ping_status', + 'post_name', + 'post_modified', + 'post_modified_gmt', + 'post_content_filtered', + 'post_parent', + 'guid', + 'comment_count', + 'filter', + 'link', + 'thumbnail' + ]; + + public function __construct($import_structure_and_mapping = false) + { parent::__construct(); $this->remove_import_method('file'); @@ -50,7 +74,6 @@ class Old_Tainacan extends Importer if(!empty($link)) { $items = wp_remote_get( $link."/items/?includeMetadata=1" ); - if(isset($items['body'])) { $items_array = json_decode($items['body']); @@ -78,8 +101,18 @@ class Old_Tainacan extends Importer $item = $file_content->items[0]; - $fields = array_keys((array)$item->item); + $fields = []; + //Default meta + foreach ($item->item as $meta_name => $value) + { + if(!in_array($meta_name, $this->avoid)) + { + $fields[] = $meta_name; + } + } + + //Added meta foreach ($item->metadata as $metadata) { $fields[] = ['name' => $metadata->name, 'type' => $metadata->type]; @@ -105,7 +138,6 @@ class Old_Tainacan extends Importer // search the index in the file and get values $file = new \SplFileObject( $this->tmp_file, 'r' ); $file_content = unserialize($file->fread($file->getSize())); - $values = $file_content->items[$index]; foreach ($headers as $header) { @@ -147,31 +179,10 @@ class Old_Tainacan extends Importer public function create_fields_and_mapping() { + $Tainacan_Fields = \Tainacan\Repositories\Fields::get_instance(); $fields_repository = \Tainacan\Repositories\Fields::get_instance(); $file_fields = $this->get_fields(); - $avoid = [ - /*'ID', - 'post_author', - 'post_date', - 'post_date_gmt', - 'post_content', - 'post_title', - 'post_excerpt', - 'post_status', - 'comment_status', - 'ping_status', - 'post_name', - 'post_modified', - 'post_modified_gmt', - 'post_content_filtered', - 'post_parent', - 'guid', - 'comment_count', - 'filter', - 'link', - 'thumbnail'*/ - ]; foreach($file_fields as $index => $meta_info) { @@ -186,7 +197,7 @@ class Old_Tainacan extends Importer $type = 'Text'; } - if(!in_array($meta_name, $avoid)) + if(!in_array($meta_name, $this->avoid)) { $newField = new \Tainacan\Entities\Field(); @@ -200,6 +211,16 @@ class Old_Tainacan extends Importer $newField = $fields_repository->insert($newField); $mapping[$newField->get_id()] = $file_fields[$index]; + }else + { + $fields = $Tainacan_Fields->fetch_by_collection( $this->collection, [], 'OBJECT' ) ; + foreach ($fields as $field) + { + if($field->WP_Post->post_name === 'title' || $field->WP_Post->post_name === 'description') + { + $mapping[$field->get_id()] = $file_fields[$meta_name]; + } + } } } @@ -209,7 +230,7 @@ class Old_Tainacan extends Importer public function define_type($type) { $type = strtolower($type); - $tainacan_types = ['text', 'textarea', 'numerica', 'date']; + $tainacan_types = ['text', 'textarea', 'numeric', 'date']; $types_to_work = ['item', 'tree']; if(in_array($type, $tainacan_types)) diff --git a/tests/test-importer.php b/tests/test-importer.php index cdeca4e88..f043b9d8d 100644 --- a/tests/test-importer.php +++ b/tests/test-importer.php @@ -29,7 +29,7 @@ class ImporterTests extends TAINACAN_UnitTestCase { $this->assertEquals( $collection->get_id(), $_SESSION['tainacan_importer'][$id]->collection->get_id() ); } - /* public function test_automapping_old_tainacan() + /*public function test_automapping_old_tainacan() { $Tainacan_Items = \Tainacan\Repositories\Items::get_instance(); $Tainacan_Fields = \Tainacan\Repositories\Fields::get_instance();