From 6fd5806f9f7ec93cc9872da4863e2c953cc2957f Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Fri, 12 Nov 2021 13:38:04 +0100 Subject: [PATCH] wip #4877 @2 --- src/app/CubistCrudPanel.php | 10 +++ .../Controllers/CubistMagicController.php | 76 ------------------- src/app/Magic/Fields/APIToken.php | 3 +- src/app/Magic/Fields/Field.php | 5 +- .../Models/CubistMagicAuthenticatable.php | 4 +- src/app/Middleware/BunchRequest.php | 47 ++++++++++++ 6 files changed, 62 insertions(+), 83 deletions(-) create mode 100644 src/app/Middleware/BunchRequest.php diff --git a/src/app/CubistCrudPanel.php b/src/app/CubistCrudPanel.php index 1ed0b46..24d830b 100644 --- a/src/app/CubistCrudPanel.php +++ b/src/app/CubistCrudPanel.php @@ -5,6 +5,9 @@ namespace Cubist\Backpack; use Backpack\CRUD\app\Exceptions\AccessDeniedException; use Backpack\CRUD\app\Library\CrudPanel\CrudPanel; use Cubist\Backpack\Magic\Models\CubistMagicAbstractModel; +use Cubist\Backpack\Magic\Requests\CubistMagicRequest; +use Illuminate\Http\Request; +use Illuminate\Support\Arr; /** * @property CubistMagicAbstractModel $model @@ -161,4 +164,11 @@ class CubistCrudPanel extends CrudPanel } return $this->entry; } + + + + public function getStrippedSaveRequest() + { + dd($this->getRequest()); + } } diff --git a/src/app/Magic/Controllers/CubistMagicController.php b/src/app/Magic/Controllers/CubistMagicController.php index 7d6eea9..dd5b018 100644 --- a/src/app/Magic/Controllers/CubistMagicController.php +++ b/src/app/Magic/Controllers/CubistMagicController.php @@ -196,82 +196,6 @@ class CubistMagicController extends CubistCrudController { return $this->crud->getModel(); } - - /** - * @return CubistMagicRequest - */ - protected function _prepareCRUDData() - { - $request = $this->crud->validateRequest(); - - $appendComposite = []; - foreach ($request->all() as $field => $content) { - $initialContent = $content; - $initialField = $field; - - $change = false; - $unsetInitial = false; - $e = explode('___', $field); - if (count($e) > 1) { - $change = false; - $unsetInitial = true; - $field = $e[0]; - for ($i = 1; $i < count($e); $i++) { - $field .= '.' . $e[$i]; - } - Arr::set($appendComposite, $field, $content); - } - - if ($change) { - $request->request->set($field, $content); - } - if ($unsetInitial) { - $request->request->set($initialField, null); - } - } - - foreach ($appendComposite as $key => $value) { - $request->request->set($key, $value); - } - - return $request; - } - - /** - * @return \Illuminate\Database\Eloquent\Model|\Illuminate\Http\RedirectResponse - */ - public function store() - { - $this->_forgetCache(); - - $request = $this->_prepareCRUDData(); - $this->getModelInstance()->onBeforeStore($this, $request); - - // your additional operations before save here - $redirect_location = parent::store(); - // your additional operations after save here - // use $this->data['entry'] or $this->crud->entry - return $redirect_location; - } - - /** - * @param CubistMagicUpdateRequest $request - * @return \Illuminate\Http\RedirectResponse - */ - public function update() - { - $this->_forgetCache(); - - $request = $this->_prepareCRUDData(); - $this->getModelInstance()->onBeforeUpdate($this, $request); - - // your additional operations before save here - $redirect_location = parent::update(); - // your additional operations after save here - // use $this->data['entry'] or $this->crud->entry - return $redirect_location; - } - protected function _forgetCache() { try { diff --git a/src/app/Magic/Fields/APIToken.php b/src/app/Magic/Fields/APIToken.php index bd42f33..1af53cc 100644 --- a/src/app/Magic/Fields/APIToken.php +++ b/src/app/Magic/Fields/APIToken.php @@ -11,7 +11,8 @@ class APIToken extends Text protected $_databaseUnique = true; protected $_databaseType = 'string'; - protected $_translatable=false; + protected $_translatable = false; + protected $_databaseDefault = null; public function __construct($attributes) { diff --git a/src/app/Magic/Fields/Field.php b/src/app/Magic/Fields/Field.php index 7e59388..b5ed09e 100644 --- a/src/app/Magic/Fields/Field.php +++ b/src/app/Magic/Fields/Field.php @@ -306,10 +306,7 @@ class Field implements \ArrayAccess $this->_databaseAttributes ); - $default = $this->getAttribute('database_default') ?? $this->getAttribute('default'); - if ($default !== null) { - $attributes['default'] = $default; - } + $attributes['default'] = $this->getAttribute('database_default'); $length = $this->getDatabaseLength(); if (null !== $length) { $attributes['length'] = $length; diff --git a/src/app/Magic/Models/CubistMagicAuthenticatable.php b/src/app/Magic/Models/CubistMagicAuthenticatable.php index 3c1b2c3..feeb279 100644 --- a/src/app/Magic/Models/CubistMagicAuthenticatable.php +++ b/src/app/Magic/Models/CubistMagicAuthenticatable.php @@ -57,7 +57,8 @@ class CubistMagicAuthenticatable extends CubistMagicAbstractModel 'label' => 'Enabled', 'type' => 'checkbox', 'tab' => 'Login', - 'default' => true]); + 'default' => true, + 'database_default' => true]); $this->addField(['name' => 'remember_token', 'type' => 'Text', @@ -81,7 +82,6 @@ class CubistMagicAuthenticatable extends CubistMagicAbstractModel $this->addField(['name' => 'api_token', 'label' => 'API Token', 'type' => 'APIToken', - 'database_default' => '', 'tab' => 'Login', 'can' => 'users:admin' ]); diff --git a/src/app/Middleware/BunchRequest.php b/src/app/Middleware/BunchRequest.php new file mode 100644 index 0000000..ea37c8b --- /dev/null +++ b/src/app/Middleware/BunchRequest.php @@ -0,0 +1,47 @@ +all() as $field => $content) { + $initialContent = $content; + $initialField = $field; + + $change = false; + $unsetInitial = false; + $e = explode('___', $field); + if (count($e) > 1) { + $change = false; + $unsetInitial = true; + $field = $e[0]; + for ($i = 1; $i < count($e); $i++) { + $field .= '.' . $e[$i]; + } + Arr::set($appendComposite, $field, $content); + } + + if ($change) { + $request->request->set($field, $content); + } + if ($unsetInitial) { + $request->request->set($initialField, null); + } + } + + + foreach ($appendComposite as $key => $value) { + $request->request->set($key, $value); + } + return $this->getResponse(); + } +} -- 2.39.5