]> _ Git - cubist_cms-back.git/commitdiff
wip #3262
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Fri, 13 Dec 2019 16:09:28 +0000 (17:09 +0100)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Fri, 13 Dec 2019 16:09:28 +0000 (17:09 +0100)
src/app/Console/Commands/LocaleSlugReset.php
src/app/Magic/Models/CubistMagicAbstractModel.php

index 1259c858be2b6af8f795ddb3d1af7f1d02275766..8cf3c1478ca155cf92517560b534798672a11430 100644 (file)
@@ -23,7 +23,7 @@ class LocaleSlugReset extends CubistCommand
 
     public function handle()
     {
-        $this->call('backup:run');
+        //$this->call('backup:run');
         $this->_handleMagicFolder([$this, '_resetSlug']);
         $this->call('cache:clear');
     }
@@ -38,9 +38,39 @@ class LocaleSlugReset extends CubistCommand
         }
         $class = get_class($model);
         $all = $class::all();
+        if ($class !== 'App\Models\Page') {
+            return;
+        }
+        $locale = $this->argument('locale');
         foreach ($all as $instance) {
-            /** @var CubistMagicAbstractModel $instance */
-            echo $instance->getAttribute('slug');
+            $instance->setLocale($locale);
+            /** @var CubistMagicTranslatableModel $instance */
+            $translations = $instance->getTranslations('slug');
+            $reset = !isset($translations[$locale]) || !$translations[$locale] || $instance->getAttribute('name') === 'home';
+            if (!$reset) {
+                foreach ($translations as $loc => $translation) {
+                    if ($loc === $locale) {
+                        continue;
+                    }
+                    if ($translation === $translations[$locale]) {
+                        $reset = true;
+                        break;
+                    }
+                }
+            }
+
+            if (!$reset) {
+                echo 'skip ' . $translations[$locale] . "\n";
+                continue;
+            }
+
+            $new = $instance->getSlugOrTitleAttribute(true);
+            if (!$new) {
+                continue;
+            }
+            echo 'reset "' . $translations[$locale] . '" -> ' . $new . "\n";
+            $instance->setTranslation('slug', $locale, $new);
+            $instance->save();
         }
     }
 }
index d59e88ae658c20ae6d7d820841b877cb364abb17..4f1bc9e1dc5a8012c3b111030234c9be95d55937 100644 (file)
@@ -154,24 +154,33 @@ class CubistMagicAbstractModel extends Model implements HasMedia
     }
 
     // The slug is created automatically from the "title" field if no slug exists.
-    public function getSlugOrTitleAttribute()
+    public function getSlugOrTitleAttribute($reset = false)
     {
+        if ($this->getAttribute('name') === 'home') {
+            return 'home';
+        }
         foreach ($this->_slugFields as $item) {
             $components = explode('+', $item);
 
             $slug = [];
             foreach ($components as $component) {
-                $v = $this->getAttribute($component);
+                if ($reset && $component === 'slug') {
+                    continue;
+                }
+                $v = $this->getAttributeValue($component);
 
                 if ($v) {
-                    $slug[] = $v;
+                    $slug[$component] = $v;
                 }
             }
             if (count($slug) > 0) {
                 return Str::slug(implode('-', $slug));
             }
         }
-        return Str::slug($this->getAttribute('id'));
+        if ($reset) {
+            return false;
+        }
+        return Str::slug($this->getAttributeValue('id'));
     }
 
     public function getOption($key, $default = null)