protected $_cast = false;
protected $_translatable = false;
+ protected $_migrateTranslatable = false;
/**
* @var CubistMagicAbstractModel
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' => []];
}
}
+ /**
+ * @return bool
+ */
+ public function isMigrateTranslation()
+ {
+ return $this->getAttribute('translatable', false) && $this->getAttribute('migrateTranslatable', false);
+ }
+
/**
* @param mixed $value
* @return mixed
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
{
{
$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";
$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);
{
protected $table = 'cubist_locales';
+ protected static $_locales = null;
+
protected $_options = ['name' => 'locale',
'singular' => 'langue',
'plural' => 'langues'];
]);
}
+ /**
+ * @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)) {
{
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