user edit form
This commit is contained in:
@@ -54,21 +54,24 @@ class TableController extends Controller
|
||||
/** @var User $u */
|
||||
$u = \Auth::user();
|
||||
|
||||
$this->validate($request, [
|
||||
'name' => 'required|string|max:255',
|
||||
'title' => 'string|string|max:255',
|
||||
'description' => 'string|nullable|max:4000',
|
||||
'license' => 'string|nullable|max:4000',
|
||||
'origin' => 'string|nullable|max:4000',
|
||||
$input = $this->validate($request, [
|
||||
'name' => [
|
||||
'required',
|
||||
VALI_NAME,
|
||||
Rule::unique('tables'),
|
||||
],
|
||||
'title' => ['required', VALI_LINE],
|
||||
'description' => ['nullable', VALI_TEXT],
|
||||
'license' => ['nullable', VALI_TEXT],
|
||||
'origin' => ['nullable', VALI_TEXT],
|
||||
'columns' => 'required|string',
|
||||
'data' => 'string|nullable',
|
||||
]);
|
||||
|
||||
// Check if table name is unique for user
|
||||
$tabName = $request->get('name');
|
||||
if ($u->tables()->where('name', $tabName)->exists()) {
|
||||
if ($u->tables()->where('name', $input->name)->exists()) {
|
||||
return $this->backWithErrors([
|
||||
'name' => "A table called \"$tabName\" already exists in your account.",
|
||||
'name' => "A table called \"$input->name\" already exists in your account.",
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -76,7 +79,7 @@ class TableController extends Controller
|
||||
/** @var Column[] $columns */
|
||||
$columns = [];
|
||||
$column_keys = []; // for checking duplicates
|
||||
$colTable = array_map('str_getcsv', explode("\n", $request->get('columns')));
|
||||
$colTable = array_map('str_getcsv', explode("\n", $input->columns));
|
||||
|
||||
// prevent griefing via long list of columns
|
||||
if (count($colTable) > 100) return $this->backWithErrors(['columns' => "Too many columns"]);
|
||||
@@ -104,7 +107,7 @@ class TableController extends Controller
|
||||
}
|
||||
if (count($columns) == 0) return $this->backWithErrors(['columns' => "Define at least one column"]);
|
||||
|
||||
$rowTable = array_map('str_getcsv', explode("\n", $request->get('data')));
|
||||
$rowTable = array_map('str_getcsv', explode("\n", $input->data));
|
||||
|
||||
// Preparing data to insert into the Rows table
|
||||
$rowsData = null;
|
||||
@@ -116,6 +119,10 @@ class TableController extends Controller
|
||||
$parsed = [];
|
||||
foreach ($row as $i => $val) {
|
||||
$key = $columns[$i]->name;
|
||||
if (strlen($val) > 255) {
|
||||
// try to stop people inserting unstructured crap / malformed CSV
|
||||
throw new NotApplicableException("Value for column $key too long.");
|
||||
}
|
||||
$parsed[$key] = $columns[$i]->cast($val);
|
||||
}
|
||||
return [
|
||||
@@ -127,7 +134,7 @@ class TableController extends Controller
|
||||
}
|
||||
|
||||
$revisionFields = [
|
||||
'note' => "Initial revision of table $u->name/$tabName",
|
||||
'note' => "Initial revision of table $u->name/$input->name",
|
||||
'columns' => json_encode($columns),
|
||||
'row_count' => count($rowsData),
|
||||
];
|
||||
@@ -135,11 +142,11 @@ class TableController extends Controller
|
||||
$tableFields = [
|
||||
'owner_id' => $u->id,
|
||||
'revision_id' => 0,
|
||||
'name' => $tabName,
|
||||
'title' => $request->get('title'),
|
||||
'description' => $request->get('description'),
|
||||
'license' => $request->get('license'),
|
||||
'origin' => $request->get('origin'),
|
||||
'name' => $input->name,
|
||||
'title' => $input->title,
|
||||
'description' => $input->description,
|
||||
'license' => $input->license,
|
||||
'origin' => $input->origin,
|
||||
];
|
||||
|
||||
\DB::transaction(function () use ($revisionFields, $tableFields, $rowsData) {
|
||||
@@ -156,6 +163,6 @@ class TableController extends Controller
|
||||
$revision->rows()->createMany($rowsData);
|
||||
});
|
||||
|
||||
return redirect(route('table.view', ['user' => $u, 'table' => $tabName]));
|
||||
return redirect(route('table.view', ['user' => $u, 'table' => $input->name]));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user