From: Vincent Vanwaelscappel Date: Thu, 12 Dec 2019 15:42:22 +0000 (+0100) Subject: wip #3262 @3 X-Git-Url: http://git.cubedesigners.com/?a=commitdiff_plain;h=e14912ab0c9b408c5f620410b914ec0525f26b9e;p=cubist_cms-back.git wip #3262 @3 --- diff --git a/src/app/Http/Controllers/CubistPageController.php b/src/app/Http/Controllers/CubistPageController.php index a1110cb..ba1999f 100644 --- a/src/app/Http/Controllers/CubistPageController.php +++ b/src/app/Http/Controllers/CubistPageController.php @@ -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']; diff --git a/src/app/Magic/Menu/Item.php b/src/app/Magic/Menu/Item.php index aaa8e29..2fce0a8 100644 --- a/src/app/Magic/Menu/Item.php +++ b/src/app/Magic/Menu/Item.php @@ -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 */ diff --git a/src/app/Magic/Menu/Menu.php b/src/app/Magic/Menu/Menu.php index dce07f7..fa85585 100644 --- a/src/app/Magic/Menu/Menu.php +++ b/src/app/Magic/Menu/Menu.php @@ -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 .= ''; return $res; } + + /** + * @param Item $currentPage + */ + public function setCurrentPage(Item $currentPage): void + { + $this->_currentPage = $currentPage; + } + + /** + * @return Item + */ + public function getCurrentPage(): Item + { + return $this->_currentPage; + } } diff --git a/src/app/Magic/Models/Locale.php b/src/app/Magic/Models/Locale.php index 4669e78..cadab1f 100644 --- a/src/app/Magic/Models/Locale.php +++ b/src/app/Magic/Models/Locale.php @@ -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]; + } }