]> _ Git - cubist_cms-back.git/commitdiff
#2843
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Thu, 27 Jun 2019 17:03:04 +0000 (19:03 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Thu, 27 Jun 2019 17:03:04 +0000 (19:03 +0200)
src/app/Magic/Fields/Files.php
src/app/Magic/Models/CubistMagicAbstractModel.php
src/resources/views/fields/dropzone_media.blade.php

index ca2deeaafcc5dffccf493bdfea06fa04c8fde827..002e584bbdfd8397898fa4dfa7f48383d70f8d4e 100644 (file)
@@ -12,17 +12,18 @@ class Files extends Field
     protected $_adminType = 'dropzone_media';
     protected $_viewNamespace = CubistBackpackServiceProvider::NAMESPACE . '::fields';
     protected $_databaseType = 'text';
-    protected $_collection = 'default';
     protected $_thumbCollection = 'backpack_thumb';
     protected $_maxFiles = 1;
 
     public function getDefaultAttributes()
     {
+        $collection = hash('sha256', 'c_' . rand(1, 10000000000000));
         return array_merge(parent::getDefaultAttributes(), [
             'form' => 'update',
             'mime_types' => $this->_mimeTypes,
-            'collection' => $this->_collection,
+            'collection' => $collection,
             'thumb_collection' => $this->_thumbCollection,
+            'default' => $collection,
             'options' => ['thumbnailHeight' => 368, 'thumbnailWidth' => 232, 'maxFilesize' => 1024, 'addRemoveLinks' => true, 'createImageThumbnails' => true, 'maxFiles' => $this->_maxFiles]]);
     }
 
@@ -37,9 +38,4 @@ class Files extends Field
         ]);
     }
 
-    protected function _postSetAttributes()
-    {
-        parent::_postSetAttributes();
-        $this->setAttributeIfNotSet('collection', $this->getAttribute('name'));
-    }
 }
index bfaec3cb6d7c665ae1dd4e839930a615421f644a..4252fe7c21483c239c8754eaba595c6fe11a2cdd 100644 (file)
@@ -469,6 +469,11 @@ class CubistMagicAbstractModel extends Model implements HasMedia
     }
 
 
+    public function getAllMedia()
+    {
+        return Media::where('model_id', $this->getKey())->where('model_type', get_class($this))->get();
+    }
+
     /**
      * @param $controller CubistMagicController
      */
index 1443a2ca5a6a7965d1cb70dcb3a8128a56540bf6..2d5e9c869e7536d2a07995e75d6009fdfac4232c 100644 (file)
@@ -1,3 +1,7 @@
+<?php
+$collection = old(square_brackets_to_dots($field['name'])) ?? $field['value'] ?? $field['default'] ?? [];
+?>
+
 @section('previewTemplate')
     <div class="dz-preview dz-file-preview">
         <div class="dz-image">
 
 <div class="form-group col-md-12">
     <strong>{{ $field['label'] }}</strong> <br>
-    <input type="hidden" value="{{$field['collection']}}" name="{{$field['name']}}"/>
+    <input type="hidden" value="{{$collection}}" name="{{$field['name']}}"/>
     <div data-url="{{ url($crud->route . '/' . $entry->id . '/media') }}"
          data-options='@json($field['options'])'
-         data-collection="{{$field['collection']}}"
          class="dropzone dz-clickable sortable">
         <div class="dz-message">
             Drop files here or click to upload.
 
         $files = [];
         $filesrand = 'files_' . rand(1, 10000);
-        foreach ($entry->getMedia() as $media) {
-            if (!isset($files[$field['collection']])) {
-                $files[$field['collection']] = [];
+        foreach ($entry->getAllMedia() as $media) {
+            $collection = $media->collection_name;
+            if (!isset($files[$collection])) {
+                $files[$collection] = [];
             }
-            $files[$field['collection']][] = [
+            $files[$collection][] = [
                 'id' => $media->id,
                 'order_column' => $media->order_column,
                 'size' => $media->size,
                     var el = $(el);
                     var base_url = el.data('url');
                     var el_id = $(el).attr('id');
-                    var collection = $(el).data('collection');
+                    var collection = $(el).prev().val();
                     var dOptions = {
                         url: base_url,
                         previewTemplate: '{!! str_replace(array("\r\n", "\r", "\n"), "", addslashes(View::getSection("previewTemplate"))); !!}',
                         init: function () {
                             var files = f_{{$filesrand}}[collection];
+                            if (files === undefined) {
+                                files = [];
+                            }
+                            console.log(collection, files);
 
                             for (var i = 0; i < files.length; i++) {
                                 var file = files[i];