From c34c6cd81337b972c4456656cfcbe75f7ab8ee7e Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Fri, 25 Feb 2022 19:22:34 +0100 Subject: [PATCH] wip #5127 @1 --- .../Controllers/CubistSelect2Controller.php | 22 +++++++++++++++++++ src/app/Magic/Fields/SelectFromArray.php | 18 ++++++++++++++- .../views/fields/select2_from_array.blade.php | 21 ++++++++++++------ src/routes/cubist/backpack/select2.php | 8 +++++++ 4 files changed, 61 insertions(+), 8 deletions(-) create mode 100644 src/app/Http/Controllers/CubistSelect2Controller.php create mode 100644 src/routes/cubist/backpack/select2.php diff --git a/src/app/Http/Controllers/CubistSelect2Controller.php b/src/app/Http/Controllers/CubistSelect2Controller.php new file mode 100644 index 0000000..958eb20 --- /dev/null +++ b/src/app/Http/Controllers/CubistSelect2Controller.php @@ -0,0 +1,22 @@ +setStatusCode('401'); + } + if (!can('edition')) { + return response(null)->setStatusCode('403'); + } + $data = json_decode(storage_path('select2/' . $hash . '.json')); + $res = []; + return response()->json($res); + } +} diff --git a/src/app/Magic/Fields/SelectFromArray.php b/src/app/Magic/Fields/SelectFromArray.php index a7e4dc3..9161da8 100644 --- a/src/app/Magic/Fields/SelectFromArray.php +++ b/src/app/Magic/Fields/SelectFromArray.php @@ -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() diff --git a/src/resources/views/fields/select2_from_array.blade.php b/src/resources/views/fields/select2_from_array.blade.php index e79254a..2f3eaa7 100644 --- a/src/resources/views/fields/select2_from_array.blade.php +++ b/src/resources/views/fields/select2_from_array.blade.php @@ -16,21 +16,23 @@ return false; } } + + $ajax=isset($field['ajax']) && $field['ajax']; @endphp @include('crud::fields.inc.wrapper_start') @if (isset($field['allows_multiple']) && $field['allows_multiple']==true) - + @endif {{-- HINT --}} @@ -90,11 +93,15 @@ 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 index 0000000..3452b6b --- /dev/null +++ b/src/routes/cubist/backpack/select2.php @@ -0,0 +1,8 @@ + 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' => '.*']); +}); -- 2.39.5