]> _ Git - cubist_cms-back.git/commitdiff
#2810
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Wed, 12 Jun 2019 14:44:08 +0000 (16:44 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Wed, 12 Jun 2019 14:44:08 +0000 (16:44 +0200)
src/app/Magic/Controllers/CubistMagicControllerTrait.php
src/app/Magic/Fields/Field.php
src/app/Magic/Fields/SelectFromModel.php
src/app/Magic/Fields/SelectFromModelMultiple.php [deleted file]

index 3b511f4b059dcb1802e76cd52daad65a580bb379..da2f4b04bffaf321429e0b5e08b0ad34a73ead86 100644 (file)
@@ -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
index 89517a7c5c9c26c376241c99b177f680bdb0176a..49e7737be7f16b7ac6477e87a0323ddac719615c 100644 (file)
@@ -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();
index 4db6a3db3edd07e1d84d628b126a89029f1f2004..5bbce1c771f64c4b0ecf8631bf70d649ff9d5ef9 100644 (file)
@@ -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 (file)
index 7461e40..0000000
+++ /dev/null
@@ -1,25 +0,0 @@
-<?php
-
-
-namespace Cubist\Backpack\app\Magic\Fields;
-
-
-class SelectFromModelMultiple extends SelectFromModel
-{
-    protected $_multiple = true;
-    protected $_cast = 'array';
-    protected $_order = false;
-
-    public function _postSetAttributes()
-    {
-        parent::_postSetAttributes(); // TODO: Change the autogenerated stub
-        if ($this->getAttribute('order')) {
-            $this->_adminType = 'select_and_order';
-        }
-    }
-
-    public function getDefaultAttributes()
-    {
-        return array_merge(parent::getDefaultAttributes(), ['order' => $this->_order]);
-    }
-}