add option to upload file when creating a table

This commit is contained in:
2018-08-12 22:38:08 +02:00
parent 7ae41b368e
commit 13b22f7cb4
5 changed files with 82 additions and 15 deletions
+35 -8
View File
@@ -81,16 +81,17 @@ class TableController extends Controller
*/
public function create()
{
$exampleData =
"Mercenaria mercenaria,hard clam,40\n" .
"Magallana gigas,pacific oyster,30\n" .
"Patella vulgata,common limpet,20";
$exampleData = "";
// "Mercenaria mercenaria,hard clam,40\n" .
// "Magallana gigas,pacific oyster,30\n" .
// "Patella vulgata,common limpet,20";
$columns = Column::columnsFromJson([
// fake 'id' to satisfy the check in Column constructor
['id' => 1, 'name' => 'latin', 'type' => 'string', 'title' => 'Latin Name'],
['id' => 2, 'name' => 'common', 'type' => 'string', 'title' => 'Common Name'],
['id' => 3, 'name' => 'lifespan', 'type' => 'int', 'title' => 'Lifespan (years)']
['id' => 1, 'name' => 'column_1', 'type' => 'string', 'title' => 'First Column'],
// ['id' => 1, 'name' => 'latin', 'type' => 'string', 'title' => 'Latin Name'],
// ['id' => 2, 'name' => 'common', 'type' => 'string', 'title' => 'Common Name'],
// ['id' => 3, 'name' => 'lifespan', 'type' => 'int', 'title' => 'Lifespan (years)']
]);
return view('table.create', [
@@ -210,7 +211,33 @@ class TableController extends Controller
}
// --- DATA ---
$dataCsvLines = Utils::csvToArray($input->data);
$fname = 'csv-file';
$dataCsvLines = [];
if ($request->hasFile($fname)) {
try {
$file = $request->file($fname);
if ($file->isValid() && $file->isReadable()) {
$handle = $file->openFile();
$dataCsvLines = Utils::csvToArray($handle);
if (empty($dataCsvLines)) throw new \Exception("Failed to parse CSV file.");
$handle = null;
} else {
throw new \Exception("File not valid.");
}
} catch (\Exception $e) {
return $this->backWithErrors(['csv-file' => $e->getMessage()]);
}
}
else if ($input->data) {
try {
$text = trim($input->data);
if (!empty($text)) {
$dataCsvLines = Utils::csvToArray($text);
}
} catch (\Exception $e) {
return $this->backWithErrors(['data' => $e->getMessage()]);
}
}
// Preparing data to insert into the Rows table
$rowsToInsert = null;
+1
View File
@@ -83,6 +83,7 @@ class Row extends BaseModel
*/
public static function allocateRowIDs($count)
{
if ($count == 0) return null;
$last = \DB::selectOne("SELECT multi_nextval('global_row_id_seq', ?) AS last_id;", [$count])->last_id;
return [$last - $count + 1, $last];
}