]> _ Git - cubist_cms-back.git/commitdiff
wip #3198 @3
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Mon, 18 Nov 2019 17:23:48 +0000 (18:23 +0100)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Mon, 18 Nov 2019 17:23:48 +0000 (18:23 +0100)
src/app/Magic/Menu/Item.php
src/app/Magic/Menu/Menu.php
src/app/Magic/Menu/PageItem.php
src/app/Magic/Models/Translate.php

index e14f8f43f637d34e5319b4edeb92308617c72721..aaa8e29d8aeed84b4582f91460bc4962ed418c48 100644 (file)
@@ -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
      */
index 74299da1da2cb692d3fceb3e74f0cc54e20ef2a0..dce07f720dc827f82657d3c3ff6e81d17272b4cd 100644 (file)
@@ -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;
index 778c33f1a1cfeb2f2cd3a0897342051484e9db14..49a04483379aeb4bc9bfc5bb58d159b6f9ae0e09 100644 (file)
@@ -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 '#';
index 361bab90a7ab7be59c38aa47f4bf78f94d7637e3..579fddb369d5432388708e2776371f6f6a734820 100644 (file)
@@ -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);