]> _ Git - cubist_cms-back.git/commitdiff
wip #4210 @1.5
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Thu, 16 Jun 2022 07:19:37 +0000 (09:19 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Thu, 16 Jun 2022 07:19:37 +0000 (09:19 +0200)
src/app/Console/Commands/PrecacheCommand.php [new file with mode: 0644]
src/app/CubistBackpackServiceProvider.php
src/app/Http/Controllers/Operations/CloneEditOperation.php
src/app/Magic/Fields/Model.php
src/app/Magic/Fields/SelectFromArray.php
src/app/Magic/Models/CubistMagicAbstractModel.php

diff --git a/src/app/Console/Commands/PrecacheCommand.php b/src/app/Console/Commands/PrecacheCommand.php
new file mode 100644 (file)
index 0000000..0a3e5a0
--- /dev/null
@@ -0,0 +1,28 @@
+<?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);
+    }
+}
index 6be34f8fb831d09d91f8f81f894ceecc02268ea0..8a81bd4bcbaa4f8c9f359db34a592144e5a33af8 100644 (file)
@@ -7,6 +7,7 @@ use Cubist\Backpack\Console\Commands\GenerateCommand;
 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;
@@ -80,7 +81,8 @@ class CubistBackpackServiceProvider extends ServiceProvider
             RefreshComposedAttributesCommand::class,
             SearchIndexCommand::class,
             LocaleCopy::class,
-            LocaleSlugReset::class
+            LocaleSlugReset::class,
+            PrecacheCommand::class
         ]);
     }
 }
index 5df97fdb9a1901047a2a8337817e9947c6618341..c55cb64e206046339a272c12afe25df38a5dee50 100644 (file)
@@ -27,24 +27,20 @@ trait CloneEditOperation
         $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);
     }
 }
index ae29387ad456c114060c89ee7e1623ada37e71aa..ad147d9d04459bb44ec27e18420127e0892719ef 100644 (file)
@@ -24,7 +24,12 @@ class Model extends Field
         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;
 
@@ -34,17 +39,14 @@ class Model extends Field
         $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();
@@ -53,21 +55,32 @@ class Model extends Field
                 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 */
@@ -79,6 +92,9 @@ class Model extends Field
             });
         }
 
+        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) {
index ac7ac274ed97ee21e87880d8ede462598529d4f8..5c67a2611ed78289dd69ff8596f8cdc0c91fbc83 100644 (file)
@@ -51,16 +51,15 @@ class SelectFromArray extends Field
         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()
     {
index 262a070d3c6845f0a725bb4a921bd1aff0c9a106..4c5e195b446694e4b952c73fd97bd69d05b5bd4f 100644 (file)
@@ -1053,12 +1053,12 @@ class CubistMagicAbstractModel extends Model implements HasMedia
         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();