/** * IDの最大値を取得する * * @param string $tableName * @return int $id * @access protecteds */ function _getMaxId($tableName) { if ($tableName) { $index = $tableName; } else { $index = 0; } if (!isset($this->connection[$index])) { if (!$this->_connect($tableName)) { return false; } } $maxId = 0; // ヘッダ取得 $this->_loadCsvFields($index); $idNum = ''; foreach ($this->_csvFields as $key => $value) { if ($value == 'id') { $idNum = $key; break; } } while (($record = fgetcsvReg($this->connection[$index], 10240)) !== false) { if ($record[$idNum] >= $maxId) { $maxId = $record[$idNum]; } } return $maxId; }
/** * ファイルポインタから行を取得し、CSVフィールドを処理する * * @param string $content CSVの内容 * @param int $length length * @param string $d delimiter * @param string $e enclosure * @param string $expext 期待値 * @param string $message テスト失敗時に表示するメッセージ * @dataProvider fgetcsvRegDataProvider */ public function testFgetcsvReg($content, $length, $d, $e, $expect, $message) { $csv = new File(CACHE . 'test.csv'); $csv->write($content); $csv->close(); $csv->open(); $result = fgetcsvReg($csv->handle, $length, $d, $e); $this->assertEquals($expect, $result, $message); $csv->close(); }
/** * CSVよりデータを配列として読み込む * * @param string $path * @return mixed boolean Or array */ public function loadCsvToArray($path, $encoding) { if (!$encoding) { $encoding = $this->_dbEncToPhp($this->getEncoding()); } $appEncoding = Configure::read('App.encoding'); // ヘッダ取得 $fp = fopen($path, 'r'); if (!$fp) { return false; } $head = fgetcsv($fp, 10240); $datas = array(); while (($record = fgetcsvReg($fp, 10240)) !== false) { if ($appEncoding != $encoding) { mb_convert_variables($appEncoding, $encoding, $record); } $values = array(); foreach ($record as $key => $value) { $values[$head[$key]] = $value; } $datas[] = $values; } fclose($fp); return $datas; }
/** * 初期データセットをダウンロードする */ public function admin_download_default_data_pattern() { /* コアのCSVを生成 */ $tmpDir = TMP . 'csv' . DS; $Folder = new Folder(); $Folder->create($tmpDir); emptyFolder($tmpDir); clearAllCache(); $excludes = array('plugins', 'dblogs', 'users', 'favorites'); $this->_writeCsv('baser', 'core', $tmpDir, $excludes); /* プラグインのCSVを生成 */ $plugins = CakePlugin::loaded(); foreach ($plugins as $plugin) { $Folder->create($tmpDir . $plugin); emptyFolder($tmpDir . $plugin); $this->_writeCsv('plugin', $plugin, $tmpDir . $plugin . DS); } /* site_configsの編集 (email / google_analytics_id / version) */ $targets = array('email', 'google_analytics_id', 'version'); $path = $tmpDir . 'site_configs.csv'; $fp = fopen($path, 'a+'); $records = array(); while (($record = fgetcsvReg($fp, 10240)) !== false) { if (in_array($record[1], $targets)) { $record[2] = ''; } $records[] = '"' . implode('","', $record) . '"'; } ftruncate($fp, 0); fwrite($fp, implode("\n", $records)); /* ZIPに固めてダウンロード */ $fileName = 'default'; $Simplezip = new Simplezip(); $Simplezip->addFolder($tmpDir); $Simplezip->download($fileName); emptyFolder($tmpDir); exit; }
/** * CSVファイルをDBに読み込む * * @param array $options [ path / encoding ] * @return boolean * @access public */ function loadCsv($options) { extract($options); if (!isset($path)) { return false; } if (!isset($encoding)) { $encoding = $this->_dbEncToPhp($this->getEncoding()); } $appEncoding = Configure::read('App.encoding'); $table = basename($path, '.csv'); $fullTableName = $this->config['prefix'] . $table; $schema = $this->readSchema(basename($path, '.csv')); if (isset($schema['tables'][$table]['indexes']['PRIMARY']['column'])) { $indexField = $schema['tables'][$table]['indexes']['PRIMARY']['column']; } else { $indexField = ''; } // ヘッダ取得 $fp = fopen($path, 'r'); $_head = fgetcsv($fp, 10240); foreach ($_head as $value) { $head[] = $this->name($value); } while (($_record = fgetcsvReg($fp, 10240)) !== false) { if ($appEncoding != $encoding) { mb_convert_variables($appEncoding, $encoding, $_record); } $values = array(); // 配列の添え字をフィールド名に変換 foreach ($_record as $key => $value) { // 主キーでデータが空の場合はスキップ if ($_head[$key] == $indexField && !$value) { unset($head[$key]); continue; } if ($_head[$key] == 'created' && !$value) { $value = date('Y-m-d H:i:s'); } $values[] = $this->value($value, $schema['tables'][$table][$_head[$key]]['type'], false); } $query = array('table' => $this->name($fullTableName), 'fields' => implode(', ', $head), 'values' => implode(', ', $values)); $sql = $this->renderStatement('create', $query); if (!$this->execute($sql)) { return false; } } fclose($fp); return true; }