]> _ Git - cubist_cms-back.git/commitdiff
#2783
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Wed, 22 May 2019 16:23:41 +0000 (18:23 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Wed, 22 May 2019 16:23:41 +0000 (18:23 +0200)
src/app/Magic/Controllers/CubistMagicController.php
src/app/Magic/CubistCrud.php
src/app/Magic/Models/CubistMagicModelAbstract.php
src/resources/cubistmagic/Controller.stub [new file with mode: 0644]

index d8f6b1429e7f73d619e50a91b1df446bb0fd732f..4e0d69c764a1b02a75c9042937190e92c1e176d3 100644 (file)
@@ -17,9 +17,20 @@ class CubistMagicController extends CrudController
     protected $_routeURL;
     protected $_singular;
     protected $_plural;
+    protected $_fields = [];
 
     public function setup()
     {
+        if (!$this->_routeURL) {
+            $this->_routeURL = $this->_singular;
+        }
+        if (!$this->_plural) {
+            $this->_plural = $this->_singular . 's';
+        }
+        if(!$this->_modelNamespace){
+
+        }
+
         /*
         |--------------------------------------------------------------------------
         | CrudPanel Basic Information
@@ -36,29 +47,10 @@ class CubistMagicController extends CrudController
         |--------------------------------------------------------------------------
         */
 
-        $this->addField(['type' => 'text',
-            'name' => 'name',
-            'label' => 'Model table name',
-            'column' => true], 'create');
-        $this->addField(['type' => 'text', 'name' => 'label', 'label' => 'Model label', 'column' => true], 'both');
-
-        $this->addField([
-            'name' => 'fields',
-            'type' => 'select2_from_ajax_multiple',
-            'label' => 'Fields definitions',
-            'view_namespace' => 'webfactor::fields',
-            'model' => CubistModelField::class,
-            'entity' => 'modelfield',
-            'attribute' => 'name',
-            'placeholder' => 'Choose',
-            'pagination' => 20, // optional, default: 10
-            'minimum_input_length' => 0,
-            'on_the_fly' => [
-                'entity' => 'modelfield', // e. g. user, contact, company etc...
-                'create' => true,
-                'edit' => true,
-                'delete' => true,
-            ]], 'both');
+        foreach ($this->_fields as $field) {
+            $this->addField($field);
+        }
+
 
         // add asterisk for fields that are required in ModelRequest
         $this->crud->setRequiredFields(CubistMagicStoreRequest::class, 'create');
index 6dadc1b259ec256779c4df8af51855fd26681a97..c9332c918ee82c8cfef742eac727d3754a2bc8b0 100644 (file)
@@ -11,10 +11,16 @@ trait CubistCrud
     use CrudTrait;
 
     /**
-     * @param $field CubistMagicField
+     * @param $field CubistMagicField|array
+     * @throws \Exception
      */
     public function addField($field)
     {
+        if (is_array($field)) {
+            $field = CubistMagicField::getInstance($field);
+        }
+        /** @var $field CubistMagicField */
+
         if ($field->isDisplayColumn()) {
             $this->crud->addColumn($field->getColumnData());
         }
index 5649838a3c4b17f451d141ad49a52d7ae5e1a2c0..0232730536e47fa82c11d7180aea11871ea2c279 100644 (file)
@@ -9,15 +9,12 @@ use Illuminate\Database\Eloquent\Model;
 use Cubist\Backpack\app\Magic\CubistMagicAttribute;
 use Illuminate\Support\Facades\Route;
 use Backpack\CRUD;
+use Illuminate\Support\Str;
 
 class CubistMagicModelAbstract extends Model
 {
 
     use CubistMagicAttribute;
-    /**
-     * @var CubistMagicController
-     */
-    protected $_controller = null;
 
     /**
      * @var array
@@ -29,18 +26,6 @@ class CubistMagicModelAbstract extends Model
         parent::__construct($attributes);
     }
 
-    /**
-     * @return CubistMagicController
-     */
-    public function getController(): CubistMagicController
-    {
-        if (null === $this->_controller) {
-            $this->_controller = new CubistMagicController();
-        }
-
-        return $this->_controller;
-    }
-
     /**
      * @param $attributes
      */
@@ -48,8 +33,6 @@ class CubistMagicModelAbstract extends Model
     {
         /** @var CubistMagicField $field */
         $field = CubistMagicField::getInstance($attributes);
-        $this->getController()->addField($field);
-
         $this->_fields[$field->getAttribute('name')] = $field;
     }
 
@@ -63,7 +46,45 @@ class CubistMagicModelAbstract extends Model
             CRUD::resource('model', 'CubistModelCrudController');
             CRUD::resource('model', 'CubistModelCrudController');
         }); // this should be the absolute last line of this file
+    }
 
+    public function generateCode()
+    {
+        $this->_generateControllerCode();
+    }
 
+    protected function _generateControllerCode()
+    {
+        $this->_replaceInCode($this->_getStubPath() . 'Controller.stub',
+            app_path() . '/Http/Controllers/' . $this->getCamelName() . 'CubistMagicController.php');
+    }
+
+
+    protected function _replaceInCode($stub, $dest)
+    {
+        $vars = ['CAMELNAME' => $this->getCamelName(),
+            'ROUTEURL' => $this->getAttribute('name'),
+            'SINGULAR' => $this->getAttribute('singular', $this->getAttribute('name')),
+            'PLURAL' => $this->getAttribute('plural', ''),
+            'MODELNAMESPACE' => __NAMESPACE__ . '\\' . get_class($this),
+            'FIELDS' => var_export($this->_fields, true)
+        ];
+
+        $res = file_get_contents($stub);
+        foreach ($vars as $name => $value) {
+            $res = str_replace('_' . $name . '_', $value, $res);
+        }
+
+        file_put_contents($dest, $res);
+    }
+
+    protected function _getStubPath()
+    {
+        return __DIR__ . '/../../../resources/cubistmagic/';
+    }
+
+    public function getCamelName()
+    {
+        return Str::camel($this->getAttribute('name'));
     }
 }
diff --git a/src/resources/cubistmagic/Controller.stub b/src/resources/cubistmagic/Controller.stub
new file mode 100644 (file)
index 0000000..2d5deb3
--- /dev/null
@@ -0,0 +1,12 @@
+<?php
+
+namespace App\Htpp\Controllers\CubistMagic;
+
+class _CAMELNAME_CubistMagicController extends CubistMagicController
+{
+    protected $_modelNamespace = '_MODELNAMESPACE_';
+    protected $_routeURL = '_ROUTEURL_';
+    protected $_singular = '_SINGULAR_';
+    protected $_plural = '_PLURAL_';
+    protected $_fields = _FILEDS_;
+}