]> _ Git - cubist_cms-back.git/commitdiff
wip #2878 @1
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Tue, 9 Jul 2019 16:56:23 +0000 (18:56 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Tue, 9 Jul 2019 16:56:23 +0000 (18:56 +0200)
src/app/Magic/Menu/Item.php
src/app/Magic/Menu/Menu.php
src/app/Magic/Menu/PageItem.php
src/app/Template/InternalRedirection.php

index f5e1a92347505a006093de62b576e61bc56c3435..fd4eb99b1f0d135c4a9e0b25a70486f121e96058 100644 (file)
@@ -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());
+                }
+            }
+        }
+    }
 }
index 1f27b96e61af45b2ad13c528004bfe1a9162791f..7f5fe70d21102e6bd46ce9b3720fc004f1786bee 100644 (file)
@@ -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());
                         }
                     }
                 }
index fc8725a778da04267c58810501590ff3f971eac8..4e40de92f7ed8e57b36e34aa92d361fb0cf2daa8 100644 (file)
@@ -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();
     }
 
index 5d13ea847b3129b0ff457c3ea18b66cc9f212aad..fb97004e77be3828c26d4f95f64e74b20eb0daca 100644 (file)
@@ -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',