From: Vincent Vanwaelscappel Date: Mon, 1 Jul 2019 08:52:19 +0000 (+0200) Subject: #2843 X-Git-Url: http://git.cubedesigners.com/?a=commitdiff_plain;h=7d383db9353330e6d0c8ed9894aa1dc0cdf4d732;p=cubist_cms-back.git #2843 --- diff --git a/src/app/Magic/Controllers/CubistMagicController.php b/src/app/Magic/Controllers/CubistMagicController.php index 2449b7b..d681e57 100644 --- a/src/app/Magic/Controllers/CubistMagicController.php +++ b/src/app/Magic/Controllers/CubistMagicController.php @@ -5,11 +5,18 @@ namespace Cubist\Backpack\app\Magic\Controllers; use Cubist\Backpack\app\Http\Controllers\CubistCrudController; use Backpack\CRUD\CrudTrait; +use Cubist\Backpack\app\Magic\Fields\Field; +use Cubist\Backpack\app\Magic\Models\CubistMagicAbstractModel; +use Cubist\Backpack\app\Magic\Requests\CubistMagicRequest; +use Cubist\Backpack\app\Magic\Requests\CubistMagicStoreRequest; +use Cubist\Backpack\app\Magic\Requests\CubistMagicUpdateRequest; use Gaspertrix\Backpack\DropzoneField\Traits\HandleAjaxMedia; +use Illuminate\Support\Arr; +use Illuminate\Support\Facades\Redirect; class CubistMagicController extends CubistCrudController { - use CrudTrait, CubistMagicControllerTrait, HandleAjaxMedia; + use CrudTrait, HandleAjaxMedia; protected $_modelNamespace; protected $_routeURL; @@ -17,6 +24,7 @@ class CubistMagicController extends CubistCrudController protected $_plural; protected $_bulk; protected $_oneInstance; + protected $_nested = false; public function __construct() { @@ -28,7 +36,207 @@ class CubistMagicController extends CubistCrudController } + protected $_fields = []; + public function setup() + { + if (!$this->_routeURL) { + $this->_routeURL = $this->_singular; + } + if (!$this->_plural) { + $this->_plural = $this->_singular . 's'; + } + + + if ($this->_oneInstance) { + $this->crud->denyAccess(['create', 'delete']); + } else { + $this->crud->setCreateView('cubist_back::create'); + if ($this->_clonable) { + $this->crud->allowAccess('clone'); + } + $this->crud->enableExportButtons(); + $this->crud->enablePersistentTable(); + } + + $this->crud->allowAccess('revisions'); + $this->crud->setEditView('cubist_back::edit'); + + if($this->_nested){ + $this->crud->allowAccess('reorder'); + $this->crud->enableReorder('name', 2); + } + +// $this->crud->with('revisionHistory'); + + + /* + |-------------------------------------------------------------------------- + | CrudPanel Basic Information + |-------------------------------------------------------------------------- + */ + $this->crud->setModel($this->_modelNamespace); + $this->_postSetModel(); + if ($this->_bulk) { + $this->crud->enableBulkActions(); + if ($this->_clonable) { + $this->crud->addButton('bottom', 'bulk_clone', 'view', 'crud::buttons.bulk_clone', 'beginning'); + } + $this->crud->addBulkDeleteButton(); + } + $this->crud->setRoute(config('backpack.base.route_prefix') . '/' . $this->_routeURL); + $this->crud->setEntityNameStrings($this->_singular, $this->_plural); + + + /* + |-------------------------------------------------------------------------- + | CrudPanel Configuration + |-------------------------------------------------------------------------- + */ + + $model = $this->getModelInstance(); + + $this->crud->addColumn(['name' => $model->getPrimaryKey(), 'type' => 'number', 'label' => "#", 'searchLogic' => 'text']); + + $this->updateFieldsFromModel($model); + + } + + public function updateFieldsFromModel($model = null) + { + if (null === $model) { + $model = $this->getModelInstance(); + } + foreach ($model->getFields() as $field) { + if (isset($this->_fields[$field->getAttribute('name')])) { + continue; + } + $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()); + + $this->_fields[$field->getAttribute('name')] = $field; + } + + /** + * @return CubistMagicAbstractModel + */ + public function getModelInstance() + { + return $this->crud->getModel(); + } + + /** + * @param CubistMagicRequest $request + * @return CubistMagicRequest + */ + protected function _prepareCRUDData(CubistMagicRequest $request) + { + + $appendComposite = []; + foreach ($request->all() as $field => $content) { + $initialContent = $content; + $initialField = $field; + + $change = false; + $unsetInitial = false; + if (is_array($content)) { + $content = json_encode($content); + $change = true; + } + $e = explode('___', $field); + if (count($e) > 1) { + $change = false; + $unsetInitial = true; + $field = $e[0]; + for ($i = 1; $i < count($e); $i++) { + $field .= '.' . $e[$i]; + } + Arr::set($appendComposite, $field, $content); + } + + if ($change) { + $request->request->set($field, $content); + } + if ($unsetInitial) { + $request->request->set($initialField, null); + } + } + + foreach ($appendComposite as $key => $value) { + $request->request->set($key, $value); + } + + return $request; + } + + /** + * @param CubistMagicStoreRequest $request + * @return \Illuminate\Database\Eloquent\Model|\Illuminate\Http\RedirectResponse + */ + public function store(CubistMagicStoreRequest $request) + { + $request = $this->_prepareCRUDData($request); + $this->getModelInstance()->onBeforeStore($this, $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; + } + + /** + * @param CubistMagicUpdateRequest $request + * @return \Illuminate\Http\RedirectResponse + */ + public function update(CubistMagicUpdateRequest $request) + { + $request = $this->_prepareCRUDData($request); + $this->getModelInstance()->onBeforeUpdate($this, $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; + } + + + public function index() + { + if ($this->_oneInstance) { + return Redirect::to('admin/' . $this->_routeURL . '/1/edit'); + } + return parent::index(); + } + + public function edit($id) + { + if ($this->_oneInstance) { + $id = 1; + } + $this->getModelInstance()->onBeforeEdit($this, $id); + return parent::edit($id); + } + + public function create() + { + $this->getModelInstance()->onBeforeCreate($this); + return parent::create(); + } } diff --git a/src/app/Magic/Controllers/CubistMagicControllerTrait.php b/src/app/Magic/Controllers/CubistMagicControllerTrait.php deleted file mode 100644 index b7caf8e..0000000 --- a/src/app/Magic/Controllers/CubistMagicControllerTrait.php +++ /dev/null @@ -1,213 +0,0 @@ -_routeURL) { - $this->_routeURL = $this->_singular; - } - if (!$this->_plural) { - $this->_plural = $this->_singular . 's'; - } - - - if ($this->_oneInstance) { - $this->crud->denyAccess(['create', 'delete']); - } else { - $this->crud->setCreateView('cubist_back::create'); - if ($this->_clonable) { - $this->crud->allowAccess('clone'); - } - $this->crud->enableExportButtons(); - $this->crud->enablePersistentTable(); - } - - $this->crud->allowAccess('revisions'); - - - $this->crud->setEditView('cubist_back::edit'); - -// $this->crud->with('revisionHistory'); - - - /* - |-------------------------------------------------------------------------- - | CrudPanel Basic Information - |-------------------------------------------------------------------------- - */ - $this->crud->setModel($this->_modelNamespace); - $this->_postSetModel(); - if ($this->_bulk) { - $this->crud->enableBulkActions(); - if ($this->_clonable) { - $this->crud->addButton('bottom', 'bulk_clone', 'view', 'crud::buttons.bulk_clone', 'beginning'); - } - $this->crud->addBulkDeleteButton(); - } - $this->crud->setRoute(config('backpack.base.route_prefix') . '/' . $this->_routeURL); - $this->crud->setEntityNameStrings($this->_singular, $this->_plural); - - - /* - |-------------------------------------------------------------------------- - | CrudPanel Configuration - |-------------------------------------------------------------------------- - */ - - $model = $this->getModelInstance(); - - $this->crud->addColumn(['name' => $model->getPrimaryKey(), 'type' => 'number', 'label' => "#", 'searchLogic' => 'text']); - - $this->updateFieldsFromModel($model); - - } - - public function updateFieldsFromModel($model = null) - { - if (null === $model) { - $model = $this->getModelInstance(); - } - foreach ($model->getFields() as $field) { - if (isset($this->_fields[$field->getAttribute('name')])) { - continue; - } - $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()); - - $this->_fields[$field->getAttribute('name')] = $field; - } - - /** - * @return CubistMagicAbstractModel - */ - public function getModelInstance() - { - return $this->crud->getModel(); - } - - /** - * @param CubistMagicRequest $request - * @return CubistMagicRequest - */ - protected function _prepareCRUDData(CubistMagicRequest $request) - { - - $appendComposite = []; - foreach ($request->all() as $field => $content) { - $initialContent = $content; - $initialField = $field; - - $change = false; - $unsetInitial = false; - if (is_array($content)) { - $content = json_encode($content); - $change = true; - } - $e = explode('___', $field); - if (count($e) > 1) { - $change = false; - $unsetInitial = true; - $field = $e[0]; - for ($i = 1; $i < count($e); $i++) { - $field .= '.' . $e[$i]; - } - Arr::set($appendComposite, $field, $content); - } - - if ($change) { - $request->request->set($field, $content); - } - if ($unsetInitial) { - $request->request->set($initialField, null); - } - } - - foreach ($appendComposite as $key => $value) { - $request->request->set($key, $value); - } - - return $request; - } - - /** - * @param CubistMagicStoreRequest $request - * @return \Illuminate\Database\Eloquent\Model|\Illuminate\Http\RedirectResponse - */ - public function store(CubistMagicStoreRequest $request) - { - $request = $this->_prepareCRUDData($request); - $this->getModelInstance()->onBeforeStore($this, $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; - } - - /** - * @param CubistMagicUpdateRequest $request - * @return \Illuminate\Http\RedirectResponse - */ - public function update(CubistMagicUpdateRequest $request) - { - $request = $this->_prepareCRUDData($request); - $this->getModelInstance()->onBeforeUpdate($this, $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; - } - - - public function index() - { - if ($this->_oneInstance) { - return Redirect::to('admin/' . $this->_routeURL . '/1/edit'); - } - return parent::index(); - } - - public function edit($id) - { - if ($this->_oneInstance) { - $id = 1; - } - $this->getModelInstance()->onBeforeEdit($this, $id); - return parent::edit($id); - } - - public function create() - { - $this->getModelInstance()->onBeforeCreate($this); - return parent::create(); - } -} diff --git a/src/app/Magic/Controllers/CubistMagicNestedController.php b/src/app/Magic/Controllers/CubistMagicNestedController.php index f133654..aa736d8 100644 --- a/src/app/Magic/Controllers/CubistMagicNestedController.php +++ b/src/app/Magic/Controllers/CubistMagicNestedController.php @@ -3,22 +3,8 @@ namespace Cubist\Backpack\app\Magic\Controllers; -use Backpack\CRUD\CrudTrait; -use Gaspertrix\Backpack\DropzoneField\Traits\HandleAjaxMedia; -use Webfactor\Laravel\Backpack\NestedModels\Controllers\NestedModelsCrudController; -class CubistMagicNestedController extends NestedModelsCrudController +class CubistMagicNestedController extends CubistMagicController { - use CrudTrait, CubistMagicControllerTrait, HandleAjaxMedia; - - protected $_modelNamespace; - protected $_routeURL; - protected $_singular; - protected $_plural; - protected $_bulk; - - public function _postSetModel() - { - $this->treeSetup(); - } + protected $_nested = true; } diff --git a/src/app/Magic/Models/CMSMenu.php b/src/app/Magic/Models/CMSMenu.php new file mode 100644 index 0000000..52de912 --- /dev/null +++ b/src/app/Magic/Models/CMSMenu.php @@ -0,0 +1,38 @@ + 'menu', + 'singular' => 'élément de menu', + 'plural' => 'éléments de menu']; + + public function setFields() + { + parent::setFields(); + + $this->addField(['name' => 'name', + 'label' => 'Label de la page dans la navigation', + 'type' => 'Text', + 'column' => true] + ); + $this->addField(['name' => 'type', + 'label' => 'Type de page', + 'type' => 'SelectFromArray', + 'options' => ['page_link' => 'Page du site', 'link' => 'Lien externe'], + 'column' => true]); + + $this->addField(['name' => 'page_id', + 'type' => 'SelectFromModel', + 'label' => 'Page du site', + 'optionsmodel' => 'App\Models\Page']); + + $this->addField(['name' => 'link', + 'type' => 'URL', + 'label' => 'Lien externe']); + } +} diff --git a/src/app/Magic/Models/CubistMagicNestedModel.php b/src/app/Magic/Models/CubistMagicNestedModel.php new file mode 100644 index 0000000..56eeb3c --- /dev/null +++ b/src/app/Magic/Models/CubistMagicNestedModel.php @@ -0,0 +1,30 @@ +addColumn('parent_id', 'integer', ['unsigned' => true, 'notnull' => false]); + $table->addIndex(['parent_id']); + $table->addColumn('lft', 'integer', ['unsigned' => true, 'default' => 0]); + $table->addIndex(['lft']); + $table->addColumn('rgt', 'integer', ['unsigned' => true, 'default' => 0]); + $table->addIndex(['rgt']); + $table->addColumn('depth', 'integer', ['unsigned' => true, 'default' => 0]); + + return $table; + } + + /** + * @return string + */ + protected function _getBaseController() + { + return 'CubistMagicNestedController'; + } +}