You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

222 lines
8.1 KiB

5 years ago
5 years ago
5 years ago
  1. <?php
  2. namespace Wms\Menu\Classes;
  3. use Backend\Models\User;
  4. use System\Classes\PluginManager;
  5. class Extend
  6. {
  7. /**
  8. * @var User
  9. */
  10. protected static $user;
  11. /**
  12. * @var PluginManager
  13. */
  14. protected static $manager;
  15. protected static $icon;
  16. protected static $defaultMenuName;
  17. protected static $navigation = [
  18. 'first' => null,
  19. 'sideMenu' => [],
  20. 'subMenus' => [],
  21. 'permissions' => [],
  22. ];
  23. /**
  24. * @param PluginManager $pluginManager
  25. * @param string $defaultMenuName
  26. * @param string $defaultIcon
  27. * @return array
  28. */
  29. public static function collectNavigation(string $defaultMenuName, string $defaultIcon): array
  30. {
  31. self::$manager = PluginManager::instance();
  32. self::$defaultMenuName = $defaultMenuName;
  33. self::$icon = $defaultIcon;
  34. self::$user = \BackendAuth::getUser();
  35. $navigation = self::getPluginsNavigation();
  36. self::removeOrder($navigation);
  37. if (!$navCount = count($navigation)) {
  38. return [null, null];
  39. }
  40. foreach ($navigation as $group => $pages) {
  41. if ($group == '{default_name}') {
  42. $group = self::$defaultMenuName;
  43. }
  44. foreach ($pages as $name => $page) {
  45. if ($page) {
  46. self::$navigation['sideMenu'][$name] = [
  47. 'label' => isset($page['label']) ? $page['label'] : 'wms.menu::lang.no-name',
  48. 'group' => $group,
  49. 'url' => isset($page['url']) ? $page['url'] : '#',
  50. 'icon' => isset($page['icon']) ? $page['icon'] : 'icon-leaf',
  51. 'icon_base64' => isset($page['icon_base64']) ? $page['icon_base64'] : '',
  52. 'icon_base64_minimized' => isset($page['icon_base64_minimized']) ? $page['icon_base64_minimized'] : '',
  53. 'permissions' => isset($page['permissions']) ? $page['permissions'] : ['wms.*'],
  54. ];
  55. }
  56. }
  57. }
  58. return [
  59. self::$navigation,
  60. self::$defaultMenuName,
  61. ];
  62. }
  63. protected static function removeOrder(&$array, $name = null)
  64. {
  65. if ($name && isset($array['subMenu']) && is_array($array['subMenu'])) {
  66. $subPermissions = [];
  67. $subFirst = null;
  68. $subCount = 0;
  69. foreach ($array['subMenu'] as $subGroup => $subMenu) {
  70. if ($subGroup == '{default_name}') {
  71. $subGroup = self::$defaultMenuName;
  72. }
  73. foreach ($subMenu as $subName => $subPage) {
  74. $subPermission = isset($subPage['permissions']) ? $subPage['permissions'] : ['wms.*'];
  75. if (!is_array($subPermission)) {
  76. $subPermission = [$subPermission];
  77. }
  78. $hasPermissions = false;
  79. foreach ($subPermission as $perm) {
  80. $hasPermissions = $hasPermissions || self::$user->hasPermission($perm);
  81. }
  82. if ($hasPermissions) {
  83. $subUrl = isset($subPage['url']) ? $subPage['url'] : '#';
  84. foreach ($subPermission as $perm) {
  85. $subPermissions[$perm] = true;
  86. }
  87. if (!$subFirst && isset($subPage['first']) && $subPage['first'] && self::$user->hasPermission($subPermission)) {
  88. $subFirst = $subUrl;
  89. }
  90. self::$navigation['subMenus'][$name][$subGroup][$subName] = [
  91. 'label' => isset($subPage['label']) ? $subPage['label'] : 'wms.menu::lang.no-name',
  92. 'group' => $subGroup,
  93. 'url' => $subUrl,
  94. 'icon' => isset($subPage['icon']) ? $subPage['icon'] : 'icon-leaf',
  95. 'icon_base64' => isset($subPage['icon_base64']) ? $subPage['icon_base64'] : '',
  96. 'icon_base64_minimized' => isset($subPage['icon_base64_minimized']) ? $subPage['icon_base64_minimized'] : '',
  97. 'permissions' => $subPermission,
  98. ];
  99. $subCount++;
  100. }
  101. }
  102. }
  103. $subFirst = $subFirst ?: \Backend::url("wms/menu/$name");
  104. if ($subFirst && $subCount) {
  105. $array['url'] = $subFirst;
  106. $array['permissions'] = array_keys($subPermissions);
  107. unset($array['subMenu']);
  108. } else {
  109. $array = [];
  110. }
  111. }
  112. foreach ($array as $key => $val) {
  113. if (is_array($array[$key])) {
  114. if (isset($array[$key]['order'])) {
  115. unset($array[$key]['order']);
  116. }
  117. self::removeOrder($array[$key], $key);
  118. }
  119. }
  120. if (isset($array['first']) && $array['first']) {
  121. unset($array['first']);
  122. if (!self::$navigation['first']) {
  123. self::$navigation['first'] = self::getFirstUrl($array);
  124. }
  125. }
  126. if (isset($array['permissions'])) {
  127. foreach ($array['permissions'] as $permission) {
  128. self::$navigation['permissions'][$permission] = true;
  129. }
  130. }
  131. }
  132. protected static function getFirstUrl($field)
  133. {
  134. if (!isset($field['url'])) {
  135. return null;
  136. }
  137. $url = null;
  138. if (isset($field['permissions'])) {
  139. $hasPermissions = false;
  140. if (!is_array($field['permissions'])) {
  141. $field['permissions'] = [$field['permissions']];
  142. }
  143. foreach ($field['permissions'] as $permission) {
  144. $hasPermissions = $hasPermissions || self::$user->hasPermission($permission);
  145. }
  146. if ($hasPermissions) {
  147. $url = $field['url'];
  148. }
  149. } else {
  150. $url = $field['url'];
  151. }
  152. return $url;
  153. }
  154. protected static function getPluginsNavigation()
  155. {
  156. $navigation = [];
  157. foreach (self::$manager->getRegistrationMethodValues('addToMainNavigation') as $plugin => $subNavigation) {
  158. if (empty($subNavigation)) {
  159. continue;
  160. }
  161. $class = str_replace('.', '\\', $plugin) . '\Plugin';
  162. if (defined($class . '::DEFAULT_MENU_NAME') && !empty($class::DEFAULT_MENU_NAME)) {
  163. self::$defaultMenuName = $class::DEFAULT_MENU_NAME;
  164. }
  165. $details = self::$manager->getRegistrationMethodValues('pluginDetails');
  166. if (isset($details[$plugin]['icon'])) {
  167. $defaultIcon = $details[$plugin]['icon'];
  168. } else {
  169. $defaultIcon = self::$icon;
  170. }
  171. foreach ($subNavigation as $group => $pages) {
  172. foreach ($pages as $name => $page) {
  173. if (is_array($page)) {
  174. if (!isset($page['icon'])) {
  175. $subNavigation[$group][$name]['icon'] = $defaultIcon;
  176. }
  177. }
  178. }
  179. }
  180. self::arrayMerge($navigation, $subNavigation);
  181. }
  182. return $navigation;
  183. }
  184. protected static function arrayMerge(&$array, $merge)
  185. {
  186. foreach ($merge as $key => $val) {
  187. if (!is_array($val)) {
  188. $array[$key] = $val;
  189. } else {
  190. if (!isset($array[$key])) {
  191. $array[$key] = [];
  192. }
  193. self::arrayMerge($array[$key], $merge[$key]);
  194. }
  195. }
  196. uasort($array, function(&$arr1, &$arr2) {
  197. $order1 = isset($arr1['order']) ? $arr1['order'] : -1;
  198. $order2 = isset($arr2['order']) ? $arr2['order'] : -1;
  199. return $order1 == $order2 ? 0 : ($order1 < $order2 ? -1 : 1);
  200. });
  201. }
  202. }