From: Vincent Vanwaelscappel Date: Tue, 19 Nov 2019 16:43:28 +0000 (+0100) Subject: wip #3200 @4 X-Git-Url: http://git.cubedesigners.com/?a=commitdiff_plain;h=8225f96083278be29daa595d54dd9c93e636f876;p=cubist_cms-back.git wip #3200 @4 --- diff --git a/src/app/Magic/Fields/Field.php b/src/app/Magic/Fields/Field.php index e4133f8..b05ccd9 100644 --- a/src/app/Magic/Fields/Field.php +++ b/src/app/Magic/Fields/Field.php @@ -27,6 +27,7 @@ class Field implements \ArrayAccess protected $_cast = false; protected $_translatable = false; + protected $_migrateTranslatable = false; /** * @var CubistMagicAbstractModel @@ -96,7 +97,8 @@ class Field implements \ArrayAccess public function getDefaultAttributes() { return ['type' => $this->_adminType, 'view_namespace' => $this->_viewNamespace, 'column' => false, 'form' => 'both', 'rules' => '', - 'fillable' => true, 'guarded' => false, 'hidden' => false, 'translatable' => $this->_translatable, + 'fillable' => true, 'guarded' => false, 'hidden' => false, + 'translatable' => $this->_translatable, 'migrateTranslatable' => $this->_migrateTranslatable, 'column_type' => $this->_columnType, 'default' => '', 'cast' => $this->_cast, 'column_view_namespace' => $this->_columnViewNamespace, 'searchLogic' => $this->_searchLogic, 'allow_null' => true, 'fake' => false, 'store_in' => 'extras', 'attributes' => []]; @@ -207,6 +209,14 @@ class Field implements \ArrayAccess } } + /** + * @return bool + */ + public function isMigrateTranslation() + { + return $this->getAttribute('translatable', false) && $this->getAttribute('migrateTranslatable', false); + } + /** * @param mixed $value * @return mixed diff --git a/src/app/Magic/Models/CubistMagicTranslatableModel.php b/src/app/Magic/Models/CubistMagicTranslatableModel.php index 74a2c7e..b11eb00 100644 --- a/src/app/Magic/Models/CubistMagicTranslatableModel.php +++ b/src/app/Magic/Models/CubistMagicTranslatableModel.php @@ -6,6 +6,7 @@ namespace Cubist\Backpack\app\Magic\Models; use Backpack\CRUD\ModelTraits\SpatieTranslatable\HasTranslations; use Backpack\CRUD\ModelTraits\SpatieTranslatable\Sluggable; use Backpack\CRUD\ModelTraits\SpatieTranslatable\SluggableScopeHelpers; +use Cubist\Util\Json; class CubistMagicTranslatableModel extends CubistMagicAbstractModel { @@ -80,9 +81,7 @@ class CubistMagicTranslatableModel extends CubistMagicAbstractModel { $i = 0; foreach (static::all() as $item) { - //echo 'instance ' . $item->id . ' ' . implode(', ', $item->translatable) . "\n"; $item->copyTranslations($from, $to, $overwrite); - $i++; } echo 'copy translation of ' . $i . ' instances of ' . get_class($this) . ' from ' . $from . ' to ' . $to . "\n"; @@ -106,6 +105,32 @@ class CubistMagicTranslatableModel extends CubistMagicAbstractModel $this->save(); } + public function getAttribute($key) + { + $f = $this->getField($key); + $value = parent::getAttribute($key); + if (!$value && null !== $f && $f->isMigrateTranslation()) { + $value = $this->_migrateTranslation($key); + } + + if (null === $f) { + return $value; + } + return $f->filterValue($value); + } + + protected function _migrateTranslation($key) + { + $v = Json::decodeRecursive($this->attributes[$key], Json::TYPE_ARRAY); + $value = []; + foreach (Locale::getLocalesCodes() as $locale) { + $value[$locale] = $v; + } + $this->setAttribute($key, $value); + $this->save(); + return $value[$this->getLocale()]; + } + public function update(array $attributes = [], array $options = []) { return $this->updateTranslations($this->_prepareData($attributes), $options); diff --git a/src/app/Magic/Models/Locale.php b/src/app/Magic/Models/Locale.php index febc19f..d2da0eb 100644 --- a/src/app/Magic/Models/Locale.php +++ b/src/app/Magic/Models/Locale.php @@ -8,6 +8,8 @@ class Locale extends CubistMagicAbstractModel { protected $table = 'cubist_locales'; + protected static $_locales = null; + protected $_options = ['name' => 'locale', 'singular' => 'langue', 'plural' => 'langues']; @@ -47,8 +49,53 @@ class Locale extends CubistMagicAbstractModel ]); } + /** + * @return array + */ + public static function getLocalesData() + { + if (self::$_locales === null) { + $class = Locale::getLocaleClass(); + + $localeEntities = $class::orderBy('default', 'DESC')->get(); + $defaultLocale = null; + $locales = []; + foreach ($localeEntities as $locale) { + $locales[$locale->locale] = $locale; + if ($locale->default) { + $defaultLocale = $locale->locale; + } + } + + self::$_locales = ['locales' => $locales, 'default' => $defaultLocale]; + } + return self::$_locales; + } + + /** + * @return Locale[] + */ + public static function getLocales() + { + return self::getLocalesData()['locales']; + } + + public static function getLocalesCodes() + { + return array_keys(self::getLocales()); + } + + /** + * @return string + */ + public static function getDefaultLocale() + { + return self::getLocalesData()['default']; + } + public static function getLocaleClass() { + $class = self::class; $config = config('cubist.locale_model', 'Cubist\Backpack\app\Magic\Models\Locale'); if (class_exists($config)) { diff --git a/src/app/Middleware/LocaleSelector.php b/src/app/Middleware/LocaleSelector.php index e3c29f2..ca09813 100644 --- a/src/app/Middleware/LocaleSelector.php +++ b/src/app/Middleware/LocaleSelector.php @@ -14,16 +14,8 @@ class LocaleSelector extends CubistMiddleware { parent::handle($request, $next); - $class = Locale::getLocaleClass(); - - $localeEntities = $class::orderBy('default', 'DESC')->get(); - $locales = []; - foreach ($localeEntities as $locale) { - $locales[$locale->locale] = $locale; - if ($locale->default) { - $defaultLocale = $locale->locale; - } - } + $locales = Locale::getLocales(); + $defaultLocale = Locale::getDefaultLocale(); $selectedLocale = $this->_getLocaleByDomain($request, $locales); // If the locale is not found, we redirect to the default