From e352acc73e23cfc813ee041ae67584275d244b7c Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Wed, 22 May 2019 16:37:04 +0200 Subject: [PATCH] wip #2783 @4 --- src/CubistBackpackServiceProvider.php | 21 +---- src/app/Http/Controllers/CubistCrud.php | 30 ------- .../CubistModelFieldCrudController.php | 69 ---------------- .../Controllers/CubistPageCrudController.php | 1 + .../Controllers/CubistMagicController.php} | 42 +++++----- src/app/Magic/CubistCrud.php | 23 ++++++ src/app/Magic/CubistMagicAttribute.php | 16 ++++ src/app/Magic/Fields/CubistMagicField.php | 79 +++++++++++++++++++ .../Magic/Models/CubistMagicModelAbstract.php | 69 ++++++++++++++++ .../Requests/CubistMagicRequest.php} | 7 +- .../Requests/CubistMagicStoreRequest.php | 12 +++ .../Requests/CubistMagicUpdateRequest.php | 12 +++ src/app/Models/CubistModel.php | 58 -------------- src/app/Models/CubistModelField.php | 14 ---- 14 files changed, 239 insertions(+), 214 deletions(-) delete mode 100644 src/app/Http/Controllers/CubistCrud.php delete mode 100644 src/app/Http/Controllers/CubistModelFieldCrudController.php rename src/app/{Http/Controllers/CubistModelCrudController.php => Magic/Controllers/CubistMagicController.php} (68%) create mode 100644 src/app/Magic/CubistCrud.php create mode 100644 src/app/Magic/CubistMagicAttribute.php create mode 100644 src/app/Magic/Fields/CubistMagicField.php create mode 100644 src/app/Magic/Models/CubistMagicModelAbstract.php rename src/app/{Http/Requests/ModelRequest.php => Magic/Requests/CubistMagicRequest.php} (91%) create mode 100644 src/app/Magic/Requests/CubistMagicStoreRequest.php create mode 100644 src/app/Magic/Requests/CubistMagicUpdateRequest.php delete mode 100644 src/app/Models/CubistModel.php delete mode 100644 src/app/Models/CubistModelField.php diff --git a/src/CubistBackpackServiceProvider.php b/src/CubistBackpackServiceProvider.php index ee72546..c33dc4e 100644 --- a/src/CubistBackpackServiceProvider.php +++ b/src/CubistBackpackServiceProvider.php @@ -22,23 +22,6 @@ class CubistBackpackServiceProvider extends ServiceProvider */ public function boot() { - // define the routes for the application - //$this->setupRoutes($this->app->router); - -// // only use the Settings package if the Settings table is present in the database -// if (!\App::runningInConsole() && count(Schema::getColumnListing('settings'))) { -// // get all settings from the database -// $settings = Setting::all(); -// -// // bind all settings to the Laravel config, so you can call them like -// // Config::get('settings.contact_email') -// foreach ($settings as $key => $setting) { -// Config::set('settings.' . $setting->key, $setting->value); -// } -// } - - $this->publishes([__DIR__.'/database/migrations' => database_path('migrations')], 'migrations'); - $this->loadTranslationsFrom(__DIR__ . '/resources/lang', 'cubist_back'); foreach (glob(__DIR__ . '/routes/cubist/backpack/*.php') as $filename) { $this->loadRoutesFrom($filename); @@ -46,6 +29,8 @@ class CubistBackpackServiceProvider extends ServiceProvider $this->loadViewsFrom(__FILE__ . '/resources/views','cubist_back'); } + + /** * Register any package services. * @@ -54,8 +39,6 @@ class CubistBackpackServiceProvider extends ServiceProvider public function register() { - - // $this->app->bind('templates', function ($app) { // return new Template($app); // }); diff --git a/src/app/Http/Controllers/CubistCrud.php b/src/app/Http/Controllers/CubistCrud.php deleted file mode 100644 index 6f1d427..0000000 --- a/src/app/Http/Controllers/CubistCrud.php +++ /dev/null @@ -1,30 +0,0 @@ - $field['name'], 'label' => $field['label'], 'type' => 'text']; - if (isset($field['column'])) { - unset($field['column']); - } - if (isset($field['column_label'])) { - $column_data['label'] = $field['column_label']; - unset($field['column_label']); - } - if (isset($field['column_type'])) { - $column_data['type'] = $field['column_type']; - unset($column_data['type']); - } - $this->crud->addColumn($column_data); - } - $this->crud->addField($field, $form); - } -} diff --git a/src/app/Http/Controllers/CubistModelFieldCrudController.php b/src/app/Http/Controllers/CubistModelFieldCrudController.php deleted file mode 100644 index d3174f1..0000000 --- a/src/app/Http/Controllers/CubistModelFieldCrudController.php +++ /dev/null @@ -1,69 +0,0 @@ -crud->setModel('Cubist\Backpack\app\Models\CubistModelField'); - $this->crud->setRoute(config('backpack.base.route_prefix') . '/modelfield'); - $this->crud->setEntityNameStrings('modelfield', 'modelfieldss'); - - - $this->setAjaxEntity('modelfield'); - - /* - |-------------------------------------------------------------------------- - | CrudPanel Configuration - |-------------------------------------------------------------------------- - */ - - $this->addField(['type' => 'text', - 'name' => 'name', - 'label' => 'Field column name', - 'column' => true], 'create'); - $this->addField(['type' => 'text', 'name' => 'label', 'label' => 'Field label', 'column' => true], 'both'); - $this->addField(['type' => 'table', 'name' => 'attributes', 'label' => 'Attributes'], 'both'); - - // add asterisk for fields that are required in ModelRequest - $this->crud->setRequiredFields(StoreRequest::class, 'create'); - $this->crud->setRequiredFields(UpdateRequest::class, 'edit'); - } - - public function store(StoreRequest $request) - { - // your additional operations before save here - $redirect_location = parent::storeCrud($request); - // your additional operations after save here - // use $this->data['entry'] or $this->crud->entry - return $redirect_location; - } - - public function update(UpdateRequest $request) - { - // your additional operations before save here - $redirect_location = parent::updateCrud($request); - // your additional operations after save here - // use $this->data['entry'] or $this->crud->entry - return $redirect_location; - } -} - diff --git a/src/app/Http/Controllers/CubistPageCrudController.php b/src/app/Http/Controllers/CubistPageCrudController.php index 853f414..deea027 100644 --- a/src/app/Http/Controllers/CubistPageCrudController.php +++ b/src/app/Http/Controllers/CubistPageCrudController.php @@ -4,6 +4,7 @@ namespace Cubist\Backpack\app\Http\Controllers; use Backpack\PageManager\app\Http\Controllers\Admin\PageCrudController; use Cubist\Backpack\app\Template\TemplateAbstract; +use Cubist\Backpack\app\Magic\CubistCrud; use Illuminate\Support\Str; class CubistPageCrudController extends PageCrudController diff --git a/src/app/Http/Controllers/CubistModelCrudController.php b/src/app/Magic/Controllers/CubistMagicController.php similarity index 68% rename from src/app/Http/Controllers/CubistModelCrudController.php rename to src/app/Magic/Controllers/CubistMagicController.php index d7c414b..d8f6b14 100644 --- a/src/app/Http/Controllers/CubistModelCrudController.php +++ b/src/app/Magic/Controllers/CubistMagicController.php @@ -1,24 +1,22 @@ crud->setModel('Cubist\Backpack\app\Models\CubistModel'); - $this->crud->setRoute(config('backpack.base.route_prefix') . '/model'); - $this->crud->setEntityNameStrings('model', 'models'); + $this->crud->setModel($this->_modelNamespace); + $this->crud->setRoute(config('backpack.base.route_prefix') . '/' . $this->_routeURL); + $this->crud->setEntityNameStrings($this->_singular, $this->_plural); /* @@ -49,7 +47,7 @@ class CubistModelCrudController extends CrudController 'type' => 'select2_from_ajax_multiple', 'label' => 'Fields definitions', 'view_namespace' => 'webfactor::fields', - 'model' => Entity::class, + 'model' => CubistModelField::class, 'entity' => 'modelfield', 'attribute' => 'name', 'placeholder' => 'Choose', @@ -63,11 +61,11 @@ class CubistModelCrudController extends CrudController ]], 'both'); // add asterisk for fields that are required in ModelRequest - $this->crud->setRequiredFields(StoreRequest::class, 'create'); - $this->crud->setRequiredFields(UpdateRequest::class, 'edit'); + $this->crud->setRequiredFields(CubistMagicStoreRequest::class, 'create'); + $this->crud->setRequiredFields(CubistMagicUpdateRequest::class, 'edit'); } - public function store(StoreRequest $request) + public function store(CubistMagicStoreRequest $request) { // your additional operations before save here $redirect_location = parent::storeCrud($request); @@ -76,7 +74,7 @@ class CubistModelCrudController extends CrudController return $redirect_location; } - public function update(UpdateRequest $request) + public function update(CubistMagicUpdateRequest $request) { // your additional operations before save here $redirect_location = parent::updateCrud($request); diff --git a/src/app/Magic/CubistCrud.php b/src/app/Magic/CubistCrud.php new file mode 100644 index 0000000..6dadc1b --- /dev/null +++ b/src/app/Magic/CubistCrud.php @@ -0,0 +1,23 @@ +isDisplayColumn()) { + $this->crud->addColumn($field->getColumnData()); + } + $this->crud->addField($field, $field->getCRUDForm()); + } +} diff --git a/src/app/Magic/CubistMagicAttribute.php b/src/app/Magic/CubistMagicAttribute.php new file mode 100644 index 0000000..ae42677 --- /dev/null +++ b/src/app/Magic/CubistMagicAttribute.php @@ -0,0 +1,16 @@ +_attributes[$key])) { + return $this->_attributes[$key]; + } + return $default; + } +} diff --git a/src/app/Magic/Fields/CubistMagicField.php b/src/app/Magic/Fields/CubistMagicField.php new file mode 100644 index 0000000..2bac171 --- /dev/null +++ b/src/app/Magic/Fields/CubistMagicField.php @@ -0,0 +1,79 @@ + 'text', 'column' => false, 'form' => 'both']; + } + + public function __construct($attributes) + { + $this->_attributes = array_merge($attributes, $this->getDefaultAttributes()); + $this->init(); + } + + public function init() + { + + } + + /** + * @return bool + */ + public function isDisplayColumn() + { + return !!$this->getAttribute('column'); + } + + public function getColumnData() + { + $res = [ + 'type' => $this->getAttribute('column_type', $this->getAttribute('type')), + 'label' => $this->getAttribute('column_label', $this->getAttribute('label')) + ]; + + return $res; + } + + public function getCRUDForm() + { + return $this->getAttribute('form'); + } + + public function getDatabaseSchema() + { + + } +} diff --git a/src/app/Magic/Models/CubistMagicModelAbstract.php b/src/app/Magic/Models/CubistMagicModelAbstract.php new file mode 100644 index 0000000..5649838 --- /dev/null +++ b/src/app/Magic/Models/CubistMagicModelAbstract.php @@ -0,0 +1,69 @@ +_controller) { + $this->_controller = new CubistMagicController(); + } + + return $this->_controller; + } + + /** + * @param $attributes + */ + public function addField($attributes) + { + /** @var CubistMagicField $field */ + $field = CubistMagicField::getInstance($attributes); + $this->getController()->addField($field); + + $this->_fields[$field->getAttribute('name')] = $field; + } + + public function loadRoute() + { + Route::group([ + 'prefix' => config('backpack.base.route_prefix', 'admin'), + 'middleware' => ['web', config('backpack.base.middleware_key', 'admin')], + 'namespace' => 'Cubist\Backpack\app\Http\Controllers', + ], function () { // custom admin routes + CRUD::resource('model', 'CubistModelCrudController'); + CRUD::resource('model', 'CubistModelCrudController'); + }); // this should be the absolute last line of this file + + + } +} diff --git a/src/app/Http/Requests/ModelRequest.php b/src/app/Magic/Requests/CubistMagicRequest.php similarity index 91% rename from src/app/Http/Requests/ModelRequest.php rename to src/app/Magic/Requests/CubistMagicRequest.php index 88b98b5..d5c64d4 100644 --- a/src/app/Http/Requests/ModelRequest.php +++ b/src/app/Magic/Requests/CubistMagicRequest.php @@ -1,10 +1,11 @@ check(); } + /** * Get the validation rules that apply to the request. * @@ -52,4 +54,5 @@ class CubistModelRequest extends FormRequest // ]; } + } diff --git a/src/app/Magic/Requests/CubistMagicStoreRequest.php b/src/app/Magic/Requests/CubistMagicStoreRequest.php new file mode 100644 index 0000000..adf513c --- /dev/null +++ b/src/app/Magic/Requests/CubistMagicStoreRequest.php @@ -0,0 +1,12 @@ +