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/TableEditController.php

123 lines
3.4 KiB

<?php
namespace App\Http\Controllers;
use App\Models\Row;
use App\Models\Table;
use App\Models\User;
use App\Tables\Changeset;
use App\Tables\Column;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
class TableEditController extends Controller
{
/**
* Initialize the session-stored changeset, if not set yet
*
* @param Table $table
* @return Changeset
*/
private function getChangeset(Table $table)
{
$session_key = "proposal_{$table->id}";
if (Input::has('reset')) {
session()->forget($session_key);
}
/** @var Changeset $changeset */
return session()->remember($session_key, function () use ($table) {
$changeset = new Changeset();
$changeset->table = $table;
$changeset->revision = $table->revision;
return $changeset;
});
}
private function storeChangeset(Changeset $chs)
{
$session_key = "proposal_{$chs->table->id}";
session()->put($session_key, $chs);
session()->save();
}
public function draft(User $user, string $table, $tab = null)
{
/** @var Table $tableModel */
$tableModel = $user->tables()->where('name', $table)->first();
if ($tableModel === null) abort(404, "No such table.");
if ($tab == null) $tab = 'edit-rows';
$tabs = ['edit-rows', 'add-rows', 'manage-columns', 'review'];
if (!in_array($tab, $tabs)) abort(404, "No such tab: $tab");
$changeset = $this->getChangeset($tableModel);
if (Input::has('dump')) {
dd($changeset);
}
return $this->{camel_case($tab)}($changeset);
}
/** @noinspection PhpUnusedPrivateMethodInspection */
private function editRows(Changeset $changeset)
{
$revision = $changeset->revision;
$columns = $changeset->fetchAndTransformColumns();
$rows = $revision->rowsData($columns, true, false)->paginate(25, []);
return view('table.propose.edit-rows', [
'changeset' => $changeset,
'table' => $changeset->table,
'columns' => collect($columns),
'rows' => $rows,
]);
}
// TODO other tab handlers
public function draftUpdate(User $user, string $table, Request $request)
{
/** @var Table $tableModel */
$tableModel = $user->tables()->where('name', $table)->first();
if ($tableModel === null) abort(404, "No such table.");
$changeset = $this->getChangeset($tableModel);
$input = objBag($request->all(), false);
$code = 200;
switch ($input->action) {
case 'row.update':
$data = (object)$input->data;
$changeset->rowUpdate($data);
$resp = $changeset->fetchAndTransformRow($data->_id);
break;
case 'row.remove':
$changeset->rowRemove($input->id);
$resp = $changeset->fetchAndTransformRow($input->id);
break;
case 'row.restore':
$changeset->rowRestore($input->id);
$resp = $changeset->fetchAndTransformRow($input->id);
break;
default:
$resp = "Bad Action";
$code = 400;
break;
}
$this->storeChangeset($changeset);
return $this->jsonResponse($resp, $code);
}
}