From bf10f033ee5bba5982859f65cafbd3e898b8dda6 Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Mon, 27 May 2019 19:03:02 +0200 Subject: [PATCH] #2783 --- .../Controllers/CubistMagicController.php | 86 +------------------ .../CubistMagicControllerTrait.php | 83 ++++++++++++++++++ .../CubistNestedMagicController.php | 21 +++++ .../Magic/Models/CubistMagicModelAbstract.php | 4 +- src/resources/cubistmagic/Controller.stub | 5 +- 5 files changed, 112 insertions(+), 87 deletions(-) create mode 100644 src/app/Magic/Controllers/CubistMagicControllerTrait.php create mode 100644 src/app/Magic/Controllers/CubistNestedMagicController.php diff --git a/src/app/Magic/Controllers/CubistMagicController.php b/src/app/Magic/Controllers/CubistMagicController.php index 9c58e31..5910a08 100644 --- a/src/app/Magic/Controllers/CubistMagicController.php +++ b/src/app/Magic/Controllers/CubistMagicController.php @@ -3,95 +3,15 @@ namespace Cubist\Backpack\app\Magic\Controllers; +use Backpack\CRUD\app\Http\Controllers\CrudController; use Backpack\CRUD\CrudTrait; -use Cubist\Backpack\app\Magic\Fields\Field; -use Cubist\Backpack\app\Magic\Models\CubistMagicModelAbstract; -use Cubist\Backpack\app\Magic\Requests\CubistMagicStoreRequest; -use Cubist\Backpack\app\Magic\Requests\CubistMagicUpdateRequest; -use Webfactor\Laravel\Backpack\NestedModels\Controllers\NestedModelsCrudController; -class CubistMagicController extends NestedModelsCrudController +class CubistMagicController extends CrudController { - use CrudTrait; + use CrudTrait,CubistMagicControllerTrait; protected $_modelNamespace; protected $_routeURL; protected $_singular; protected $_plural; - protected $_nested = false; - - protected $_storeRequest; - protected $_updateRequest; - - public function setup() - { - if (!$this->_routeURL) { - $this->_routeURL = $this->_singular; - } - if (!$this->_plural) { - $this->_plural = $this->_singular . 's'; - } - - /* - |-------------------------------------------------------------------------- - | CrudPanel Basic Information - |-------------------------------------------------------------------------- - */ - $this->crud->setModel($this->_modelNamespace); - if ($this->_nested) { - $this->treeSetup(); - } - $this->crud->setRoute(config('backpack.base.route_prefix') . '/' . $this->_routeURL); - $this->crud->setEntityNameStrings($this->_singular, $this->_plural); - - - /* - |-------------------------------------------------------------------------- - | CrudPanel Configuration - |-------------------------------------------------------------------------- - */ - foreach ($this->getModelInstance()->getFields() as $field) { - $this->addField($field); - } - - } - - public function addField($field) - { - if (is_array($field)) { - $field = Field::getInstance($field); - } - /** @var $field Field */ - if ($field->isDisplayColumn()) { - $this->crud->addColumn($field->getColumnData()); - } - $this->crud->addField($field->getDefinition(), $field->getCRUDForm()); - } - - /** - * @return CubistMagicModelAbstract - */ - public function getModelInstance() - { - return $this->crud->getModel(); - } - - - public function store(CubistMagicStoreRequest $request) - { - // your additional operations before save here - $redirect_location = parent::storeCrud($request); - // your additional operations after save here - // use $this->data['entry'] or $this->crud->entry - return $redirect_location; - } - - public function update(CubistMagicUpdateRequest $request) - { - // your additional operations before save here - $redirect_location = parent::updateCrud($request); - // your additional operations after save here - // use $this->data['entry'] or $this->crud->entry - return $redirect_location; - } } diff --git a/src/app/Magic/Controllers/CubistMagicControllerTrait.php b/src/app/Magic/Controllers/CubistMagicControllerTrait.php new file mode 100644 index 0000000..d44f414 --- /dev/null +++ b/src/app/Magic/Controllers/CubistMagicControllerTrait.php @@ -0,0 +1,83 @@ +_routeURL) { + $this->_routeURL = $this->_singular; + } + if (!$this->_plural) { + $this->_plural = $this->_singular . 's'; + } + + /* + |-------------------------------------------------------------------------- + | CrudPanel Basic Information + |-------------------------------------------------------------------------- + */ + $this->crud->setModel($this->_modelNamespace); + $this->_afterSetModel(); + $this->crud->setRoute(config('backpack.base.route_prefix') . '/' . $this->_routeURL); + $this->crud->setEntityNameStrings($this->_singular, $this->_plural); + + + /* + |-------------------------------------------------------------------------- + | CrudPanel Configuration + |-------------------------------------------------------------------------- + */ + foreach ($this->getModelInstance()->getFields() as $field) { + $this->addField($field); + } + + } + + public function addField($field) + { + if (is_array($field)) { + $field = Field::getInstance($field); + } + /** @var $field Field */ + if ($field->isDisplayColumn()) { + $this->crud->addColumn($field->getColumnData()); + } + $this->crud->addField($field->getDefinition(), $field->getCRUDForm()); + } + + /** + * @return CubistMagicModelAbstract + */ + public function getModelInstance() + { + return $this->crud->getModel(); + } + + + public function store(CubistMagicStoreRequest $request) + { + // your additional operations before save here + $redirect_location = parent::storeCrud($request); + // your additional operations after save here + // use $this->data['entry'] or $this->crud->entry + return $redirect_location; + } + + public function update(CubistMagicUpdateRequest $request) + { + // your additional operations before save here + $redirect_location = parent::updateCrud($request); + // your additional operations after save here + // use $this->data['entry'] or $this->crud->entry + return $redirect_location; + } + + +} diff --git a/src/app/Magic/Controllers/CubistNestedMagicController.php b/src/app/Magic/Controllers/CubistNestedMagicController.php new file mode 100644 index 0000000..b2e86ab --- /dev/null +++ b/src/app/Magic/Controllers/CubistNestedMagicController.php @@ -0,0 +1,21 @@ +treeSetup(); + } +} diff --git a/src/app/Magic/Models/CubistMagicModelAbstract.php b/src/app/Magic/Models/CubistMagicModelAbstract.php index 0ae20ce..8840436 100644 --- a/src/app/Magic/Models/CubistMagicModelAbstract.php +++ b/src/app/Magic/Models/CubistMagicModelAbstract.php @@ -3,6 +3,7 @@ namespace Cubist\Backpack\app\Magic\Models; +use Backpack\CRUD\CrudTrait; use Cubist\Backpack\app\Magic\Fields\Field; use Cubist\Backpack\app\Magic\Util; use Cviebrock\EloquentSluggable\Sluggable; @@ -17,6 +18,7 @@ class CubistMagicModelAbstract extends Model { use CubistMagicAttribute; use SluggableScopeHelpers; + use CrudTrait; use NestedModelTrait { replicate as private replicateNodeTrait; } @@ -141,7 +143,7 @@ class CubistMagicModelAbstract extends Model 'SINGULAR' => $this->getOption('singular', $this->getOption('name')), 'PLURAL' => $this->getOption('plural', ''), 'MODELNAMESPACE' => get_class($this), - 'NESTED' => $this->nested ? 'true' : 'false', + 'EXTENDS' => $this->nested ? 'CubistNestedMagicController' : 'CubistMagicController', ]; $res = file_get_contents($stub); diff --git a/src/resources/cubistmagic/Controller.stub b/src/resources/cubistmagic/Controller.stub index d7d0a2f..e0e025a 100644 --- a/src/resources/cubistmagic/Controller.stub +++ b/src/resources/cubistmagic/Controller.stub @@ -2,13 +2,12 @@ namespace App\Http\Controllers\Admin; -use Cubist\Backpack\app\Magic\Controllers\CubistMagicController; +use Cubist\Backpack\app\Magic\Controllers\*; -class _CONTROLLERCLASS_ extends CubistMagicController +class _CONTROLLERCLASS_ extends _EXTENDS_ { protected $_modelNamespace = '_MODELNAMESPACE_'; protected $_routeURL = '_ROUTEURL_'; protected $_singular = '_SINGULAR_'; protected $_plural = '_PLURAL_'; - protected $_nested = _NESTED_; } -- 2.39.5