/**
* @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
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);
public function setChildrenFromData($data, $filter = null)
{
foreach ($data as $item) {
- if ($item->id != $filter) {
+ if ($item->parent_id != $filter) {
continue;
}
$this->addChildFromData($item, $data);
return $this->_children;
}
+ public function hasChildren()
+ {
+ return count($this->getChildren()) > 0;
+ }
+
/**
* @param $item 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());
+ }
+ }
+ }
+ }
}
namespace Cubist\Backpack\app\Magic\Menu;
-use Cubist\Backpack\app\Magic\Models\CMSPage;
use Lavary\Menu\Menu as BaseMenu;
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());
}
}
}
$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();
}