From: Vincent Vanwaelscappel Date: Wed, 12 Jun 2019 14:44:08 +0000 (+0200) Subject: #2810 X-Git-Url: http://git.cubedesigners.com/?a=commitdiff_plain;h=048aa2c7d101aa52b8b12456883b149de930a3eb;p=cubist_cms-back.git #2810 --- diff --git a/src/app/Magic/Controllers/CubistMagicControllerTrait.php b/src/app/Magic/Controllers/CubistMagicControllerTrait.php index 3b511f4..da2f4b0 100644 --- a/src/app/Magic/Controllers/CubistMagicControllerTrait.php +++ b/src/app/Magic/Controllers/CubistMagicControllerTrait.php @@ -4,6 +4,7 @@ namespace Cubist\Backpack\app\Magic\Controllers; use Cubist\Backpack\app\Magic\Fields\Field; use Cubist\Backpack\app\Magic\Models\CubistMagicAbstractModel; +use Cubist\Backpack\app\Magic\Requests\CubistMagicRequest; use Cubist\Backpack\app\Magic\Requests\CubistMagicStoreRequest; use Cubist\Backpack\app\Magic\Requests\CubistMagicUpdateRequest; @@ -83,9 +84,28 @@ trait CubistMagicControllerTrait return $this->crud->getModel(); } + /** + * @param CubistMagicRequest $request + * @return CubistMagicRequest + */ + protected function _prepareCRUDData(CubistMagicRequest $request) + { + foreach ($request->all() as $field => $content) { + if (is_array($content)) { + $request->request->set($field, json_encode($content)); + } + } + return $request; + } + /** + * @param CubistMagicStoreRequest $request + * @return \Illuminate\Database\Eloquent\Model|\Illuminate\Http\RedirectResponse + */ public function store(CubistMagicStoreRequest $request) { + $request = $this->_prepareCRUDData($request); + // your additional operations before save here $redirect_location = parent::storeCrud($request); // your additional operations after save here @@ -93,8 +113,14 @@ trait CubistMagicControllerTrait return $redirect_location; } + /** + * @param CubistMagicUpdateRequest $request + * @return \Illuminate\Http\RedirectResponse + */ public function update(CubistMagicUpdateRequest $request) { + $request = $this->_prepareCRUDData($request); + // your additional operations before save here $redirect_location = parent::updateCrud($request); // your additional operations after save here diff --git a/src/app/Magic/Fields/Field.php b/src/app/Magic/Fields/Field.php index 89517a7..49e7737 100644 --- a/src/app/Magic/Fields/Field.php +++ b/src/app/Magic/Fields/Field.php @@ -68,7 +68,7 @@ class Field $this->_modelInstance = $modelInstance; return $this; } - + public function getDatabaseType() { @@ -102,6 +102,14 @@ class Field public function __construct($attributes) { + $aliases = ['multiple' => 'allows_multiple']; + foreach ($aliases as $alias => $real) { + if (isset($attributes[$alias])) { + $attributes[$real] = $attributes[$alias]; + unset($attributes[$alias]); + } + } + $this->_attributes = array_merge($this->getDefaultAttributes(), $attributes); $this->_postSetAttributes(); $this->init(); diff --git a/src/app/Magic/Fields/SelectFromModel.php b/src/app/Magic/Fields/SelectFromModel.php index 4db6a3d..5bbce1c 100644 --- a/src/app/Magic/Fields/SelectFromModel.php +++ b/src/app/Magic/Fields/SelectFromModel.php @@ -8,12 +8,27 @@ class SelectFromModel extends Model { protected $_adminType = 'select2_from_array'; protected $_columnType = 'select_from_array'; - protected $_databaseType = 'text'; + protected $_databaseType = 'string'; protected $_multiple = false; + protected $_order = false; + + public function getDefaultAttributes() + { + return array_merge(parent::getDefaultAttributes(), ['order' => $this->_order]); + } public function _postSetAttributes() { - parent::_postSetAttributes(); $this->setAttribute('options', $this->_getOptions()); + + if ($this->getAttribute('allows_multiple') && $this->getAttribute('order')) { + $this->setAttribute('type', 'select_and_order'); + } + + if ($this->getAttribute('allows_multiple')) { + $this->setAttribute('cast', 'json'); + } + + parent::_postSetAttributes(); } } diff --git a/src/app/Magic/Fields/SelectFromModelMultiple.php b/src/app/Magic/Fields/SelectFromModelMultiple.php deleted file mode 100644 index 7461e40..0000000 --- a/src/app/Magic/Fields/SelectFromModelMultiple.php +++ /dev/null @@ -1,25 +0,0 @@ -getAttribute('order')) { - $this->_adminType = 'select_and_order'; - } - } - - public function getDefaultAttributes() - { - return array_merge(parent::getDefaultAttributes(), ['order' => $this->_order]); - } -}