logo, improved listing, some helpers, mail confirms table, confirmed flag, user title column..
This commit is contained in:
@@ -8,6 +8,7 @@ use App\Models\Table;
|
||||
use App\Models\User;
|
||||
use App\Utils\Column;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Validation\Rule;
|
||||
use MightyPork\Exceptions\NotApplicableException;
|
||||
|
||||
class TableController extends Controller
|
||||
@@ -16,7 +17,7 @@ class TableController extends Controller
|
||||
{
|
||||
/** @var Table $tableModel */
|
||||
$tableModel = $user->tables()->where('name', $table)->first();
|
||||
$revision = $tableModel->activeRevision;
|
||||
$revision = $tableModel->revision;
|
||||
|
||||
return view('table.view', [
|
||||
'table' => $tableModel,
|
||||
@@ -54,12 +55,12 @@ class TableController extends Controller
|
||||
$u = \Auth::user();
|
||||
|
||||
$this->validate($request, [
|
||||
'name' => 'required',
|
||||
'title' => 'string',
|
||||
'description' => 'string|nullable',
|
||||
'license' => 'string|nullable',
|
||||
'origin' => 'string|nullable',
|
||||
'columns' => 'required',
|
||||
'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',
|
||||
'columns' => 'required|string',
|
||||
'data' => 'string|nullable',
|
||||
]);
|
||||
|
||||
@@ -74,16 +75,24 @@ class TableController extends Controller
|
||||
// Parse and validate the columns specification
|
||||
/** @var Column[] $columns */
|
||||
$columns = [];
|
||||
$column_keys = []; // for checking duplicates
|
||||
$colTable = array_map('str_getcsv', explode("\n", $request->get('columns')));
|
||||
|
||||
// prevent griefing via long list of columns
|
||||
if (count($colTable) > 100) return $this->backWithErrors(['columns' => "Too many columns"]);
|
||||
|
||||
foreach ($colTable as $col) {
|
||||
$col = array_map('trim', $col);
|
||||
if (count($col) < 2) {
|
||||
return $this->backWithErrors([
|
||||
'columns' => "All columns must have at least name and type.",
|
||||
]);
|
||||
if (count($col) < 2 || strlen($col[0])==0) {
|
||||
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]"]);
|
||||
}
|
||||
$column_keys[] = $col[0];
|
||||
|
||||
$columns[] = new Column([
|
||||
'name' => $col[0],
|
||||
'type' => $col[1],
|
||||
@@ -93,6 +102,7 @@ class TableController extends Controller
|
||||
return $this->backWithErrors(['columns' => $e->getMessage()]);
|
||||
}
|
||||
}
|
||||
if (count($columns) == 0) return $this->backWithErrors(['columns' => "Define at least one column"]);
|
||||
|
||||
$rowTable = array_map('str_getcsv', explode("\n", $request->get('data')));
|
||||
|
||||
@@ -110,38 +120,42 @@ class TableController extends Controller
|
||||
}
|
||||
return [
|
||||
'data' => json_encode($parsed),
|
||||
'refs' => 1,
|
||||
];
|
||||
}, $rowTable);
|
||||
} catch (\Exception $e) {
|
||||
return $this->backWithErrors(['columns' => $e->getMessage()]);
|
||||
}
|
||||
|
||||
$revision = Revision::create([
|
||||
'refs' => 1, // from the new table
|
||||
$revisionFields = [
|
||||
'note' => "Initial revision of table $u->name/$tabName",
|
||||
'columns' => json_encode($columns),
|
||||
]);
|
||||
'row_count' => count($rowsData),
|
||||
];
|
||||
|
||||
$table = Table::create([
|
||||
$tableFields = [
|
||||
'owner_id' => $u->id,
|
||||
'revision_id' => $revision->id,
|
||||
'revision_id' => 0,
|
||||
'name' => $tabName,
|
||||
'title' => $request->get('title'),
|
||||
'description' => $request->get('description'),
|
||||
'license' => $request->get('license'),
|
||||
'origin' => $request->get('origin'),
|
||||
]);
|
||||
];
|
||||
|
||||
// Attach the revision to the table, set as current
|
||||
$table->revisions()->attach($revision);
|
||||
$table->activeRevision()->associate($revision);
|
||||
\DB::transaction(function () use ($revisionFields, $tableFields, $rowsData) {
|
||||
$revision = Revision::create($revisionFields);
|
||||
|
||||
// Spawn the rows, linked to the revision
|
||||
$revision->rows()->createMany($rowsData);
|
||||
$tableFields['revision_id'] = $revision->id; // to meet the not-null constraint
|
||||
$table = Table::create($tableFields);
|
||||
|
||||
// Now we create rows, a revision pointing to them, and the table using it.
|
||||
// Attach the revision to the table, set as current
|
||||
$table->revisions()->attach($revision);
|
||||
$table->revision()->associate($revision);
|
||||
|
||||
return "Ok.";
|
||||
// Spawn rows, linked to the revision
|
||||
$revision->rows()->createMany($rowsData);
|
||||
});
|
||||
|
||||
return redirect(route('table.view', ['user' => $u, 'table' => $tabName]));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user