From 941937724801ae8fa86a0f9778f5d9bbba8bdb09 Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Fri, 13 Dec 2019 17:09:28 +0100 Subject: [PATCH] wip #3262 --- src/app/Console/Commands/LocaleSlugReset.php | 36 +++++++++++++++++-- .../Magic/Models/CubistMagicAbstractModel.php | 17 ++++++--- 2 files changed, 46 insertions(+), 7 deletions(-) diff --git a/src/app/Console/Commands/LocaleSlugReset.php b/src/app/Console/Commands/LocaleSlugReset.php index 1259c85..8cf3c14 100644 --- a/src/app/Console/Commands/LocaleSlugReset.php +++ b/src/app/Console/Commands/LocaleSlugReset.php @@ -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(); } } } diff --git a/src/app/Magic/Models/CubistMagicAbstractModel.php b/src/app/Magic/Models/CubistMagicAbstractModel.php index d59e88a..4f1bc9e 100644 --- a/src/app/Magic/Models/CubistMagicAbstractModel.php +++ b/src/app/Magic/Models/CubistMagicAbstractModel.php @@ -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) -- 2.39.5