/** * Akce pro zobrazení náhledu na prvních X řádků nahrávaného souboru - pracující s lokálními soubory... * @param string $file * @param string $delimiter * @param string $encoding * @param string $enclosure * @param string $escapeCharacter * @param string $nullValue * @param string $locale * @param int $rowsCount = 20 * @throws BadRequestException */ public function actionPreviewData($file, $delimiter = '', $encoding = 'utf-8', $enclosure = '"', $escapeCharacter = '\\', $nullValue = '', $locale = 'en', $rowsCount = 20) { //TODO detekce locale... (podle desetinné tečky/čárky...) if ($delimiter == '') { //detekce výchozího separátoru $delimiter = $this->fileImportsFacade->getCSVDelimiter($file); } //změna kódování souboru $this->fileImportsFacade->changeFileEncoding($file, $encoding); //připravení výstupního pole s daty $resultArr = ['config' => ['delimiter' => $delimiter, 'encoding' => $encoding, 'enclosure' => $enclosure, 'escapeCharacter' => $escapeCharacter, 'nullValue' => $nullValue, 'locale' => $locale], 'columnNames' => [], 'dataTypes' => [], 'data' => []]; //načtení informací o datových sloupcích $columns = $this->fileImportsFacade->getColsInCSV($file, $delimiter, $enclosure, $escapeCharacter, $rowsCount + 1); if (empty($columns)) { throw new BadRequestException('No columns detected!'); } foreach ($columns as $column) { $resultArr['columnNames'][] = $column->name; $resultArr['dataTypes'][] = $column->type == DbField::TYPE_NOMINAL ? 'nominal' : 'numeric'; } //načtení potřebných řádků... $resultArr['data'] = $this->fileImportsFacade->getRowsFromCSV($file, $rowsCount, $delimiter, $enclosure, $escapeCharacter, $nullValue, 1); //odeslání kompletní odpovědi... $this->sendJsonResponse($resultArr); }
/** * Akce pro import CSV * @param string $file * @param string $name * @throws BadRequestException * @throws \Exception */ public function actionImportCsv($file, $name) { $type = FileImportsFacade::FILE_TYPE_CSV; //kontrola, jestli zadaný soubor existuje if (!$this->fileImportsFacade->checkFileExists($file)) { throw new BadRequestException('Requested file was not found.'); } /** @var Form $form */ $form = $this->getComponent('importCsvForm'); $defaultsArr = ['file' => $file, 'type' => $type, 'table' => $this->databasesFacade->prepareNewTableName($name, false)]; //detekce pravděpodobného oddělovače $separator = $this->fileImportsFacade->getCSVDelimiter($file); $defaultsArr['separator'] = $separator; //připojení k DB pro zjištění názvu tabulky, který zatím není obsazen (dle typu preferované databáze) $csvColumnsCount = $this->fileImportsFacade->getColsCountInCSV($file, $separator); $databaseType = $this->databasesFacade->prefferedDatabaseType($csvColumnsCount); $newDatasource = $this->datasourcesFacade->prepareNewDatasourceForUser($this->usersFacade->findUser($this->user->id), $databaseType); $this->databasesFacade->openDatabase($newDatasource->getDbConnection()); $defaultsArr['table'] = $this->databasesFacade->prepareNewTableName($name); $form->setDefaults($defaultsArr); }