Menu.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Backend\Block;
  7. /**
  8. * Backend menu block
  9. *
  10. * @method $this setAdditionalCacheKeyInfo(array $cacheKeyInfo)
  11. * @method array getAdditionalCacheKeyInfo()
  12. * @api
  13. * @since 100.0.2
  14. *
  15. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  16. */
  17. class Menu extends \Magento\Backend\Block\Template
  18. {
  19. const CACHE_TAGS = 'BACKEND_MAINMENU';
  20. /**
  21. * @var string
  22. */
  23. protected $_containerRenderer;
  24. /**
  25. * @var string
  26. */
  27. protected $_itemRenderer;
  28. /**
  29. * Backend URL instance
  30. *
  31. * @var \Magento\Backend\Model\UrlInterface
  32. */
  33. protected $_url;
  34. /**
  35. * Current selected item
  36. *
  37. * @var \Magento\Backend\Model\Menu\Item|false|null
  38. */
  39. protected $_activeItemModel = null;
  40. /**
  41. * @var \Magento\Backend\Model\Menu\Filter\IteratorFactory
  42. */
  43. protected $_iteratorFactory;
  44. /**
  45. * @var \Magento\Backend\Model\Auth\Session
  46. */
  47. protected $_authSession;
  48. /**
  49. * @var \Magento\Backend\Model\Menu\Config
  50. */
  51. protected $_menuConfig;
  52. /**
  53. * @var \Magento\Framework\Locale\ResolverInterface
  54. */
  55. protected $_localeResolver;
  56. /**
  57. * @var MenuItemChecker
  58. */
  59. private $menuItemChecker;
  60. /**
  61. * @var AnchorRenderer
  62. */
  63. private $anchorRenderer;
  64. /**
  65. * @var \Magento\Framework\App\Route\ConfigInterface
  66. */
  67. private $routeConfig;
  68. /**
  69. * @param \Magento\Backend\Block\Template\Context $context
  70. * @param \Magento\Backend\Model\UrlInterface $url
  71. * @param \Magento\Backend\Model\Menu\Filter\IteratorFactory $iteratorFactory
  72. * @param \Magento\Backend\Model\Auth\Session $authSession
  73. * @param \Magento\Backend\Model\Menu\Config $menuConfig
  74. * @param \Magento\Framework\Locale\ResolverInterface $localeResolver
  75. * @param array $data
  76. * @param MenuItemChecker|null $menuItemChecker
  77. * @param AnchorRenderer|null $anchorRenderer
  78. * @param \Magento\Framework\App\Route\ConfigInterface|null $routeConfig
  79. *
  80. * @SuppressWarnings(PHPMD.ExcessiveParameterList)
  81. */
  82. public function __construct(
  83. \Magento\Backend\Block\Template\Context $context,
  84. \Magento\Backend\Model\UrlInterface $url,
  85. \Magento\Backend\Model\Menu\Filter\IteratorFactory $iteratorFactory,
  86. \Magento\Backend\Model\Auth\Session $authSession,
  87. \Magento\Backend\Model\Menu\Config $menuConfig,
  88. \Magento\Framework\Locale\ResolverInterface $localeResolver,
  89. array $data = [],
  90. MenuItemChecker $menuItemChecker = null,
  91. AnchorRenderer $anchorRenderer = null,
  92. \Magento\Framework\App\Route\ConfigInterface $routeConfig = null
  93. ) {
  94. $this->_url = $url;
  95. $this->_iteratorFactory = $iteratorFactory;
  96. $this->_authSession = $authSession;
  97. $this->_menuConfig = $menuConfig;
  98. $this->_localeResolver = $localeResolver;
  99. $this->menuItemChecker = $menuItemChecker;
  100. $this->anchorRenderer = $anchorRenderer;
  101. $this->routeConfig = $routeConfig ?:
  102. \Magento\Framework\App\ObjectManager::getInstance()
  103. ->get(\Magento\Framework\App\Route\ConfigInterface::class);
  104. parent::__construct($context, $data);
  105. }
  106. /**
  107. * Initialize template and cache settings
  108. *
  109. * @return void
  110. */
  111. protected function _construct()
  112. {
  113. parent::_construct();
  114. $this->setCacheTags([self::CACHE_TAGS]);
  115. }
  116. /**
  117. * Render menu item anchor label
  118. *
  119. * @param \Magento\Backend\Model\Menu\Item $menuItem
  120. * @return string
  121. */
  122. protected function _getAnchorLabel($menuItem)
  123. {
  124. return $this->escapeHtml(__($menuItem->getTitle()));
  125. }
  126. /**
  127. * Render menu item mouse events
  128. *
  129. * @param \Magento\Backend\Model\Menu\Item $menuItem
  130. * @return string
  131. */
  132. protected function _renderMouseEvent($menuItem)
  133. {
  134. return $menuItem->hasChildren()
  135. ? 'onmouseover="Element.addClassName(this,\'over\')" onmouseout="Element.removeClassName(this,\'over\')"'
  136. : '';
  137. }
  138. /**
  139. * Render item css class
  140. *
  141. * @param \Magento\Backend\Model\Menu\Item $menuItem
  142. * @param int $level
  143. * @return string
  144. */
  145. protected function _renderItemCssClass($menuItem, $level)
  146. {
  147. $isLast = 0 == $level && (bool)$this->getMenuModel()->isLast($menuItem) ? 'last' : '';
  148. $isItemActive = $this->menuItemChecker->isItemActive(
  149. $this->getActiveItemModel(),
  150. $menuItem,
  151. $level
  152. ) ? '_current _active' : '';
  153. $output = $isItemActive .
  154. ' ' .
  155. ($menuItem->hasChildren() ? 'parent' : '') .
  156. ' ' .
  157. $isLast .
  158. ' ' .
  159. 'level-' .
  160. $level;
  161. return $output;
  162. }
  163. /**
  164. * Get menu filter iterator
  165. *
  166. * @param \Magento\Backend\Model\Menu $menu
  167. * @return \Magento\Backend\Model\Menu\Filter\Iterator
  168. */
  169. protected function _getMenuIterator($menu)
  170. {
  171. return $this->_iteratorFactory->create(['iterator' => $menu->getIterator()]);
  172. }
  173. /**
  174. * Processing block html after rendering
  175. *
  176. * @param string $html
  177. * @return string
  178. */
  179. protected function _afterToHtml($html)
  180. {
  181. $html = preg_replace_callback(
  182. '#' . \Magento\Backend\Model\UrlInterface::SECRET_KEY_PARAM_NAME . '/\$([^\/].*)/([^\/].*)/([^\$].*)\$#U',
  183. [$this, '_callbackSecretKey'],
  184. $html
  185. );
  186. return $html;
  187. }
  188. /**
  189. * Replace Callback Secret Key
  190. *
  191. * @param string[] $match
  192. * @return string
  193. */
  194. protected function _callbackSecretKey($match)
  195. {
  196. $routeId = $this->routeConfig->getRouteByFrontName($match[1]);
  197. return \Magento\Backend\Model\UrlInterface::SECRET_KEY_PARAM_NAME . '/' . $this->_url->getSecretKey(
  198. $routeId ?: $match[1],
  199. $match[2],
  200. $match[3]
  201. );
  202. }
  203. /**
  204. * Retrieve cache lifetime
  205. *
  206. * @return int
  207. */
  208. public function getCacheLifetime()
  209. {
  210. return 86400;
  211. }
  212. /**
  213. * Get Key pieces for caching block content
  214. *
  215. * @return array
  216. */
  217. public function getCacheKeyInfo()
  218. {
  219. $cacheKeyInfo = [
  220. 'admin_top_nav',
  221. $this->getActive(),
  222. $this->_authSession->getUser()->getId(),
  223. $this->_localeResolver->getLocale(),
  224. ];
  225. // Add additional key parameters if needed
  226. $newCacheKeyInfo = $this->getAdditionalCacheKeyInfo();
  227. if (is_array($newCacheKeyInfo) && !empty($newCacheKeyInfo)) {
  228. $cacheKeyInfo = array_merge($cacheKeyInfo, $newCacheKeyInfo);
  229. }
  230. return $cacheKeyInfo;
  231. }
  232. /**
  233. * Get menu config model
  234. *
  235. * @return \Magento\Backend\Model\Menu
  236. */
  237. public function getMenuModel()
  238. {
  239. return $this->_menuConfig->getMenu();
  240. }
  241. /**
  242. * Render menu
  243. *
  244. * @param \Magento\Backend\Model\Menu $menu
  245. * @param int $level
  246. * @return string HTML
  247. */
  248. public function renderMenu($menu, $level = 0)
  249. {
  250. $output = '<ul ' . (0 == $level ? 'id="nav" role="menubar"' : '') . ' >';
  251. /** @var $menuItem \Magento\Backend\Model\Menu\Item */
  252. foreach ($this->_getMenuIterator($menu) as $menuItem) {
  253. $output .= '<li ' . $this->_renderMouseEvent(
  254. $menuItem
  255. ) . ' class="' . $this->_renderItemCssClass(
  256. $menuItem,
  257. $level
  258. ) . '"' . $this->getUiId(
  259. $menuItem->getId()
  260. ) . 'role="menuitem">';
  261. $output .= $this->anchorRenderer->renderAnchor($this->getActiveItemModel(), $menuItem, $level);
  262. if ($menuItem->hasChildren()) {
  263. $output .= $this->renderMenu($menuItem->getChildren(), $level + 1);
  264. }
  265. $output .= '</li>';
  266. }
  267. $output .= '</ul>';
  268. return $output;
  269. }
  270. /**
  271. * Count All Subnavigation Items
  272. *
  273. * @param \Magento\Backend\Model\Menu $items
  274. * @return int
  275. */
  276. protected function _countItems($items)
  277. {
  278. $total = count($items);
  279. foreach ($items as $item) {
  280. /** @var $item \Magento\Backend\Model\Menu\Item */
  281. if ($item->hasChildren()) {
  282. $total += $this->_countItems($item->getChildren());
  283. }
  284. }
  285. return $total;
  286. }
  287. /**
  288. * Building Array with Column Brake Stops
  289. *
  290. * @param \Magento\Backend\Model\Menu $items
  291. * @param int $limit
  292. * @return array|void
  293. * @todo: Add Depth Level limit, and better logic for columns
  294. */
  295. protected function _columnBrake($items, $limit)
  296. {
  297. $total = $this->_countItems($items);
  298. if ($total <= $limit) {
  299. return;
  300. }
  301. $result[] = ['total' => $total, 'max' => ceil($total / ceil($total / $limit))];
  302. $count = 0;
  303. foreach ($items as $item) {
  304. $place = $this->_countItems($item->getChildren()) + 1;
  305. $count += $place;
  306. if ($place - $result[0]['max'] > $limit - $result[0]['max']) {
  307. $colbrake = true;
  308. $count = 0;
  309. } elseif ($count - $result[0]['max'] > $limit - $result[0]['max']) {
  310. $colbrake = true;
  311. $count = $place;
  312. } else {
  313. $colbrake = false;
  314. }
  315. $result[] = ['place' => $place, 'colbrake' => $colbrake];
  316. }
  317. return $result;
  318. }
  319. /**
  320. * Add sub menu HTML code for current menu item
  321. *
  322. * @param \Magento\Backend\Model\Menu\Item $menuItem
  323. * @param int $level
  324. * @param int $limit
  325. * @param int|null $id
  326. * @return string HTML code
  327. */
  328. protected function _addSubMenu($menuItem, $level, $limit, $id = null)
  329. {
  330. $output = '';
  331. if (!$menuItem->hasChildren()) {
  332. return $output;
  333. }
  334. $output .= '<div class="submenu"' . ($level == 0 && isset($id) ? ' aria-labelledby="' . $id . '"' : '') . '>';
  335. $colStops = [];
  336. if ($level == 0 && $limit) {
  337. $colStops = $this->_columnBrake($menuItem->getChildren(), $limit);
  338. $output .= '<strong class="submenu-title">' . $this->_getAnchorLabel($menuItem) . '</strong>';
  339. $output .= '<a href="#" class="action-close _close" data-role="close-submenu"></a>';
  340. }
  341. $output .= $this->renderNavigation($menuItem->getChildren(), $level + 1, $limit, $colStops);
  342. $output .= '</div>';
  343. return $output;
  344. }
  345. /**
  346. * Render Navigation
  347. *
  348. * @param \Magento\Backend\Model\Menu $menu
  349. * @param int $level
  350. * @param int $limit
  351. * @param array $colBrakes
  352. * @return string HTML
  353. * @SuppressWarnings(PHPMD.NPathComplexity)
  354. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  355. */
  356. public function renderNavigation($menu, $level = 0, $limit = 0, $colBrakes = [])
  357. {
  358. $itemPosition = 1;
  359. $outputStart = '<ul ' . (0 == $level ? 'id="nav" role="menubar"' : 'role="menu"') . ' >';
  360. $output = '';
  361. /** @var $menuItem \Magento\Backend\Model\Menu\Item */
  362. foreach ($this->_getMenuIterator($menu) as $menuItem) {
  363. $menuId = $menuItem->getId();
  364. $itemName = substr($menuId, strrpos($menuId, '::') + 2);
  365. $itemClass = str_replace('_', '-', strtolower($itemName));
  366. if (is_array($colBrakes)
  367. && count($colBrakes)
  368. && $colBrakes[$itemPosition]['colbrake']
  369. && $itemPosition != 1
  370. ) {
  371. $output .= '</ul></li><li class="column"><ul role="menu">';
  372. }
  373. $id = $this->getJsId($menuItem->getId());
  374. $subMenu = $this->_addSubMenu($menuItem, $level, $limit, $id);
  375. $anchor = $this->anchorRenderer->renderAnchor($this->getActiveItemModel(), $menuItem, $level);
  376. $output .= '<li ' . $this->getUiId($menuItem->getId())
  377. . ' class="item-' . $itemClass . ' ' . $this->_renderItemCssClass($menuItem, $level)
  378. . ($level == 0 ? '" id="' . $id . '" aria-haspopup="true' : '')
  379. . '" role="menu-item">' . $anchor . $subMenu . '</li>';
  380. $itemPosition++;
  381. }
  382. if (is_array($colBrakes) && count($colBrakes) && $limit) {
  383. $output = '<li class="column"><ul role="menu">' . $output . '</ul></li>';
  384. }
  385. return $outputStart . $output . '</ul>';
  386. }
  387. /**
  388. * Get current selected menu item
  389. *
  390. * @return \Magento\Backend\Model\Menu\Item|false
  391. */
  392. public function getActiveItemModel()
  393. {
  394. if ($this->_activeItemModel === null) {
  395. $this->_activeItemModel = $this->getMenuModel()->get($this->getActive());
  396. if (false == $this->_activeItemModel instanceof \Magento\Backend\Model\Menu\Item) {
  397. $this->_activeItemModel = false;
  398. }
  399. }
  400. return $this->_activeItemModel;
  401. }
  402. }