]> _ Git - cubist_cms-back.git/commitdiff
wip #3274 @1
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Wed, 8 Jan 2020 13:47:41 +0000 (14:47 +0100)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Wed, 8 Jan 2020 13:47:41 +0000 (14:47 +0100)
src/app/Application.php
src/app/Facades/App.php [new file with mode: 0644]
src/app/Magic/Models/Locale.php
src/app/Magic/Models/Settings.php
src/app/Middleware/VariantSelector.php [new file with mode: 0644]

index c083b35183045a8bf138570e52a2825226d995d7..324396d387fc0fe9063136d20c878d3097b3db5f 100644 (file)
@@ -4,6 +4,11 @@ namespace Cubist\Backpack;
 
 class Application extends \Illuminate\Foundation\Application
 {
+    /**
+     * @var string
+     */
+    protected $variant = 'default';
+
     /**
      * @return array
      */
@@ -19,4 +24,20 @@ class Application extends \Illuminate\Foundation\Application
     {
         return count($this->getVariants()) > 0;
     }
+
+    /**
+     * @param string $variant
+     */
+    public function setVariant(string $variant): void
+    {
+        $this->variant = $variant;
+    }
+
+    /**
+     * @return string
+     */
+    public function getVariant(): string
+    {
+        return $this->variant;
+    }
 }
diff --git a/src/app/Facades/App.php b/src/app/Facades/App.php
new file mode 100644 (file)
index 0000000..cfd6049
--- /dev/null
@@ -0,0 +1,18 @@
+<?php
+
+namespace Cubist\Backpack\Facades;
+
+use Illuminate\Support\Facades\App as Base;
+
+/**
+ * Class App
+ * @package Cubist\Backpack\Facades
+ * @method static array getVariants()
+ * @method static bool hasVariant()
+ * @method static void setVariant(string $variant)
+ * @method static string getVariant()
+ */
+class App extends Base
+{
+
+}
index e852dd45a98ae9ce24d09a6fda166fc5863a8426..86761ef005c3e3465b93eab1770d0eb4714d342f 100644 (file)
@@ -4,7 +4,7 @@
 namespace Cubist\Backpack\app\Magic\Models;
 
 
-use Illuminate\Support\Facades\App;
+use Cubist\Backpack\Facades\App;
 
 class Locale extends CubistMagicAbstractModel
 {
@@ -60,8 +60,9 @@ class Locale extends CubistMagicAbstractModel
     /**
      * @return array
      */
-    public static function getLocalesData()
+    public static function getLocalesData($filterVariant = true)
     {
+        $variant = App::getVariant();
         if (self::$_locales === null) {
             $class = Locale::getLocaleClass();
 
@@ -69,6 +70,9 @@ class Locale extends CubistMagicAbstractModel
             $defaultLocale = null;
             $locales = [];
             foreach ($localeEntities as $locale) {
+                if ($filterVariant && $variant !== $locale->variant) {
+                    continue;
+                }
                 $locales[$locale->locale] = $locale;
                 if ($locale->default) {
                     $defaultLocale = $locale->locale;
@@ -83,9 +87,9 @@ class Locale extends CubistMagicAbstractModel
     /**
      * @return Locale[]
      */
-    public static function getLocales()
+    public static function getLocales($filterVariant = true)
     {
-        return self::getLocalesData()['locales'];
+        return self::getLocalesData($filterVariant)['locales'];
     }
 
     public static function getLocalesCodes()
@@ -101,9 +105,9 @@ class Locale extends CubistMagicAbstractModel
     /**
      * @return string
      */
-    public static function getDefaultLocale()
+    public static function getDefaultLocale($filterVariant = true)
     {
-        return self::getLocalesData()['default'];
+        return self::getLocalesData($filterVariant)['default'];
     }
 
     public static function getLocaleClass()
index 8e2f975f163f38f696c223496c07a3775492bac0..5ccd7b4db1b4adcbc82969eadc5b20c8c12ee191 100644 (file)
@@ -18,7 +18,7 @@ class Settings extends CubistMagicTranslatableModel
     protected $_options = ['name' => 'settings',
         'singular' => 'paramètre',
         'plural' => 'paramètres',
-        'oneinstance' => true];
+        'oneinstance' => false];
 
     public function registerMediaConversions(Media $media = null)
     {
@@ -98,6 +98,13 @@ class Settings extends CubistMagicTranslatableModel
 
     protected function _seo()
     {
+        $this->addField(['name' => 'variant',
+            'label' => 'Variante du site',
+            'type' => 'SiteVariant',
+            'translatable' => false,
+            'tab' => 'Variante',
+            'column' => true]);
+
         $this->addField(['name' => 'meta_title',
             'label' => 'Titre long par défaut',
             'type' => 'Text',
@@ -155,7 +162,7 @@ class Settings extends CubistMagicTranslatableModel
     public static function getData()
     {
 
-        $locale=App::getLocale();
+        $locale = App::getLocale();
         if (!isset(self::$_data[$locale])) {
             $settings_class = self::getSettingsClass();
             $entity = $settings_class::find(1);
diff --git a/src/app/Middleware/VariantSelector.php b/src/app/Middleware/VariantSelector.php
new file mode 100644 (file)
index 0000000..b63038f
--- /dev/null
@@ -0,0 +1,53 @@
+<?php
+
+namespace Cubist\Backpack\app\Middleware;
+
+use Closure;
+use Cubist\Backpack\app\Magic\Models\Locale;
+use Illuminate\Http\Request;
+use Cubist\Backpack\Facades\App;
+
+
+class VariantSelector extends CubistMiddleware
+{
+    public function handle(Request $request, Closure $next)
+    {
+        parent::handle($request, $next);
+
+        if (!App::hasVariant()) {
+            App::setVariant('default');
+            return $this->getResponse();
+        }
+
+        $locales = Locale::getLocales();
+        $variant = $this->_getVariantByDomain($request, $locales);
+        App::setVariant($variant);
+
+        return $this->getResponse();
+    }
+
+    /**
+     * @param Request $request
+     * @param Locale[] $locales
+     * @return mixed|null
+     */
+    protected function _getVariantByDomain(Request $request, $locales)
+    {
+        $httpdomain = $request->getHttpHost();
+        $variants = App::getVariants();
+        $default = $variants[0];
+        $map = [];
+        foreach ($locales as $locale) {
+            $domains = $locale->domains;
+            if (is_string($locale->domains)) {
+                $domains = json_decode($locale->domains, true);
+            }
+            foreach ($domains as $domain) {
+                $map[trim($domain['domain'])] = $locale->variant;
+            }
+        }
+        return $map[$httpdomain] ?? $default;
+    }
+
+
+}