allow change csv enclosure and create test for iso8859 strings

This commit is contained in:
eduardohumberto 2018-07-04 22:39:14 -03:00
parent ff411072de
commit 0de09ad146
2 changed files with 51 additions and 10 deletions

View File

@ -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 .= '<label class="label">' . __('Multivalued metadata delimiter', 'tainacan') . '</label>';
$form .= '<input type="text" class="input" name="multivalued_delimiter" value="' . $this->get_option('multivalued_delimiter') . '" />';
$form .= '<label class="label">' . __('Encoding', 'tainacan') . '</label>';
$form .= '<input type="text" class="input" name="encode" value="' . $this->get_option('encode') . '" />';
$form .= '<div class="control">';
$form = '<label class="label">' . __('Encoding', 'tainacan') . '</label>';
$form .= '<label class="label">' . __('Cell Encapsulate', 'tainacan') . '</label>';
$form .= '<input type="text" class="input" name="cell_encapsulate" value="' . $this->get_option('cell_encapsulate') . '" />';
$form .= '<label class="radio">';
$form .= '<input type="radio" name="encode" value="utf8" '
. ( !$this->get_option('encode') || $this->get_option('encode') === 'utf8' ) ? 'checked' : '' . ' />';
$form .= __('UTF8', 'tainacan') . '</label>';
$form .= '<label class="radio">';
$form .= '<input type="radio" name="encode" value="iso88591" '
. ( !$this->get_option('encode') || $this->get_option('encode') === 'iso88591' ) ? 'checked' : '' . ' />';
$form .= __('ISO 8859-1', 'tainacan') . '</label>';
$form .= '</div>';
$form .= '<label class="label">' . __('Enclosure character', 'tainacan') . '</label>';
$form .= '<input type="text" class="input" size="1" name="enclosure" value="' . $this->get_option('enclosure') . '" />';
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;
}
}
}

View File

@ -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');