]> _ Git - cubist_cms-back.git/commitdiff
wip #5127 @0.5
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Mon, 28 Feb 2022 09:39:28 +0000 (10:39 +0100)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Mon, 28 Feb 2022 09:39:28 +0000 (10:39 +0100)
src/app/Magic/Fields/Model.php
src/app/Magic/Fields/SelectFromArray.php
src/app/Magic/Fields/SelectFromModel.php

index df3bb410753bb10baa6ec345c5d6a2fdfdb18fce..a9f1677f662279e67f04f2f9f7c7891dc5631f5f 100644 (file)
@@ -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)
index 1a1055cf274588390c7840c233560489f073ffee..7b7deccbb7a6c0838918a3f76306f0297015b959 100644 (file)
@@ -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()
index 2d44b72514d645b25b1a65a724f507de3872bbe5..4eb054392b35aacb13ab7778cfc6c74df655ce46 100644 (file)
@@ -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(), [