From fa46878de2379ad2883a34fae056a4fe051efae8 Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Wed, 22 May 2019 18:23:41 +0200 Subject: [PATCH] #2783 --- .../Controllers/CubistMagicController.php | 38 +++++-------- src/app/Magic/CubistCrud.php | 8 ++- .../Magic/Models/CubistMagicModelAbstract.php | 57 +++++++++++++------ src/resources/cubistmagic/Controller.stub | 12 ++++ 4 files changed, 73 insertions(+), 42 deletions(-) create mode 100644 src/resources/cubistmagic/Controller.stub diff --git a/src/app/Magic/Controllers/CubistMagicController.php b/src/app/Magic/Controllers/CubistMagicController.php index d8f6b14..4e0d69c 100644 --- a/src/app/Magic/Controllers/CubistMagicController.php +++ b/src/app/Magic/Controllers/CubistMagicController.php @@ -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'); diff --git a/src/app/Magic/CubistCrud.php b/src/app/Magic/CubistCrud.php index 6dadc1b..c9332c9 100644 --- a/src/app/Magic/CubistCrud.php +++ b/src/app/Magic/CubistCrud.php @@ -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()); } diff --git a/src/app/Magic/Models/CubistMagicModelAbstract.php b/src/app/Magic/Models/CubistMagicModelAbstract.php index 5649838..0232730 100644 --- a/src/app/Magic/Models/CubistMagicModelAbstract.php +++ b/src/app/Magic/Models/CubistMagicModelAbstract.php @@ -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 index 0000000..2d5deb3 --- /dev/null +++ b/src/resources/cubistmagic/Controller.stub @@ -0,0 +1,12 @@ +