simple vue-based column editor
This commit is contained in:
@@ -72,19 +72,22 @@ class TableController extends Controller
|
||||
*/
|
||||
public function create()
|
||||
{
|
||||
$exampleColumns =
|
||||
"latin,string,Latin Name\n" .
|
||||
"common,string,Common Name\n" .
|
||||
"lifespan,int,Lifespan (years)";
|
||||
|
||||
$exampleData =
|
||||
"Mercenaria mercenaria,hard clam,40\n" .
|
||||
"Magallana gigas,pacific oyster,30\n" .
|
||||
"Patella vulgata,common limpet,20";
|
||||
|
||||
return view('table.create',
|
||||
compact('exampleColumns', 'exampleData')
|
||||
);
|
||||
$columns = Column::columnsFromJson([
|
||||
['name' => 'latin', 'type' => 'string', 'title' => 'Latin Name'],
|
||||
['name' => 'common', 'type' => 'string', 'title' => 'Common Name'],
|
||||
['name' => 'lifespan', 'type' => 'int', 'title' => 'Lifespan (years)']
|
||||
]);
|
||||
|
||||
return view('table.create', [
|
||||
'exampleColumns' => '',
|
||||
'columns' => $columns,
|
||||
'exampleData' => $exampleData,
|
||||
]);
|
||||
}
|
||||
|
||||
public function settings(Request $request, User $user, string $table)
|
||||
@@ -141,7 +144,7 @@ class TableController extends Controller
|
||||
'description' => ['nullable', VALI_TEXT],
|
||||
'license' => ['nullable', VALI_TEXT],
|
||||
'origin' => ['nullable', VALI_TEXT],
|
||||
'columns' => 'required|string',
|
||||
'columns' => 'required|json',
|
||||
'data' => 'string|nullable',
|
||||
]);
|
||||
|
||||
@@ -156,28 +159,24 @@ class TableController extends Controller
|
||||
/** @var Column[] $columns */
|
||||
$columns = [];
|
||||
$column_keys = []; // for checking duplicates
|
||||
$colTable = array_map('str_getcsv', explode("\n", $input->columns));
|
||||
$colsArray = fromJSON($input->columns);
|
||||
|
||||
// prevent griefing via long list of columns
|
||||
if (count($colTable) > 100) return $this->backWithErrors(['columns' => "Too many columns"]);
|
||||
if (count($colsArray) > 100) return $this->backWithErrors(['columns' => "Too many columns"]);
|
||||
|
||||
foreach ($colTable as $col) {
|
||||
$col = array_map('trim', $col);
|
||||
if (count($col) < 2 || strlen($col[0])==0) {
|
||||
foreach ($colsArray as $col) {
|
||||
if (!isset($col->name) || !isset($col->type) || empty($col->name) || empty($col->type)) {
|
||||
return $this->backWithErrors(['columns' => "All columns must have at least name and type."]);
|
||||
}
|
||||
|
||||
try {
|
||||
if (in_array($col[0], $column_keys)) {
|
||||
return $this->backWithErrors(['columns' => "Duplicate column: $col[0]"]);
|
||||
if (in_array($col->name, $column_keys)) {
|
||||
return $this->backWithErrors(['columns' => "Duplicate column: $col->name"]);
|
||||
}
|
||||
$column_keys[] = $col[0];
|
||||
$column_keys[] = $col->name;
|
||||
|
||||
$columns[] = new Column([
|
||||
'name' => $col[0],
|
||||
'type' => $col[1],
|
||||
'title' => count($col) >= 3 ? $col[2] : $col[0], // title falls back to =name if not specified,
|
||||
]);
|
||||
if (!isset($col->title)) $col->title = $col->name;
|
||||
$columns[] = new Column($col);
|
||||
} catch (\Exception $e) {
|
||||
return $this->backWithErrors(['columns' => $e->getMessage()]);
|
||||
}
|
||||
|
||||
@@ -67,3 +67,27 @@ function vali($arr) {
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* like old(), but decodes stringified json
|
||||
*
|
||||
* @param string $name
|
||||
* @param object|array $default
|
||||
* @return object|array
|
||||
*/
|
||||
function old_json($name, $default) {
|
||||
$old = old($name, null);
|
||||
if (is_string($old)) return json_decode($old);
|
||||
return $default;
|
||||
}
|
||||
|
||||
// Safe JSON funcs
|
||||
function toJSON($object) {
|
||||
return \GuzzleHttp\json_encode($object, JSON_UNESCAPED_SLASHES + JSON_UNESCAPED_UNICODE);
|
||||
}
|
||||
|
||||
function fromJSON($object, $assoc=false) {
|
||||
return \GuzzleHttp\json_decode($object, $assoc);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user