From 0de09ad1469f82629d5a11671b175edfa82dd572 Mon Sep 17 00:00:00 2001 From: eduardohumberto Date: Wed, 4 Jul 2018 22:39:14 -0300 Subject: [PATCH] allow change csv enclosure and create test for iso8859 strings --- src/importer/class-tainacan-csv.php | 57 ++++++++++++++++++++++++----- tests/test-importer.php | 4 +- 2 files changed, 51 insertions(+), 10 deletions(-) diff --git a/src/importer/class-tainacan-csv.php b/src/importer/class-tainacan-csv.php index 29785a2e2..553ee8008 100644 --- a/src/importer/class-tainacan-csv.php +++ b/src/importer/class-tainacan-csv.php @@ -15,12 +15,19 @@ class CSV extends Importer { $this->set_default_options([ 'delimiter' => ',', 'multivalued_delimiter' => '||', - 'encode' => 'UTF-8', - 'cell_encapsulate' => '"' + 'encode' => 'utf8', + 'enclosure' => '"' ]); } + /** + * alter the default options + */ + public function set_option($key,$value){ + $this->default_options[$key] = $value; + } + /** * @inheritdoc */ @@ -45,9 +52,9 @@ class CSV extends Importer { if( $index === 0 ){ $file->current(); $file->next(); - $values = $file->fgetcsv( $this->get_option('delimiter') ); + $values = $file->fgetcsv( $this->get_option('delimiter'), $this->get_option('enclosure') ); }else{ - $values = $file->fgetcsv( $this->get_option('delimiter') ); + $values = $file->fgetcsv( $this->get_option('delimiter'), $this->get_option('enclosure') ); } if( count( $headers ) !== count( $values ) ){ @@ -58,8 +65,10 @@ class CSV extends Importer { foreach ( $collection_definition['mapping'] as $metadatum_id => $header) { $metadatum = new \Tainacan\Entities\Metadatum($metadatum_id); + $column = $this->handle_encoding( $values[ $cont ] ); + $processedItem[ $header ] = ( $metadatum->get_multiple() ) ? - explode( $this->get_option('multivalued_delimiter'), $values[ $cont ]) : $values[ $cont ]; + explode( $this->get_option('multivalued_delimiter'), $column) : $column; $cont++; } @@ -89,13 +98,43 @@ class CSV extends Importer { $form .= ''; $form .= ''; - $form .= ''; - $form .= ''; + $form .= '
'; + $form = ''; - $form .= ''; - $form .= ''; + $form .= ''; + + $form .= ''; + + $form .= '
'; + + $form .= ''; + $form .= ''; return $form; } + + /** + * get the encode option and return as expected + */ + private function handle_encoding($string){ + + switch( $this->get_option('encode') ){ + + case 'utf8': + return $string; + + case 'iso88591': + return utf8_encode($string); + + default: + return $string; + } + } } \ No newline at end of file diff --git a/tests/test-importer.php b/tests/test-importer.php index 7207919d5..c4c23dc5b 100644 --- a/tests/test-importer.php +++ b/tests/test-importer.php @@ -263,7 +263,7 @@ class ImporterTests extends TAINACAN_UnitTestCase { $data = array( array('Data 11', 'Data 12', 'Data 13||TESTE', 'Data 14', 'Data 15'), array('Data 21', 'Data 22', 'Data 23', 'Data 24', 'Data 25'), - array('Data 31', 'Data 32', 'Data 33||ROUPA', 'Data 34', 'Data 35'), + array('Data 31', 'Data 32', utf8_decode( 'Data 33||Rééço' ), 'Data 34', 'Data 35'), array('Data 41', 'Data 42', 'Data 43||limbbo', 'Data 44', 'Data 45'), array('Data 51', 'Data 52', 'Data 53', 'Data 54', 'Data 55') ); @@ -344,6 +344,8 @@ class ImporterTests extends TAINACAN_UnitTestCase { // add the collection $_SESSION['tainacan_importer'][$id]->add_collection( $collection_definition ); + $_SESSION['tainacan_importer'][$id]->set_option('encode','iso88591'); + //$_SESSION['tainacan_importer'][$id]->set_option('encode','utf8'); //execute the process $this->assertEquals(1, $_SESSION['tainacan_importer'][$id]->run(), 'first step should import 1 item');