]> _ Git - cubist_cms-back.git/commitdiff
wip #5127 @1
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Fri, 25 Feb 2022 18:22:34 +0000 (19:22 +0100)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Fri, 25 Feb 2022 18:22:34 +0000 (19:22 +0100)
src/app/Http/Controllers/CubistSelect2Controller.php [new file with mode: 0644]
src/app/Magic/Fields/SelectFromArray.php
src/resources/views/fields/select2_from_array.blade.php
src/routes/cubist/backpack/select2.php [new file with mode: 0644]

diff --git a/src/app/Http/Controllers/CubistSelect2Controller.php b/src/app/Http/Controllers/CubistSelect2Controller.php
new file mode 100644 (file)
index 0000000..958eb20
--- /dev/null
@@ -0,0 +1,22 @@
+<?php
+
+namespace Cubist\Backpack\Http\Controllers;
+
+use Cubist\Backpack\Http\Controllers\Base\XSendFileController;
+use Illuminate\Support\Facades\Auth;
+
+class CubistSelect2Controller extends XSendFileController
+{
+    public function search($hash)
+    {
+        if (!Auth::check()) {
+            return response(null)->setStatusCode('401');
+        }
+        if (!can('edition')) {
+            return response(null)->setStatusCode('403');
+        }
+        $data = json_decode(storage_path('select2/' . $hash . '.json'));
+        $res = [];
+        return response()->json($res);
+    }
+}
index a7e4dc3ca464c3850fa858d769ba7ae82f401b46..9161da8702f8bd3ab0e13757256b6e65f586884a 100644 (file)
@@ -19,10 +19,11 @@ class SelectFromArray extends Field
     protected $_options = [];
     protected $_options_aliases = [];
     protected $_filterType = 'dropdown';
+    protected $_ajax = false;
 
     public function getDefaultAttributes()
     {
-        return array_merge(parent::getDefaultAttributes(), ['options' => $this->getOptions(), 'options_aliases' => $this->_options_aliases, 'allows_null' => $this->_allowNull, 'allows_multiple' => $this->_multiple]);
+        return array_merge(parent::getDefaultAttributes(), ['options' => $this->getOptions(), 'options_aliases' => $this->_options_aliases, 'allows_null' => $this->_allowNull, 'allows_multiple' => $this->_multiple, 'ajax' => $this->_ajax]);
     }
 
     protected function _postSetAttributes()
@@ -31,6 +32,21 @@ class SelectFromArray extends Field
         if ($this->getAttribute('default') !== null && $this->getAttribute('allows_null')) {
             $this->setAttribute('allows_null', false);
         }
+        if ($this->getAttribute('ajax', false)) {
+            $results = [];
+            foreach ($this->getOptions() as $k => $v) {
+                $results[] = ['id' => $k, 'text' => $v];
+            }
+            $data = ['results' => $results, 'pagination' => ['more' => false]];
+            $hash = hash('sha256', print_r($data, true));
+            $name = $hash . '.json';
+            $dir = \Cubist\Util\Files\Files::mkdir(public_path('select2'));
+            $file = $dir . '/' . $name;
+            if (!file_exists($file)) {
+                file_put_contents($file, json_encode($data));
+            }
+            $this->setAttribute('ajax', $name);
+        }
     }
 
 //    public function getDatabaseLength()
index e79254a1aed0f842c7a738cfa601af74e11def11..2f3eaa78d429315817ac1598027f0a8c3d24f097 100644 (file)
             return false;
         }
     }
+
+    $ajax=isset($field['ajax']) && $field['ajax'];
 @endphp
 
 <!-- select2 from array -->
 @include('crud::fields.inc.wrapper_start')
 <label>{!! $field['label'] !!}</label>
 @if (isset($field['allows_multiple']) && $field['allows_multiple']==true)
-    <input type="hidden" name="{{ $field['name'] }}" value="" />
+    <input type="hidden" name="{{ $field['name'] }}" value=""/>
 @endif
 <select
     name="{{ $field['name'] }}@if (isset($field['allows_multiple']) && $field['allows_multiple']==true)[]@endif"
     style="width: 100%"
     @include('crud::fields.inc.attributes', ['default_class' =>  'form-control select2_from_array'])
     @if (isset($field['allows_multiple']) && $field['allows_multiple']==true)multiple @endif
+    @if(isset($field['ajax']) && $field['ajax']) data-ajax="{{$field['ajax']}}" @endif
 >
-
     @if (isset($field['allows_null']) && $field['allows_null']==true)
         @if($is_null)
             <option value="" selected>-</option>
             @endphp
             @if($selected)
                 <option value="{{ $key }}" selected>{{ $value }}</option>
-            @else
-                <option value="{{ $key }}">{{ $value }}</option>
+            @else if(!$ajax)
+            <option value="{{ $key }}">{{ $value }}</option>
             @endif
         @endforeach
     @endif
+
 </select>
 
 {{-- HINT --}}
                         if ($(obj).closest('.item.sample').length > 0) {
                             return;
                         }
+                        var options = {
+                            theme: "bootstrap"
+                        };
+                        if ($(this).is(['data-ajax'])) {
+                            options.ajax = {url: $(this).data('ajax'), dataType: 'json'};
+                        }
 
                         if (!$(obj).hasClass("select2-hidden-accessible")) {
-                            $(obj).select2({
-                                theme: "bootstrap"
-                            });
+                            $(obj).select2(options);
                         }
                     });
                 }
diff --git a/src/routes/cubist/backpack/select2.php b/src/routes/cubist/backpack/select2.php
new file mode 100644 (file)
index 0000000..3452b6b
--- /dev/null
@@ -0,0 +1,8 @@
+<?php
+
+Route::group([
+    //'prefix' => config('backpack.base.route_prefix', 'admin'),
+    'middleware' => ['web', config('backpack.base.middleware_key', 'admin')],
+], function () { // custom admin routes
+    Route::any('storage/{hash?}', '\Cubist\Backpack\Http\Controllers\CubistSelect2Controller@search')->where(['hash' => '.*']);
+});