forget($table->draftSessionKey); } /** @var Changeset $changeset */ return session()->remember($table->draftSessionKey, function () use ($table) { $changeset = new Changeset(); $changeset->table = $table; $changeset->revision = $table->revision; return $changeset; }); } private function storeChangeset(Changeset $chs) { session()->put($chs->table->draftSessionKey, $chs); } /** * Discard draft and redirect to table view */ public function discard(User $user, string $table) { /** @var Table $tableModel */ $tableModel = $user->tables()->where('name', $table)->first(); if ($tableModel === null) abort(404, "No such table."); session()->forget($tableModel->draftSessionKey); return redirect($tableModel->viewRoute); } 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, ]); } /** @noinspection PhpUnusedPrivateMethodInspection */ private function addRows(Changeset $changeset) { $columns = $changeset->fetchAndTransformColumns(); $rows = $changeset->getAddedRows(25); return view('table.propose.add-rows', [ 'changeset' => $changeset, 'table' => $changeset->table, 'columns' => collect($columns), 'rows' => $rows, ]); } /** @noinspection PhpUnusedPrivateMethodInspection */ private function manageColumns(Changeset $changeset) { $columns = $changeset->fetchAndTransformColumns(); return view('table.propose.manage-columns', [ 'changeset' => $changeset, 'table' => $changeset->table, 'columns' => collect($columns), ]); } public function draftUpdate(Request $request, User $user, string $table) { /** @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); $resp = null; $code = 200; try { switch ($input->action) { case 'row.update': $data = (object)$input->data; $resp = $changeset->rowUpdate($data); break; case 'row.update-many': $newVals = $input->data; $updated = []; foreach ($newVals as $rowUpdate) { $r = $changeset->rowUpdate($rowUpdate); $updated[$r->_id] = $r; } $resp = $updated; break; case 'row.remove': $isNew = $changeset->isNewRow($input->id); $changeset->rowRemove($input->id); $resp = $isNew ? null : $changeset->fetchAndTransformRow($input->id); break; case 'rows.remove-empty-new': $changeset->removeEmptyNewRows(); break; case 'row.restore': $changeset->rowRestore($input->id); $resp = $changeset->fetchAndTransformRow($input->id); break; case 'rows.add': $changeset->addBlankRows($input->count); break; case 'rows.add-csv': try { $changeset->addFilledRows(Utils::csvToArray($input->data)); } catch (\Exception $e) { return $this->backWithErrors(['data' => $e->getMessage()]); } break; case 'col.update': $data = (object)$input->data; $resp = $changeset->columnUpdate($data); break; case 'col.remove': $isNew = $changeset->isNewColumn($input->id); $changeset->columnRemove($input->id); $resp = $isNew ? null : $changeset->fetchAndTransformColumn($input->id); break; case 'col.restore': $changeset->columnRestore($input->id); $resp = $changeset->fetchAndTransformColumn($input->id); break; case 'col.add': $resp = $changeset->addBlankCol(); break; case 'col.sort': $changeset->setColOrder($input->order); break; default: $resp = "Bad Action"; $code = 400; break; } } catch (SimpleValidationException $e) { return $this->jsonResponse(['errors' => $e->getMessageBag()->getMessages()], 400); } $this->storeChangeset($changeset); // Redirect requested via form if ($code == 200 && $input->has('redirect')) { return redirect($input->redirect); } return $this->jsonResponse($resp, $code); } }