From f96bd42c176f31821dc890d3dbb93016ad2ab25e Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Wed, 16 Dec 2020 11:28:07 +0100 Subject: [PATCH] wip #3753 @1 --- src/app/CubistCrudPanel.php | 10 ++++ src/app/Magic/Fields/Field.php | 20 +++++++- src/app/Magic/Fields/Model.php | 51 ++++++++++++++----- .../Magic/Models/CubistMagicAbstractModel.php | 13 +++++ .../Models/CubistMagicAuthenticatable.php | 11 ++-- 5 files changed, 85 insertions(+), 20 deletions(-) diff --git a/src/app/CubistCrudPanel.php b/src/app/CubistCrudPanel.php index be6e03d..d5acaf6 100644 --- a/src/app/CubistCrudPanel.php +++ b/src/app/CubistCrudPanel.php @@ -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]); + } } diff --git a/src/app/Magic/Fields/Field.php b/src/app/Magic/Fields/Field.php index 55db62a..65ed88b 100644 --- a/src/app/Magic/Fields/Field.php +++ b/src/app/Magic/Fields/Field.php @@ -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]; + } } diff --git a/src/app/Magic/Fields/Model.php b/src/app/Magic/Fields/Model.php index 02a4fc8..8c207eb 100644 --- a/src/app/Magic/Fields/Model.php +++ b/src/app/Magic/Fields/Model.php @@ -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]; } diff --git a/src/app/Magic/Models/CubistMagicAbstractModel.php b/src/app/Magic/Models/CubistMagicAbstractModel.php index b0a9847..be8310b 100644 --- a/src/app/Magic/Models/CubistMagicAbstractModel.php +++ b/src/app/Magic/Models/CubistMagicAbstractModel.php @@ -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(); + } + } + } diff --git a/src/app/Magic/Models/CubistMagicAuthenticatable.php b/src/app/Magic/Models/CubistMagicAuthenticatable.php index a4ce6fb..ce3ab24 100644 --- a/src/app/Magic/Models/CubistMagicAuthenticatable.php +++ b/src/app/Magic/Models/CubistMagicAuthenticatable.php @@ -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' ]); } } -- 2.39.5