Instantly share code, notes, and snippets.
Created
March 25, 2019 10:30
-
Star
0
(0)
You must be signed in to star a gist -
Fork
0
(0)
You must be signed in to fork a gist
-
-
Save fyulistian/fa55be1b240650560646ab526f8b0e19 to your computer and use it in GitHub Desktop.
Recursive Menu by PHP CI and SmartAdmin (3 level max)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| if ( ! defined('BASEPATH')) exit('No direct script access allowed'); | |
| class MYMenu { | |
| // private var | |
| private $_ci; | |
| private $_priviledges; | |
| // constructor | |
| function __construct() { | |
| $this->_ci =& get_instance(); // get a reference to CodeIgniter. | |
| } | |
| // render menus | |
| function base_menu($actived_menu = []) { | |
| /** | |
| * List of main menu on side bar | |
| * | |
| * parent_id = 0 (this is main menu a parent) | |
| * parent_id === id (child of parent. reff to main menu id) | |
| * | |
| * id = initial of menu can be use as control of menu (uac by menu) | |
| * :: must be unique (can same as data-uac) | |
| * title = name of menu | |
| * href = url location | |
| * icon = icon for menu | |
| * parent === true (is parent) | |
| * had_child === true (parent who have child) | |
| * sub_nav === true (characteristic of sub menu) | |
| * | |
| * :: for now only only support 3 level of menu | |
| * - Parent Menu | |
| * - Child menu | |
| * - Grand child menu | |
| * - Grand child menu | |
| * - Child menu | |
| * - Grand child menu | |
| * - Grand child menu | |
| * | |
| * :: parent without child (level 1) | |
| * "parent" => true, | |
| * "had_child" => false, | |
| * "sub_nav" => false, | |
| * | |
| * :: parent with child (level 1 -> 2) | |
| * "parent" => true, | |
| * "had_child" => true, | |
| * "sub_nav" => false, | |
| * | |
| * :: parent with child and grand child (level 1 -> 2 -> 3) | |
| * "parent" => true, | |
| * "had_child" => true, | |
| * "sub_nav" => true, | |
| * | |
| * @var array | |
| */ | |
| $list_menu = [ | |
| [ | |
| "id" => "dashboard", | |
| "parent_id" => "0", | |
| "title" => "Dashboard", | |
| "href" => "/manager/dashboard", | |
| "icon" => "fa fa-fw fa-dashboard", | |
| "parent" => true, | |
| "had_child" => false, | |
| "sub_nav" => false, | |
| ], | |
| [ | |
| "id" => "media", | |
| "parent_id" => "0", | |
| "title" => "Media", | |
| "href" => "/manager/media", | |
| "icon" => "fa fa-fw fa-photo", | |
| "parent" => true, | |
| "had_child" => false, | |
| "sub_nav" => false, | |
| ], | |
| [ | |
| "id" => "contact", | |
| "parent_id" => "0", | |
| "title" => "Contact", | |
| "icon" => "fa fa-fw fa-phone", | |
| "parent" => true, | |
| "had_child" => false, | |
| "sub_nav" => false, | |
| "href" => "/manager/contact", | |
| ], | |
| [ | |
| "id" => "menu-menu", | |
| "parent_id" => "0", | |
| "title" => "Menu", | |
| "href" => "/manager/menu", | |
| "icon" => "fa fa-fw fa-cubes", | |
| "parent" => true, | |
| "had_child" => false, | |
| "sub_nav" => false, | |
| ], | |
| [ | |
| "id" => "admin-menu", | |
| "parent_id" => "0", | |
| "title" => "Admin", | |
| "icon" => "fa fa-fw fa-users", | |
| "parent" => true, | |
| "had_child" => true, | |
| "sub_nav" => false, | |
| "href" => "javascript:void(0)", | |
| ], | |
| [ | |
| "id" => "admin-list", | |
| "parent_id" => "admin-menu", | |
| "title" => "List Admin", | |
| "parent" => false, | |
| "had_child" => false, | |
| "sub_nav" => true, | |
| "href" => "/manager/admin", | |
| ], | |
| [ | |
| "id" => "admin-create", | |
| "parent_id" => "admin-menu", | |
| "title" => "Tambah Admin", | |
| "parent" => false, | |
| "had_child" => false, | |
| "sub_nav" => true, | |
| "href" => "/manager/admin/create", | |
| ], | |
| [ | |
| "id" => "album-menu", | |
| "parent_id" => "0", | |
| "title" => "Album", | |
| "icon" => "fa fa-fw fa-file-image-o", | |
| "parent" => true, | |
| "had_child" => true, | |
| "sub_nav" => false, | |
| "href" => "javascript:void(0)", | |
| ], | |
| [ | |
| "id" => "album-list", | |
| "parent_id" => "album-menu", | |
| "title" => "List Album", | |
| "parent" => false, | |
| "had_child" => false, | |
| "sub_nav" => true, | |
| "href" => "/manager/album", | |
| ], | |
| [ | |
| "id" => "album-create", | |
| "parent_id" => "album-menu", | |
| "title" => "Tambah Album", | |
| "parent" => false, | |
| "had_child" => false, | |
| "sub_nav" => true, | |
| "href" => "/manager/album/create", | |
| ], | |
| [ | |
| "id" => "album-category", | |
| "parent_id" => "album-menu", | |
| "title" => "Category Album", | |
| "parent" => false, | |
| "had_child" => false, | |
| "sub_nav" => true, | |
| "href" => "/manager/album/category", | |
| ], | |
| [ | |
| "id" => "post-menu", | |
| "parent_id" => "0", | |
| "title" => "Post", | |
| "icon" => "fa fa-fw fa-rss", | |
| "parent" => true, | |
| "had_child" => true, | |
| "sub_nav" => false, | |
| "href" => "javascript:void(0)", | |
| ], | |
| [ | |
| "id" => "post-list", | |
| "parent_id" => "post-menu", | |
| "title" => "List Post", | |
| "parent" => false, | |
| "had_child" => false, | |
| "sub_nav" => true, | |
| "href" => "/manager/post", | |
| ], | |
| [ | |
| "id" => "post-create", | |
| "parent_id" => "post-menu", | |
| "title" => "Tambah Post", | |
| "parent" => false, | |
| "had_child" => false, | |
| "sub_nav" => true, | |
| "href" => "/manager/post/create", | |
| ], | |
| [ | |
| "id" => "post-category", | |
| "parent_id" => "post-menu", | |
| "title" => "Category Post", | |
| "parent" => false, | |
| "had_child" => false, | |
| "sub_nav" => true, | |
| "href" => "/manager/post/category", | |
| ], | |
| [ | |
| "id" => "page-menu", | |
| "parent_id" => "0", | |
| "title" => "Page", | |
| "icon" => "fa fa-fw fa-columns", | |
| "parent" => true, | |
| "had_child" => true, | |
| "sub_nav" => false, | |
| "href" => "javascript:void(0)", | |
| ], | |
| [ | |
| "id" => "page-list", | |
| "parent_id" => "page-menu", | |
| "title" => "List Page", | |
| "parent" => false, | |
| "had_child" => false, | |
| "sub_nav" => true, | |
| "href" => "/manager/page", | |
| ], | |
| [ | |
| "id" => "page-create", | |
| "parent_id" => "page-menu", | |
| "title" => "Tambah Page", | |
| "parent" => false, | |
| "had_child" => false, | |
| "sub_nav" => true, | |
| "href" => "/manager/page/create", | |
| ], | |
| [ | |
| "id" => "uac-main-menu", | |
| "parent_id" => "0", | |
| "title" => "User Access Control", | |
| "icon" => "fa fa-fw fa-user-circle-o", | |
| "parent" => true, | |
| "had_child" => true, | |
| "sub_nav" => false, | |
| "href" => "javascript:void(0)", | |
| ], | |
| [ | |
| "id" => "uac-menu", | |
| "parent_id" => "uac-main-menu", | |
| "title" => "Usecase", | |
| "parent" => true, | |
| "had_child" => true, | |
| "sub_nav" => true, | |
| "href" => "javascript:void(0)", | |
| ], | |
| [ | |
| "id" => "uac-list", | |
| "parent_id" => "uac-menu", | |
| "title" => "List Usecase", | |
| "parent" => false, | |
| "had_child" => false, | |
| "sub_nav" => false, | |
| "href" => "/manager/access-control/lists", | |
| "icon" => "fa fa-fw fa-list-ul", | |
| ], | |
| [ | |
| "id" => "uac-create", | |
| "parent_id" => "uac-menu", | |
| "title" => "Tambah Usecase", | |
| "parent" => false, | |
| "had_child" => false, | |
| "sub_nav" => false, | |
| "href" => "/manager/access-control/create-usecase", | |
| "icon" => "fa fa-fw fa-plus-square-o", | |
| ], | |
| [ | |
| "id" => "group-uac-menu", | |
| "parent_id" => "uac-main-menu", | |
| "title" => "Group", | |
| "parent" => true, | |
| "had_child" => true, | |
| "sub_nav" => true, | |
| "href" => "javascript:void(0)", | |
| ], | |
| [ | |
| "id" => "group-uac-list", | |
| "parent_id" => "group-uac-menu", | |
| "title" => "List Group", | |
| "parent" => false, | |
| "had_child" => false, | |
| "sub_nav" => false, | |
| "href" => "/manager/access-control/list-groups", | |
| "icon" => "fa fa-fw fa-list-ul", | |
| ], | |
| [ | |
| "id" => "group-uac-create", | |
| "parent_id" => "group-uac-menu", | |
| "title" => "Tambah Group", | |
| "parent" => false, | |
| "had_child" => false, | |
| "sub_nav" => false, | |
| "href" => "/manager/access-control/create-group", | |
| "icon" => "fa fa-fw fa-plus-square-o", | |
| ], | |
| ]; | |
| // render list menu | |
| $data = $this->render_menu($actived_menu, $list_menu); | |
| // return | |
| return $data; | |
| } | |
| /** | |
| * Menu control | |
| * Recursive and prints the elements by parent id. | |
| * parent_id 0 = main utama | |
| * @param $menu | |
| * @param string $parent_id | |
| * @return string $html | |
| */ | |
| function render_menu($actived_menu, $menu, $parent_id = "0") { | |
| // initialize var ... | |
| $html = ""; | |
| $is_parent = false; | |
| // access control | |
| $this->_priviledges = $this->_ci->session->sess_login_admin['priviledges']; // get privileges from session | |
| $access_control = $this->_priviledges; // get access (privileges) | |
| if (empty($access_control)) return FALSE; // get out if no access | |
| // loop all menu, finding the parent | |
| foreach ($menu as $item) { | |
| if ($item["parent_id"] === $parent_id) { | |
| $is_parent = true; break; | |
| } | |
| } | |
| // prepare menu | |
| if ($is_parent) { | |
| // each menu as item | |
| foreach ($menu as $item) { | |
| // basic rule of template | |
| $parent = $item['parent']; $had_child = $item['had_child']; $sub_nav = $item['sub_nav']; | |
| $sub_nav = ($sub_nav !== true) ? "sa-sub-nav" : "sa-nav-second-level"; | |
| $has_child = ($parent == true && $had_child == true) ? true : false; | |
| $has_arrow = ($item['parent_id'] == "0" && $had_child == true) ? "has-arrow" : ""; | |
| $item_parent = ($item['parent_id'] == "0" && $parent == true) ? "menu-item-parent" : ""; | |
| $expand_icon = ($had_child == true) ? " | |
| <b class='collapse-sign'> | |
| <em class='fa fa-plus-square-o'></em> | |
| <em class='fa fa-minus-square-o'></em> | |
| </b>" : ""; | |
| // check access control for showin side menu | |
| if (in_array("all-usecases", $access_control) || in_array($item["id"], $access_control)) { | |
| // is parent or not ? | |
| if ($item['parent_id'] === $parent_id) { | |
| $id = $item['id']; // get id's | |
| $name = $item['title']; // get title | |
| $icon = isset($item['icon']) ? $item["icon"] : ""; // check icon | |
| $href = isset($item['href']) ? $item["href"] : "javascript:void(0)"; // check href | |
| // get actived menu from controller | |
| $active = (in_array($id, $actived_menu, true)) ? "active" : ""; | |
| // template as menu aside | |
| $html .= "\n | |
| <li class='$active'>"; // li wrapper | |
| // menu header | |
| $html .= "\n | |
| <a class='$has_arrow' href='$href' title='$name'>"; | |
| // this template only for parent $item_parent and parent is_true | |
| if ($item['parent_id'] == 0 && $parent == true) { | |
| $html .= "\n | |
| <span class='$icon'> </span> | |
| <span class='$item_parent'> $name </span>"; | |
| } else { | |
| $html .= "\n | |
| <span class='$icon'> </span> $name"; | |
| } | |
| // expand icon is_true ? | |
| $html .= "\n | |
| $expand_icon | |
| </a>"; | |
| // have child ? | |
| if ($has_child == true) $html .= "\n | |
| <ul aria-expanded='true' class='$sub_nav collapse'>"; | |
| $html .= $this->render_menu($actived_menu, $menu, $item['id']); | |
| if ($has_child == true) $html .= "\n | |
| </ul>"; | |
| $html .= "</li>"; | |
| } | |
| } | |
| } | |
| } | |
| // return as string | |
| return $html; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment