/app/code/core/Mage/Backend/Model/Menu.php

https://bitbucket.org/jokusafet/magento2 · PHP · 233 lines · 123 code · 14 blank · 96 comment · 27 complexity · 9230696f46f489cc214dc9b86e3b2337 MD5 · raw file

  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  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_Backend
  23. * @copyright Copyright (c) 2012 X.commerce, Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Backend menu model
  28. */
  29. class Mage_Backend_Model_Menu extends ArrayObject
  30. {
  31. /**
  32. * Name of special logger key for debugging building menu
  33. */
  34. const LOGGER_KEY = 'menu-debug';
  35. /**
  36. * Path in tree structure
  37. *
  38. * @var string
  39. */
  40. protected $_path = '';
  41. /**
  42. * @var Mage_Core_Model_Logger
  43. */
  44. protected $_logger;
  45. /**
  46. * @param Mage_Core_Model_Logger $logger
  47. * @param string $pathInMenuStructure
  48. */
  49. public function __construct(Mage_Core_Model_Logger $logger, $pathInMenuStructure = '')
  50. {
  51. if ($pathInMenuStructure) {
  52. $this->_path = $pathInMenuStructure . '/';
  53. }
  54. $this->_logger = $logger;
  55. $this->setIteratorClass('Mage_Backend_Model_Menu_Iterator');
  56. }
  57. /**
  58. * Add child to menu item
  59. *
  60. * @param Mage_Backend_Model_Menu_Item $item
  61. * @param string $parentId
  62. * @param int $index
  63. * @throws InvalidArgumentException
  64. */
  65. public function add(Mage_Backend_Model_Menu_Item $item, $parentId = null, $index = null)
  66. {
  67. if (!is_null($parentId)) {
  68. $parentItem = $this->get($parentId);
  69. if ($parentItem === null) {
  70. throw new InvalidArgumentException("Item with identifier {$parentId} does not exist");
  71. }
  72. $parentItem->getChildren()->add($item, null, $index);
  73. } else {
  74. $index = intval($index);
  75. if (!isset($this[$index])) {
  76. $this->offsetSet($index, $item);
  77. $this->_logger->logDebug(
  78. sprintf('Add of item with id %s was processed', $item->getId()),
  79. self::LOGGER_KEY
  80. );
  81. } else {
  82. $this->add($item, $parentId, $index + 1);
  83. }
  84. }
  85. }
  86. /**
  87. * Retrieve menu item by id
  88. *
  89. * @param string $itemId
  90. * @return Mage_Backend_Model_Menu_Item|null
  91. */
  92. public function get($itemId)
  93. {
  94. $result = null;
  95. foreach ($this as $item) {
  96. /** @var $item Mage_Backend_Model_Menu_Item */
  97. if ($item->getId() == $itemId) {
  98. $result = $item;
  99. break;
  100. }
  101. if ($item->hasChildren() && ($result = $item->getChildren()->get($itemId))) {
  102. break;
  103. }
  104. }
  105. return $result;
  106. }
  107. /**
  108. * Move menu item
  109. *
  110. * @param string $itemId
  111. * @param string $toItemId
  112. * @param int $sortIndex
  113. */
  114. public function move($itemId, $toItemId, $sortIndex = null)
  115. {
  116. $item = $this->get($itemId);
  117. if ($item === null) {
  118. throw new InvalidArgumentException("Item with identifier {$itemId} does not exist");
  119. }
  120. $this->remove($itemId);
  121. $this->add($item, $toItemId, $sortIndex);
  122. }
  123. /**
  124. * Remove menu item by id
  125. *
  126. * @param string $itemId
  127. * @return bool
  128. */
  129. public function remove($itemId)
  130. {
  131. $result = false;
  132. foreach ($this as $key => $item) {
  133. /** @var $item Mage_Backend_Model_Menu_Item */
  134. if ($item->getId() == $itemId) {
  135. unset($this[$key]);
  136. $result = true;
  137. $this->_logger->logDebug(
  138. sprintf('Remove on item with id %s was processed', $item->getId()),
  139. self::LOGGER_KEY
  140. );
  141. break;
  142. }
  143. if ($item->hasChildren() && ($result = $item->getChildren()->remove($itemId))) {
  144. break;
  145. }
  146. }
  147. return $result;
  148. }
  149. /**
  150. * Change order of an item in its parent menu
  151. *
  152. * @param string $itemId
  153. * @param int $position
  154. * @return bool
  155. */
  156. public function reorder($itemId, $position)
  157. {
  158. $result = false;
  159. foreach ($this as $key => $item) {
  160. /** @var $item Mage_Backend_Model_Menu_Item */
  161. if ($item->getId() == $itemId) {
  162. unset($this[$key]);
  163. $this->add($item, null, $position);
  164. $result = true;
  165. break;
  166. } else if ($item->hasChildren() && $result = $item->getChildren()->reorder($itemId, $position)) {
  167. break;
  168. }
  169. }
  170. return $result;
  171. }
  172. /**
  173. * Check whether provided item is last in list
  174. *
  175. * @param Mage_Backend_Model_Menu_Item $item
  176. * @return bool
  177. */
  178. public function isLast(Mage_Backend_Model_Menu_Item $item)
  179. {
  180. return $this->offsetGet(max(array_keys($this->getArrayCopy())))->getId() == $item->getId();
  181. }
  182. /**
  183. * Find first menu item that user is able to access
  184. *
  185. * @return Mage_Backend_Model_Menu_Item|null
  186. */
  187. public function getFirstAvailable()
  188. {
  189. $result = null;
  190. /** @var $item Mage_Backend_Model_Menu_Item */
  191. foreach ($this as $item) {
  192. if ($item->isAllowed() && !$item->isDisabled()) {
  193. if ($item->hasChildren()) {
  194. $result = $item->getChildren()->getFirstAvailable();
  195. if (false == is_null($result)) {
  196. break;
  197. }
  198. } else {
  199. $result = $item;
  200. break;
  201. }
  202. }
  203. }
  204. return $result;
  205. }
  206. /**
  207. * Hack to unset logger instance which cannot be serialized
  208. *
  209. * @return string
  210. */
  211. public function serialize()
  212. {
  213. $logger = $this->_logger;
  214. unset($this->_logger);
  215. $result = parent::serialize();
  216. $this->_logger = $logger;
  217. return $result;
  218. }
  219. }