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;
protected $_plural;
protected $_bulk;
protected $_oneInstance;
+ protected $_nested = false;
public function __construct()
{
}
+ 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();
+ }
}
+++ /dev/null
-<?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();
- }
-}
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;
}
--- /dev/null
+<?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']);
+ }
+}
--- /dev/null
+<?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';
+ }
+}