]> _ Git - cubist_cms-back.git/commitdiff
wip #3200 @4
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Tue, 19 Nov 2019 16:43:28 +0000 (17:43 +0100)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Tue, 19 Nov 2019 16:43:28 +0000 (17:43 +0100)
src/app/Magic/Fields/Field.php
src/app/Magic/Models/CubistMagicTranslatableModel.php
src/app/Magic/Models/Locale.php
src/app/Middleware/LocaleSelector.php

index e4133f82823f562e93f28285a94c900a0bfab29a..b05ccd9284787be0edc89892e3675f4c52812108 100644 (file)
@@ -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
index 74a2c7ed501e163d8be792e9c890450c05e45292..b11eb004304aa7ac3ef01abe8121cabd454319ea 100644 (file)
@@ -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);
index febc19fa82d61a310cc8e0d7f3bbb10de19126a8..d2da0eb36ed5782f933d8061d78a43f5435bd8e0 100644 (file)
@@ -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)) {
index e3c29f287458e8325168598be430d6064fd3a3d1..ca0981382940f0c465dd4b6cc77cdd57c43f7030 100644 (file)
@@ -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