]> _ Git - cubist_cms-back.git/commitdiff
wip #3262 @3
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Thu, 12 Dec 2019 15:42:22 +0000 (16:42 +0100)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Thu, 12 Dec 2019 15:42:22 +0000 (16:42 +0100)
src/app/Http/Controllers/CubistPageController.php
src/app/Magic/Menu/Item.php
src/app/Magic/Menu/Menu.php
src/app/Magic/Models/Locale.php

index a1110cbe69baf4088c86dbcabd5e872da16ca82f..ba1999fd2d1100bfbd80ac11c980cf93531d613e 100644 (file)
@@ -2,6 +2,7 @@
 
 namespace Cubist\Backpack\app\Http\Controllers;
 
+use Cubist\Backpack\app\Magic\Menu\Facade as CubistMenu;
 use Cubist\Backpack\app\Magic\Menu\Menu;
 use Cubist\Backpack\app\Magic\Models\CMSPage;
 use Illuminate\Http\Request;
@@ -57,6 +58,7 @@ class CubistPageController extends CubistFrontController
                 $this->_404();
             }
             $c = $item->getController();
+            CubistMenu::setCurrentPage($item);
 
             // Make Request object available as first parameter
             $c['params'] = ['request' => $request] + $c['params'];
index aaa8e29d8aeed84b4582f91460bc4962ed418c48..2fce0a806d6966db113ce954808ed18c65a62bc2 100644 (file)
@@ -4,6 +4,7 @@
 namespace Cubist\Backpack\app\Magic\Menu;
 
 use Cubist\Backpack\app\Magic\Models\CMSPage;
+use Cubist\Backpack\app\Magic\Models\Locale;
 use Cubist\Backpack\app\Template\Navigation;
 use Illuminate\Support\Facades\App;
 use Illuminate\Support\Str;
@@ -395,6 +396,19 @@ class Item
         $this->_href = $href;
     }
 
+    public function getURL()
+    {
+        $domain = Locale::getMainDomain($this->getLocale());
+        $href = $this->getHref();
+        if ($href === 'home') {
+            $href = '';
+        }
+        if (!$domain) {
+            return $href;
+        }
+        return 'https://' . $domain . $href;
+    }
+
     public function isNavigable()
     {
         return $this->_href != '#';
@@ -563,6 +577,15 @@ class Item
         return $this->_robots;
     }
 
+    public function getTranslatedPage($locale)
+    {
+        if ($this->getLocale() === $locale) {
+            return $this;
+        }
+        $res = Menu::getNavigation($locale)->findOneById($this->getId());
+        return $res;
+    }
+
     /**
      * @return string
      */
index dce07f720dc827f82657d3c3ff6e81d17272b4cd..fa85585838d2648c2a9b3a18c1a804a4975f05e5 100644 (file)
@@ -20,12 +20,18 @@ class Menu extends BaseMenu
 
     protected $_registeredMenuMakers = [];
 
+    /**
+     * @var Item
+     */
+    protected $_currentPage;
+
 
     public function registerMenuMaker($name, $callback)
     {
         $this->_registeredMenuMakers[$name] = $callback;
     }
 
+
     public function get($key, $name = self::_STANDARD_PREFIX, $locale = null)
     {
         if (null === $locale) {
@@ -263,4 +269,20 @@ class Menu extends BaseMenu
         $res .= '</div>';
         return $res;
     }
+
+    /**
+     * @param Item $currentPage
+     */
+    public function setCurrentPage(Item $currentPage): void
+    {
+        $this->_currentPage = $currentPage;
+    }
+
+    /**
+     * @return Item
+     */
+    public function getCurrentPage(): Item
+    {
+        return $this->_currentPage;
+    }
 }
index 4669e78dea98c01084c9af6758b4086b2ec4e94b..cadab1f5aec911e0ccb7c1ad79a67f18d8bc766d 100644 (file)
@@ -11,6 +11,7 @@ class Locale extends CubistMagicAbstractModel
     protected $table = 'cubist_locales';
 
     protected static $_locales = null;
+    protected static $_mainDomains = [];
 
     protected $_options = ['name' => 'locale',
         'singular' => 'langue',
@@ -108,19 +109,43 @@ class Locale extends CubistMagicAbstractModel
 
     public static function getLocalesDataForFront()
     {
+        $current = App::getLocale();
+
         $data = self::getLocalesData();
         $res = [];
-        $active = $data['locales'][$data['default']];
-        $res['active_code'] = $data['default'];
+        $active = $data['locales'][$current];
+        $res['active_code'] = $current;
         $res['active_flag'] = $active->flag;
-        $res['active_name'] = \Cubist\Locale\Locale::translate($data['default'], $data['default']);
+        $res['active_name'] = \Cubist\Locale\Locale::translate($current, $current);
         $res['others'] = [];
         foreach ($data['locales'] as $code => $locale) {
-            if (!$locale->enabled || $code == $data['default']) {
+            if (!$locale->enabled || $code === $current) {
                 continue;
             }
             $res['others'][$code] = ['code' => $code, 'flag' => $locale->flag, 'name' => \Cubist\Locale\Locale::translate($code, $code)];
         }
         return $res;
     }
+
+    public static function getMainDomain($locale)
+    {
+        if (!isset(self::$_mainDomains[$locale])) {
+            $all = self::getLocalesData();
+            if (!isset($all[$locale])) {
+                return false;
+            }
+            $domains = $all[$locale]->domains;
+            if (is_string($domains)) {
+                $domains = json_decode($domains, true);
+            }
+            foreach ($domains as $domain) {
+                if ($domain['env'] === App::environment()) {
+                    $res = $domain['domain'];
+                    break;
+                }
+            }
+            self::$_mainDomains[$locale] = $res ?? false;
+        }
+        return self::$_mainDomains[$locale];
+    }
 }