PageRenderTime 28ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/app/code/core/Mage/Adminhtml/Block/Page/Menu.php

https://bitbucket.org/kdms/sh-magento
PHP | 310 lines | 154 code | 35 blank | 121 comment | 24 complexity | 365b57e3a7463c565d7fa8af2f4c8bac MD5 | raw file
  1. <?php
  2. /**
  3. * Magento Enterprise Edition
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Magento Enterprise Edition License
  8. * that is bundled with this package in the file LICENSE_EE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://www.magentocommerce.com/license/enterprise-edition
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magentocommerce.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magentocommerce.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Adminhtml
  23. * @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
  24. * @license http://www.magentocommerce.com/license/enterprise-edition
  25. */
  26. /**
  27. * Adminhtml menu block
  28. *
  29. * @method Mage_Adminhtml_Block_Page_Menu setAdditionalCacheKeyInfo(array $cacheKeyInfo)
  30. * @method array getAdditionalCacheKeyInfo()
  31. *
  32. * @category Mage
  33. * @package Mage_Adminhtml
  34. * @author Magento Core Team <core@magentocommerce.com>
  35. */
  36. class Mage_Adminhtml_Block_Page_Menu extends Mage_Adminhtml_Block_Template
  37. {
  38. const CACHE_TAGS = 'BACKEND_MAINMENU';
  39. /**
  40. * Adminhtml URL instance
  41. *
  42. * @var Mage_Adminhtml_Model_Url
  43. */
  44. protected $_url;
  45. /**
  46. * Initialize template and cache settings
  47. *
  48. */
  49. protected function _construct()
  50. {
  51. parent::_construct();
  52. $this->setTemplate('page/menu.phtml');
  53. $this->_url = Mage::getModel('adminhtml/url');
  54. $this->setCacheTags(array(self::CACHE_TAGS));
  55. }
  56. /**
  57. * Retrieve cache lifetime
  58. *
  59. * @return int
  60. */
  61. public function getCacheLifetime()
  62. {
  63. return 86400;
  64. }
  65. /**
  66. * Get Key pieces for caching block content
  67. *
  68. * @return array
  69. */
  70. public function getCacheKeyInfo()
  71. {
  72. $cacheKeyInfo = array(
  73. 'admin_top_nav',
  74. $this->getActive(),
  75. Mage::getSingleton('admin/session')->getUser()->getId(),
  76. Mage::app()->getLocale()->getLocaleCode()
  77. );
  78. // Add additional key parameters if needed
  79. $additionalCacheKeyInfo = $this->getAdditionalCacheKeyInfo();
  80. if (is_array($additionalCacheKeyInfo) && !empty($additionalCacheKeyInfo)) {
  81. $cacheKeyInfo = array_merge($cacheKeyInfo, $additionalCacheKeyInfo);
  82. }
  83. return $cacheKeyInfo;
  84. }
  85. /**
  86. * Retrieve Adminhtml Menu array
  87. *
  88. * @return array
  89. */
  90. public function getMenuArray()
  91. {
  92. return $this->_buildMenuArray();
  93. }
  94. /**
  95. * Retrieve Title value for menu node
  96. *
  97. * @param Varien_Simplexml_Element $child
  98. * @return string
  99. */
  100. protected function _getHelperValue(Varien_Simplexml_Element $child)
  101. {
  102. $helperName = 'adminhtml';
  103. $titleNodeName = 'title';
  104. $childAttributes = $child->attributes();
  105. if (isset($childAttributes['module'])) {
  106. $helperName = (string)$childAttributes['module'];
  107. }
  108. // if (isset($childAttributes['translate'])) {
  109. // $titleNodeName = (string)$childAttributes['translate'];
  110. // }
  111. return Mage::helper($helperName)->__((string)$child->$titleNodeName);
  112. }
  113. /**
  114. * Recursive Build Menu array
  115. *
  116. * @param Varien_Simplexml_Element $parent
  117. * @param string $path
  118. * @param int $level
  119. * @return array
  120. */
  121. protected function _buildMenuArray(Varien_Simplexml_Element $parent=null, $path='', $level=0)
  122. {
  123. if (is_null($parent)) {
  124. $parent = Mage::getSingleton('admin/config')->getAdminhtmlConfig()->getNode('menu');
  125. }
  126. $parentArr = array();
  127. $sortOrder = 0;
  128. foreach ($parent->children() as $childName => $child) {
  129. if (1 == $child->disabled) {
  130. continue;
  131. }
  132. $aclResource = 'admin/' . ($child->resource ? (string)$child->resource : $path . $childName);
  133. if (!$this->_checkAcl($aclResource)) {
  134. continue;
  135. }
  136. if ($child->depends && !$this->_checkDepends($child->depends)) {
  137. continue;
  138. }
  139. $menuArr = array();
  140. $menuArr['label'] = $this->_getHelperValue($child);
  141. $menuArr['sort_order'] = $child->sort_order ? (int)$child->sort_order : $sortOrder;
  142. if ($child->action) {
  143. $menuArr['url'] = $this->_url->getUrl((string)$child->action, array('_cache_secret_key' => true));
  144. } else {
  145. $menuArr['url'] = '#';
  146. $menuArr['click'] = 'return false';
  147. }
  148. $menuArr['active'] = ($this->getActive()==$path.$childName)
  149. || (strpos($this->getActive(), $path.$childName.'/')===0);
  150. $menuArr['level'] = $level;
  151. if ($child->children) {
  152. $menuArr['children'] = $this->_buildMenuArray($child->children, $path.$childName.'/', $level+1);
  153. }
  154. $parentArr[$childName] = $menuArr;
  155. $sortOrder++;
  156. }
  157. uasort($parentArr, array($this, '_sortMenu'));
  158. while (list($key, $value) = each($parentArr)) {
  159. $last = $key;
  160. }
  161. if (isset($last)) {
  162. $parentArr[$last]['last'] = true;
  163. }
  164. return $parentArr;
  165. }
  166. /**
  167. * Sort menu comparison function
  168. *
  169. * @param int $a
  170. * @param int $b
  171. * @return int
  172. */
  173. protected function _sortMenu($a, $b)
  174. {
  175. return $a['sort_order']<$b['sort_order'] ? -1 : ($a['sort_order']>$b['sort_order'] ? 1 : 0);
  176. }
  177. /**
  178. * Check Depends
  179. *
  180. * @param Varien_Simplexml_Element $depends
  181. * @return bool
  182. */
  183. protected function _checkDepends(Varien_Simplexml_Element $depends)
  184. {
  185. if ($depends->module) {
  186. $modulesConfig = Mage::getConfig()->getNode('modules');
  187. foreach ($depends->module as $module) {
  188. if (!$modulesConfig->$module || !$modulesConfig->$module->is('active')) {
  189. return false;
  190. }
  191. }
  192. }
  193. if ($depends->config) {
  194. foreach ($depends->config as $path) {
  195. if (!Mage::getStoreConfigFlag((string)$path)) {
  196. return false;
  197. }
  198. }
  199. }
  200. return true;
  201. }
  202. /*protected function _checkAcl(Varien_Simplexml_Element $acl)
  203. {
  204. return true;
  205. $resource = (string)$acl->resource;
  206. $privilege = (string)$acl->privilege;
  207. return Mage::getSingleton('admin/session')->isAllowed($resource, $privilege);
  208. }*/
  209. /**
  210. * Check is Allow menu item for admin user
  211. *
  212. * @param string $resource
  213. * @return bool
  214. */
  215. protected function _checkAcl($resource)
  216. {
  217. try {
  218. $res = Mage::getSingleton('admin/session')->isAllowed($resource);
  219. } catch (Exception $e) {
  220. return false;
  221. }
  222. return $res;
  223. }
  224. /**
  225. * Processing block html after rendering
  226. *
  227. * @param string $html
  228. * @return string
  229. */
  230. protected function _afterToHtml($html)
  231. {
  232. $html = preg_replace_callback('#'.Mage_Adminhtml_Model_Url::SECRET_KEY_PARAM_NAME.'/\$([^\/].*)/([^\$].*)\$#', array($this, '_callbackSecretKey'), $html);
  233. return $html;
  234. }
  235. /**
  236. * Replace Callback Secret Key
  237. *
  238. * @param array $match
  239. * @return string
  240. */
  241. protected function _callbackSecretKey($match)
  242. {
  243. return Mage_Adminhtml_Model_Url::SECRET_KEY_PARAM_NAME . '/'
  244. . $this->_url->getSecretKey($match[1], $match[2]);
  245. }
  246. /**
  247. * Get menu level HTML code
  248. *
  249. * @param array $menu
  250. * @param int $level
  251. * @return string
  252. */
  253. public function getMenuLevel($menu, $level = 0)
  254. {
  255. $html = '<ul ' . (!$level ? 'id="nav"' : '') . '>' . PHP_EOL;
  256. foreach ($menu as $item) {
  257. $html .= '<li ' . (!empty($item['children']) ? 'onmouseover="Element.addClassName(this,\'over\')" '
  258. . 'onmouseout="Element.removeClassName(this,\'over\')"' : '') . ' class="'
  259. . (!$level && !empty($item['active']) ? ' active' : '') . ' '
  260. . (!empty($item['children']) ? ' parent' : '')
  261. . (!empty($level) && !empty($item['last']) ? ' last' : '')
  262. . ' level' . $level . '"> <a href="' . $item['url'] . '" '
  263. . (!empty($item['title']) ? 'title="' . $item['title'] . '"' : '') . ' '
  264. . (!empty($item['click']) ? 'onclick="' . $item['click'] . '"' : '') . ' class="'
  265. . ($level === 0 && !empty($item['active']) ? 'active' : '') . '"><span>'
  266. . $this->escapeHtml($item['label']) . '</span></a>' . PHP_EOL;
  267. if (!empty($item['children'])) {
  268. $html .= $this->getMenuLevel($item['children'], $level + 1);
  269. }
  270. $html .= '</li>' . PHP_EOL;
  271. }
  272. $html .= '</ul>' . PHP_EOL;
  273. return $html;
  274. }
  275. }