From: Vincent Vanwaelscappel Date: Mon, 27 May 2019 15:19:45 +0000 (+0200) Subject: #2783 X-Git-Url: http://git.cubedesigners.com/?a=commitdiff_plain;h=1c427d3f716b501dfe30bf617708b0f7d65315a8;p=cubist_cms-back.git #2783 --- diff --git a/src/app/Magic/Controllers/CubistMagicController.php b/src/app/Magic/Controllers/CubistMagicController.php index 4d2d2df..8573a57 100644 --- a/src/app/Magic/Controllers/CubistMagicController.php +++ b/src/app/Magic/Controllers/CubistMagicController.php @@ -3,21 +3,23 @@ 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; +use Webfactor\Laravel\Backpack\NestedModels\Traits\NestedModelTrait; -class CubistMagicController extends CrudController +class CubistMagicController extends NestedModelsCrudController { - use CrudTrait; + use CrudTrait, NestedModelTrait; protected $_modelNamespace; protected $_routeURL; protected $_singular; protected $_plural; + protected $_nested = false; protected $_storeRequest; protected $_updateRequest; @@ -37,6 +39,9 @@ class CubistMagicController extends CrudController |-------------------------------------------------------------------------- */ $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); diff --git a/src/app/Magic/Models/CubistMagicModelAbstract.php b/src/app/Magic/Models/CubistMagicModelAbstract.php index 4113b87..8338b3c 100644 --- a/src/app/Magic/Models/CubistMagicModelAbstract.php +++ b/src/app/Magic/Models/CubistMagicModelAbstract.php @@ -13,15 +13,17 @@ use Doctrine\DBAL\Schema\Table; use Illuminate\Database\Eloquent\Model; use Cubist\Backpack\app\Magic\CubistMagicAttribute; use Illuminate\Support\Str; +use Webfactor\Laravel\Backpack\NestedModels\Traits\NestedModelTrait; class CubistMagicModelAbstract extends Model { use CubistMagicAttribute; - use CrudTrait; + use CrudTrait, NestedModelTrait; use Sluggable, SluggableScopeHelpers; protected static $_doctrineTypesMapping = ['int' => 'integer']; + protected $nested = false; protected $primaryKey = 'id'; public $timestamps = true; @@ -135,7 +137,8 @@ class CubistMagicModelAbstract extends Model 'ROUTEURL' => $this->getOption('name'), 'SINGULAR' => $this->getOption('singular', $this->getOption('name')), 'PLURAL' => $this->getOption('plural', ''), - 'MODELNAMESPACE' => get_class($this) + 'MODELNAMESPACE' => get_class($this), + 'NESTED' => $this->nested ? 'true' : 'false', ]; $res = file_get_contents($stub); @@ -174,6 +177,16 @@ class CubistMagicModelAbstract extends Model $field->defineDbColumn($table); } + if ($this->nested) { + $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]); + } + if ($this->timestamps) { $options = ['notnull' => false]; $table->addColumn(static::CREATED_AT, 'date', $options); diff --git a/src/resources/cubistmagic/Controller.stub b/src/resources/cubistmagic/Controller.stub index 8d40583..d7d0a2f 100644 --- a/src/resources/cubistmagic/Controller.stub +++ b/src/resources/cubistmagic/Controller.stub @@ -10,4 +10,5 @@ class _CONTROLLERCLASS_ extends CubistMagicController protected $_routeURL = '_ROUTEURL_'; protected $_singular = '_SINGULAR_'; protected $_plural = '_PLURAL_'; + protected $_nested = _NESTED_; }