1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27: 28: 29: 30: 31: 32: 33: 34: 35: 36:
37: class extends Mage_Adminhtml_Block_Template
38: {
39: const CACHE_TAGS = 'BACKEND_MAINMENU';
40:
41: 42: 43: 44: 45:
46: protected $_url;
47:
48: 49: 50: 51:
52: protected function _construct()
53: {
54: parent::_construct();
55: $this->setTemplate('page/menu.phtml');
56: $this->_url = Mage::getModel('adminhtml/url');
57: $this->setCacheTags(array(self::CACHE_TAGS));
58: }
59:
60: 61: 62: 63: 64:
65: public function getCacheLifetime()
66: {
67: return 86400;
68: }
69:
70: 71: 72: 73: 74:
75: public function getCacheKeyInfo()
76: {
77: $cacheKeyInfo = array(
78: 'admin_top_nav',
79: $this->getActive(),
80: Mage::getSingleton('admin/session')->getUser()->getId(),
81: Mage::app()->getLocale()->getLocaleCode()
82: );
83:
84: $additionalCacheKeyInfo = $this->getAdditionalCacheKeyInfo();
85: if (is_array($additionalCacheKeyInfo) && !empty($additionalCacheKeyInfo)) {
86: $cacheKeyInfo = array_merge($cacheKeyInfo, $additionalCacheKeyInfo);
87: }
88: return $cacheKeyInfo;
89: }
90:
91: 92: 93: 94: 95:
96: public function ()
97: {
98: return $this->_buildMenuArray();
99: }
100:
101: 102: 103: 104: 105: 106:
107: protected function _getHelperValue(Varien_Simplexml_Element $child)
108: {
109: $helperName = 'adminhtml';
110: $titleNodeName = 'title';
111: $childAttributes = $child->attributes();
112: if (isset($childAttributes['module'])) {
113: $helperName = (string)$childAttributes['module'];
114: }
115:
116:
117:
118:
119: return Mage::helper($helperName)->__((string)$child->$titleNodeName);
120: }
121:
122: 123: 124: 125: 126: 127: 128: 129:
130: protected function (Varien_Simplexml_Element $parent=null, $path='', $level=0)
131: {
132: if (is_null($parent)) {
133: $parent = Mage::getSingleton('admin/config')->getAdminhtmlConfig()->getNode('menu');
134: }
135:
136: $parentArr = array();
137: $sortOrder = 0;
138: foreach ($parent->children() as $childName => $child) {
139: if (1 == $child->disabled) {
140: continue;
141: }
142:
143: $aclResource = 'admin/' . ($child->resource ? (string)$child->resource : $path . $childName);
144: if (!$this->_checkAcl($aclResource)) {
145: continue;
146: }
147:
148: if ($child->depends && !$this->_checkDepends($child->depends)) {
149: continue;
150: }
151:
152: $menuArr = array();
153:
154: $menuArr['label'] = $this->_getHelperValue($child);
155:
156: $menuArr['sort_order'] = $child->sort_order ? (int)$child->sort_order : $sortOrder;
157:
158: if ($child->action) {
159: $menuArr['url'] = $this->_url->getUrl((string)$child->action, array('_cache_secret_key' => true));
160: } else {
161: $menuArr['url'] = '#';
162: $menuArr['click'] = 'return false';
163: }
164:
165: $menuArr['active'] = ($this->getActive()==$path.$childName)
166: || (strpos($this->getActive(), $path.$childName.'/')===0);
167:
168: $menuArr['level'] = $level;
169:
170: if ($child->children) {
171: $menuArr['children'] = $this->_buildMenuArray($child->children, $path.$childName.'/', $level+1);
172: }
173: $parentArr[$childName] = $menuArr;
174:
175: $sortOrder++;
176: }
177:
178: uasort($parentArr, array($this, '_sortMenu'));
179:
180: while (list($key, $value) = each($parentArr)) {
181: $last = $key;
182: }
183: if (isset($last)) {
184: $parentArr[$last]['last'] = true;
185: }
186:
187: return $parentArr;
188: }
189:
190: 191: 192: 193: 194: 195: 196:
197: protected function ($a, $b)
198: {
199: return $a['sort_order']<$b['sort_order'] ? -1 : ($a['sort_order']>$b['sort_order'] ? 1 : 0);
200: }
201:
202: 203: 204: 205: 206: 207:
208: protected function _checkDepends(Varien_Simplexml_Element $depends)
209: {
210: if ($depends->module) {
211: $modulesConfig = Mage::getConfig()->getNode('modules');
212: foreach ($depends->module as $module) {
213: if (!$modulesConfig->$module || !$modulesConfig->$module->is('active')) {
214: return false;
215: }
216: }
217: }
218:
219: if ($depends->config) {
220: foreach ($depends->config as $path) {
221: if (!Mage::getStoreConfigFlag((string)$path)) {
222: return false;
223: }
224: }
225: }
226:
227: return true;
228: }
229:
230: 231: 232: 233: 234: 235: 236:
237:
238: 239: 240: 241: 242: 243:
244: protected function _checkAcl($resource)
245: {
246: try {
247: $res = Mage::getSingleton('admin/session')->isAllowed($resource);
248: } catch (Exception $e) {
249: return false;
250: }
251: return $res;
252: }
253:
254: 255: 256: 257: 258: 259:
260: protected function _afterToHtml($html)
261: {
262: $html = preg_replace_callback('#'.Mage_Adminhtml_Model_Url::SECRET_KEY_PARAM_NAME.'/\$([^\/].*)/([^\$].*)\$#', array($this, '_callbackSecretKey'), $html);
263:
264: return $html;
265: }
266:
267: 268: 269: 270: 271: 272:
273: protected function _callbackSecretKey($match)
274: {
275: return Mage_Adminhtml_Model_Url::SECRET_KEY_PARAM_NAME . '/'
276: . $this->_url->getSecretKey($match[1], $match[2]);
277: }
278:
279: 280: 281: 282: 283: 284: 285:
286: public function ($menu, $level = 0)
287: {
288: $html = '<ul ' . (!$level ? 'id="nav"' : '') . '>' . PHP_EOL;
289: foreach ($menu as $item) {
290: $html .= '<li ' . (!empty($item['children']) ? 'onmouseover="Element.addClassName(this,\'over\')" '
291: . 'onmouseout="Element.removeClassName(this,\'over\')"' : '') . ' class="'
292: . (!$level && !empty($item['active']) ? ' active' : '') . ' '
293: . (!empty($item['children']) ? ' parent' : '')
294: . (!empty($level) && !empty($item['last']) ? ' last' : '')
295: . ' level' . $level . '"> <a href="' . $item['url'] . '" '
296: . (!empty($item['title']) ? 'title="' . $item['title'] . '"' : '') . ' '
297: . (!empty($item['click']) ? 'onclick="' . $item['click'] . '"' : '') . ' class="'
298: . ($level === 0 && !empty($item['active']) ? 'active' : '') . '"><span>'
299: . $this->escapeHtml($item['label']) . '</span></a>' . PHP_EOL;
300:
301: if (!empty($item['children'])) {
302: $html .= $this->getMenuLevel($item['children'], $level + 1);
303: }
304: $html .= '</li>' . PHP_EOL;
305: }
306: $html .= '</ul>' . PHP_EOL;
307:
308: return $html;
309: }
310: }
311: