]> _ Git - cubist_cms-back.git/commitdiff
#2843
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Thu, 4 Jul 2019 09:59:37 +0000 (11:59 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Thu, 4 Jul 2019 09:59:37 +0000 (11:59 +0200)
composer.json
src/app/Magic/CubistMenu.php [new file with mode: 0644]
src/app/Magic/Models/CMSPage.php

index e0b55925221b32353550f557e5e011ecbb377d70..09c2cec941fa7b986573a653dfae152eecb2438c 100644 (file)
@@ -33,7 +33,8 @@
         "venturecraft/revisionable": "^1.30",
         "gaspertrix/laravel-backpack-dropzone-field": "^1.0",
         "cviebrock/eloquent-sluggable": "^4.8",
-        "cocur/slugify": "^3.2"
+        "cocur/slugify": "^3.2",
+        "lavary/laravel-menu": "^1.7"
     },
     "require-dev": {
         "filp/whoops": "^2.3",
diff --git a/src/app/Magic/CubistMenu.php b/src/app/Magic/CubistMenu.php
new file mode 100644 (file)
index 0000000..351bde0
--- /dev/null
@@ -0,0 +1,99 @@
+<?php
+
+namespace Cubist\Backpack\app\Magic;
+
+use Cubist\Backpack\app\Magic\Models\CMSPage;
+use Lavary\Menu\Menu;
+
+class CubistMenu extends Menu
+{
+    public function get($key)
+    {
+        if (!self::exists($key)) {
+            self::makeAllMenus();
+        }
+        return parent::get($key);
+    }
+
+    public static function makeAllMenus()
+    {
+        $tree = CMSPage::getTree();
+        foreach ($tree as $mainKey => $main) {
+            $nav_items = [];
+            foreach ($main['children'] as $name => $item) {
+                $submenus = null;
+
+                $links = [];
+
+                foreach ($item['children'] as $key => $child) {
+                    $links[$child['element']->title] = $child['element']->slug;
+                }
+
+                if (count($links) > 0) {
+                    $submenus = [['links' => $links]];
+                }
+
+                $s = ['url' => $item['element']->slug];
+                if (null !== $submenus) {
+                    $s['submenus'] = $submenus;
+                }
+
+                $nav_items[$item['element']->title] = $s;
+            }
+
+            \Menu::make($mainKey, 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);
+                            }
+                        }
+                    }
+
+                }
+
+            });
+        }
+
+
+        if ($mainKey == '#main') {
+            // Also make a menu for the breadcrumbs - this one is simpler and doesn't have the submenu headings
+            \Menu::make('breadcrumbs', function ($menu) use ($nav_items) {
+
+                // Start with home link
+                $menu = $menu->add('Home', '');
+
+                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) {
+                            foreach ($submenu_data['links'] as $label => $url) {
+                                $parent->add($label, $url);
+                            }
+                        }
+                    }
+                }
+            });
+        }
+    }
+}
index e48bb425461b33d47bde7e9b6f77b02921a4aee1..bdaf96c4bc7fc21669c8ecd33eb26310a0b88e31 100644 (file)
@@ -20,6 +20,11 @@ class CMSPage extends CubistMagicNestedModel
 
     protected $defaultFieldAttributes = ['translatable' => true];
 
+    public static function boot()
+    {
+        parent::boot();
+    }
+
     public function setFields()
     {
         parent::setFields();
@@ -163,5 +168,9 @@ class CMSPage extends CubistMagicNestedModel
         return parent::update($attributes, $options);
     }
 
+    public static function getMenu(){
+
+    }
+
 
 }