]> _ Git - cubist_cms-back.git/commitdiff
#2843
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Thu, 20 Jun 2019 13:20:38 +0000 (15:20 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Thu, 20 Jun 2019 13:20:38 +0000 (15:20 +0200)
src/app/CubistCrudPanel.php [new file with mode: 0644]
src/app/Http/Controllers/CubistCrudController.php [new file with mode: 0644]
src/app/Magic/Controllers/CubistMagicController.php
src/app/Magic/Fields/BunchOfFields.php
src/app/Magic/Fields/Files.php
src/app/Magic/Models/CMSPage.php [new file with mode: 0644]
src/app/PageManager/Models/Page.php
src/app/Template/TemplateAbstract.php
src/resources/views/fields/bunch.blade.php
src/resources/views/fields/dropzone_media.blade.php [new file with mode: 0644]
src/resources/views/inc/show_bunch_fields.blade.php [new file with mode: 0644]

diff --git a/src/app/CubistCrudPanel.php b/src/app/CubistCrudPanel.php
new file mode 100644 (file)
index 0000000..2ea006d
--- /dev/null
@@ -0,0 +1,10 @@
+<?php
+
+namespace Cubist\Backpack;
+
+use Backpack\CRUD\CrudPanel;
+
+class CubistCrudPanel extends CrudPanel
+{
+
+}
diff --git a/src/app/Http/Controllers/CubistCrudController.php b/src/app/Http/Controllers/CubistCrudController.php
new file mode 100644 (file)
index 0000000..725e460
--- /dev/null
@@ -0,0 +1,28 @@
+<?php
+
+
+namespace app\Http\Controllers;
+
+
+use Backpack\CRUD\app\Http\Controllers\CrudController;
+use Cubist\Backpack\CubistCrudPanel;
+
+class CubistCrudController extends CrudController
+{
+    public function __construct()
+    {
+        if (!$this->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);
+            });
+        }
+    }
+}
index 64cc0813597905d34f731be2260f2e2f063261dd..cdfc21df2674a82f84c7b6e6528797a07fb5b10b 100644 (file)
@@ -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()
     {
 
index 9c02d90317a88bee197050fef139089804be3bc3..68054b43b1b9098352e1753c1fd2b6c94c39c8df 100644 (file)
@@ -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);
     }
 }
index 0ff1fabe063f36696f4d69e5f3285a57d4078ee1..dcfeaef4613ff5efa02815d1e15961e6892cf1e6 100644 (file)
@@ -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 (file)
index 0000000..5482ee8
--- /dev/null
@@ -0,0 +1,65 @@
+<?php
+
+
+namespace Cubist\Backpack\app\Magic\Models;
+
+class CMSPage extends CubistMagicModel
+{
+    protected $table = 'cms_pages';
+
+    protected $_options = ['name' => '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;
+    }
+}
index e4b1fdef351a4ae7ac114373e772ccad427e05ab..1698368258425f9acb291b46afabecf8c8345c56 100644 (file)
@@ -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;
 }
index fcc598c24b8c312bcf2838fa65cc9c8d85874b53..dc3f15d8862bbe960e52081f1d9a887361e59bb4 100644 (file)
@@ -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;
-    }
 }
index c5f95936582214e7d05af6d2d195e377913ce0f5..cf4c688a474bf7eba7733a72996ff43838dd3a5a 100644 (file)
@@ -4,21 +4,21 @@
 
 
 ?>
+<div class="bunch__wrapper col-xs-12">
+    <div @include('crud::inc.field_wrapper_attributes') >
+        <label>{!! $field['label'] !!}</label>
+        @include('crud::inc.field_translatable_icon')
+        <div class="clearfix"></div>
 
-<div @include('crud::inc.field_wrapper_attributes') >
-    <label>{!! $field['label'] !!}</label>
-    @include('crud::inc.field_translatable_icon')
-    <div class="clearfix"></div>
+        @include('cubist_back::inc.show_bunch_fields', array('bunchfields'=>$field['bunchfields']))
 
-    @include('crud::inc.show_fields', array('fields'=>$field['bunchfields']))
-
-    <div class="clearfix"></div>
-
-    {{-- HINT --}}
-    @if (isset($field['hint']))
-        <p class="help-block">{!! $field['hint'] !!}</p>
-    @endif
+        <div class="clearfix"></div>
 
+        {{-- HINT --}}
+        @if (isset($field['hint']))
+            <p class="help-block">{!! $field['hint'] !!}</p>
+        @endif
+    </div>
 </div>
 
 
     {{-- FIELD CSS - will be loaded in the after_styles section --}}
     @push('crud_fields_styles')
         <style type="text/css">
-
+            .bunch__wrapper {
+                padding: 15px;
+                border: 1px solid #eee;
+                background: #fafafa;
+                margin-bottom: 15px;
+            }
+            .bunch__wrapper .form-group{
+                padding: 0;
+            }
+
+            .bunch__wrapper > .form-group > label{
+                font-size: 1.2em;
+                margin-bottom: 24px;
+            }
         </style>
     @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 (file)
index 0000000..64f0c42
--- /dev/null
@@ -0,0 +1,251 @@
+@section('previewTemplate')
+    <div class="dz-preview dz-file-preview">
+        <div class="dz-image">
+            <img data-dz-thumbnail/>
+        </div>
+        <div class="dz-details">
+            <div class="dz-size">
+                <span data-dz-size></span>
+            </div>
+            <div class="dz-filename">
+                <span data-dz-name></span>
+            </div>
+        </div>
+        <div class="dz-progress">
+            <span class="dz-upload" data-dz-uploadprogress></span>
+        </div>
+        <div class="dz-error-message">
+            <span data-dz-errormessage></span>
+        </div>
+        <div class="dz-success-mark">
+            <svg width="54px" height="54px" viewBox="0 0 54 54" version="1.1" xmlns="http://www.w3.org/2000/svg"
+                 xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:sketch="http://www.bohemiancoding.com/sketch/ns">
+                <title>Check</title>
+                <defs></defs>
+                <g id="Page-1" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd" sketch:type="MSPage">
+                    <path
+                        d="M23.5,31.8431458 L17.5852419,25.9283877 C16.0248253,24.3679711 13.4910294,24.366835 11.9289322,25.9289322 C10.3700136,27.4878508 10.3665912,30.0234455 11.9283877,31.5852419 L20.4147581,40.0716123 C20.5133999,40.1702541 20.6159315,40.2626649 20.7218615,40.3488435 C22.2835669,41.8725651 24.794234,41.8626202 26.3461564,40.3106978 L43.3106978,23.3461564 C44.8771021,21.7797521 44.8758057,19.2483887 43.3137085,17.6862915 C41.7547899,16.1273729 39.2176035,16.1255422 37.6538436,17.6893022 L23.5,31.8431458 Z M27,53 C41.3594035,53 53,41.3594035 53,27 C53,12.6405965 41.3594035,1 27,1 C12.6405965,1 1,12.6405965 1,27 C1,41.3594035 12.6405965,53 27,53 Z"
+                        id="Oval-2" stroke-opacity="0.198794158" stroke="#747474" fill-opacity="0.816519475"
+                        fill="#FFFFFF" sketch:type="MSShapeGroup"></path>
+                </g>
+            </svg>
+        </div>
+        <div class="dz-error-mark">
+            <svg width="54px" height="54px" viewBox="0 0 54 54" version="1.1" xmlns="http://www.w3.org/2000/svg"
+                 xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:sketch="http://www.bohemiancoding.com/sketch/ns">
+                <title>Error</title>
+                <defs></defs>
+                <g id="Page-1" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd" sketch:type="MSPage">
+                    <g id="Check-+-Oval-2" sketch:type="MSLayerGroup" stroke="#747474" stroke-opacity="0.198794158"
+                       fill="#FFFFFF" fill-opacity="0.816519475">
+                        <path
+                            d="M32.6568542,29 L38.3106978,23.3461564 C39.8771021,21.7797521 39.8758057,19.2483887 38.3137085,17.6862915 C36.7547899,16.1273729 34.2176035,16.1255422 32.6538436,17.6893022 L27,23.3431458 L21.3461564,17.6893022 C19.7823965,16.1255422 17.2452101,16.1273729 15.6862915,17.6862915 C14.1241943,19.2483887 14.1228979,21.7797521 15.6893022,23.3461564 L21.3431458,29 L15.6893022,34.6538436 C14.1228979,36.2202479 14.1241943,38.7516113 15.6862915,40.3137085 C17.2452101,41.8726271 19.7823965,41.8744578 21.3461564,40.3106978 L27,34.6568542 L32.6538436,40.3106978 C34.2176035,41.8744578 36.7547899,41.8726271 38.3137085,40.3137085 C39.8758057,38.7516113 39.8771021,36.2202479 38.3106978,34.6538436 L32.6568542,29 Z M27,53 C41.3594035,53 53,41.3594035 53,27 C53,12.6405965 41.3594035,1 27,1 C12.6405965,1 1,12.6405965 1,27 C1,41.3594035 12.6405965,53 27,53 Z"
+                            id="Oval-2" sketch:type="MSShapeGroup"></path>
+                    </g>
+                </g>
+            </svg>
+        </div>
+    </div>
+@endsection
+
+<div class="form-group col-md-12">
+    <strong>{{ $field['label'] }}</strong> <br>
+    <div id="dropzone_{{ $field['name'] }}" class="dropzone dz-clickable sortable">
+        <div class="dz-message">
+            Drop files here or click to upload.
+        </div>
+    </div>
+</div>
+
+{{-- ########################################## --}}
+{{-- 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')
+        <!-- include dropzone css-->
+        <link rel="stylesheet"
+              href="{{ asset('vendor/gaspertrix/laravel-backpack-dropzone-field/dropzone/dropzone.min.css') }}"/>
+    @endpush
+
+    {{-- FIELD JS - will be loaded in the after_scripts section --}}
+    @push('crud_fields_scripts')
+        <!-- include dropzone js-->
+        <script
+            src="{{ asset('vendor/gaspertrix/laravel-backpack-dropzone-field/dropzone/dropzone.min.js') }}"></script>
+        <script
+            src="{{ asset('vendor/gaspertrix/laravel-backpack-dropzone-field/sortable/Sortable.min.js') }}"></script>
+    @endpush
+
+@endif
+
+@push('crud_fields_scripts')
+    <script type="text/javascript">
+        Dropzone.autoDiscover = false;
+
+        jQuery(document).ready(function () {
+
+            Dropzone.autoDiscover = false;
+
+            var dOptions = {
+                url: "{{ url($crud->route . '/' . $entry->id . '/media') }}",
+                previewTemplate: '{!! str_replace(array("\r\n", "\r", "\n"), "", addslashes(View::getSection("previewTemplate"))); !!}',
+                init: function () {
+                    var files = [];
+
+                    @foreach ($entry->getMedia($field['collection']) as $media)
+                    files.push({
+                        id: {{ $media->id }},
+                        order_column: {{ $media->order_column }},
+                        size: "{{ $media->size }}",
+                        name: "{{ $media->file_name }}",
+                        full_url: "{{ $media->getUrl() }}",
+                        thumb_url: "{{ $media->getUrl($field['thumb_collection'] ?? '') }}"
+                    });
+                        @endforeach
+
+                    for (var i = 0; i < files.length; i++) {
+                        var file = files[i];
+
+                        this.emit('addedfile', file);
+
+                        if (typeof file.full_url != 'undefined') {
+                            this.emit('thumbnail', file, file.full_url);
+                        }
+
+                        this.emit('success', file, {success: true, media: file});
+                        this.emit('complete', file);
+                    }
+
+                    if (this.options.maxFiles !== null) {
+                        this.options.maxFiles = this.options.maxFiles - files.length;
+                    }
+                },
+                sending: function (file, xhr, formData) {
+                    formData.append('_token', $('meta[name="csrf-token"]').attr('content'));
+
+                    @if (isset($field['collection']) AND !empty($field['collection']))
+                    formData.append('collection', "{{ $field['collection'] }}");
+                    @endif
+                },
+                success: function (file, response) {
+                    if (typeof response != 'undefined' && response.success == true) {
+                        file.media = response.media;
+                        file.previewElement.setAttribute('data-id', response.media.id);
+                        file.previewElement.setAttribute('data-position', response.media.order_column);
+                    }
+
+                    if (file.previewElement) {
+                        return file.previewElement.classList.add("dz-success");
+                    }
+                },
+                removedfile: function (file) {
+                    if (typeof file.media != 'undefined') {
+                        $.ajax({
+                            url: "{{ url($crud->route . '/' . $entry->id . '/media') }}" + '/' + file.media.id,
+                            type: 'DELETE'
+                        })
+                            .done(function (response) {
+                                var notification_type;
+
+                                if (response.success == true) {
+                                    notification_type = 'success';
+
+                                    if (file.previewElement != null && file.previewElement.parentNode != null) {
+                                        file.previewElement.parentNode.removeChild(file.previewElement);
+                                    }
+                                } else {
+                                    notification_type = 'error';
+                                }
+
+                                new PNotify({
+                                    text: response.message,
+                                    type: notification_type,
+                                    icon: false
+                                });
+                            })
+                            .fail(function (xhr) {
+                                var message = 'Deletion failed';
+
+                                if (xhr.responseJSON != 'undefined' && xhr.responseJSON.message != 'undefined') {
+                                    message = xhr.responseJSON.message;
+                                }
+
+                                new PNotify({
+                                    text: message,
+                                    type: 'error',
+                                    icon: false
+                                });
+                            });
+
+                        return this._updateMaxFilesReachedClass();
+                    }
+
+                    if (file.previewElement != null && file.previewElement.parentNode != null) {
+                        file.previewElement.parentNode.removeChild(file.previewElement);
+                    }
+
+                    return this._updateMaxFilesReachedClass();
+                },
+            };
+
+            var cOptions = @json($field['options']);
+
+            var dropzone_{{ $field['name'] }} = new Dropzone("#dropzone_{{ $field['name'] }}", jQuery.extend(dOptions, cOptions));
+            dropzone_{{ $field['name'] }}.on("maxfilesexceeded", function (file) {
+                this.removeFile(file);
+                alert('You can\'t add new files !')
+            });
+
+            var dropzone_{{ $field['name'] }}_sortable = new Sortable(document.getElementById("dropzone_{{ $field['name'] }}"), {
+                handle: ".dz-preview",
+                draggable: ".dz-preview",
+                onEnd: function (evt) {
+                    var ids = this.toArray();
+
+                    if (ids.length > 0) {
+                        $.ajax({
+                            url: "{{ url($crud->route . '/' . $entry->id . '/media/reorder') }}",
+                            type: 'POST',
+                            data: {
+                                ids: ids
+                            }
+                        })
+                            .done(function (response) {
+                                var notification_type;
+
+                                if (response.success != true) {
+                                    var message = 'Order failed';
+
+                                    if (response.message != 'undefined') {
+                                        message = response.message;
+                                    }
+
+                                    new PNotify({
+                                        text: message,
+                                        type: 'error',
+                                        icon: false
+                                    });
+                                }
+
+
+                            })
+                            .fail(function (xhr) {
+                                var message = 'Order failed';
+
+                                if (xhr.responseJSON != 'undefined' && xhr.responseJSON.message != 'undefined') {
+                                    message = xhr.responseJSON.message;
+                                }
+
+                                new PNotify({
+                                    text: message,
+                                    type: 'error',
+                                    icon: false
+                                });
+                            });
+                    }
+                }
+            });
+        });
+    </script>
+@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 (file)
index 0000000..68509f2
--- /dev/null
@@ -0,0 +1,9 @@
+{{-- Show the inputs --}}
+@foreach ($bunchfields as $field)
+    <!-- load the view from type and view_namespace attribute if set -->
+    @php
+        $fieldsViewNamespace = $field['view_namespace'] ?? 'crud::fields';
+    @endphp
+
+    @include($fieldsViewNamespace.'.'.$field['type'], ['field' => $field])
+@endforeach