Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 26 additions & 17 deletions Model/Datasource/CsvSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -182,23 +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));
$dataComma = explode(',', $line);
$dataSemicolon = explode(';', $line);

if (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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is this needed for?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mb_convert_variables is not work for multi byte keys
ex)
1row | タイトル(title)|
2row | いいい|
3row | ううう|

array (
0 =>
array (
'modelname' =>
array (
'"{xxxxxx:bad encode}(title)"' => 'いいい',

}
$this->fields = $data;
$this->maxCol = count($data);

fclose($handle);
return true;

}

/**
Expand Down Expand Up @@ -313,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'])) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indentation level in this block is incorrect

mb_convert_variables($this->config['encoding'], "ASCII,UTF-8,SJIS-win", $resultSet);

return $resultSet;
}

return $resultSet;
}

Expand Down