From 83c62adc84daa5f7b1f6d012fa1d3bc848e628c9 Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Thu, 20 Jun 2019 15:20:38 +0200 Subject: [PATCH] #2843 --- src/app/CubistCrudPanel.php | 10 + .../Http/Controllers/CubistCrudController.php | 28 ++ .../Controllers/CubistMagicController.php | 6 +- src/app/Magic/Fields/BunchOfFields.php | 8 +- src/app/Magic/Fields/Files.php | 3 + src/app/Magic/Models/CMSPage.php | 65 +++++ src/app/PageManager/Models/Page.php | 4 +- src/app/Template/TemplateAbstract.php | 72 +---- src/resources/views/fields/bunch.blade.php | 39 ++- .../views/fields/dropzone_media.blade.php | 251 ++++++++++++++++++ .../views/inc/show_bunch_fields.blade.php | 9 + 11 files changed, 406 insertions(+), 89 deletions(-) create mode 100644 src/app/CubistCrudPanel.php create mode 100644 src/app/Http/Controllers/CubistCrudController.php create mode 100644 src/app/Magic/Models/CMSPage.php create mode 100644 src/resources/views/fields/dropzone_media.blade.php create mode 100644 src/resources/views/inc/show_bunch_fields.blade.php diff --git a/src/app/CubistCrudPanel.php b/src/app/CubistCrudPanel.php new file mode 100644 index 0000000..2ea006d --- /dev/null +++ b/src/app/CubistCrudPanel.php @@ -0,0 +1,10 @@ +crud) { + $this->crud = app()->make(CubistCrudPanel::class); + + // call the setup function inside this closure to also have the request there + // this way, developers can use things stored in session (auth variables, etc) + $this->middleware(function ($request, $next) { + $this->request = $request; + $this->crud->request = $request; + $this->setup(); + + return $next($request); + }); + } + } +} diff --git a/src/app/Magic/Controllers/CubistMagicController.php b/src/app/Magic/Controllers/CubistMagicController.php index 64cc081..cdfc21d 100644 --- a/src/app/Magic/Controllers/CubistMagicController.php +++ b/src/app/Magic/Controllers/CubistMagicController.php @@ -7,7 +7,7 @@ use Backpack\CRUD\app\Http\Controllers\CrudController; use Backpack\CRUD\CrudTrait; use Gaspertrix\Backpack\DropzoneField\Traits\HandleAjaxMedia; -class CubistMagicController extends CrudController +class CubistMagicController extends CubistCrudController { use CrudTrait, CubistMagicControllerTrait, HandleAjaxMedia; @@ -17,6 +17,10 @@ class CubistMagicController extends CrudController protected $_plural; protected $_bulk; + public function __construct() + { + } + public function _postSetModel() { diff --git a/src/app/Magic/Fields/BunchOfFields.php b/src/app/Magic/Fields/BunchOfFields.php index 9c02d90..68054b4 100644 --- a/src/app/Magic/Fields/BunchOfFields.php +++ b/src/app/Magic/Fields/BunchOfFields.php @@ -46,16 +46,18 @@ class BunchOfFields extends Field } } + $crudfields = []; foreach ($this->_fields as $field) { $name = $field->getAttribute('name'); $e = explode('[', $name); $main = array_shift($e); - $name = $this->getAttribute('name') . '[' . $main . ']'; + $name = $this->getAttribute('name') . '___' . $main; foreach ($e as $item) { - $name .= '[' . $item . ']'; + $name .= '-_-' . $item; } $field->setAttribute('name', $name); + $crudfields[] = $field->getDefinition(); } - $this->setAttribute('bunchfields', $this->getFields()); + $this->setAttribute('bunchfields', $crudfields); } } diff --git a/src/app/Magic/Fields/Files.php b/src/app/Magic/Fields/Files.php index 0ff1fab..dcfeaef 100644 --- a/src/app/Magic/Fields/Files.php +++ b/src/app/Magic/Fields/Files.php @@ -3,11 +3,14 @@ namespace Cubist\Backpack\app\Magic\Fields; +use Cubist\Backpack\CubistBackpackServiceProvider; + class Files extends Field { protected $_mimeTypes = null; protected $_multiple = false; protected $_adminType = 'dropzone_media'; + protected $_viewNamespace = CubistBackpackServiceProvider::NAMESPACE . '::fields'; protected $_databaseType = 'text'; protected $_collection = null; protected $_thumbCollection = 'backpack_thumb'; diff --git a/src/app/Magic/Models/CMSPage.php b/src/app/Magic/Models/CMSPage.php new file mode 100644 index 0000000..5482ee8 --- /dev/null +++ b/src/app/Magic/Models/CMSPage.php @@ -0,0 +1,65 @@ + 'page', + 'singular' => 'page', + 'plural' => 'pages']; + + public function setFields() + { + parent::setFields(); + + $this->addField(['name' => 'template', + 'type' => 'SelectFromArray', + 'label' => 'Template', + 'options' => $this->getTemplates(), + 'column' => true, + 'tab'=>'General', + ]); + + $this->addField(['name' => 'name', + 'label' => trans('Page name'), + 'type' => 'Text', + 'column' => true, + 'hint' => trans('for admin use'), + 'tab'=>'General', + ]); + + $this->addField(['name' => 'title', + 'label' => trans('Page title'), + 'type' => 'Text', + ]); + + $this->addField(['name' => 'slug', + 'type' => 'Slug', + 'label' => 'Slug (URL)' + ]); + } + + public function getTemplates($template_name = false) + { + if (null === self::$_templates) { + $templates_root = app_path() . '/Templates'; + $dr = opendir($templates_root); + while ($file = readdir($dr)) { + if ($file == '.' || $file == '..' || is_dir($templates_root . '/' . $file)) { + continue; + } + $e = explode('.', $file); + $classname = '\\App\\Templates\\' . $e[0]; + self::$_templates[] = new $classname(); + } + + if (!count(self::$_templates)) { + abort(503, trans('backpack::pagemanager.template_not_found')); + } + } + return self::$_templates; + } +} diff --git a/src/app/PageManager/Models/Page.php b/src/app/PageManager/Models/Page.php index e4b1fde..1698368 100644 --- a/src/app/PageManager/Models/Page.php +++ b/src/app/PageManager/Models/Page.php @@ -3,10 +3,10 @@ namespace Cubist\Backpack\app\PageManager\Models; - +use Spatie\MediaLibrary\HasMedia\HasMedia; use Spatie\MediaLibrary\HasMedia\HasMediaTrait; -class Page extends \Backpack\PageManager\app\Models\Page +class Page extends \Backpack\PageManager\app\Models\Page implements HasMedia { use HasMediaTrait; } diff --git a/src/app/Template/TemplateAbstract.php b/src/app/Template/TemplateAbstract.php index fcc598c..dc3f15d 100644 --- a/src/app/Template/TemplateAbstract.php +++ b/src/app/Template/TemplateAbstract.php @@ -2,30 +2,12 @@ namespace Cubist\Backpack\app\Template; -use Backpack\CRUD\CrudPanel; use Cubist\Backpack\app\Magic\BunchOfFields; -use Cubist\Backpack\app\Magic\Fields\Field; use Illuminate\Support\Str; class TemplateAbstract { - use BunchOfFields { - addField as protected bunchAddField; - } - - /** - * @var CrudPanel - */ - public $crud; - - /** - * @param CrudPanel $crud - */ - public function use($crud) - { - $this->crud = $crud; - $this->init(); - } + use BunchOfFields; protected function _seo() { @@ -59,42 +41,13 @@ class TemplateAbstract protected function _common() { - $this->addField([ - 'name' => 'name', - 'label' => trans('backpack::pagemanager.page_name'), - 'type' => 'Text', - 'wrapperAttributes' => [ - 'class' => 'form-group col-md-6', - ], - 'fake' => false, - 'tab' => 'General', - // 'disabled' => 'disabled' - ]); - $this->addField([ - 'name' => 'title', - 'label' => trans('backpack::pagemanager.page_title'), - 'type' => 'Text', - 'fake' => false, - 'tab' => 'General', - // 'disabled' => 'disabled' - ]); - $this->addField([ - 'name' => 'slug', - 'label' => trans('backpack::pagemanager.page_slug'), - 'type' => 'Slug', - 'hint' => trans('backpack::pagemanager.page_slug_hint'), - 'fake' => false, - 'tab' => 'General', - // 'disabled' => 'disabled' - ]); - $this->addField([ 'name' => 'status', 'type' => 'SelectFromArray', 'default' => '0', 'label' => __('Status'), 'options' => ['0' => __('Offline'), '1' => __('Published')], - 'tab' => 'General', + 'tab' => 'Général', ]); $this->_seo(); } @@ -117,25 +70,4 @@ class TemplateAbstract return Str::slug($class); } - - /** - * @param array $attributes - * @return Field - * @throws \Exception - */ - public function addField(array $attributes) - { - // Set default options of field - $defaults = ['tab' => 'Contents', - 'fake' => true, - 'store_in' => 'extras', - 'form' => 'both']; - $attributes = array_merge($defaults, $attributes); - - $field = $this->bunchAddField($attributes); - - $this->crud->addField($field->getDefinition(), $field->getAttribute('form')); - - return $field; - } } diff --git a/src/resources/views/fields/bunch.blade.php b/src/resources/views/fields/bunch.blade.php index c5f9593..cf4c688 100644 --- a/src/resources/views/fields/bunch.blade.php +++ b/src/resources/views/fields/bunch.blade.php @@ -4,21 +4,21 @@ ?> +
+
+ + @include('crud::inc.field_translatable_icon') +
-
- - @include('crud::inc.field_translatable_icon') -
+ @include('cubist_back::inc.show_bunch_fields', array('bunchfields'=>$field['bunchfields'])) - @include('crud::inc.show_fields', array('fields'=>$field['bunchfields'])) - -
- - {{-- HINT --}} - @if (isset($field['hint'])) -

{!! $field['hint'] !!}

- @endif +
+ {{-- HINT --}} + @if (isset($field['hint'])) +

{!! $field['hint'] !!}

+ @endif +
@@ -30,7 +30,20 @@ {{-- FIELD CSS - will be loaded in the after_styles section --}} @push('crud_fields_styles') @endpush diff --git a/src/resources/views/fields/dropzone_media.blade.php b/src/resources/views/fields/dropzone_media.blade.php new file mode 100644 index 0000000..64f0c42 --- /dev/null +++ b/src/resources/views/fields/dropzone_media.blade.php @@ -0,0 +1,251 @@ +@section('previewTemplate') +
+
+ +
+
+
+ +
+
+ +
+
+
+ +
+
+ +
+
+ + Check + + + + + +
+
+ + Error + + + + + + + +
+
+@endsection + +
+ {{ $field['label'] }}
+
+
+ Drop files here or click to upload. +
+
+
+ +{{-- ########################################## --}} +{{-- Extra CSS and JS for this particular field --}} +{{-- If a field type is shown multiple times on a form, the CSS and JS will only be loaded once --}} +@if (true || $crud->checkIfFieldIsFirstOfItsType($field, $fields)) + {{-- FIELD CSS - will be loaded in the after_styles section --}} + @push('crud_fields_styles') + + + @endpush + + {{-- FIELD JS - will be loaded in the after_scripts section --}} + @push('crud_fields_scripts') + + + + @endpush + +@endif + +@push('crud_fields_scripts') + +@endpush diff --git a/src/resources/views/inc/show_bunch_fields.blade.php b/src/resources/views/inc/show_bunch_fields.blade.php new file mode 100644 index 0000000..68509f2 --- /dev/null +++ b/src/resources/views/inc/show_bunch_fields.blade.php @@ -0,0 +1,9 @@ +{{-- Show the inputs --}} +@foreach ($bunchfields as $field) + + @php + $fieldsViewNamespace = $field['view_namespace'] ?? 'crud::fields'; + @endphp + + @include($fieldsViewNamespace.'.'.$field['type'], ['field' => $field]) +@endforeach -- 2.39.5