]> _ Git - cubist_cms-back.git/commitdiff
#2843
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Thu, 4 Jul 2019 17:06:15 +0000 (19:06 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Thu, 4 Jul 2019 17:06:15 +0000 (19:06 +0200)
src/app/Magic/Fields/PageInternal.php [new file with mode: 0644]
src/app/Magic/Models/CMSPage.php
src/app/Magic/Models/CubistMagicAbstractModel.php
src/app/Template/TemplateAbstract.php
src/resources/views/fields/button.blade.php

diff --git a/src/app/Magic/Fields/PageInternal.php b/src/app/Magic/Fields/PageInternal.php
new file mode 100644 (file)
index 0000000..b4c1322
--- /dev/null
@@ -0,0 +1,17 @@
+<?php
+
+
+namespace Cubist\Backpack\app\Magic\Fields;
+
+
+use Cubist\Backpack\app\Magic\Models\CMSPage;
+
+class PageInternal extends SelectFromArray
+{
+
+    protected function _postSetAttributes()
+    {
+        parent::_postSetAttributes();
+        $this->setAttribute('options', CMSPage::getPagesList());
+    }
+}
index bdaf96c4bc7fc21669c8ecd33eb26310a0b88e31..9a13b0976bce242c073cbac6fd74fa6a9ca178f0 100644 (file)
@@ -7,11 +7,17 @@ use Cubist\Backpack\app\Magic\Controllers\CubistMagicController;
 use Cubist\Backpack\app\Template\TemplateAbstract;
 use Doctrine\DBAL\Schema\Schema;
 use Doctrine\DBAL\Schema\Table;
+use Illuminate\Support\Facades\DB;
 
 class CMSPage extends CubistMagicNestedModel
 {
     protected static $_templates = [];
+    protected $_usedTemplate = null;
 
+    protected static $_templatesById = null;
+    protected static $_pagesList = null;
+
+    protected static $_table = 'cubist_cms_pages';
     protected $table = 'cubist_cms_pages';
 
     protected $_options = ['name' => 'page',
@@ -20,11 +26,6 @@ class CMSPage extends CubistMagicNestedModel
 
     protected $defaultFieldAttributes = ['translatable' => true];
 
-    public static function boot()
-    {
-        parent::boot();
-    }
-
     public function setFields()
     {
         parent::setFields();
@@ -81,7 +82,7 @@ class CMSPage extends CubistMagicNestedModel
      */
     public function onBeforeCreate($controller)
     {
-        $this->useTemplate(request('template'), $controller);
+        $this->useTemplateIfNotSet(request('template'), $controller);
         parent::onBeforeCreate($controller);
     }
 
@@ -90,24 +91,26 @@ class CMSPage extends CubistMagicNestedModel
         $template = request('template');
         // if the template in the GET parameter is missing, figure it out from the db
         if ($template == false) {
-            /** @var self $entry */
-            $controller->data['entry'] = self::findOrFail($id);
-            $template = $controller->data['entry']->template;
+            $entry = self::findOrFail($id);
+            $template = $entry->template;
         }
 
-        $this->useTemplate($template, $controller);
+        $this->useTemplateIfNotSet($template);
+        $controller->updateFieldsFromModel($this);
         parent::onBeforeEdit($controller, $id);
     }
 
     public function onBeforeStore($controller, $request)
     {
-        $this->useTemplate(\Request::input('template'), $controller);
+        $this->useTemplateIfNotSet(\Request::input('template'));
+        $controller->updateFieldsFromModel($this);
         parent::onBeforeStore($controller, $request);
     }
 
     public function onBeforeUpdate($controller, $request)
     {
-        $this->useTemplate(\Request::input('template'), $controller);
+        $this->useTemplateIfNotSet(\Request::input('template'));
+        $controller->updateFieldsFromModel($this);
         parent::onBeforeUpdate($controller, $request);
     }
 
@@ -116,15 +119,18 @@ class CMSPage extends CubistMagicNestedModel
      * @param $controller CubistMagicController
      * @throws \Exception
      */
-    protected function useTemplate($template, $controller = null)
+    public function useTemplate($template)
     {
         if (is_string($template)) {
             $template = TemplateAbstract::getTemplateIntanceByName($template);
         }
+
         if (null === $template) {
             return;
         }
 
+        $this->_usedTemplate = $template;
+
         $fields = $template->getFields();
         if (!count($fields)) {
             return;
@@ -133,8 +139,12 @@ class CMSPage extends CubistMagicNestedModel
             $attr = array_merge($this->defaultFieldAttributes, $field);
             $this->addFakeField($attr);
         }
-        if (null !== $controller) {
-            $controller->updateFieldsFromModel();
+    }
+
+    public function useTemplateIfNotSet($template)
+    {
+        if ($this->_usedTemplate === null) {
+            $this->useTemplate($template);
         }
     }
 
@@ -168,9 +178,22 @@ class CMSPage extends CubistMagicNestedModel
         return parent::update($attributes, $options);
     }
 
-    public static function getMenu(){
 
-    }
+    public function setRawAttributes(array $attributes, $sync = false)
+    {
+        if (isset($attributes['template']) && null === $this->_usedTemplate) {
+            $this->useTemplateIfNotSet($attributes['template']);
+        }
 
+        $res = parent::setRawAttributes($attributes, $sync);
+        return $res;
+    }
 
+    public static function getPagesList()
+    {
+        if (null === static::$_pagesList) {
+            static::$_pagesList = DB::table(self::$_table)->orderBy('lft')->get()->pluck('name', 'id');
+        }
+        return static::$_pagesList;
+    }
 }
index de85ba7bc76977f4fcae4c94247a179a9786685e..0c7df5cddf3372dce34150754401f7818f658e8b 100644 (file)
@@ -16,6 +16,7 @@ use Doctrine\DBAL\Schema\Schema;
 use Doctrine\DBAL\Schema\Table;
 use Illuminate\Database\Eloquent\Model;
 use Illuminate\Support\Str;
+use mysql_xdevapi\Exception;
 use Spatie\MediaLibrary\HasMedia\HasMedia;
 use Spatie\MediaLibrary\HasMedia\HasMediaTrait;
 use Spatie\MediaLibrary\Models\Media;
@@ -191,11 +192,13 @@ class CubistMagicAbstractModel extends Model implements HasMedia
             if (!in_array($name, $this->fillable)) {
                 $this->fillable[] = $name;
             }
-            //$this->casts[$store_in] = 'array';
-            if ($field->getAttribute('translatable')) {
+            $this->casts[$store_in] = 'array';
+            if ($field->getAttribute('translatable') && !in_array($store_in, $this->translatable)) {
                 $this->translatable[] = $store_in;
             }
-            $this->fakeColumns[] = $store_in;
+            if (!in_array($store_in, $this->fakeColumns)) {
+                $this->fakeColumns[] = $store_in;
+            }
         } else {
             if ($field->getAttribute('fillable')) {
                 $this->fillable[] = $name;
@@ -447,15 +450,6 @@ class CubistMagicAbstractModel extends Model implements HasMedia
     protected function _prepareData($attributes)
     {
         return Json::decodeRecursive($attributes, Json::TYPE_ARRAY);
-
-        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 = [])
index b8e97a156ee4b1956ab3104b1507b1d90b5bcccc..c65f7a02a02ef1921e5b0add8345fc04c9dc4092 100644 (file)
@@ -73,9 +73,8 @@ class TemplateAbstract
      */
     public static function getTemplates()
     {
-        $exclude = ['App\\Templates\\Base', 'Cubist\\Backpack\\app\\Template\\TemplateAbstract', 'Cubist\\Backpack\\app\\Template\\TemplatePage'];
-
         if (null === self::$_templates) {
+            $exclude = ['App\\Templates\\Base', 'Cubist\\Backpack\\app\\Template\\TemplateAbstract', 'Cubist\\Backpack\\app\\Template\\TemplatePage'];
             self::$_templates = [];
             $roots = [__DIR__ => 'Cubist\\Backpack\\app\\Template', app_path() . '/Templates' => 'App\\Templates'];
             foreach ($roots as $templates_root => $prefix) {
index 694969f9d9ea639bc7ad2e29e4ae001d3abf7c02..1e254045dcc3d4075693b72c455b2174bf94546f 100644 (file)
@@ -4,7 +4,7 @@
 $link_types = ['none' => 'Désactiver', 'page_link' => trans('backpack::crud.page_link'), 'internal_link' => trans('backpack::crud.internal_link'), 'external_link' => trans('backpack::crud.external_link')];
 $field['allows_null'] = false;
 $page_model = $field['page_model'];
-$active_pages = $page_model::all();
+$active_pages = \Cubist\Backpack\app\Magic\Models\CMSPage::getPagesList();
 
 $empty = ['label' => '', 'type' => 'page_link', 'link' => '', 'external_link' => '', 'page_id' => ''];
 $value = old(square_brackets_to_dots($field['name'])) ?? $field['value'] ?? $field['default'] ?? [];
@@ -84,12 +84,12 @@ $value = array_merge($empty, \Cubist\Util\ArrayUtil::asArray($value));
                     @if (!count($active_pages))
                         <option value="">-</option>
                     @else
-                        @foreach ($active_pages as $key => $page)
-                            <option value="{{ $page->id }}"
-                                    @if (isset($value['page_id']) && $page->id==$value['page_id'])
+                        @foreach ($active_pages as $id => $page)
+                            <option value="{{ $id }}"
+                                    @if (isset($value['page_id']) && $id==$value['page_id'])
                                     selected
                                 @endif
-                            >{{ $page->name }}</option>
+                            >{{ $page }}</option>
                         @endforeach
                     @endif