]> _ Git - cubist_cms-back.git/commitdiff
#2783
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Fri, 31 May 2019 12:51:12 +0000 (14:51 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Fri, 31 May 2019 12:51:12 +0000 (14:51 +0200)
src/app/Magic/Controllers/CubistMagicNestedController.php [new file with mode: 0644]
src/app/Magic/Controllers/CubistNestedMagicController.php [deleted file]
src/app/Magic/Models/CubistMagicAbstractModel.php
src/app/Magic/Models/CubistMagicNestedModel.php

diff --git a/src/app/Magic/Controllers/CubistMagicNestedController.php b/src/app/Magic/Controllers/CubistMagicNestedController.php
new file mode 100644 (file)
index 0000000..54ee64a
--- /dev/null
@@ -0,0 +1,21 @@
+<?php
+
+
+namespace Cubist\Backpack\app\Magic\Controllers;
+
+use Backpack\CRUD\CrudTrait;
+use Webfactor\Laravel\Backpack\NestedModels\Controllers\NestedModelsCrudController;
+
+class CubistMagicNestedController extends NestedModelsCrudController
+{
+    use CrudTrait, CubistMagicControllerTrait;
+
+    protected $_modelNamespace;
+    protected $_routeURL;
+    protected $_singular;
+    protected $_plural;
+
+    public function _afterSetModel(){
+        $this->treeSetup();
+    }
+}
diff --git a/src/app/Magic/Controllers/CubistNestedMagicController.php b/src/app/Magic/Controllers/CubistNestedMagicController.php
deleted file mode 100644 (file)
index b2e86ab..0000000
+++ /dev/null
@@ -1,21 +0,0 @@
-<?php
-
-
-namespace Cubist\Backpack\app\Magic\Controllers;
-
-use Backpack\CRUD\CrudTrait;
-use Webfactor\Laravel\Backpack\NestedModels\Controllers\NestedModelsCrudController;
-
-class CubistNestedMagicController extends NestedModelsCrudController
-{
-    use CrudTrait, CubistMagicControllerTrait;
-
-    protected $_modelNamespace;
-    protected $_routeURL;
-    protected $_singular;
-    protected $_plural;
-
-    public function _afterSetModel(){
-        $this->treeSetup();
-    }
-}
index a828648a23a129ace26fbef1a915cf35f793a98d..fd8a04399eab61ae120d52b7acc7c9a8250103bd 100644 (file)
@@ -10,6 +10,7 @@ use Backpack\CRUD\ModelTraits\SpatieTranslatable\Sluggable;
 use Backpack\CRUD\ModelTraits\SpatieTranslatable\SluggableScopeHelpers;
 use Backpack\CRUD\ModelTraits\SpatieTranslatable\HasTranslations;
 use Doctrine\DBAL\Schema\Schema;
+use Doctrine\DBAL\Schema\Table;
 use Illuminate\Database\Eloquent\Model;
 use Illuminate\Support\Str;
 
@@ -25,7 +26,6 @@ class CubistMagicAbstractModel extends Model
 
     protected static $_doctrineTypesMapping = ['int' => 'integer'];
 
-    protected $nested = false;
     protected $primaryKey = 'id';
     public $timestamps = true;
 
@@ -52,6 +52,7 @@ class CubistMagicAbstractModel extends Model
     public function __construct(array $attributes = [])
     {
         $this->setFields();
+        $this->afterSetFields();
         $this->bootIfNotBooted();
         $this->initializeTraits();
         $this->syncOriginal();
@@ -63,6 +64,20 @@ class CubistMagicAbstractModel extends Model
 
     }
 
+
+    public function afterSetFields()
+    {
+        if (!isset($this->_fields['slug'])) {
+            $this->addField(['name' => 'slug',
+                'type' => 'Slug',
+                'label' => 'Slug',
+                'translatable' => true,
+                'hidden' => true,
+                'fillable' => false]);
+        }
+    }
+
+
     public function getFields()
     {
         return $this->_fields;
@@ -141,6 +156,14 @@ class CubistMagicAbstractModel extends Model
         return $this->getStudlyName() . 'CrudController';
     }
 
+    /**
+     * @return string
+     */
+    protected function _getBaseController()
+    {
+        return 'CubistMagicController';
+    }
+
     protected function _replaceInCode($stub, $dest)
     {
         $vars = ['CONTROLLERCLASS' => $this->getControllerClass(),
@@ -148,7 +171,7 @@ class CubistMagicAbstractModel extends Model
             'SINGULAR' => $this->getOption('singular', $this->getOption('name')),
             'PLURAL' => $this->getOption('plural', ''),
             'MODELNAMESPACE' => get_class($this),
-            'EXTENDS' => $this->nested ? 'CubistNestedMagicController' : 'CubistMagicController',
+            'EXTENDS' => $this->_getBaseController(),
         ];
 
         $res = file_get_contents($stub);
@@ -170,6 +193,7 @@ class CubistMagicAbstractModel extends Model
 
     /**
      * @param $schema Schema
+     * @return Table
      */
     public function setSchema($schema)
     {
@@ -186,22 +210,13 @@ class CubistMagicAbstractModel 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);
             $table->addColumn(static::UPDATED_AT, 'date', $options);
             $table->addColumn('deleted_at', 'date', $options);
         }
+        return $table;
     }
 
     public function replicate(array $except = null)
index 0f6902b152fd6be5e671536b61981e827f6e802b..cb23bce3308aa5a4a2ebb5538aba59d3c9b9f18c 100644 (file)
@@ -3,7 +3,6 @@
 
 namespace Cubist\Backpack\app\Magic\Models;
 
-
 use Webfactor\Laravel\Backpack\NestedModels\Traits\NestedModelTrait;
 
 class CubistMagicNestedModel extends CubistMagicModel
@@ -17,4 +16,27 @@ class CubistMagicNestedModel extends CubistMagicModel
         $this->replicateNodeTrait($except);
         $this->replicateSluggable($except);
     }
+
+    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 'CubistMagicController';
+    }
+
 }