Row adding, some webpack tweaks
This commit is contained in:
@@ -207,39 +207,13 @@ class TableController extends Controller
|
||||
}
|
||||
|
||||
// --- DATA ---
|
||||
$dataCsvLines = array_map('str_getcsv', explode("\n", $input->data));
|
||||
$dataCsvLines = Utils::csvToArray($input->data);
|
||||
|
||||
// Preparing data to insert into the Rows table
|
||||
$rowsToInsert = null;
|
||||
$rowNumerator = null;
|
||||
try {
|
||||
$rowsToInsert = collect($dataCsvLines)->map(function ($row) use ($columns) {
|
||||
if (count($row) == 0 || count($row) == 1 && $row[0] == '') return null; // discard empty lines
|
||||
if (count($row) != count($columns)) {
|
||||
throw new NotApplicableException("All rows must have " . count($columns) . " fields.");
|
||||
}
|
||||
|
||||
$data = [];
|
||||
|
||||
foreach ($row as $i => $val) {
|
||||
$col = $columns[$i];
|
||||
|
||||
if (strlen($val) > 1000) {
|
||||
// try to stop people inserting unstructured crap / malformed CSV
|
||||
throw new NotApplicableException("Value for column {$col->name} too long.");
|
||||
}
|
||||
$data[$col->id] = $col->cast($val);
|
||||
}
|
||||
|
||||
// return in a format prepared for filling eloquent
|
||||
return ['data' => $data];
|
||||
})->filter()->all(); // remove nulls, to array
|
||||
|
||||
// attach _id labels to all rows
|
||||
$rowNumerator = new RowNumerator(count($dataCsvLines));
|
||||
foreach ($rowsToInsert as &$item) {
|
||||
$item['data']['_id'] = $rowNumerator->next();
|
||||
}
|
||||
$rowsToInsert = Changeset::csvToRowsArray($columns, $dataCsvLines, true)->all();
|
||||
} catch (\Exception $e) {
|
||||
return $this->backWithErrors(['data' => $e->getMessage()]);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user