From 405d7ae568b56c37ae23258d77cf6d033cac8348 Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Wed, 8 Jan 2020 16:52:53 +0100 Subject: [PATCH] wip #3274 @1 --- src/app/Application.php | 2 +- src/app/Magic/Fields/SiteVariantMultiple.php | 4 +- src/app/Magic/Menu/Item.php | 30 +++++++++++- src/app/Magic/Menu/Menu.php | 15 ++++-- src/app/Magic/Models/Locale.php | 48 ++++++++++++++++---- src/app/Magic/Models/Settings.php | 9 +++- src/app/Middleware/LocaleSelector.php | 7 ++- src/app/Middleware/VariantSelector.php | 5 +- 8 files changed, 93 insertions(+), 27 deletions(-) diff --git a/src/app/Application.php b/src/app/Application.php index 324396d..3b5f7d5 100644 --- a/src/app/Application.php +++ b/src/app/Application.php @@ -7,7 +7,7 @@ class Application extends \Illuminate\Foundation\Application /** * @var string */ - protected $variant = 'default'; + protected $variant = 'unset'; /** * @return array diff --git a/src/app/Magic/Fields/SiteVariantMultiple.php b/src/app/Magic/Fields/SiteVariantMultiple.php index 58a60ca..8a0a186 100644 --- a/src/app/Magic/Fields/SiteVariantMultiple.php +++ b/src/app/Magic/Fields/SiteVariantMultiple.php @@ -1,13 +1,13 @@ _multiple = true; + $attributes['allows_multiple'] = true; + $attributes['cast'] = 'array'; parent::__construct($attributes); } } diff --git a/src/app/Magic/Menu/Item.php b/src/app/Magic/Menu/Item.php index 0b0f502..44caab3 100644 --- a/src/app/Magic/Menu/Item.php +++ b/src/app/Magic/Menu/Item.php @@ -8,7 +8,7 @@ use Cubist\Backpack\app\Magic\Models\CubistMagicPageModel; use Cubist\Backpack\app\Magic\Models\Locale; use Cubist\Backpack\app\Magic\PageData; use Cubist\Backpack\app\Template\Navigation; -use Illuminate\Support\Facades\App; +use Cubist\Backpack\Facades\App; use Illuminate\Support\Str; class Item @@ -104,6 +104,11 @@ class Item */ protected $_locale = ''; + /** + * @var string + */ + protected $_variant = 'default'; + /** * @var CubistMagicPageModel */ @@ -121,7 +126,11 @@ class Item { $class = CMSPage::getPageClass(); /** @var $all CMSPage[] */ - $all = $class::orderBy('lft')->get(); + $r = $class::orderBy('lft'); + if (App::hasVariant()) { + $r->whereRaw('variant IS NULL OR JSON_CONTAINS(variant, \'["' . App::getVariant() . '"]\')'); + } + $all = $r->get(); $this->setId('#root'); $this->setRoot($this); @@ -156,6 +165,7 @@ class Item $child = new PageItem(); $child->setEntity($data); $child->setLocale($this->getLocale()); + $child->setVariant($this->getVariant()); if ($data->getUsedTemplate() instanceof Navigation) { $child->setBreadcrumbs(false); } @@ -645,6 +655,22 @@ class Item $this->_locale = $locale; } + /** + * @return string + */ + public function getVariant(): string + { + return $this->_variant; + } + + /** + * @param string $variant + */ + public function setVariant(string $variant): void + { + $this->_variant = $variant; + } + /** * @return bool */ diff --git a/src/app/Magic/Menu/Menu.php b/src/app/Magic/Menu/Menu.php index c2a1b80..9ef7045 100644 --- a/src/app/Magic/Menu/Menu.php +++ b/src/app/Magic/Menu/Menu.php @@ -4,7 +4,7 @@ namespace Cubist\Backpack\app\Magic\Menu; use Cubist\Backpack\app\Template\Navigation; use Cubist\Backpack\app\Template\Redirection; -use Illuminate\Support\Facades\App; +use Cubist\Backpack\Facades\App; use Lavary\Menu\Menu as BaseMenu; use Illuminate\Support\Facades\Cache; @@ -50,16 +50,21 @@ class Menu extends BaseMenu /** * @return Item */ - public static function getNavigation($locale = null) + public static function getNavigation($locale = null, $variant = null) { if (null === $locale) { $locale = App::getLocale(); } + if (null === $variant) { + $variant = App::getVariant(); + } + if (!isset(self::$_nav[$locale])) { \Barryvdh\Debugbar\Facade::startMeasure('nav', 'Init Navigation object'); - self::$_nav[$locale] = Cache::tags(self::CACHE_TAG)->remember('navigation_' . $locale, 43200, function () use ($locale) { + self::$_nav[$locale] = Cache::tags(self::CACHE_TAG)->remember('navigation_' . $variant . '_' . $locale, 43200, function () use ($locale, $variant) { $nav = new Item(); $nav->setLocale($locale); + $nav->setVariant($variant); $nav->initFromDatabase(); return $nav; }); @@ -72,9 +77,9 @@ class Menu extends BaseMenu /** * @return Cubist\Backpack\app\Magic\Menu\Item[] */ - public static function getAllNavigablePages($locale = null) + public static function getAllNavigablePages($locale = null, $variant = null) { - $pages = self::getNavigation($locale)->findAll(); + $pages = self::getNavigation($locale, $variant)->findAll(); $res = []; diff --git a/src/app/Magic/Models/Locale.php b/src/app/Magic/Models/Locale.php index 86761ef..cecc1f5 100644 --- a/src/app/Magic/Models/Locale.php +++ b/src/app/Magic/Models/Locale.php @@ -70,10 +70,10 @@ class Locale extends CubistMagicAbstractModel $defaultLocale = null; $locales = []; foreach ($localeEntities as $locale) { - if ($filterVariant && $variant !== $locale->variant) { + if ($filterVariant && $variant !== 'unset' && $variant !== $locale->variant) { continue; } - $locales[$locale->locale] = $locale; + $locales[] = $locale; if ($locale->default) { $defaultLocale = $locale->locale; } @@ -94,7 +94,11 @@ class Locale extends CubistMagicAbstractModel public static function getLocalesCodes() { - return array_keys(self::getLocales()); + $res = []; + foreach (self::getLocales() as $locale) { + $res[] = $locale->locale; + } + return array_unique($res); } public static function getEnabledLocalesCodes() @@ -128,16 +132,22 @@ class Locale extends CubistMagicAbstractModel $data = self::getLocalesData(); $res = []; - $active = $data['locales'][$current]; + foreach ($data['locales'] as $locale) { + if ($current === $locale->locale) { + $active = $locale; + break; + } + } + $res['active_code'] = $current; $res['active_flag'] = $active->flag; $res['active_name'] = \Cubist\Locale\Locale::translate($current, $current); $res['enabled'] = []; - foreach ($data['locales'] as $code => $locale) { + foreach ($data['locales'] as $locale) { if (!$locale->enabled) { continue; } - $res['enabled'][$code] = ['code' => $code, 'flag' => $locale->flag, 'name' => \Cubist\Locale\Locale::translate($code, $code)]; + $res['enabled'][$locale->locale] = ['code' => $locale->locale, 'flag' => $locale->flag, 'name' => \Cubist\Locale\Locale::translate($locale->locale, $locale->locale)]; } $res['others'] = $res['enabled']; if (isset($res['others'][$current])) { @@ -146,14 +156,25 @@ class Locale extends CubistMagicAbstractModel return $res; } + public static function getLocaleData($locale) + { + $all = self::getLocalesData()['locales']; + foreach ($all as $data) { + if ($data->locale === $locale) { + return $data; + } + } + return null; + } + public static function getMainDomain($locale) { if (!isset(self::$_mainDomains[$locale])) { - $all = self::getLocalesData(); - if (!isset($all['locales'][$locale])) { + $loc = self::getLocaleData($locale); + if (null === $loc) { return false; } - $domains = $all['locales'][$locale]->domains; + $domains = $loc->domains; if (is_string($domains)) { $domains = json_decode($domains, true); } @@ -167,4 +188,13 @@ class Locale extends CubistMagicAbstractModel } return self::$_mainDomains[$locale]; } + + public static function isLocaleEnabled($locale) + { + $data = self::getLocaleData($locale); + if (null === $data) { + return false; + } + return $data->enabled; + } } diff --git a/src/app/Magic/Models/Settings.php b/src/app/Magic/Models/Settings.php index 5ccd7b4..77d3683 100644 --- a/src/app/Magic/Models/Settings.php +++ b/src/app/Magic/Models/Settings.php @@ -5,7 +5,7 @@ namespace Cubist\Backpack\app\Magic\Models; use Cubist\Backpack\app\Http\Controllers\CubistPWAController; use Cubist\Backpack\app\Magic\PageData; -use Illuminate\Support\Facades\App; +use Cubist\Backpack\Facades\App; use Spatie\MediaLibrary\Models\Media; class Settings extends CubistMagicTranslatableModel @@ -163,9 +163,14 @@ class Settings extends CubistMagicTranslatableModel { $locale = App::getLocale(); + $variant = App::getVariant(); if (!isset(self::$_data[$locale])) { $settings_class = self::getSettingsClass(); - $entity = $settings_class::find(1); + $r = $settings_class::limit(1); + if (App::hasVariant()) { + $r->where('variant', $variant); + } + $entity = $r->first(); $entity->setLocale($locale); self::$_data[$locale] = $entity->getPageData(); } diff --git a/src/app/Middleware/LocaleSelector.php b/src/app/Middleware/LocaleSelector.php index ca09813..f5af9cc 100644 --- a/src/app/Middleware/LocaleSelector.php +++ b/src/app/Middleware/LocaleSelector.php @@ -14,10 +14,9 @@ class LocaleSelector extends CubistMiddleware { parent::handle($request, $next); - $locales = Locale::getLocales(); $defaultLocale = Locale::getDefaultLocale(); - $selectedLocale = $this->_getLocaleByDomain($request, $locales); + $selectedLocale = $this->_getLocaleByDomain($request, Locale::getLocales()); // If the locale is not found, we redirect to the default // If the locale is disabled, the user is not an admin and the url // is not in the backend, we redirect too. @@ -26,11 +25,11 @@ class LocaleSelector extends CubistMiddleware null === $selectedLocale || !( $this->onAdmin() || - $locales[$selectedLocale]->enabled || + Locale::isLocaleEnabled($selectedLocale) || $this->can('backpack_translation') ) ) { - $domain = $this->_getDomainByLocale($locales[$defaultLocale]); + $domain = $this->_getDomainByLocale(Locale::getLocaleData($defaultLocale)); // redirect to default locale if (null === $domain) { abort(401); diff --git a/src/app/Middleware/VariantSelector.php b/src/app/Middleware/VariantSelector.php index b63038f..1bd216c 100644 --- a/src/app/Middleware/VariantSelector.php +++ b/src/app/Middleware/VariantSelector.php @@ -19,8 +19,7 @@ class VariantSelector extends CubistMiddleware return $this->getResponse(); } - $locales = Locale::getLocales(); - $variant = $this->_getVariantByDomain($request, $locales); + $variant = $this->_getVariantByDomain($request, Locale::getLocales()); App::setVariant($variant); return $this->getResponse(); @@ -46,6 +45,8 @@ class VariantSelector extends CubistMiddleware $map[trim($domain['domain'])] = $locale->variant; } } + + return $map[$httpdomain] ?? $default; } -- 2.39.5