]> _ Git - cubist_cms-back.git/commitdiff
fix #2878 @4
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Tue, 9 Jul 2019 16:04:01 +0000 (18:04 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Tue, 9 Jul 2019 16:04:01 +0000 (18:04 +0200)
src/app/Magic/Menu/Item.php [new file with mode: 0644]
src/app/Magic/Menu/Menu.php
src/app/Magic/Menu/PageItem.php [new file with mode: 0644]
src/app/Magic/Models/CMSPage.php
src/app/Magic/Models/CubistMagicNestedModel.php

diff --git a/src/app/Magic/Menu/Item.php b/src/app/Magic/Menu/Item.php
new file mode 100644 (file)
index 0000000..f5e1a92
--- /dev/null
@@ -0,0 +1,162 @@
+<?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;
+    }
+}
index a2e99133841cce4b909db3d71280c046c0c53fda..1f27b96e61af45b2ad13c528004bfe1a9162791f 100644 (file)
@@ -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 (file)
index 0000000..fc8725a
--- /dev/null
@@ -0,0 +1,49 @@
+<?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;
+    }
+}
index 6c10f6e1d19d906304033575aa74dcc19525e3f0..42b0f446bbffc457e454486b1a46050c0ff41fd3 100644 (file)
@@ -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;
+    }
 }
index 1110ce0966db318a765439d8248cb47ff61c623e..b456f536717edb8674d3e3529c1b802cfb27191a 100644 (file)
@@ -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);