--- /dev/null
+<?php
+
+
+namespace Cubist\Backpack\app\Magic\Menu;
+
+use Cubist\Backpack\app\Magic\Models\CMSPage;
+
+class Item
+{
+ /** @var Item[] */
+ protected $_children = [];
+
+ /**
+ * @var string
+ */
+ protected $_id;
+
+ /**
+ * @var string
+ */
+ protected $_name;
+
+ /**
+ * @var string
+ */
+ protected $_slug;
+
+ /**
+ * @var string
+ */
+ protected $_title;
+
+ /**
+ * @param string $id
+ */
+
+ public function initFromDatabase($id = '#root')
+ {
+ /** @var $all CMSPage[] */
+ try {
+ $all = CMSPage::orderBy('lft')->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;
+ }
+}
class Menu extends BaseMenu
{
+ protected $_nav = null;
+
public function get($key)
{
if (!$this->exists($key)) {
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']);
--- /dev/null
+<?php
+
+
+namespace Cubist\Backpack\app\Magic\Menu;
+
+
+use Cubist\Backpack\app\Magic\Models\CMSPage;
+
+class PageItem extends Item
+{
+ /**
+ * @var CMSPage
+ */
+ protected $_page;
+
+ /**
+ * @param $page CMSPage
+ * @param $all CMSPage[]
+ */
+ public function initFromPage($page, $all)
+ {
+ $this->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;
+ }
+}
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;
protected static $_table = 'cubist_cms_pages';
protected $table = 'cubist_cms_pages';
+ protected static $_tree = null;
+
protected $_options = ['name' => 'page',
'singular' => 'page',
'plural' => 'pages'];
{
return Json::decodeRecursive(parent::_prepareData($attributes), Json::TYPE_ARRAY);
}
+
+ /**
+ * @return bool
+ */
+ public function isRedirection()
+ {
+ return $this->_usedTemplate instanceof Redirection;
+ }
}
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);