]> _ Git - cubist_cms-back.git/commitdiff
#2843
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Mon, 1 Jul 2019 08:52:19 +0000 (10:52 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Mon, 1 Jul 2019 08:52:19 +0000 (10:52 +0200)
src/app/Magic/Controllers/CubistMagicController.php
src/app/Magic/Controllers/CubistMagicControllerTrait.php [deleted file]
src/app/Magic/Controllers/CubistMagicNestedController.php
src/app/Magic/Models/CMSMenu.php [new file with mode: 0644]
src/app/Magic/Models/CubistMagicNestedModel.php [new file with mode: 0644]

index 2449b7b47651d4659bf0eac8b2553c733b7a1241..d681e576e5c77433311b2e99bfd7397f8c4f086e 100644 (file)
@@ -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 (file)
index b7caf8e..0000000
+++ /dev/null
@@ -1,213 +0,0 @@
-<?php
-
-namespace Cubist\Backpack\app\Magic\Controllers;
-
-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 Illuminate\Support\Arr;
-use Illuminate\Support\Facades\Redirect;
-
-trait CubistMagicControllerTrait
-{
-    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');
-
-//        $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();
-    }
-}
index f133654f1bc71a3426c52405c79b9f9a98dafc82..aa736d8b80db3af022a084ccaf315a78fcadb6fb 100644 (file)
@@ -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 (file)
index 0000000..52de912
--- /dev/null
@@ -0,0 +1,38 @@
+<?php
+
+
+namespace Cubist\Backpack\app\Magic\Models;
+
+
+class CMSMenu extends CubistMagicNestedModel
+{
+    protected $table = 'cubist_menu';
+    protected $_options = ['name' => '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 (file)
index 0000000..56eeb3c
--- /dev/null
@@ -0,0 +1,30 @@
+<?php
+
+namespace Cubist\Backpack\app\Magic\Models;
+
+class CubistMagicNestedModel extends CubistMagicModel
+{
+
+    public function setSchema($schema)
+    {
+        $table = parent::setSchema($schema);
+
+        $table->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';
+    }
+}