/**
* @var string
*/
- protected $variant = 'default';
+ protected $variant = 'unset';
/**
* @return array
<?php
-
namespace Cubist\Backpack\app\Magic\Fields;
class SiteVariantMultiple extends SiteVariant
{
public function __construct($attributes)
{
- $this->_multiple = true;
+ $attributes['allows_multiple'] = true;
+ $attributes['cast'] = 'array';
parent::__construct($attributes);
}
}
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
*/
protected $_locale = '';
+ /**
+ * @var string
+ */
+ protected $_variant = 'default';
+
/**
* @var CubistMagicPageModel
*/
{
$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);
$child = new PageItem();
$child->setEntity($data);
$child->setLocale($this->getLocale());
+ $child->setVariant($this->getVariant());
if ($data->getUsedTemplate() instanceof Navigation) {
$child->setBreadcrumbs(false);
}
$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
*/
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;
/**
* @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;
});
/**
* @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 = [];
$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;
}
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()
$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])) {
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);
}
}
return self::$_mainDomains[$locale];
}
+
+ public static function isLocaleEnabled($locale)
+ {
+ $data = self::getLocaleData($locale);
+ if (null === $data) {
+ return false;
+ }
+ return $data->enabled;
+ }
}
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
{
$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();
}
{
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.
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);
return $this->getResponse();
}
- $locales = Locale::getLocales();
- $variant = $this->_getVariantByDomain($request, $locales);
+ $variant = $this->_getVariantByDomain($request, Locale::getLocales());
App::setVariant($variant);
return $this->getResponse();
$map[trim($domain['domain'])] = $locale->variant;
}
}
+
+
return $map[$httpdomain] ?? $default;
}