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.

230 lines
8.5 KiB

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