]> _ Git - cubist_cms-back.git/commitdiff
#2843
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Wed, 3 Jul 2019 18:08:45 +0000 (20:08 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Wed, 3 Jul 2019 18:08:45 +0000 (20:08 +0200)
src/app/Magic/Models/CMSPage.php
src/app/Magic/Models/CubistMagicAbstractModel.php

index 15df6c82a93e371092fddf30f07ade67f830ef41..40d5693501c2df3f5d8c85c3cbad5aeeeec48f3d 100644 (file)
@@ -5,6 +5,8 @@ namespace Cubist\Backpack\app\Magic\Models;
 
 use Cubist\Backpack\app\Magic\Controllers\CubistMagicController;
 use Cubist\Backpack\app\Template\TemplateAbstract;
+use Doctrine\DBAL\Schema\Schema;
+use Doctrine\DBAL\Schema\Table;
 
 class CMSPage extends CubistMagicNestedModel
 {
@@ -107,7 +109,7 @@ class CMSPage extends CubistMagicNestedModel
      * @param $controller CubistMagicController
      * @throws \Exception
      */
-    protected function useTemplate($template, $controller)
+    protected function useTemplate($template, $controller = null)
     {
         if (is_string($template)) {
             $template = TemplateAbstract::getTemplateIntanceByName($template);
@@ -124,7 +126,27 @@ class CMSPage extends CubistMagicNestedModel
             $attr = array_merge($this->defaultFieldAttributes, $field);
             $this->addFakeField($attr);
         }
-        $controller->updateFieldsFromModel();
+        if (null !== $controller) {
+            $controller->updateFieldsFromModel();
+        }
+    }
+
+    /**
+     * @param $schema Schema
+     * @return Table
+     */
+    public function setSchema($schema)
+    {
+        // We need to know all the columns that will be needed by all the templates
+        $this->useAllTemplates();
+        parent::setSchema($schema);
+    }
+
+    public function useAllTemplates()
+    {
+        foreach (TemplateAbstract::getTemplates() as $template) {
+            $this->useTemplate($template);
+        }
     }
 
     public function update(array $attributes = [], array $options = [])
index 876f0ab26cd9c51e8a1539eae3eaf97aa84d8fe7..060336f734725314ae9a3206527d207661710a37 100644 (file)
@@ -445,20 +445,35 @@ class CubistMagicAbstractModel extends Model implements HasMedia
         return Media::where('model_id', $this->getKey())->where('model_type', get_class($this))->get();
     }
 
-    public function update(array $attributes = [], array $options = [])
+    protected function _prepareData($attributes)
     {
+        print_r($attributes);
         $attributes = Json::decodeRecursive($attributes, Json::TYPE_ARRAY);
-        return $this->updateTranslations($attributes, $options);
+        $res = [];
+        foreach ($attributes as $key => $attribute) {
+            if (is_array($attribute) || is_object($attribute)) {
+                $res[$key] = json_encode($attribute);
+            } else {
+                $res[$key] = $attribute;
+            }
+        }
+        return $res;
+    }
+
+    public function update(array $attributes = [], array $options = [])
+    {
+
+        return $this->updateTranslations($this->_prepareData($attributes), $options);
     }
 
     public function create(array $attributes = [])
     {
-        $attributes = Json::decodeRecursive($attributes, Json::TYPE_ARRAY);
-        return $this->createTranslations($attributes);
+        return $this->createTranslations($this->_prepareData($attributes));
     }
 
-    public function getSlugAttribute($value){
-        if(!$value){
+    public function getSlugAttribute($value)
+    {
+        if (!$value) {
             return Str::slug($this->title);
         }
         return $value;