Guest User

Untitled

a guest
Sep 1st, 2022
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.84 KB | None | 0 0
  1. <?php
  2. /*
  3. *
  4. * /app/helpers/Menu.php
  5. *
  6. *
  7. * in view file /themes/altum/views/partials: echo \Altum\Menu::navigation('top');
  8. *
  9. * SQL STRUCTURE:
  10.  
  11. CREATE TABLE `menu` (
  12. `menu_id` int(255) NOT NULL,
  13. `menu_type` enum('page','link','blank') NOT NULL,
  14. `parent_id` int(255) DEFAULT 0,
  15. `position` enum('top','buttom','top,buttom') DEFAULT 'top',
  16. `open_in_new_tab` enum('_blank','_self') DEFAULT '_self',
  17. `sort_order` int(255) NOT NULL DEFAULT 0,
  18. `icon` varchar(200) DEFAULT NULL,
  19. `status` enum('active','disabled') NOT NULL DEFAULT 'active'
  20. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3;
  21.  
  22. INSERT INTO `menu` (`menu_id`, `menu_type`, `parent_id`, `position`, `open_in_new_tab`, `sort_order`, `icon`, `status`) VALUES
  23. (1, 'link', 0, 'top', '_self', 1, 'fa-fw fa-plug', 'active'),
  24. (2, 'link', 0, 'top', '_self', 2, 'fa-fw fa-plug', 'active'),
  25. (3, 'link', 2, 'top', '_self', 1, NULL, 'active'),
  26. (4, 'link', 2, 'top', '_self', 2, NULL, 'active'),
  27. (5, 'link', 0, 'top', '_blank', 3, NULL, 'active'),
  28. (6, 'link', 0, 'top', '_self', 4, NULL, 'active');
  29.  
  30.  
  31. CREATE TABLE `menu_translation` (
  32. `menu_translate_id` int(255) NOT NULL,
  33. `menu_id` int(255) NOT NULL DEFAULT 0,
  34. `menu_name` varchar(255) NOT NULL,
  35. `menu_url` varchar(255) NOT NULL,
  36. `language` varchar(32) DEFAULT NULL
  37. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3;
  38.  
  39. INSERT INTO `menu_translation` (`menu_translate_id`, `menu_id`, `menu_name`, `menu_url`, `language`) VALUES
  40. (1, 1, 'Home', '/', 'en'),
  41. (2, 2, 'Services', '/services', 'en'),
  42. (3, 3, 'Pricing', '/pricing', 'en'),
  43. (4, 4, 'Features', '/features', 'en'),
  44. (5, 5, 'Testing', '/testing', 'en'),
  45. (6, 6, 'Testing2', '/testing2', 'en'),
  46. (7, 1, 'Начало', '/', 'bg'),
  47. (8, 2, 'Услуги', '/uslugi', 'bg'),
  48. (9, 3, 'Цени', '/ceni', 'bg'),
  49. (10, 4, 'Функции', '/funkcii', 'bg'),
  50. (11, 5, 'Тест', '/test', 'bg'),
  51. (12, 6, 'Тест2', '/test2', 'bg');
  52.  
  53. ALTER TABLE `menu`
  54. ADD PRIMARY KEY (`menu_id`);
  55.  
  56. ALTER TABLE `menu`
  57. MODIFY `menu_id` int(255) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=7;
  58.  
  59. ALTER TABLE `menu_translation`
  60. ADD PRIMARY KEY (`menu_translate_id`);
  61.  
  62.  
  63. ALTER TABLE `menu_translation`
  64. MODIFY `menu_translate_id` int(255) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=13;*/
  65. namespace Altum;
  66.  
  67. use Altum\Language;
  68.  
  69. class Menu {
  70.  
  71. public static function navigation($position) {
  72. // Select all entries from the menu table
  73. $query = database()->query("SELECT m.*, mt.*
  74. FROM `menu` m INNER JOIN `menu_translation` mt ON `m`.`menu_id` = `mt`.`menu_id`
  75. WHERE `mt`.`language` = '".\Altum\Language::$code."'
  76. AND `m`.`position` = '".$position."'
  77. AND `m`.`status`='active'
  78. ORDER BY `sort_order` ASC;");
  79.  
  80. // Create a multidimensional array to contain a list of items and parents
  81. $menu = array(
  82. 'items' => array(),
  83. 'parents' => array()
  84. );
  85. // Builds the array lists with data from the menu table
  86. while($menus = $query->fetch_object()) {
  87. // Creates entry into items array with current menu item id ie. $menu['items'][1]
  88. $menu['items'][$menus->menu_id] = $menus;
  89. // Creates entry into parents array. Parents array contains a list of all items with children
  90. $menu['parents'][$menus->parent_id][] = $menus->menu_id;
  91. }
  92.  
  93. if ($menu) {
  94. $result = \Altum\Menu::build_navigation(0, $menu);
  95. return $result;
  96. } else {
  97. return FALSE;
  98. }
  99. }
  100.  
  101. // Menu builder function, parentId 0 is the root
  102. public static function build_navigation($parent, $menu) {
  103. $html = "";
  104. if (isset($menu['parents'][$parent])) {
  105. $html .= "";//<ul>\n
  106. foreach ($menu['parents'][$parent] as $itemId) {
  107. if (!isset($menu['parents'][$itemId])) {
  108. $MenuIcon = $menu['items'][$itemId]->icon ? '<i class="fa '.$menu['items'][$itemId]->icon.'"></i>' : '';
  109. $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";
  110. }
  111. if (isset($menu['parents'][$itemId])) {
  112. $MenuIcon = $menu['items'][$itemId]->icon ? '<i class="fa '.$menu['items'][$itemId]->icon.'"></i>' : '';
  113. $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";
  114. $html .= \Altum\Menu::build_navigation($itemId, $menu);
  115. $html .= "</li>\n";
  116. }
  117. }
  118. $html .= "";//</ul>\n
  119. }
  120. return $html;
  121. }
  122. }
  123. ?>
Advertisement
Add Comment
Please, Sign In to add comment