]> _ Git - cubist_cms-back.git/commitdiff
wip #2628 @0.5
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Mon, 11 Mar 2019 16:15:51 +0000 (17:15 +0100)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Mon, 11 Mar 2019 16:15:51 +0000 (17:15 +0100)
.editorconfig [new file with mode: 0644]
composer.json
src/CubistBackpackServiceProvider.php [new file with mode: 0644]
src/app/Http/Controllers/TemplateCrudController.php [new file with mode: 0644]
src/app/Http/Requests/TemplateRequest.php [new file with mode: 0644]
src/app/Models/Template.php [new file with mode: 0644]
src/routes/cubist/backpack/template.php [new file with mode: 0644]

diff --git a/.editorconfig b/.editorconfig
new file mode 100644 (file)
index 0000000..6f313c6
--- /dev/null
@@ -0,0 +1,15 @@
+root = true
+
+[*]
+charset = utf-8
+end_of_line = lf
+insert_final_newline = true
+indent_style = space
+indent_size = 4
+trim_trailing_whitespace = true
+
+[*.md]
+trim_trailing_whitespace = false
+
+[*.yml]
+indent_size = 2
index 5dcad91c5a8da5b1a875cc70ae6b973c045600b2..44ce851b5542e2e13475c9c97bf0730e60a498a9 100644 (file)
   "require-dev": {
     "backpack/generators": "^1.2"
   },
+  "extra": {
+    "branch-alias": {
+      "dev-master": "1.0-dev"
+    },
+    "laravel": {
+      "providers": [
+        "Cubeist\\Backpack\\ServiceProvider"
+      ]
+    }
+  },
   "repositories": [
     {
       "type": "composer",
diff --git a/src/CubistBackpackServiceProvider.php b/src/CubistBackpackServiceProvider.php
new file mode 100644 (file)
index 0000000..8c0f71a
--- /dev/null
@@ -0,0 +1,91 @@
+<?php
+
+namespace Cubist\Backpack;
+
+use Backpack\Settings\app\Models\Template as Setting;
+use Config;
+use Illuminate\Routing\Router;
+use Illuminate\Support\Facades\Schema;
+use Illuminate\Support\ServiceProvider;
+use Route;
+
+class CubistBackpackServiceProvider extends ServiceProvider
+{
+    /**
+     * Indicates if loading of the provider is deferred.
+     *
+     * @var bool
+     */
+    protected $defer = false;
+
+    /**
+     * Where the route file lives, both inside the package and in the app (if overwritten).
+     *
+     * @var string
+     */
+    public $routeFilePath = '/routes/cubist/backpack/settings.php';
+
+    /**
+     * Perform post-registration booting of services.
+     *
+     * @return void
+     */
+    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);
+//            }
+//        }
+        // publish the migrations and seeds
+        $this->publishes([__DIR__ . '/database/migrations/' => database_path('migrations')], 'migrations');
+
+        // publish translation files
+        //$this->publishes([__DIR__ . '/resources/lang' => resource_path('lang/vendor/backpack')], 'lang');
+    }
+
+    /**
+     * Define the routes for the application.
+     *
+     * @param \Illuminate\Routing\Router $router
+     *
+     * @return void
+     */
+    public function setupRoutes(Router $router)
+    {
+        // by default, use the routes file provided in vendor
+        $routeFilePathInUse = __DIR__ . $this->routeFilePath;
+
+        // but if there's a file with the same name in routes/backpack, use that one
+        if (file_exists(base_path() . $this->routeFilePath)) {
+            $routeFilePathInUse = base_path() . $this->routeFilePath;
+        }
+
+        $this->loadRoutesFrom($routeFilePathInUse);
+    }
+
+    /**
+     * Register any package services.
+     *
+     * @return void
+     */
+    public function register()
+    {
+        $this->app->bind('settings', function ($app) {
+            return new Settings($app);
+        });
+
+        // register their aliases
+        $loader = \Illuminate\Foundation\AliasLoader::getInstance();
+        $loader->alias('Setting', \Backpack\Settings\app\Models\Template::class);
+    }
+}
diff --git a/src/app/Http/Controllers/TemplateCrudController.php b/src/app/Http/Controllers/TemplateCrudController.php
new file mode 100644 (file)
index 0000000..ce97aac
--- /dev/null
@@ -0,0 +1,90 @@
+<?php
+
+namespace Cubist\Backpack\app\Http\Controllers;
+
+use Backpack\CRUD\app\Http\Controllers\CrudController;
+// VALIDATION
+use Cubist\Backpack\app\Http\Requests\TemplateRequest as StoreRequest;
+use Cubist\Backpack\app\Http\Requests\TemplateRequest as UpdateRequest;
+
+class TemplateCrudController extends CrudController
+{
+    public function setup()
+    {
+        parent::setup();
+
+        $this->crud->setModel("Cubist\Backpack\app\Models\Setting");
+        $this->crud->setEntityNameStrings(trans('backpack::settings.setting_singular'), trans('backpack::settings.setting_plural'));
+        $this->crud->setRoute(backpack_url('setting'));
+        $this->crud->addClause('where', 'active', 1);
+        $this->crud->denyAccess(['create', 'delete']);
+        $this->crud->setColumns([
+            [
+                'name' => 'name',
+                'label' => trans('backpack::settings.name'),
+            ],
+            [
+                'name' => 'value',
+                'label' => trans('backpack::settings.value'),
+            ],
+            [
+                'name' => 'description',
+                'label' => trans('backpack::settings.description'),
+            ],
+        ]);
+        $this->crud->addField([
+            'name' => 'name',
+            'label' => trans('backpack::settings.name'),
+            'type' => 'text',
+            'attributes' => [
+                'disabled' => 'disabled',
+            ],
+        ]);
+    }
+
+    /**
+     * Display all rows in the database for this entity.
+     * This overwrites the default CrudController behaviour:
+     * - instead of showing all entries, only show the "active" ones.
+     *
+     * @return Response
+     */
+    public function index()
+    {
+        return parent::index();
+    }
+
+    public function store(StoreRequest $request)
+    {
+        return parent::storeCrud();
+    }
+
+    /**
+     * Show the form for editing the specified resource.
+     *
+     * @param int $id
+     *
+     * @return Response
+     */
+    public function edit($id)
+    {
+        $this->crud->hasAccessOrFail('update');
+
+        $this->data['entry'] = $this->crud->getEntry($id);
+        $this->crud->addField(json_decode($this->data['entry']->field, true)); // <---- this is where it's different
+        $this->data['crud'] = $this->crud;
+        $this->data['saveAction'] = $this->getSaveAction();
+        $this->data['fields'] = $this->crud->getUpdateFields($id);
+        $this->data['title'] = trans('backpack::crud.edit') . ' ' . $this->crud->entity_name;
+
+        $this->data['id'] = $id;
+
+        // load the view from /resources/views/vendor/backpack/crud/ if it exists, otherwise load the one in the package
+        return view($this->crud->getEditView(), $this->data);
+    }
+
+    public function update(UpdateRequest $request)
+    {
+        return parent::updateCrud();
+    }
+}
diff --git a/src/app/Http/Requests/TemplateRequest.php b/src/app/Http/Requests/TemplateRequest.php
new file mode 100644 (file)
index 0000000..0762303
--- /dev/null
@@ -0,0 +1,33 @@
+<?php
+
+namespace Cubist\Backpack\app\Http\Requests;
+
+use Illuminate\Foundation\Http\FormRequest;
+
+class TemplateRequest 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 [
+            // 'key' => 'required|min:3|max:255',
+            // 'name' => 'required|min:3|max:255',
+            // 'field' => 'required'
+        ];
+    }
+}
diff --git a/src/app/Models/Template.php b/src/app/Models/Template.php
new file mode 100644 (file)
index 0000000..fc5a7a0
--- /dev/null
@@ -0,0 +1,60 @@
+<?php
+
+namespace Backpack\Settings\app\Models;
+
+use Backpack\CRUD\CrudTrait;
+use Config;
+use Illuminate\Database\Eloquent\Model;
+
+class Template extends Model
+{
+    use CrudTrait;
+
+    protected $table = 'cubist_templates';
+    protected $fillable = ['value'];
+
+    /**
+     * Grab a setting value from the database.
+     *
+     * @param string $key The setting key, as defined in the key db column
+     *
+     * @return string The setting value.
+     */
+    public static function get($key)
+    {
+        $setting = new self();
+        $entry = $setting->where('key', $key)->first();
+
+        if (!$entry) {
+            return;
+        }
+
+        return $entry->value;
+    }
+
+    /**
+     * Update a setting's value.
+     *
+     * @param string $key   The setting key, as defined in the key db column
+     * @param string $value The new value.
+     */
+    public static function set($key, $value = null)
+    {
+        $prefixed_key = 'settings.'.$key;
+        $setting = new self();
+        $entry = $setting->where('key', $key)->firstOrFail();
+
+        // update the value in the database
+        $entry->value = $value;
+        $entry->saveOrFail();
+
+        // update the value in the session
+        Config::set($prefixed_key, $value);
+
+        if (Config::get($prefixed_key) == $value) {
+            return true;
+        }
+
+        return false;
+    }
+}
diff --git a/src/routes/cubist/backpack/template.php b/src/routes/cubist/backpack/template.php
new file mode 100644 (file)
index 0000000..f167c4f
--- /dev/null
@@ -0,0 +1,19 @@
+<?php
+
+/*
+|--------------------------------------------------------------------------
+| Backpack\Settings Routes
+|--------------------------------------------------------------------------
+|
+| This file is where you may define all of the routes that are
+| handled by the Backpack\Settings package.
+|
+*/
+
+Route::group([
+    'namespace'  => 'Cubist\Backpack\app\Http\Controllers',
+    'prefix'     => config('backpack.base.route_prefix', 'admin'),
+    'middleware' => ['web', backpack_middleware()],
+], function () {
+    CRUD::resource('template', 'TemplateCrudController');
+});