]> _ Git - cubist_cms-back.git/commitdiff
wip #3274 @1
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Wed, 8 Jan 2020 15:52:53 +0000 (16:52 +0100)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Wed, 8 Jan 2020 15:52:53 +0000 (16:52 +0100)
src/app/Application.php
src/app/Magic/Fields/SiteVariantMultiple.php
src/app/Magic/Menu/Item.php
src/app/Magic/Menu/Menu.php
src/app/Magic/Models/Locale.php
src/app/Magic/Models/Settings.php
src/app/Middleware/LocaleSelector.php
src/app/Middleware/VariantSelector.php

index 324396d387fc0fe9063136d20c878d3097b3db5f..3b5f7d5849af09597564c117d7d8b24e20bfbb4c 100644 (file)
@@ -7,7 +7,7 @@ class Application extends \Illuminate\Foundation\Application
     /**
      * @var string
      */
-    protected $variant = 'default';
+    protected $variant = 'unset';
 
     /**
      * @return array
index 58a60ca1bfd35248ab1b21c4d9c1d017da9ee847..8a0a18668af0bb836cc90eb5671caa1cf17376b0 100644 (file)
@@ -1,13 +1,13 @@
 <?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);
     }
 }
index 0b0f502297b7e35d3b4c8442f751ce260fca9193..44caab324713d30f98b349416b4973ac97e0a78c 100644 (file)
@@ -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
      */
index c2a1b800dc59448f64d32a9f6f630d3025f92e07..9ef7045da1ad350811ff3b678b7f799508660f0a 100644 (file)
@@ -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 = [];
 
index 86761ef005c3e3465b93eab1770d0eb4714d342f..cecc1f5f2e0c7129263e4cd879a0046c735952b7 100644 (file)
@@ -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;
+    }
 }
index 5ccd7b4db1b4adcbc82969eadc5b20c8c12ee191..77d36836fc9d1751c19c854e9d51d177435703ac 100644 (file)
@@ -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();
         }
index ca0981382940f0c465dd4b6cc77cdd57c43f7030..f5af9cc4746fa36bacb07375b7bc85ba02e64f24 100644 (file)
@@ -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);
index b63038ffc6944e08551de8e3bed66c5369119384..1bd216c032ac8b0997ddf4eaf03fc65c5adc6921 100644 (file)
@@ -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;
     }