]> _ Git - cubist_cms-back.git/commitdiff
wip #2783 @4
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Wed, 22 May 2019 14:37:04 +0000 (16:37 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Wed, 22 May 2019 14:37:04 +0000 (16:37 +0200)
16 files changed:
src/CubistBackpackServiceProvider.php
src/app/Http/Controllers/CubistCrud.php [deleted file]
src/app/Http/Controllers/CubistModelCrudController.php [deleted file]
src/app/Http/Controllers/CubistModelFieldCrudController.php [deleted file]
src/app/Http/Controllers/CubistPageCrudController.php
src/app/Http/Requests/ModelRequest.php [deleted file]
src/app/Magic/Controllers/CubistMagicController.php [new file with mode: 0644]
src/app/Magic/CubistCrud.php [new file with mode: 0644]
src/app/Magic/CubistMagicAttribute.php [new file with mode: 0644]
src/app/Magic/Fields/CubistMagicField.php [new file with mode: 0644]
src/app/Magic/Models/CubistMagicModelAbstract.php [new file with mode: 0644]
src/app/Magic/Requests/CubistMagicRequest.php [new file with mode: 0644]
src/app/Magic/Requests/CubistMagicStoreRequest.php [new file with mode: 0644]
src/app/Magic/Requests/CubistMagicUpdateRequest.php [new file with mode: 0644]
src/app/Models/CubistModel.php [deleted file]
src/app/Models/CubistModelField.php [deleted file]

index ee725461b16b9f108422db14e605836f97340332..c33dc4ea68c6f318f01412cc0d449e68beaa1c77 100644 (file)
@@ -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 (file)
index 6f1d427..0000000
+++ /dev/null
@@ -1,30 +0,0 @@
-<?php
-
-
-namespace Cubist\Backpack\app\Http\Controllers;
-use Backpack\CRUD\CrudTrait;
-
-trait CubistCrud
-{
-    use CrudTrait;
-
-    public function addField($field, $form = 'both')
-    {
-        if (isset($field['column_label']) || isset($field['column']) && $field['column'] == true) {
-            $column_data = ['name' => $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/CubistModelCrudController.php b/src/app/Http/Controllers/CubistModelCrudController.php
deleted file mode 100644 (file)
index d7c414b..0000000
+++ /dev/null
@@ -1,87 +0,0 @@
-<?php
-
-namespace Cubist\Backpack\app\Http\Controllers;
-
-use Backpack\CRUD\app\Http\Controllers\CrudController;
-
-// VALIDATION: change the requests to match your own file names if you need form validation
-use Cubist\Backpack\app\Http\Requests\CubistModelRequest as StoreRequest;
-use Cubist\Backpack\app\Http\Requests\CubistModelRequest as UpdateRequest;
-use Backpack\CRUD\CrudPanel;
-use Webfactor\Laravel\Backpack\InstantFields\InstantFields;
-
-/**
- * Class ModelCrudController
- * @package App\Http\Controllers\Admin
- * @property-read CrudPanel $crud
- */
-class CubistModelCrudController extends CrudController
-{
-    use CubistCrud;
-    use InstantFields;
-
-    public function setup()
-    {
-        /*
-        |--------------------------------------------------------------------------
-        | CrudPanel Basic Information
-        |--------------------------------------------------------------------------
-        */
-        $this->crud->setModel('Cubist\Backpack\app\Models\CubistModel');
-        $this->crud->setRoute(config('backpack.base.route_prefix') . '/model');
-        $this->crud->setEntityNameStrings('model', 'models');
-
-
-        /*
-        |--------------------------------------------------------------------------
-        | CrudPanel Configuration
-        |--------------------------------------------------------------------------
-        */
-
-        $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' => Entity::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');
-
-        // 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/CubistModelFieldCrudController.php b/src/app/Http/Controllers/CubistModelFieldCrudController.php
deleted file mode 100644 (file)
index d3174f1..0000000
+++ /dev/null
@@ -1,69 +0,0 @@
-<?php
-
-
-namespace Cubist\Backpack\app\Http\Controllers;
-
-use Backpack\CRUD\app\Http\Controllers\CrudController;
-
-// VALIDATION: change the requests to match your own file names if you need form validation
-use Cubist\Backpack\app\Http\Requests\CubistModelRequest as StoreRequest;
-use Cubist\Backpack\app\Http\Requests\CubistModelRequest as UpdateRequest;
-use Webfactor\Laravel\Backpack\InstantFields\InstantFields;
-
-
-class CubistModelFieldCrudController extends CrudController
-{
-    use CubistCrud;
-    use InstantFields;
-
-    public function setup()
-    {
-        /*
-        |--------------------------------------------------------------------------
-        | CrudPanel Basic Information
-        |--------------------------------------------------------------------------
-        */
-        $this->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;
-    }
-}
-
index 853f41466f1769876328d569677d6fc190cd7fb6..deea0271fc36bd8beaaa916dfad2963acac9dd15 100644 (file)
@@ -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/Requests/ModelRequest.php b/src/app/Http/Requests/ModelRequest.php
deleted file mode 100644 (file)
index 88b98b5..0000000
+++ /dev/null
@@ -1,55 +0,0 @@
-<?php
-
-namespace Cubist\Backpack\app\Http\Requests;
-
-use Illuminate\Foundation\Http\FormRequest;
-
-class CubistModelRequest extends FormRequest
-{
-    /**
-     * Determine if the user is authorized to make this request.
-     *
-     * @return bool
-     */
-    public function authorize()
-    {
-        // only allow updates if the user is logged in
-        return backpack_auth()->check();
-    }
-
-    /**
-     * Get the validation rules that apply to the request.
-     *
-     * @return array
-     */
-    public function rules()
-    {
-        return [
-            // 'name' => 'required|min:5|max:255'
-        ];
-    }
-
-    /**
-     * Get the validation attributes that apply to the request.
-     *
-     * @return array
-     */
-    public function attributes()
-    {
-        return [
-            //
-        ];
-    }
-
-    /**
-     * Get the validation messages that apply to the request.
-     *
-     * @return array
-     */
-    public function messages()
-    {
-        return [
-            //
-        ];
-    }
-}
diff --git a/src/app/Magic/Controllers/CubistMagicController.php b/src/app/Magic/Controllers/CubistMagicController.php
new file mode 100644 (file)
index 0000000..d8f6b14
--- /dev/null
@@ -0,0 +1,85 @@
+<?php
+
+
+namespace Cubist\Backpack\app\Magic\Controllers;
+
+
+use Backpack\CRUD\app\Http\Controllers\CrudController;
+use Cubist\Backpack\app\Http\Controllers\CubistCrud;
+use Cubist\Backpack\app\Magic\Requests\CubistMagicStoreRequest;
+use Cubist\Backpack\app\Magic\Requests\CubistMagicUpdateRequest;
+
+class CubistMagicController extends CrudController
+{
+    use CubistCrud;
+
+    protected $_modelNamespace;
+    protected $_routeURL;
+    protected $_singular;
+    protected $_plural;
+
+    public function setup()
+    {
+        /*
+        |--------------------------------------------------------------------------
+        | CrudPanel Basic Information
+        |--------------------------------------------------------------------------
+        */
+        $this->crud->setModel($this->_modelNamespace);
+        $this->crud->setRoute(config('backpack.base.route_prefix') . '/' . $this->_routeURL);
+        $this->crud->setEntityNameStrings($this->_singular, $this->_plural);
+
+
+        /*
+        |--------------------------------------------------------------------------
+        | CrudPanel Configuration
+        |--------------------------------------------------------------------------
+        */
+
+        $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');
+
+        // add asterisk for fields that are required in ModelRequest
+        $this->crud->setRequiredFields(CubistMagicStoreRequest::class, 'create');
+        $this->crud->setRequiredFields(CubistMagicUpdateRequest::class, 'edit');
+    }
+
+    public function store(CubistMagicStoreRequest $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(CubistMagicUpdateRequest $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/Magic/CubistCrud.php b/src/app/Magic/CubistCrud.php
new file mode 100644 (file)
index 0000000..6dadc1b
--- /dev/null
@@ -0,0 +1,23 @@
+<?php
+
+
+namespace Cubist\Backpack\app\Magic;
+
+use Backpack\CRUD\CrudTrait;
+use Cubist\Backpack\app\Magic\Fields\CubistMagicField;
+
+trait CubistCrud
+{
+    use CrudTrait;
+
+    /**
+     * @param $field CubistMagicField
+     */
+    public function addField($field)
+    {
+        if ($field->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 (file)
index 0000000..ae42677
--- /dev/null
@@ -0,0 +1,16 @@
+<?php
+
+
+namespace Cubist\Backpack\app\Magic;
+
+
+trait CubistMagicAttribute
+{
+    public function getAttribute($key, $default = null)
+    {
+        if (isset($this->_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 (file)
index 0000000..2bac171
--- /dev/null
@@ -0,0 +1,79 @@
+<?php
+
+
+namespace Cubist\Backpack\app\Magic\Fields;
+
+use Cubist\Backpack\app\Magic\CubistMagicAttribute;
+use Exception;
+
+class CubistMagicField
+{
+    use CubistMagicAttribute;
+    protected $_attributes;
+
+    /**
+     * @param $attributes
+     * @return CubistMagicField
+     * @throws Exception
+     */
+    public static function getInstance($attributes)
+    {
+        if (!isset($attributes['type'])) {
+            throw new Exception('You must specify a field type');
+        }
+        if (!isset($attributes['name'])) {
+            throw new Exception('You must specify a field name');
+        }
+        $class = static::_getClass($attributes['type']);
+        return new $class($attributes);
+    }
+
+    protected static function _getClass($type)
+    {
+        return self::class;
+    }
+
+    public function getDefaultAttributes()
+    {
+        return ['type' => '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 (file)
index 0000000..5649838
--- /dev/null
@@ -0,0 +1,69 @@
+<?php
+
+
+namespace Cubist\Backpack\app\Magic\Models;
+
+use Cubist\Backpack\app\Magic\Controllers\CubistMagicController;
+use Cubist\Backpack\app\Magic\Fields\CubistMagicField;
+use Illuminate\Database\Eloquent\Model;
+use Cubist\Backpack\app\Magic\CubistMagicAttribute;
+use Illuminate\Support\Facades\Route;
+use Backpack\CRUD;
+
+class CubistMagicModelAbstract extends Model
+{
+
+    use CubistMagicAttribute;
+    /**
+     * @var CubistMagicController
+     */
+    protected $_controller = null;
+
+    /**
+     * @var array
+     */
+    protected $_fields = [];
+
+    public function __construct(array $attributes = [])
+    {
+        parent::__construct($attributes);
+    }
+
+    /**
+     * @return CubistMagicController
+     */
+    public function getController(): CubistMagicController
+    {
+        if (null === $this->_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/Magic/Requests/CubistMagicRequest.php b/src/app/Magic/Requests/CubistMagicRequest.php
new file mode 100644 (file)
index 0000000..d5c64d4
--- /dev/null
@@ -0,0 +1,58 @@
+<?php
+
+
+namespace app\Magic\Requests;
+
+use Illuminate\Foundation\Http\FormRequest;
+
+class CubistMagicRequest extends FormRequest
+{
+    /**
+     * Determine if the user is authorized to make this request.
+     *
+     * @return bool
+     */
+    public function authorize()
+    {
+        // only allow updates if the user is logged in
+        return backpack_auth()->check();
+    }
+
+
+    /**
+     * Get the validation rules that apply to the request.
+     *
+     * @return array
+     */
+    public function rules()
+    {
+        return [
+            // 'name' => 'required|min:5|max:255'
+        ];
+    }
+
+    /**
+     * Get the validation attributes that apply to the request.
+     *
+     * @return array
+     */
+    public function attributes()
+    {
+        return [
+            //
+        ];
+    }
+
+    /**
+     * Get the validation messages that apply to the request.
+     *
+     * @return array
+     */
+    public function messages()
+    {
+        return [
+            //
+        ];
+    }
+
+}
diff --git a/src/app/Magic/Requests/CubistMagicStoreRequest.php b/src/app/Magic/Requests/CubistMagicStoreRequest.php
new file mode 100644 (file)
index 0000000..adf513c
--- /dev/null
@@ -0,0 +1,12 @@
+<?php
+
+
+namespace Cubist\Backpack\app\Magic\Requests;
+
+
+use app\Magic\Requests\CubistMagicRequest;
+
+class CubistMagicStoreRequest extends CubistMagicRequest
+{
+
+}
diff --git a/src/app/Magic/Requests/CubistMagicUpdateRequest.php b/src/app/Magic/Requests/CubistMagicUpdateRequest.php
new file mode 100644 (file)
index 0000000..e7b181a
--- /dev/null
@@ -0,0 +1,12 @@
+<?php
+
+
+namespace Cubist\Backpack\app\Magic\Requests;
+
+
+use app\Magic\Requests\CubistMagicRequest;
+
+class CubistMagicUpdateRequest extends CubistMagicRequest
+{
+
+}
diff --git a/src/app/Models/CubistModel.php b/src/app/Models/CubistModel.php
deleted file mode 100644 (file)
index 46354be..0000000
+++ /dev/null
@@ -1,58 +0,0 @@
-<?php
-
-namespace Cubist\Backpack\app\Models;
-
-
-use Illuminate\Database\Eloquent\Model;
-use Backpack\CRUD\CrudTrait;
-
-class CubistModel extends Model
-{
-
-    use CrudTrait;
-
-    /*
-    |--------------------------------------------------------------------------
-    | GLOBAL VARIABLES
-    |--------------------------------------------------------------------------
-    */
-
-    protected $table = 'cubist_models';
-    // protected $primaryKey = 'id';
-    // public $timestamps = false;
-    // protected $guarded = ['id'];
-    protected $fillable = ['name', 'label', 'fields'];
-    // protected $hidden = [];
-    // protected $dates = [];
-
-    /*
-    |--------------------------------------------------------------------------
-    | FUNCTIONS
-    |--------------------------------------------------------------------------
-    */
-
-    /*
-    |--------------------------------------------------------------------------
-    | RELATIONS
-    |--------------------------------------------------------------------------
-    */
-
-    /*
-    |--------------------------------------------------------------------------
-    | SCOPES
-    |--------------------------------------------------------------------------
-    */
-
-    /*
-    |--------------------------------------------------------------------------
-    | ACCESORS
-    |--------------------------------------------------------------------------
-    */
-
-    /*
-    |--------------------------------------------------------------------------
-    | MUTATORS
-    |--------------------------------------------------------------------------
-    */
-}
-
diff --git a/src/app/Models/CubistModelField.php b/src/app/Models/CubistModelField.php
deleted file mode 100644 (file)
index b18c02c..0000000
+++ /dev/null
@@ -1,14 +0,0 @@
-<?php
-
-
-namespace app\Models;
-
-
-use Backpack\CRUD\CrudTrait;
-
-class CubistModelField
-{
-    use CrudTrait;
-    protected $table = 'cubist_modelfields';
-    protected $fillable = ['name', 'label', 'attributes'];
-}