--- /dev/null
+<?php
+
+namespace Cubist\Backpack\Console\Commands;
+
+use Cubist\Backpack\Magic\Models\CubistMagicAbstractModel;
+
+class PrecacheCommand extends CubistMagicCommand
+{
+ /**
+ * The name and signature of the console command.
+ *
+ * @var string
+ */
+ protected $signature = 'cubist:magic:precache';
+
+ public function handle()
+ {
+ $this->_handleMagicFolder([$this, '_precache']);
+ }
+
+ /**
+ * @param $model CubistMagicAbstractModel
+ */
+ public function _precache($model)
+ {
+ $model->preCache(true);
+ }
+}
use Cubist\Backpack\Console\Commands\InstallCommand;
use Cubist\Backpack\Console\Commands\LocaleCopy;
use Cubist\Backpack\Console\Commands\LocaleSlugReset;
+use Cubist\Backpack\Console\Commands\PrecacheCommand;
use Cubist\Backpack\Console\Commands\RefreshComposedAttributesCommand;
use Cubist\Backpack\Console\Commands\SearchIndexCommand;
use Cubist\Backpack\Console\Commands\UpdateCommand;
RefreshComposedAttributesCommand::class,
SearchIndexCommand::class,
LocaleCopy::class,
- LocaleSlugReset::class
+ LocaleSlugReset::class,
+ PrecacheCommand::class
]);
}
}
$clonedEntry->name .= ' (copy)';
$clonedEntry->save();
$newid = $clonedEntry->id;
- $url = Route::current()->uri . '?' . request()->getQueryString();
- $url = str_replace('clone-edit', 'edit', $url);
- $url = str_replace('{id}', $newid, $url);
-
if (isset($_GET['ajax'])) {
+ /** @var CubistMagicAbstractModel $entry */
$entry = call_user_func([$_GET['entry'], 'find'], $_GET['id']);
$field = $entry->getField($_GET['name']);
- if ($field instanceof Model) {
- $options = $field->_getOptions();
- } else if ($field instanceof SelectFromArray) {
- $options = $field->getOptions();
- } else {
- $options = [];
- }
- SelectFromArray::hashOptions($options, $_GET['ajax']);
+ $modelAttribute=$field->getAttribute('attribute');
+ $v=$clonedEntry->getAttributeValue($modelAttribute);
+ $d=[$newid => $v];
+ SelectFromArray::prependHashOptions($d, $_GET['ajax']);
}
+ $url = Route::current()->uri . '?' . request()->getQueryString();
+ $url = str_replace('clone-edit', 'edit', $url);
+ $url = str_replace('{id}', $newid, $url);
return response()->redirectTo($url);
}
}
return $this->___options;
}
- public function pluck($attr)
+ public function preCache($force = false)
+ {
+ $this->pluck($this->getAttribute('attribute'), true, $force);
+ }
+
+ public function pluck($attr, $onlyGlobal = false, $force = false)
{
$bui = backpack_user() === null ? '' : backpack_user()->id;
$modelOrderByWay = $this->getAttribute('optionsmodel_orderby_way', 'asc');
$baseCacheKey = '_getOption_Model_' . $modelClass . '-|' . $modelScope;
$globalCacheKey = $baseCacheKey . '|-' . $attr;
- $cacheKey = $globalCacheKey . '|-' . $bui;
- $cacheKeysKey = $baseCacheKey . '/KEYS/' . '|-' . $bui . '|-' . '!' . $modelOrderBy . '!' . $modelOrderByWay . '!!';
- if (isset(static::$_options[$cacheKey])) {
- return static::$_options[$cacheKey];
- }
$tag = 'model_' . $modelClass;
-
- if (!isset(static::$_options[$globalCacheKey])) {
+ if ($force || !isset(static::$_options[$globalCacheKey])) {
+ if ($force) {
+ cache()->forget($globalCacheKey);
+ }
start_measure($globalCacheKey, 'Get options for model ' . $modelClass . ' / ' . $attr);
- static::$_options[$globalCacheKey] = cache()->tags([$tag])->remember($globalCacheKey, 86400, function () use ($modelClass, $attr, $modelScope, $modelOrderBy, $modelOrderByWay) {
+ static::$_options[$globalCacheKey] = cache()->tags([$tag])->remember($globalCacheKey, 86400, function () use ($modelClass, $attr, $modelScope) {
set_time_limit(0);
/** @var \Illuminate\Database\Eloquent\Model $inst */
$inst = new $modelClass();
if ($modelScope) {
$q = $q->$modelScope();
}
- if ($modelOrderBy) {
- $q->orderBy($modelOrderBy, $modelOrderByWay);
- }
return $q->where('created_ok', 1)->get()->pluck($attr, $inst->getKeyName())->toArray();
});
stop_measure($globalCacheKey);
}
+ if ($onlyGlobal) {
+ return;
+ }
+
+ $cacheKey = $globalCacheKey . '|-' . $bui;
+ $cacheKeysKey = $baseCacheKey . '/KEYS/|-' . $bui . '|-!' . $modelOrderBy . '!' . $modelOrderByWay . '!!';
+
+ if (isset(static::$_options[$cacheKey])) {
+ return static::$_options[$cacheKey];
+ }
+
$globalScopes = $this->getAttribute('optionsmodel_global_scopes', true);
if (!$globalScopes) {
static::$_options[$cacheKey] = static::$_options[$globalCacheKey];
return static::$_options[$cacheKey];
}
- if (!isset(static::$_options[$cacheKeysKey])) {
+ if ($force || !isset(static::$_options[$cacheKeysKey])) {
+ if ($force ) {
+ cache()->forget($cacheKeysKey);
+ }
static::$_options[$cacheKeysKey] = cache()->tags([$tag])->remember($cacheKeysKey, 86400, function () use ($modelClass, $modelOrderBy, $modelOrderByWay) {
set_time_limit(0);
/** @var Builder $q */
});
}
+ if ($force) {
+ cache()->forget($cacheKey);
+ }
static::$_options[$cacheKey] = cache()->tags([$tag])->remember($cacheKey, 86400, function () use ($globalCacheKey, $cacheKeysKey) {
$res = [];
foreach (static::$_options[$cacheKeysKey] as $k) {
return $hash;
}
-// public function getDatabaseLength()
-// {
-// $keys = array_keys($this->getAttribute('options'));
-// $max = 0;
-// foreach ($keys as $key) {
-// $max = max($max, mb_strlen($key));
-// }
-// return round($max * 1.5);
-// }
-
+ public static function prependHashOptions($options, $hash)
+ {
+ $data = [];
+ foreach ($options as $k => $v) {
+ $data[$k] = ['t' => $v, 'n' => self::normalize($v)];
+ }
+ $data = $data + Cache::get('select2_' . $hash);
+ Cache::put('select2_' . $hash, $data, 6000);
+ }
public function getOptions()
{
return $this->editActions;
}
- public function preCache()
+ public function preCache($force = false)
{
foreach ($this->getFields() as $field) {
if ($field instanceof \Cubist\Backpack\Magic\Fields\Model) {
/** @var $field \Cubist\Backpack\Magic\Fields\Model */
- $field->_getOptions();
+ $field->preCache($force);
}
$field->getColumnData();
$field->getFilterValues();