datatable.directory codebase https://datatable.directory/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
datatable.directory/app/Http/Controllers/TableController.php

169 lines
5.8 KiB

<?php
namespace App\Http\Controllers;
7 years ago
use App\Models\Revision;
use App\Models\Row;
use App\Models\Table;
7 years ago
use App\Models\User;
use App\Utils\Column;
use Illuminate\Http\Request;
use Illuminate\Validation\Rule;
7 years ago
use MightyPork\Exceptions\NotApplicableException;
class TableController extends Controller
{
public function view(User $user, string $table)
{
/** @var Table $tableModel */
$tableModel = $user->tables()->where('name', $table)->first();
$revision = $tableModel->revision;
return view('table.view', [
'table' => $tableModel,
'revision' => $revision,
'columns' => Column::columnsFromJson(json_decode($revision->columns)),
'rows' => $revision->rows,
]);
}
/**
* SHow a form for creating a new table
*
* @return \Illuminate\Http\Response
*/
public function create()
{
$exampleColumns =
"latin,string,Latin Name\n" .
"common,string,Common Name\n" .
7 years ago
"lifespan,int,Lifespan (years)";
$exampleData =
7 years ago
"Mercenaria mercenaria,hard clam,40\n" .
"Magallana gigas,pacific oyster,30\n" .
"Patella vulgata,common limpet,20";
return view('table.create',
compact('exampleColumns', 'exampleData')
);
}
public function storeNew(Request $request)
{
7 years ago
/** @var User $u */
$u = \Auth::user();
7 years ago
$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
7 years ago
if ($u->tables()->where('name', $input->name)->exists()) {
return $this->backWithErrors([
7 years ago
'name' => "A table called \"$input->name\" already exists in your account.",
]);
}
// Parse and validate the columns specification
7 years ago
/** @var Column[] $columns */
$columns = [];
$column_keys = []; // for checking duplicates
7 years ago
$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"]);
7 years ago
foreach ($colTable as $col) {
$col = array_map('trim', $col);
if (count($col) < 2 || strlen($col[0])==0) {
return $this->backWithErrors(['columns' => "All columns must have at least name and type."]);
7 years ago
}
7 years ago
try {
if (in_array($col[0], $column_keys)) {
return $this->backWithErrors(['columns' => "Duplicate column: $col[0]"]);
}
$column_keys[] = $col[0];
7 years ago
$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,
]);
} catch (\Exception $e) {
return $this->backWithErrors(['columns' => $e->getMessage()]);
}
}
if (count($columns) == 0) return $this->backWithErrors(['columns' => "Define at least one column"]);
7 years ago
7 years ago
$rowTable = array_map('str_getcsv', explode("\n", $input->data));
7 years ago
// Preparing data to insert into the Rows table
7 years ago
$rowsData = null;
try {
$rowsData = array_map(function ($row) use ($columns) {
if (count($row) != count($columns)) {
throw new NotApplicableException("All rows must have " . count($columns) . " fields.");
7 years ago
}
$parsed = [];
foreach ($row as $i => $val) {
$key = $columns[$i]->name;
7 years ago
if (strlen($val) > 255) {
// try to stop people inserting unstructured crap / malformed CSV
throw new NotApplicableException("Value for column $key too long.");
}
7 years ago
$parsed[$key] = $columns[$i]->cast($val);
}
return [
'data' => json_encode($parsed),
];
}, $rowTable);
} catch (\Exception $e) {
7 years ago
return $this->backWithErrors(['columns' => $e->getMessage()]);
}
$revisionFields = [
7 years ago
'note' => "Initial revision of table $u->name/$input->name",
7 years ago
'columns' => json_encode($columns),
'row_count' => count($rowsData),
];
7 years ago
$tableFields = [
7 years ago
'owner_id' => $u->id,
'revision_id' => 0,
7 years ago
'name' => $input->name,
'title' => $input->title,
'description' => $input->description,
'license' => $input->license,
'origin' => $input->origin,
];
\DB::transaction(function () use ($revisionFields, $tableFields, $rowsData) {
$revision = Revision::create($revisionFields);
$tableFields['revision_id'] = $revision->id; // to meet the not-null constraint
$table = Table::create($tableFields);
// Attach the revision to the table, set as current
$table->revisions()->attach($revision);
$table->revision()->associate($revision);
7 years ago
// Spawn rows, linked to the revision
$revision->rows()->createMany($rowsData);
});
7 years ago
return redirect(route('table.view', ['user' => $u, 'table' => $input->name]));
}
}