From: Vincent Vanwaelscappel Date: Tue, 18 Jun 2019 13:59:37 +0000 (+0200) Subject: wip #2843 @2 X-Git-Url: http://git.cubedesigners.com/?a=commitdiff_plain;h=de93af30041a62975d0cd42efa17e92034946e12;p=cubist_cms-back.git wip #2843 @2 --- diff --git a/src/app/Http/Controllers/CubistPageCrudController.php b/src/app/Http/Controllers/CubistPageCrudController.php index deea027..817acc0 100644 --- a/src/app/Http/Controllers/CubistPageCrudController.php +++ b/src/app/Http/Controllers/CubistPageCrudController.php @@ -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 index 0000000..c4992ff --- /dev/null +++ b/src/app/Magic/BunchOfFields.php @@ -0,0 +1,36 @@ +_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 index 0000000..b67b400 --- /dev/null +++ b/src/app/Magic/Fields/BunchOfFields.php @@ -0,0 +1,43 @@ + []]); + } + + 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(); + } + } + } + } +} diff --git a/src/app/Magic/Models/CubistMagicAbstractModel.php b/src/app/Magic/Models/CubistMagicAbstractModel.php index 21714b0..f8bc34a 100644 --- a/src/app/Magic/Models/CubistMagicAbstractModel.php +++ b/src/app/Magic/Models/CubistMagicAbstractModel.php @@ -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 } - - } /** diff --git a/src/app/Template/TemplateAbstract.php b/src/app/Template/TemplateAbstract.php index b862b24..fcc598c 100644 --- a/src/app/Template/TemplateAbstract.php +++ b/src/app/Template/TemplateAbstract.php @@ -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; } }