From: Vincent Vanwaelscappel Date: Mon, 28 Feb 2022 09:39:28 +0000 (+0100) Subject: wip #5127 @0.5 X-Git-Url: http://git.cubedesigners.com/?a=commitdiff_plain;h=4f56cd61719d87271ace4170541a476b96d7c06b;p=cubist_cms-back.git wip #5127 @0.5 --- diff --git a/src/app/Magic/Fields/Model.php b/src/app/Magic/Fields/Model.php index df3bb41..a9f1677 100644 --- a/src/app/Magic/Fields/Model.php +++ b/src/app/Magic/Fields/Model.php @@ -7,6 +7,7 @@ namespace Cubist\Backpack\Magic\Fields; class Model extends Field { protected static $_options = []; + protected $___options = null; public function getDefaultAttributes() { @@ -15,7 +16,10 @@ class Model extends Field public function _getOptions() { - return $this->pluck($this->getAttribute('attribute')); + if (null === $this->___options) { + $this->___options = $this->pluck($this->getAttribute('attribute')); + } + return $this->___options; } public function pluck($attr) diff --git a/src/app/Magic/Fields/SelectFromArray.php b/src/app/Magic/Fields/SelectFromArray.php index 1a1055c..7b7decc 100644 --- a/src/app/Magic/Fields/SelectFromArray.php +++ b/src/app/Magic/Fields/SelectFromArray.php @@ -20,7 +20,7 @@ class SelectFromArray extends Field protected $_options = []; protected $_options_aliases = []; protected $_filterType = 'dropdown'; - protected $_ajax = false; + protected $_ajax = true; public function getDefaultAttributes() { @@ -34,19 +34,23 @@ class SelectFromArray extends Field $this->setAttribute('allows_null', false); } if ($this->getAttribute('ajax', false)) { - $data = []; - foreach ($this->getOptions() as $k => $v) { - $data[$k] = ['t' => $v, 'n' => self::normalize($v)]; - } - $hash = hash('sha256', print_r($data, true)); - $name = $hash . '.json'; - $dir = \Cubist\Util\Files\Files::mkdir(storage_path('select2')); - $file = $dir . '/' . $name; - if (!file_exists($file)) { - file_put_contents($file, json_encode($data)); - } - $this->setAttribute('ajax', $hash); + $this->setAttribute('ajax', self::hashOptions($this->getOptions())); + } + } + + public static function hashOptions($options){ + $data = []; + foreach ($options as $k => $v) { + $data[$k] = ['t' => $v, 'n' => self::normalize($v)]; + } + $hash = hash('sha256', print_r($data, true)); + $name = $hash . '.json'; + $dir = \Cubist\Util\Files\Files::mkdir(storage_path('select2')); + $file = $dir . '/' . $name; + if (!file_exists($file)) { + file_put_contents($file, json_encode($data)); } + return $hash; } // public function getDatabaseLength() diff --git a/src/app/Magic/Fields/SelectFromModel.php b/src/app/Magic/Fields/SelectFromModel.php index 2d44b72..4eb0543 100644 --- a/src/app/Magic/Fields/SelectFromModel.php +++ b/src/app/Magic/Fields/SelectFromModel.php @@ -20,12 +20,15 @@ class SelectFromModel extends Model protected $_order = false; protected $_allows_null = false; + protected $_ajax = true; + public function getDefaultAttributes() { $field = $this; return array_merge(parent::getDefaultAttributes(), [ + 'ajax' => $this->_ajax, 'order' => $this->_order, 'allows_null' => $this->_allows_null, 'searchLogic' => function ($query, $column, $searchTerm) use ($field) { @@ -46,7 +49,12 @@ class SelectFromModel extends Model protected function _preGetDefinition() { parent::_preGetDefinition(); - $this->setAttribute('options', $this->_getOptions()); + $o=$this->_getOptions(); + $this->setAttribute('options', $o); + if ($this->getAttribute('ajax', false)) { + $this->setAttribute('ajax', SelectFromArray::hashOptions($o)); + } + } public function _postSetAttributes() @@ -59,9 +67,11 @@ class SelectFromModel extends Model $this->setAttribute('cast', 'array'); } + parent::_postSetAttributes(); } + protected function _getAttributesAliases() { return array_merge(parent::_getAttributesAliases(), [