]> _ Git - cubist_cms-back.git/commitdiff
wip #2843 @2
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Tue, 18 Jun 2019 13:59:37 +0000 (15:59 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Tue, 18 Jun 2019 13:59:37 +0000 (15:59 +0200)
src/app/Http/Controllers/CubistPageCrudController.php
src/app/Magic/BunchOfFields.php [new file with mode: 0644]
src/app/Magic/Fields/BunchOfFields.php [new file with mode: 0644]
src/app/Magic/Models/CubistMagicAbstractModel.php
src/app/Template/TemplateAbstract.php

index deea0271fc36bd8beaaa916dfad2963acac9dd15..817acc00cfa569920951859ce275230b23e306e9 100644 (file)
@@ -4,12 +4,10 @@ namespace Cubist\Backpack\app\Http\Controllers;
 
 use Backpack\PageManager\app\Http\Controllers\Admin\PageCrudController;
 use Cubist\Backpack\app\Template\TemplateAbstract;
-use Cubist\Backpack\app\Magic\CubistCrud;
 use Illuminate\Support\Str;
 
 class CubistPageCrudController extends PageCrudController
 {
-    use CubistCrud;
     protected static $_templates = null;
 
     /**
diff --git a/src/app/Magic/BunchOfFields.php b/src/app/Magic/BunchOfFields.php
new file mode 100644 (file)
index 0000000..c4992ff
--- /dev/null
@@ -0,0 +1,36 @@
+<?php
+
+
+namespace Cubist\Backpack\app\Magic;
+
+
+use Cubist\Backpack\app\Magic\Fields\Field;
+
+trait BunchOfFields
+{
+    /**
+     * @var Field[]
+     */
+    protected $_fields = [];
+
+    /**
+     * @param $attributes
+     * @return Field
+     * @throws \Exception
+     */
+    public function addField(array $attributes)
+    {
+        $field = Field::getInstance($attributes);
+        $this->_fields[$field->getAttribute('name')] = $field;
+
+        return $field;
+    }
+
+    /**
+     * @return Field[]
+     */
+    public function getFields()
+    {
+        return $this->_fields;
+    }
+}
diff --git a/src/app/Magic/Fields/BunchOfFields.php b/src/app/Magic/Fields/BunchOfFields.php
new file mode 100644 (file)
index 0000000..b67b400
--- /dev/null
@@ -0,0 +1,43 @@
+<?php
+
+
+namespace Cubist\Backpack\app\Magic\Fields;
+
+class BunchOfFields extends Field
+{
+    use \Cubist\Backpack\app\Magic\BunchOfFields;
+
+    public function getDefaultAttributes()
+    {
+        return array_merge(parent::getDefaultAttributes(), ['bunch' => []]);
+    }
+
+    public function _postSetAttributes()
+    {
+        parent::_postSetAttributes();
+        $this->_setBunchOfFields();
+    }
+
+    protected function _setBunchOfFields()
+    {
+        $bunch = $this->getAttribute('bunch');
+        if (is_array($bunch)) {
+            foreach ($bunch as $item) {
+                if (is_array($item)) {
+                    $f = Field::getInstance($item);
+                } else if ($item instanceof Field) {
+                    $f = $item;
+                }
+            }
+            $this->_fields[$f->getAttribute('name')] = $f;
+        }
+        if (is_string($bunch)) {
+            if (class_exists($bunch)) {
+                $bunchInstance = new $bunch();
+                if (method_exists($bunchInstance, 'getFields')) {
+                    $this->_fields = $bunchInstance->getFields();
+                }
+            }
+        }
+    }
+}
index 21714b03f35b18680eaaf513eb72a5f6885738a4..f8bc34a9840d7e3238e69a8aee327b4189ac617c 100644 (file)
@@ -4,6 +4,7 @@
 namespace Cubist\Backpack\app\Magic\Models;
 
 use Backpack\CRUD\CrudTrait;
+use Cubist\Backpack\app\Magic\BunchOfFields;
 use Cubist\Backpack\app\Magic\Fields\Field;
 use Cubist\Backpack\app\Magic\Util;
 use Backpack\CRUD\ModelTraits\SpatieTranslatable\Sluggable;
@@ -29,6 +30,10 @@ class CubistMagicAbstractModel extends Model implements HasMedia
     use HasTranslations;
     use RevisionableTrait;
     use HasMediaTrait;
+    use BunchOfFields {
+        addField as protected bunchAddField;
+    }
+
 
     protected static $_doctrineTypesMapping = ['int' => 'integer'];
 
@@ -36,11 +41,6 @@ class CubistMagicAbstractModel extends Model implements HasMedia
     public $timestamps = true;
     public $clonable = true;
 
-    /**
-     * @var Field[]
-     */
-    protected $_fields = [];
-
     /**
      * @var array
      */
@@ -90,8 +90,8 @@ class CubistMagicAbstractModel extends Model implements HasMedia
     }
 
 
-
-    public function onRetreived(){
+    public function onRetreived()
+    {
 
     }
 
@@ -189,15 +189,13 @@ class CubistMagicAbstractModel extends Model implements HasMedia
      */
     public function addField($attributes)
     {
-        /** @var Field $field */
-        $field = Field::getInstance($attributes);
+        $field = $this->bunchAddField($attributes);
         $field->setModelInstance($this);
         if (is_callable([$field, 'getRelationship']) && null !== $field->getRelationship()) {
             $this->_addRelationship($field);
         }
 
         $name = $field->getAttribute('name');
-        $this->_fields[$name] = $field;
 
         if ($field->getAttribute('fake', false) === true) {
             $store_in = $field->getAttribute('store_in');
@@ -229,8 +227,6 @@ class CubistMagicAbstractModel extends Model implements HasMedia
         }
 
 
-
-
     }
 
     /**
index b862b24e45352f93f38c93217bf4381ed817f792..fcc598c24b8c312bcf2838fa65cc9c8d85874b53 100644 (file)
@@ -3,10 +3,16 @@
 namespace Cubist\Backpack\app\Template;
 
 use Backpack\CRUD\CrudPanel;
+use Cubist\Backpack\app\Magic\BunchOfFields;
+use Cubist\Backpack\app\Magic\Fields\Field;
 use Illuminate\Support\Str;
 
 class TemplateAbstract
 {
+    use BunchOfFields {
+        addField as protected bunchAddField;
+    }
+
     /**
      * @var CrudPanel
      */
@@ -26,21 +32,21 @@ class TemplateAbstract
         $this->addField([
             'name' => 'meta_title',
             'label' => trans('backpack::pagemanager.meta_title'),
-            'type' => 'text',
+            'type' => 'Text',
             'hint' => trans('If empty, page title is used.') . ' ' . __('Recommended length: 60 chars'),
             'tab' => 'Meta // SEO',
         ]);
         $this->addField([
             'name' => 'meta_description',
             'label' => trans('backpack::pagemanager.meta_description'),
-            'type' => 'textarea',
+            'type' => 'Textarea',
             'hint' => __('Recommended length: 160 chars'),
             'tab' => 'Meta // SEO',
         ]);
         $this->addField([
             'name' => 'robots',
             'label' => __('Allow page index by search engines'),
-            'type' => 'checkbox',
+            'type' => 'Checkbox',
             'default' => true,
             'tab' => 'Meta // SEO',
         ]);
@@ -48,8 +54,6 @@ class TemplateAbstract
 
     public function init()
     {
-
-
         $this->_common();
     }
 
@@ -58,7 +62,7 @@ class TemplateAbstract
         $this->addField([
             'name' => 'name',
             'label' => trans('backpack::pagemanager.page_name'),
-            'type' => 'text',
+            'type' => 'Text',
             'wrapperAttributes' => [
                 'class' => 'form-group col-md-6',
             ],
@@ -69,7 +73,7 @@ class TemplateAbstract
         $this->addField([
             'name' => 'title',
             'label' => trans('backpack::pagemanager.page_title'),
-            'type' => 'text',
+            'type' => 'Text',
             'fake' => false,
             'tab' => 'General',
             // 'disabled' => 'disabled'
@@ -77,7 +81,7 @@ class TemplateAbstract
         $this->addField([
             'name' => 'slug',
             'label' => trans('backpack::pagemanager.page_slug'),
-            'type' => 'text',
+            'type' => 'Slug',
             'hint' => trans('backpack::pagemanager.page_slug_hint'),
             'fake' => false,
             'tab' => 'General',
@@ -86,7 +90,7 @@ class TemplateAbstract
 
         $this->addField([
             'name' => 'status',
-            'type' => 'select_from_array',
+            'type' => 'SelectFromArray',
             'default' => '0',
             'label' => __('Status'),
             'options' => ['0' => __('Offline'), '1' => __('Published')],
@@ -115,22 +119,23 @@ class TemplateAbstract
     }
 
     /**
-     * Add a field to the create/update form or both.
-     *
-     * @param string|array $field The new field.
-     * @param string $form The CRUD form. Can be 'create', 'update' or 'both'. Default is 'both'.
-     *
-     * @return CrudPanel
+     * @param array $attributes
+     * @return Field
+     * @throws \Exception
      */
-    public function addField($field, $form = 'both')
+    public function addField(array $attributes)
     {
         // Set default options of field
         $defaults = ['tab' => 'Contents',
             'fake' => true,
-            'store_in' => 'extras'];
-        $field = array_merge($defaults, $field);
+            'store_in' => 'extras',
+            'form' => 'both'];
+        $attributes = array_merge($defaults, $attributes);
+
+        $field = $this->bunchAddField($attributes);
 
+        $this->crud->addField($field->getDefinition(), $field->getAttribute('form'));
 
-        return $this->crud->addField($field, $form);
+        return $field;
     }
 }