Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- *
- * /app/helpers/Menu.php
- *
- *
- * in view file /themes/altum/views/partials: echo \Altum\Menu::navigation('top');
- *
- * SQL STRUCTURE:
- CREATE TABLE `menu` (
- `menu_id` int(255) NOT NULL,
- `menu_type` enum('page','link','blank') NOT NULL,
- `parent_id` int(255) DEFAULT 0,
- `position` enum('top','buttom','top,buttom') DEFAULT 'top',
- `open_in_new_tab` enum('_blank','_self') DEFAULT '_self',
- `sort_order` int(255) NOT NULL DEFAULT 0,
- `icon` varchar(200) DEFAULT NULL,
- `status` enum('active','disabled') NOT NULL DEFAULT 'active'
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3;
- INSERT INTO `menu` (`menu_id`, `menu_type`, `parent_id`, `position`, `open_in_new_tab`, `sort_order`, `icon`, `status`) VALUES
- (1, 'link', 0, 'top', '_self', 1, 'fa-fw fa-plug', 'active'),
- (2, 'link', 0, 'top', '_self', 2, 'fa-fw fa-plug', 'active'),
- (3, 'link', 2, 'top', '_self', 1, NULL, 'active'),
- (4, 'link', 2, 'top', '_self', 2, NULL, 'active'),
- (5, 'link', 0, 'top', '_blank', 3, NULL, 'active'),
- (6, 'link', 0, 'top', '_self', 4, NULL, 'active');
- CREATE TABLE `menu_translation` (
- `menu_translate_id` int(255) NOT NULL,
- `menu_id` int(255) NOT NULL DEFAULT 0,
- `menu_name` varchar(255) NOT NULL,
- `menu_url` varchar(255) NOT NULL,
- `language` varchar(32) DEFAULT NULL
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3;
- INSERT INTO `menu_translation` (`menu_translate_id`, `menu_id`, `menu_name`, `menu_url`, `language`) VALUES
- (1, 1, 'Home', '/', 'en'),
- (2, 2, 'Services', '/services', 'en'),
- (3, 3, 'Pricing', '/pricing', 'en'),
- (4, 4, 'Features', '/features', 'en'),
- (5, 5, 'Testing', '/testing', 'en'),
- (6, 6, 'Testing2', '/testing2', 'en'),
- (7, 1, 'Начало', '/', 'bg'),
- (8, 2, 'Услуги', '/uslugi', 'bg'),
- (9, 3, 'Цени', '/ceni', 'bg'),
- (10, 4, 'Функции', '/funkcii', 'bg'),
- (11, 5, 'Тест', '/test', 'bg'),
- (12, 6, 'Тест2', '/test2', 'bg');
- ALTER TABLE `menu`
- ADD PRIMARY KEY (`menu_id`);
- ALTER TABLE `menu`
- MODIFY `menu_id` int(255) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=7;
- ALTER TABLE `menu_translation`
- ADD PRIMARY KEY (`menu_translate_id`);
- ALTER TABLE `menu_translation`
- MODIFY `menu_translate_id` int(255) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=13;*/
- namespace Altum;
- use Altum\Language;
- class Menu {
- public static function navigation($position) {
- // Select all entries from the menu table
- $query = database()->query("SELECT m.*, mt.*
- FROM `menu` m INNER JOIN `menu_translation` mt ON `m`.`menu_id` = `mt`.`menu_id`
- WHERE `mt`.`language` = '".\Altum\Language::$code."'
- AND `m`.`position` = '".$position."'
- AND `m`.`status`='active'
- ORDER BY `sort_order` ASC;");
- // Create a multidimensional array to contain a list of items and parents
- $menu = array(
- 'items' => array(),
- 'parents' => array()
- );
- // Builds the array lists with data from the menu table
- while($menus = $query->fetch_object()) {
- // Creates entry into items array with current menu item id ie. $menu['items'][1]
- $menu['items'][$menus->menu_id] = $menus;
- // Creates entry into parents array. Parents array contains a list of all items with children
- $menu['parents'][$menus->parent_id][] = $menus->menu_id;
- }
- if ($menu) {
- $result = \Altum\Menu::build_navigation(0, $menu);
- return $result;
- } else {
- return FALSE;
- }
- }
- // Menu builder function, parentId 0 is the root
- public static function build_navigation($parent, $menu) {
- $html = "";
- if (isset($menu['parents'][$parent])) {
- $html .= "";//<ul>\n
- foreach ($menu['parents'][$parent] as $itemId) {
- if (!isset($menu['parents'][$itemId])) {
- $MenuIcon = $menu['items'][$itemId]->icon ? '<i class="fa '.$menu['items'][$itemId]->icon.'"></i>' : '';
- $html .= "<li class=\"nav-item\">\n<a class=\"nav-link\" href='" . $menu['items'][$itemId]->menu_url . "' target='" . $menu['items'][$itemId]->open_in_new_tab . "'>".$MenuIcon."<span>" . $menu['items'][$itemId]->menu_name . "</span></a>\n</li>\n";
- }
- if (isset($menu['parents'][$itemId])) {
- $MenuIcon = $menu['items'][$itemId]->icon ? '<i class="fa '.$menu['items'][$itemId]->icon.'"></i>' : '';
- $html .= "<li class=\"nav-item\">\n<a class=\"nav-link\" href='" . $menu['items'][$itemId]->menu_url . "' target='" . $menu['items'][$itemId]->open_in_new_tab . "'>".$MenuIcon." <span>" . $menu['items'][$itemId]->menu_name . "</span></a>\n";
- $html .= \Altum\Menu::build_navigation($itemId, $menu);
- $html .= "</li>\n";
- }
- }
- $html .= "";//</ul>\n
- }
- return $html;
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment