From: Vincent Vanwaelscappel Date: Mon, 18 Nov 2019 17:23:48 +0000 (+0100) Subject: wip #3198 @3 X-Git-Url: http://git.cubedesigners.com/?a=commitdiff_plain;h=53ec21eee077d62bf55ff2fda08262d67bcef6dd;p=cubist_cms-back.git wip #3198 @3 --- diff --git a/src/app/Magic/Menu/Item.php b/src/app/Magic/Menu/Item.php index e14f8f4..aaa8e29 100644 --- a/src/app/Magic/Menu/Item.php +++ b/src/app/Magic/Menu/Item.php @@ -5,6 +5,7 @@ namespace Cubist\Backpack\app\Magic\Menu; use Cubist\Backpack\app\Magic\Models\CMSPage; use Cubist\Backpack\app\Template\Navigation; +use Illuminate\Support\Facades\App; use Illuminate\Support\Str; class Item @@ -89,6 +90,11 @@ class Item */ protected $_urlAliases = []; + /** + * @var string + */ + protected $_locale = ''; + /** * @param string $id @@ -120,7 +126,6 @@ class Item if (!$item->status) { continue; } - $this->addChildFromData($item, $data); } } @@ -131,6 +136,7 @@ class Item public function addChildFromData($data, $all) { $child = new PageItem(); + $child->setLocale($this->getLocale()); if ($data->getUsedTemplate() instanceof Navigation) { $child->setBreadcrumbs(false); } @@ -557,6 +563,25 @@ class Item return $this->_robots; } + /** + * @return string + */ + public function getLocale(): string + { + if ($this->_locale === '') { + return App::getLocale(); + } + return $this->_locale; + } + + /** + * @param string $locale + */ + public function setLocale(string $locale): void + { + $this->_locale = $locale; + } + /** * @return bool */ diff --git a/src/app/Magic/Menu/Menu.php b/src/app/Magic/Menu/Menu.php index 74299da..dce07f7 100644 --- a/src/app/Magic/Menu/Menu.php +++ b/src/app/Magic/Menu/Menu.php @@ -11,9 +11,9 @@ use Illuminate\Support\Facades\Cache; class Menu extends BaseMenu { /** - * @var Item + * @var Item[} */ - protected static $_nav = null; + protected static $_nav = []; protected const _STANDARD_PREFIX = 'cubist'; public const CACHE_TAG = 'cubist_nav'; @@ -26,13 +26,17 @@ class Menu extends BaseMenu $this->_registeredMenuMakers[$name] = $callback; } - public function get($key, $name = self::_STANDARD_PREFIX) + public function get($key, $name = self::_STANDARD_PREFIX, $locale = null) { + if (null === $locale) { + $locale = App::getLocale(); + } if ($name) { $key = $name . '_' . $key; } + $key = $locale . '_' . $key; if (!$this->exists($key)) { - $this->makeAllMenus(); + $this->makeAllMenus(App::getLocale()); } return parent::get($key); } @@ -40,18 +44,22 @@ class Menu extends BaseMenu /** * @return Item */ - public static function getNavigation() + public static function getNavigation($locale = null) { - if (self::$_nav === null) { + if (null === $locale) { + $locale = App::getLocale(); + } + if (!isset(self::$_nav[$locale])) { \Barryvdh\Debugbar\Facade::startMeasure('nav', 'Init Navigation object'); - self::$_nav = Cache::tags(self::CACHE_TAG)->remember('navigation_' . App::getLocale(), 43200, function () { + self::$_nav[$locale] = Cache::tags(self::CACHE_TAG)->remember('navigation_' . $locale, 43200, function () use ($locale) { $nav = new Item(); + $nav->setLocale($locale); $nav->initFromDatabase(); return $nav; }); \Barryvdh\Debugbar\Facade::stopMeasure('nav'); } - return self::$_nav; + return self::$_nav[$locale]; } @@ -81,11 +89,11 @@ class Menu extends BaseMenu } $href = $page->getHref(); - if ($href == '#' || $href == 'home') { + if ($href === '#' || $href === 'home') { continue; } - $url = action("PageController@catchall", ['page' => $href]); + $url = action('PageController@catchall', ['page' => $href]); if (isset($res[$url])) { continue; } @@ -96,7 +104,7 @@ class Menu extends BaseMenu public static function internalToHref($url) { - if (stristr($url, 'internal:')) { + if (stripos($url, 'internal:') !== false) { $e = explode(':', $url, 2); $url = $e[1]; } @@ -114,32 +122,32 @@ class Menu extends BaseMenu return '#internalnotfound'; } - public function makeAllMenus() + public function makeAllMenus($locale) { - $nav = self::getNavigation(); + $nav = self::getNavigation($locale); foreach ($nav->getChildren() as $main) { - $this->makeStandardMenus($main); + $this->makeStandardMenus($main, $locale); } // Also make a menu for the breadcrumbs - this one is simpler and doesn't have the submenu headings - $this->make(self::_STANDARD_PREFIX . '_breadcrumbs', function ($menu) use ($nav) { + $this->make($locale . '_' . self::_STANDARD_PREFIX . '_breadcrumbs', function ($menu) use ($nav) { // Start with home link $menu = $menu->add(__('Accueil'), ''); $this->_addToBreadcrumbs($nav, $menu); }); } - public function makeStandardMenus($item) + public function makeStandardMenus($item, $locale) { - $this->make(self::_STANDARD_PREFIX . '_' . $item->getName(), function ($menu) use ($item) { + $this->make($locale . '_' . self::_STANDARD_PREFIX . '_' . $item->getName(), function ($menu) use ($item) { $this->makeStandardMenu($menu, $item); }); - $this->make('mobile_' . $item->getName(), function ($menu) use ($item) { + $this->make($locale . '_' . 'mobile_' . $item->getName(), function ($menu) use ($item) { $this->makeMobileMenu($menu, $item); }); foreach ($this->_registeredMenuMakers as $name => $registeredMenuMaker) { - $this->make($name . '_' . $item->getName(), function ($menu) use ($item, $registeredMenuMaker) { - call_user_func($registeredMenuMaker, $menu, $item); + $this->make($locale . '_' . $name . '_' . $item->getName(), function ($menu) use ($item, $registeredMenuMaker) { + $registeredMenuMaker($menu, $item); }); } } @@ -188,7 +196,7 @@ class Menu extends BaseMenu protected function _addToBreadcrumbs($nav, $menu) { if ($nav->getTitle() && $nav->isBreadcrumbs()) { - if (substr($nav->getBreadcrumbHref(), 0, 1) == '#') { + if (strpos($nav->getBreadcrumbHref(), '#') === 0) { $parent = $menu->raw($nav->getTitle()); } else { $parent = $menu->add($nav->getTitle(), $nav->getBreadcrumbHref()); @@ -206,8 +214,8 @@ class Menu extends BaseMenu public function getSearchBreadcrumbs() { $all = $this->get('breadcrumbs')->crumbMenu()->all(); - $lastIndex = count($all )-1; - $search_breadcrumbs=[]; + $lastIndex = count($all) - 1; + $search_breadcrumbs = []; foreach ($all as $i => $item) { if ($i === 0 || $i === $lastIndex) { continue; diff --git a/src/app/Magic/Menu/PageItem.php b/src/app/Magic/Menu/PageItem.php index 778c33f..49a0448 100644 --- a/src/app/Magic/Menu/PageItem.php +++ b/src/app/Magic/Menu/PageItem.php @@ -40,25 +40,28 @@ class PageItem extends Item */ public function initFromEntity($entity) { - $data=$entity->getPageData(); + if (method_exists($entity, 'setLocale')) { + $entity->setLocale($this->getLocale()); + } + $data = $entity->getPageData(); - $this->setId($entity->id); - $this->setName($entity->name ?? get_class($entity) . '_' . $entity->id); - $this->setSlug($entity->slug); - $this->setTitle($entity->title); - $this->setRobots($data->get('robots',true)); + $this->setId($data->get('id')); + $this->setName($data->get('name', get_class($entity) . '_' . $data->get('id'))); + $this->setSlug($data->get('slug')); + $this->setTitle($data->get('title')); + $this->setRobots($data->get('robots', true)); $desktop = $entity->menu_desktop == '' ? 'children' : $entity->menu_desktop; - $this->setMenuDesktop($entity->menu_desktop != 'no'); - $this->setMenuDesktopChildren($entity->menu_desktop == 'children'); + $this->setMenuDesktop($entity->menu_desktop !== 'no'); + $this->setMenuDesktopChildren($entity->menu_desktop === 'children'); - $mobile = $entity->menu_mobile == 'desktop' || $entity->menu_mobile == '' ? $desktop : $entity->menu_mobile; + $mobile = $entity->menu_mobile === 'desktop' || $entity->menu_mobile == '' ? $desktop : $entity->menu_mobile; - $this->setMenuMobile($mobile != 'no'); - $this->setMenuMobileChildren($mobile == 'children'); + $this->setMenuMobile($mobile !== 'no'); + $this->setMenuMobileChildren($mobile === 'children'); - $dbaliases = $entity->getPageData()->url_alias; + $dbaliases = $data->get('url_alias'); if (is_array($dbaliases)) { $aliases = []; foreach ($dbaliases as $dbalias) { @@ -96,7 +99,7 @@ class PageItem extends Item if (!$template) { return true; } - if ($template == 'first_redirection' || $template == 'internal_redirection' || $template == 'redirection' || $this->isVirtual()) { + if ($template === 'first_redirection' || $template === 'internal_redirection' || $template === 'redirection' || $this->isVirtual()) { return false; } return parent::isNavigable(); @@ -109,11 +112,11 @@ class PageItem extends Item return parent::getHref(); } - if ($template == 'first_redirection' && $this->hasChildren()) { + if ($template === 'first_redirection' && $this->hasChildren()) { return $this->getChildren()[0]->getHref(); - } else if ($template == 'internal_redirection') { + } else if ($template === 'internal_redirection') { - } else if ($template == 'redirection') { + } else if ($template === 'redirection') { return $this->navigation; } @@ -138,11 +141,11 @@ class PageItem extends Item public function getBreadcrumbHref() { - if ($this->getTemplate() == 'first_redirection' && $this->hasChildren()) { + if ($this->getTemplate() === 'first_redirection' && $this->hasChildren()) { return '#'; - } else if ($this->getTemplate() == 'internal_redirection') { + } else if ($this->getTemplate() === 'internal_redirection') { return '#'; - } else if ($this->getTemplate() == 'redirection') { + } else if ($this->getTemplate() === 'redirection') { return '#'; } else if ($this->isVirtual()) { return '#'; diff --git a/src/app/Magic/Models/Translate.php b/src/app/Magic/Models/Translate.php index 361bab9..579fddb 100644 --- a/src/app/Magic/Models/Translate.php +++ b/src/app/Magic/Models/Translate.php @@ -15,7 +15,7 @@ class Translate extends CubistMagicTranslatableModel public function setFields() { parent::setFields(); - $paths = ['resources/views']; + $paths = ['resources/views', 'vendor/cubist/cms-back/src/app/Magic/Menu']; $translate = new \Cubist\Locale\Translate(); foreach ($paths as $path) { $translate->addPath(base_path() . '/' . $path);