From e9c8b836f90139e2d1ed48769c862fda04a8223e Mon Sep 17 00:00:00 2001 From: yokoshima Date: Tue, 15 Jul 2014 15:39:26 +0900 Subject: [PATCH 1/2] tsv extension support --- Model/Datasource/CsvSource.php | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/Model/Datasource/CsvSource.php b/Model/Datasource/CsvSource.php index ff80064..1cc14a7 100644 --- a/Model/Datasource/CsvSource.php +++ b/Model/Datasource/CsvSource.php @@ -185,14 +185,19 @@ protected function _getDescriptionFromFirstLine(Model $model) { $filename = $model->table . '.' . $this->config['extension']; $handle = fopen($this->config['path'] . DS . $filename, 'r'); $line = rtrim(fgets($handle)); - $dataComma = explode(',', $line); - $dataSemicolon = explode(';', $line); - - if (count($dataComma) > count($dataSemicolon)) { - $this->delimiter = ','; - $this->fields = $dataComma; - $this->maxCol = count($dataComma); - } else { + $dataTab = explode("\t", $line); + $dataComma = explode(',', $line); + $dataSemicolon = explode(';', $line); + + if (count($dataTab) > count($dataSemicolon)) { + $this->delimiter = "\t"; + $this->fields = $dataTab; + $this->maxCol = count($dataTab); + } elseif (count($dataComma) > count($dataSemicolon)) { + $this->delimiter = ','; + $this->fields = $dataComma; + $this->maxCol = count($dataComma); + }else { $this->delimiter = ';'; $this->fields = $dataSemicolon; $this->maxCol = count($dataSemicolon); From b4a8329eb1b0693f22ec8173188e1cbe6b7f0051 Mon Sep 17 00:00:00 2001 From: do9iigane Date: Tue, 15 Jul 2014 17:41:23 +0900 Subject: [PATCH 2/2] support delimiter and encode --- Model/Datasource/CsvSource.php | 48 ++++++++++++++++++---------------- 1 file changed, 26 insertions(+), 22 deletions(-) diff --git a/Model/Datasource/CsvSource.php b/Model/Datasource/CsvSource.php index 1cc14a7..b6884ab 100644 --- a/Model/Datasource/CsvSource.php +++ b/Model/Datasource/CsvSource.php @@ -22,6 +22,8 @@ * 'extension' => 'csv', // File extension * 'readonly' => true, // Mark for read only access * 'recursive' => false // Only false is supported at the moment + * 'delimiter' => ',', // File delimiter + * 'encoding' => 'UTF8' // File convert to encode * ); */ App::uses('DataSource', 'Model/Datasource'); @@ -182,28 +184,23 @@ public function describe($model) { * @return boolean True, Success */ protected function _getDescriptionFromFirstLine(Model $model) { - $filename = $model->table . '.' . $this->config['extension']; - $handle = fopen($this->config['path'] . DS . $filename, 'r'); - $line = rtrim(fgets($handle)); - $dataTab = explode("\t", $line); - $dataComma = explode(',', $line); - $dataSemicolon = explode(';', $line); - - if (count($dataTab) > count($dataSemicolon)) { - $this->delimiter = "\t"; - $this->fields = $dataTab; - $this->maxCol = count($dataTab); - } elseif (count($dataComma) > count($dataSemicolon)) { - $this->delimiter = ','; - $this->fields = $dataComma; - $this->maxCol = count($dataComma); - }else { - $this->delimiter = ';'; - $this->fields = $dataSemicolon; - $this->maxCol = count($dataSemicolon); - } - fclose($handle); - return true; + $filename = $model->table . '.' . $this->config['extension']; + $handle = fopen($this->config['path'] . DS . $filename, 'r'); + $line = rtrim(fgets($handle)); + + $this->delimiter = isset($this->config['delimiter']) ? $this->config['delimiter'] : $this->delimiter; + + $data = explode($this->delimiter, $line); + + if (isset($this->config['encoding'])) { + mb_convert_variables($this->config['encoding'], "ASCII,UTF-8,SJIS-win", $data); + } + $this->fields = $data; + $this->maxCol = count($data); + + fclose($handle); + return true; + } /** @@ -318,6 +315,13 @@ public function read(Model $model, $queryData = array(), $recursive = null) { if ($model->findQueryType === 'count') { return array(array(array('count' => count($resultSet)))); } + + if (isset($this->config['encoding'])) { + mb_convert_variables($this->config['encoding'], "ASCII,UTF-8,SJIS-win", $resultSet); + + return $resultSet; + } + return $resultSet; }