From 8e23279435890ef778966c91b74d16e4df639ed4 Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Tue, 9 Jul 2019 18:04:01 +0200 Subject: [PATCH] fix #2878 @4 --- src/app/Magic/Menu/Item.php | 162 ++++++++++++++++++ src/app/Magic/Menu/Menu.php | 26 ++- src/app/Magic/Menu/PageItem.php | 49 ++++++ src/app/Magic/Models/CMSPage.php | 11 ++ .../Magic/Models/CubistMagicNestedModel.php | 7 +- 5 files changed, 245 insertions(+), 10 deletions(-) create mode 100644 src/app/Magic/Menu/Item.php create mode 100644 src/app/Magic/Menu/PageItem.php diff --git a/src/app/Magic/Menu/Item.php b/src/app/Magic/Menu/Item.php new file mode 100644 index 0000000..f5e1a92 --- /dev/null +++ b/src/app/Magic/Menu/Item.php @@ -0,0 +1,162 @@ +get(); + } catch (\Exception $e) { + $all = []; + } + $this->setId('#root'); + $this->setName($id); + $this->setChildrenFromData($all, null); + } + + + /** + * @param $data CMSPage[] + * @param $filter null|string + */ + public function setChildrenFromData($data, $filter = null) + { + foreach ($data as $item) { + if ($item->id != $filter) { + continue; + } + $this->addChildFromData($item, $data); + } + } + + /** + * @param $data CMSPage + */ + public function addChildFromData($data, $all) + { + $child = new PageItem(); + $child->initFromPage($data, $all); + $this->addChild($child); + } + + /** + * @return mixed + */ + public function getSlug() + { + return $this->_slug; + } + + /** + * @param mixed $slug + */ + public function setSlug($slug): void + { + $this->_slug = $slug; + } + + /** + * @param string $name + */ + public function setName(string $name): void + { + $this->_name = $name; + } + + /** + * @return string + */ + public function getName(): string + { + return $this->_name; + } + + /** + * @param string $id + */ + public function setId($id): void + { + $this->_id = $id; + } + + /** + * @return string + */ + public function getId() + { + return $this->_id; + } + + /** + * @return string + */ + public function getTitle(): string + { + return $this->_title; + } + + /** + * @param string $title + */ + public function setTitle(string $title): void + { + $this->_title = $title; + } + + /** + * @return string + */ + public function getHref() + { + + } + + /** + * @return Item[] + */ + public function getChildren() + { + return $this->_children; + } + + /** + * @param $item Item + */ + public function addChild($item) + { + $this->_children[] = $item; + } +} diff --git a/src/app/Magic/Menu/Menu.php b/src/app/Magic/Menu/Menu.php index a2e9913..1f27b96 100644 --- a/src/app/Magic/Menu/Menu.php +++ b/src/app/Magic/Menu/Menu.php @@ -7,6 +7,8 @@ use Lavary\Menu\Menu as BaseMenu; class Menu extends BaseMenu { + protected $_nav = null; + public function get($key) { if (!$this->exists($key)) { @@ -15,35 +17,41 @@ class Menu extends BaseMenu return parent::get($key); } + public function getNavigation() + { + $nav = new Item(); + $nav->initFromDatabase(); + return $nav; + } + public function makeAllMenus() { - $tree = CMSPage::getTree(); - $all_nav_items = []; - foreach ($tree as $mainKey => $main) { + $nav = $this->getNavigation(); + foreach ($nav->getChildren() as $main) { $nav_items = []; - foreach ($main['children'] as $name => $item) { + foreach ($main->getChildren() as $item) { $submenus = null; $links = []; - foreach ($item['children'] as $key => $child) { - $links[$child['element']->title] = $child['element']->slug; + foreach ($item->getChildren() as $child) { + $links[$child->getTitle()] = $child->getHref(); } if (count($links) > 0) { $submenus = [['links' => $links]]; } - $s = ['url' => $item['element']->slug]; + $s = ['url' => $item->getHref()]; if (null !== $submenus) { $s['submenus'] = $submenus; } - $nav_items[$item['element']->title] = $s; + $nav_items[$item->getTitle()] = $s; } $all_nav_items[] = $nav_items; - $this->make($mainKey, function ($menu) use ($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']); diff --git a/src/app/Magic/Menu/PageItem.php b/src/app/Magic/Menu/PageItem.php new file mode 100644 index 0000000..fc8725a --- /dev/null +++ b/src/app/Magic/Menu/PageItem.php @@ -0,0 +1,49 @@ +setPage($page); + $this->setId($page->id); + $this->setName($page->name); + $this->setSlug($page->slug); + $this->setChildrenFromData($all, $this->getId()); + } + + public function getHref() + { + return $this->getSlug(); + } + + /** + * @param CMSPage $page + */ + public function setPage(CMSPage $page): void + { + $this->_page = $page; + } + + /** + * @return CMSPage + */ + public function getPage(): CMSPage + { + return $this->_page; + } +} diff --git a/src/app/Magic/Models/CMSPage.php b/src/app/Magic/Models/CMSPage.php index 6c10f6e..42b0f44 100644 --- a/src/app/Magic/Models/CMSPage.php +++ b/src/app/Magic/Models/CMSPage.php @@ -4,6 +4,7 @@ namespace Cubist\Backpack\app\Magic\Models; use Cubist\Backpack\app\Magic\Controllers\CubistMagicController; +use Cubist\Backpack\app\Template\Redirection; use Cubist\Backpack\app\Template\TemplateAbstract; use Cubist\Util\Json; use Doctrine\DBAL\Schema\Schema; @@ -21,6 +22,8 @@ class CMSPage extends CubistMagicNestedModel protected static $_table = 'cubist_cms_pages'; protected $table = 'cubist_cms_pages'; + protected static $_tree = null; + protected $_options = ['name' => 'page', 'singular' => 'page', 'plural' => 'pages']; @@ -222,4 +225,12 @@ class CMSPage extends CubistMagicNestedModel { return Json::decodeRecursive(parent::_prepareData($attributes), Json::TYPE_ARRAY); } + + /** + * @return bool + */ + public function isRedirection() + { + return $this->_usedTemplate instanceof Redirection; + } } diff --git a/src/app/Magic/Models/CubistMagicNestedModel.php b/src/app/Magic/Models/CubistMagicNestedModel.php index 1110ce0..b456f53 100644 --- a/src/app/Magic/Models/CubistMagicNestedModel.php +++ b/src/app/Magic/Models/CubistMagicNestedModel.php @@ -31,7 +31,12 @@ class CubistMagicNestedModel extends CubistMagicModel public static function getTree() { - $all = self::orderBy('lft')->get(); + + try{ + $all = self::orderBy('lft')->get(); + }catch (\Exception $e){ + $all=[]; + } $res = []; self::_appendToTree($res, null, $all); -- 2.39.5