From: Vincent Vanwaelscappel Date: Tue, 9 Jul 2019 16:56:23 +0000 (+0200) Subject: wip #2878 @1 X-Git-Url: http://git.cubedesigners.com/?a=commitdiff_plain;h=de1e5603a8628290f2170af1c9981a73a1cf1146;p=cubist_cms-back.git wip #2878 @1 --- diff --git a/src/app/Magic/Menu/Item.php b/src/app/Magic/Menu/Item.php index f5e1a92..fd4eb99 100644 --- a/src/app/Magic/Menu/Item.php +++ b/src/app/Magic/Menu/Item.php @@ -13,22 +13,22 @@ class Item /** * @var string */ - protected $_id; + protected $_id = ''; /** * @var string */ - protected $_name; + protected $_name = ''; /** * @var string */ - protected $_slug; + protected $_slug = ''; /** * @var string */ - protected $_title; + protected $_title = ''; /** * @param string $id @@ -37,11 +37,8 @@ class Item public function initFromDatabase($id = '#root') { /** @var $all CMSPage[] */ - try { - $all = CMSPage::orderBy('lft')->get(); - } catch (\Exception $e) { - $all = []; - } + $all = CMSPage::orderBy('lft')->get(); + $this->setId('#root'); $this->setName($id); $this->setChildrenFromData($all, null); @@ -55,7 +52,7 @@ class Item public function setChildrenFromData($data, $filter = null) { foreach ($data as $item) { - if ($item->id != $filter) { + if ($item->parent_id != $filter) { continue; } $this->addChildFromData($item, $data); @@ -152,6 +149,11 @@ class Item return $this->_children; } + public function hasChildren() + { + return count($this->getChildren()) > 0; + } + /** * @param $item Item */ @@ -159,4 +161,28 @@ class Item { $this->_children[] = $item; } + + /** + * @param $menu Menu + */ + public function makeMenu($menu) + { + foreach ($this->getChildren() as $child) { + $parent = $menu->add($child->getTitle(), $child->getHref()); + // Handle items with submenus + if ($child->hasChildren()) { + // Create an empty sub-element that will serve as a wrapper for the submenu(s) + $wrapper = $parent->raw('')->attr(['class' => 'nav-submenu-wrapper']); + +// // Some submenus have a title element +// if (isset($submenu_data['title'])) { +// $wrapper->raw($submenu_data['title'])->attr(['class' => 'nav-submenu-title']); +// } + + foreach ($child->getChildren() as $subitem) { + $wrapper->add($subitem->getTitle(), $subitem->getHref()); + } + } + } + } } diff --git a/src/app/Magic/Menu/Menu.php b/src/app/Magic/Menu/Menu.php index 1f27b96..7f5fe70 100644 --- a/src/app/Magic/Menu/Menu.php +++ b/src/app/Magic/Menu/Menu.php @@ -2,7 +2,6 @@ namespace Cubist\Backpack\app\Magic\Menu; -use Cubist\Backpack\app\Magic\Models\CMSPage; use Lavary\Menu\Menu as BaseMenu; class Menu extends BaseMenu @@ -28,72 +27,24 @@ class Menu extends BaseMenu { $nav = $this->getNavigation(); foreach ($nav->getChildren() as $main) { - $nav_items = []; - foreach ($main->getChildren() as $item) { - $submenus = null; - - $links = []; - - foreach ($item->getChildren() as $child) { - $links[$child->getTitle()] = $child->getHref(); - } - - if (count($links) > 0) { - $submenus = [['links' => $links]]; - } - - $s = ['url' => $item->getHref()]; - if (null !== $submenus) { - $s['submenus'] = $submenus; - } - - $nav_items[$item->getTitle()] = $s; - } - $all_nav_items[] = $nav_items; - - $this->make($main->getName(), function ($menu) use ($nav_items) { - foreach ($nav_items as $nav_label => $nav_item) { - $parent = $menu->add($nav_label, $nav_item['url']); - - // Handle items with submenus - if (isset($nav_item['submenus'])) { - - foreach ($nav_item['submenus'] as $submenu_data) { - - // Create an empty sub-element that will serve as a wrapper for the submenu(s) - $wrapper = $parent->raw('')->attr(['class' => 'nav-submenu-wrapper']); - - // Some submenus have a title element - if (isset($submenu_data['title'])) { - $wrapper->raw($submenu_data['title'])->attr(['class' => 'nav-submenu-title']); - } - - foreach ($submenu_data['links'] as $label => $url) { - $wrapper->add($label, $url); - } - } - } - } + $this->make($main->getName(), function ($menu) use ($main) { + $main->makeMenu($menu); }); } - // Also make a menu for the breadcrumbs - this one is simpler and doesn't have the submenu headings - $this->make('breadcrumbs', function ($menu) use ($all_nav_items) { + $this->make('breadcrumbs', function ($menu) use ($nav) { // Start with home link $menu = $menu->add('Home', ''); - foreach ($all_nav_items as $nav_items) { - foreach ($nav_items as $nav_label => $nav_item) { - $parent = $menu->add($nav_label, $nav_item['url']); + foreach ($nav->getChildren() as $main) { + foreach ($main->getChildren() as $child) { + $parent = $menu->add($child->getTitle(), $child->getHref()); // Handle items with submenus - if (isset($nav_item['submenus'])) { - - foreach ($nav_item['submenus'] as $submenu_data) { - foreach ($submenu_data['links'] as $label => $url) { - $parent->add($label, $url); - } + if ($child->hasChildren()) { + foreach ($child->getChildren() as $subitem) { + $parent->add($subitem->getTitle(), $subitem->getHref()); } } } diff --git a/src/app/Magic/Menu/PageItem.php b/src/app/Magic/Menu/PageItem.php index fc8725a..4e40de9 100644 --- a/src/app/Magic/Menu/PageItem.php +++ b/src/app/Magic/Menu/PageItem.php @@ -23,11 +23,23 @@ class PageItem extends Item $this->setId($page->id); $this->setName($page->name); $this->setSlug($page->slug); + $this->setTitle($page->title); $this->setChildrenFromData($all, $this->getId()); } public function getHref() { + if ($this->getPage()->isRedirection()) { + if ($this->getPage()->template == 'FirstRedirection' && $this->hasChildren()) { + return $this->getChildren()[0]->getHref(); + } else if ($this->getPage()->template == 'InternalRedirection') { + + } else if ($this->getPage()->template == 'Redirection') { + return $this->navigation; + } + + } + return $this->getSlug(); } diff --git a/src/app/Template/InternalRedirection.php b/src/app/Template/InternalRedirection.php index 5d13ea8..fb97004 100644 --- a/src/app/Template/InternalRedirection.php +++ b/src/app/Template/InternalRedirection.php @@ -14,8 +14,8 @@ class InternalRedirection extends Redirection protected function _redirection() { $this->addField(['name' => 'navigation', - 'type' => 'URL', - 'label' => 'Redirection vers un site externe', + 'type' => 'PageInternal', + 'label' => 'Redirection vers une page du site', 'store_in' => 'nav', 'translatable' => true, 'tab' => 'Informations principales',