]> _ Git - cubist_cms-back.git/commitdiff
wip #3753 @1
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Wed, 16 Dec 2020 10:28:07 +0000 (11:28 +0100)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Wed, 16 Dec 2020 10:28:07 +0000 (11:28 +0100)
src/app/CubistCrudPanel.php
src/app/Magic/Fields/Field.php
src/app/Magic/Fields/Model.php
src/app/Magic/Models/CubistMagicAbstractModel.php
src/app/Magic/Models/CubistMagicAuthenticatable.php

index be6e03d318a2ae2cae3a493f73004f9eeda52e35..d5acaf6ddf46f6a5a6c5eaeb19eced101cf9f771 100644 (file)
@@ -95,6 +95,11 @@ class CubistCrudPanel extends CrudPanel
         return true;
     }
 
+    public function isEmbeded()
+    {
+        return request()->get('embeded', '0') != '0';
+    }
+
     public function hasAccessOrFail($operation)
     {
         if (!$this->hasAccess($operation)) {
@@ -143,4 +148,9 @@ class CubistCrudPanel extends CrudPanel
 
         return $res;
     }
+
+    public function modifyDefaultValue($name, $value)
+    {
+        $this->modifyField($name, ['default' => $value, 'value' => $value]);
+    }
 }
index 55db62a33d7486f8b11d0e72cb82dffecccc534d..65ed88b41fc9f14686346b99d235b0a1329ea3ad 100644 (file)
@@ -53,6 +53,10 @@ class Field implements \ArrayAccess
 
     protected $_databaseAttributes = [];
 
+
+    protected static $_cachedCan = [];
+    protected static $_cachedCanUser = null;
+
     /**
      * @param $attributes
      * @return Field
@@ -297,7 +301,7 @@ class Field implements \ArrayAccess
     public function setPermissions()
     {
         if (null !== $this->getAttribute('can', null)) {
-            if (!can($this->getAttribute('can'))) {
+            if (!self::can($this->getAttribute('can'))) {
                 $this->setAttribute('preview', false);
                 $this->setAttribute('filter', false);
                 $this->setAttribute('column', false);
@@ -308,7 +312,7 @@ class Field implements \ArrayAccess
         }
 
         if (null !== $this->getAttribute('can_write', null)) {
-            if (!can($this->getAttribute('can_write'))) {
+            if (!self::can($this->getAttribute('can_write'))) {
                 $this->setAttribute('type', 'column_value');
                 $this->setAttribute('view_namespace', CubistBackpackServiceProvider::NAMESPACE . '::fields');
             }
@@ -342,4 +346,16 @@ class Field implements \ArrayAccess
         return $this->_isRelationship;
     }
 
+    public static function can($permission)
+    {
+        $uid = backpack_user() === null ? null : backpack_user()->id;
+        if ($uid !== self::$_cachedCanUser) {
+            self::$_cachedCanUser = $uid;
+            self::$_cachedCan = [];
+        }
+        if (!isset(self::$_cachedCan[$permission])) {
+            self::$_cachedCan[$permission] = can($permission);
+        }
+        return self::$_cachedCan[$permission];
+    }
 }
index 02a4fc8e87807c07714227484e90cc2c2d280422..8c207ebfca996e1e91f388eeca1011f66a0497eb 100644 (file)
@@ -10,35 +10,60 @@ class Model extends Field
 
     public function getDefaultAttributes()
     {
-        return array_merge(parent::getDefaultAttributes(), ['attribute' => 'name', 'column_attribute' => null, 'allows_null' => false, 'allows_multiple' => $this->_multiple]);
+        return array_merge(parent::getDefaultAttributes(), ['attribute' => 'name', 'column_attribute' => null, 'allows_null' => false, 'allows_multiple' => $this->_multiple, 'optionsmodel_global_scopes' => true]);
     }
 
-    protected function _getOptions()
+    public function _getOptions()
     {
         return $this->pluck($this->getAttribute('attribute'));
     }
 
     public function pluck($attr)
     {
-        $bui = '';
-        if (backpack_user() !== null) {
-            $bui = backpack_user()->id;
-        }
+        $bui = backpack_user() === null ? '' : backpack_user()->id;
 
         $modelClass = $this->getAttribute('optionsmodel');
+        $globalCacheKey = '_getOption_Model_' . $modelClass . '-' . $attr;
         $cacheKey = '_getOption_Model_' . $modelClass . '-' . $attr . '-' . $bui;
+        $cacheKeysKey = '_getOption_Model_' . $modelClass . '-Keys-' . $bui;
         if (isset(static::$_options[$cacheKey])) {
             return static::$_options[$cacheKey];
         }
-        start_measure($cacheKey, 'Get options for model ' . $modelClass . ' / ' . $attr);
         $tag = 'model_' . $modelClass;
-        static::$_options[$cacheKey] = cache()->tags([$tag])->remember($cacheKey, 86400, function () use ($modelClass, $attr) {
-            /** @var \Illuminate\Database\Eloquent\Model $inst */
-            $inst = new $modelClass();
-            $options = $modelClass::all();
-            return $options->pluck($attr, $inst->getKeyName())->toArray();
+
+        if (!isset(static::$_options[$globalCacheKey])) {
+            start_measure($globalCacheKey, 'Get options for model ' . $modelClass . ' / ' . $attr);
+            static::$_options[$globalCacheKey] = cache()->tags([$tag])->remember($globalCacheKey, 86400, function () use ($modelClass, $attr) {
+                set_time_limit(0);
+                /** @var \Illuminate\Database\Eloquent\Model $inst */
+                $inst = new $modelClass();
+                return $modelClass::withoutGlobalScopes()->get()->pluck($attr, $inst->getKeyName())->toArray();
+            });
+            stop_measure($globalCacheKey);
+        }
+
+        $globalScopes = $this->getAttribute('optionsmodel_global_scopes', true);
+        if (!$globalScopes) {
+            static::$_options[$cacheKey] = static::$_options[$globalCacheKey];
+            return static::$_options[$cacheKey];
+        }
+
+        if (!isset(static::$_options[$cacheKeysKey])) {
+            static::$_options[$cacheKeysKey] = cache()->tags([$tag])->remember($cacheKeysKey, 86400, function () use ($modelClass) {
+                set_time_limit(0);
+                return $modelClass::all()->modelKeys();
+            });
+        }
+
+        static::$_options[$cacheKey] = cache()->tags([$tag])->remember($cacheKey, 86400, function () use ($globalCacheKey, $cacheKeysKey) {
+            $res = [];
+            foreach (static::$_options[$cacheKeysKey] as $k) {
+                if (isset(static::$_options[$globalCacheKey][$k])) {
+                    $res[$k] = static::$_options[$globalCacheKey][$k];
+                }
+            }
+            return $res;
         });
-        stop_measure($cacheKey);
 
         return static::$_options[$cacheKey];
     }
index b0a9847bbe039bf2b08d810fb044befd8f64d1c4..be8310ba09010e52816dd712f19eab7cd0142631 100644 (file)
@@ -813,4 +813,17 @@ class CubistMagicAbstractModel extends Model implements HasMedia
 
     }
 
+    public function preCache()
+    {
+        foreach ($this->getFields() as $field) {
+            if ($field instanceof \Cubist\Backpack\Magic\Fields\Model) {
+                /** @var $field \Cubist\Backpack\Magic\Fields\Model */
+                $field->_getOptions();
+            }
+            $field->getColumnData();
+            $field->getFilterValues();
+            $field->getFilterOptions();
+        }
+    }
+
 }
index a4ce6fb42deb6f599c44a8b55d433c10d1843807..ce3ab24fdab5db9ae0fbb6a1577cf3471d8b731d 100644 (file)
@@ -56,7 +56,8 @@ class CubistMagicAuthenticatable extends CubistMagicAbstractModel
         $this->addField(['name' => 'enabled',
             'label' => 'Enabled',
             'type' => 'checkbox',
-            'tab' => 'Login']);
+            'tab' => 'Login',
+            'default' => true]);
 
         $this->addField(['name' => 'remember_token',
             'type' => 'Text',
@@ -67,28 +68,28 @@ class CubistMagicAuthenticatable extends CubistMagicAbstractModel
             'label' => 'E-mail',
             'type' => 'Email',
             'column' => true,
-            'database_unique'=>true,
+            'database_unique' => true,
             'tab' => 'Login']);
 
         $this->addField(['name' => 'password',
             'label' => 'Password',
             'type' => 'Password',
             'tab' => 'Login',
-           'can'=>'users:admin'
+            'can' => 'users:admin'
         ]);
 
         $this->addField(['name' => 'api_token',
             'label' => 'API Token',
             'type' => 'APIToken',
             'tab' => 'Login',
-            'can'=>'users:admin'
+            'can' => 'users:admin'
         ]);
 
         $this->addField(['name' => 'rolesandperms',
             'label' => '',
             'type' => 'RolesPermissions',
             'tab' => 'Login',
-            'can'=>'users:admin'
+            'can' => 'users:admin'
         ]);
     }
 }