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.

217 lines
7.6 KiB

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. 'permissions' => isset($page['permissions']) ? $page['permissions'] : ['wms.*'],
  52. ];
  53. }
  54. }
  55. }
  56. return [
  57. self::$navigation,
  58. self::$defaultMenuName,
  59. ];
  60. }
  61. protected static function removeOrder(&$array, $name = null)
  62. {
  63. if ($name && isset($array['subMenu']) && is_array($array['subMenu'])) {
  64. $subPermissions = [];
  65. $subFirst = null;
  66. $subCount = 0;
  67. foreach ($array['subMenu'] as $subGroup => $subMenu) {
  68. if ($subGroup == '{default_name}') {
  69. $subGroup = self::$defaultMenuName;
  70. }
  71. foreach ($subMenu as $subName => $subPage) {
  72. $subPermission = isset($subPage['permissions']) ? $subPage['permissions'] : ['wms.*'];
  73. if (!is_array($subPermission)) {
  74. $subPermission = [$subPermission];
  75. }
  76. $hasPermissions = false;
  77. foreach ($subPermission as $perm) {
  78. $hasPermissions = $hasPermissions || self::$user->hasPermission($perm);
  79. }
  80. if ($hasPermissions) {
  81. $subUrl = isset($subPage['url']) ? $subPage['url'] : '#';
  82. foreach ($subPermission as $perm) {
  83. $subPermissions[$perm] = true;
  84. }
  85. if (!$subFirst && isset($subPage['first']) && $subPage['first'] && self::$user->hasPermission($subPermission)) {
  86. $subFirst = $subUrl;
  87. }
  88. self::$navigation['subMenus'][$name][$subGroup][$subName] = [
  89. 'label' => isset($subPage['label']) ? $subPage['label'] : 'wms.menu::lang.no-name',
  90. 'group' => $subGroup,
  91. 'url' => $subUrl,
  92. 'icon' => isset($subPage['icon']) ? $subPage['icon'] : 'icon-leaf',
  93. 'permissions' => $subPermission,
  94. ];
  95. $subCount++;
  96. }
  97. }
  98. }
  99. $subFirst = $subFirst ?: \Backend::url("wms/menu/$name");
  100. if ($subFirst && $subCount) {
  101. $array['url'] = $subFirst;
  102. $array['permissions'] = array_keys($subPermissions);
  103. unset($array['subMenu']);
  104. } else {
  105. $array = [];
  106. }
  107. }
  108. foreach ($array as $key => $val) {
  109. if (is_array($array[$key])) {
  110. if (isset($array[$key]['order'])) {
  111. unset($array[$key]['order']);
  112. }
  113. self::removeOrder($array[$key], $key);
  114. }
  115. }
  116. if (isset($array['first']) && $array['first']) {
  117. unset($array['first']);
  118. if (!self::$navigation['first']) {
  119. self::$navigation['first'] = self::getFirstUrl($array);
  120. }
  121. }
  122. if (isset($array['permissions'])) {
  123. foreach ($array['permissions'] as $permission) {
  124. self::$navigation['permissions'][$permission] = true;
  125. }
  126. }
  127. }
  128. protected static function getFirstUrl($field)
  129. {
  130. if (!isset($field['url'])) {
  131. return null;
  132. }
  133. $url = null;
  134. if (isset($field['permissions'])) {
  135. $hasPermissions = false;
  136. if (!is_array($field['permissions'])) {
  137. $field['permissions'] = [$field['permissions']];
  138. }
  139. foreach ($field['permissions'] as $permission) {
  140. $hasPermissions = $hasPermissions || self::$user->hasPermission($permission);
  141. }
  142. if ($hasPermissions) {
  143. $url = $field['url'];
  144. }
  145. } else {
  146. $url = $field['url'];
  147. }
  148. return $url;
  149. }
  150. protected static function getPluginsNavigation()
  151. {
  152. $navigation = [];
  153. foreach (self::$manager->getRegistrationMethodValues('addToMainNavigation') as $plugin => $subNavigation) {
  154. if (empty($subNavigation)) {
  155. continue;
  156. }
  157. $class = str_replace('.', '\\', $plugin) . '\Plugin';
  158. if (defined($class . '::DEFAULT_MENU_NAME') && !empty($class::DEFAULT_MENU_NAME)) {
  159. self::$defaultMenuName = $class::DEFAULT_MENU_NAME;
  160. }
  161. $details = self::$manager->getRegistrationMethodValues('pluginDetails');
  162. if (isset($details[$plugin]['icon'])) {
  163. $defaultIcon = $details[$plugin]['icon'];
  164. } else {
  165. $defaultIcon = self::$icon;
  166. }
  167. foreach ($subNavigation as $group => $pages) {
  168. foreach ($pages as $name => $page) {
  169. if (is_array($page)) {
  170. if (!isset($page['icon'])) {
  171. $subNavigation[$group][$name]['icon'] = $defaultIcon;
  172. }
  173. }
  174. }
  175. }
  176. self::arrayMerge($navigation, $subNavigation);
  177. }
  178. return $navigation;
  179. }
  180. protected static function arrayMerge(&$array, $merge)
  181. {
  182. foreach ($merge as $key => $val) {
  183. if (!is_array($val)) {
  184. $array[$key] = $val;
  185. } else {
  186. if (!isset($array[$key])) {
  187. $array[$key] = [];
  188. }
  189. self::arrayMerge($array[$key], $merge[$key]);
  190. }
  191. }
  192. uasort($array, function(&$arr1, &$arr2) {
  193. $order1 = isset($arr1['order']) ? $arr1['order'] : -1;
  194. $order2 = isset($arr2['order']) ? $arr2['order'] : -1;
  195. return $order1 == $order2 ? 0 : ($order1 < $order2 ? -1 : 1);
  196. });
  197. }
  198. }