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
|--------------------------------------------------------------------------
*/
- $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');
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
parent::__construct($attributes);
}
- /**
- * @return CubistMagicController
- */
- public function getController(): CubistMagicController
- {
- if (null === $this->_controller) {
- $this->_controller = new CubistMagicController();
- }
-
- return $this->_controller;
- }
-
/**
* @param $attributes
*/
{
/** @var CubistMagicField $field */
$field = CubistMagicField::getInstance($attributes);
- $this->getController()->addField($field);
-
$this->_fields[$field->getAttribute('name')] = $field;
}
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'));
}
}