PageRenderTime 70ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/Croogo/Lib/CroogoNav.php

https://github.com/kareypowell/croogo
PHP | 209 lines | 111 code | 19 blank | 79 comment | 20 complexity | 259d26e5ae216c3ce84a72add09dce50 MD5 | raw file
  1. <?php
  2. /**
  3. * CroogoNav
  4. *
  5. * @package Croogo.Croogo.Lib
  6. * @since 1.4
  7. * @author Rachman Chavik <rchavik@xintesa.com>
  8. * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  9. * @link http://www.croogo.org
  10. */
  11. class CroogoNav {
  12. /**
  13. * Current active menu
  14. *
  15. * @see CroogoNav::activeMenu()
  16. */
  17. protected static $_activeMenu = 'sidebar';
  18. /**
  19. * _items
  20. *
  21. * @var array
  22. */
  23. protected static $_items = array('sidebar' => array());
  24. /**
  25. * _defaults
  26. *
  27. * @var array
  28. */
  29. protected static $_defaults = array(
  30. 'icon' => '',
  31. 'title' => false,
  32. 'url' => '#',
  33. 'weight' => 9999,
  34. 'before' => false,
  35. 'after' => false,
  36. 'access' => array(),
  37. 'children' => array(),
  38. 'htmlAttributes' => array(),
  39. );
  40. /**
  41. * Getter/setter for activeMenu
  42. */
  43. public static function activeMenu($menu = null) {
  44. if ($menu === null) {
  45. $activeMenu = self::$_activeMenu;
  46. } else {
  47. $activeMenu = $menu;
  48. }
  49. if (!array_key_exists($activeMenu, self::$_items)) {
  50. self::$_items[$activeMenu] = array();
  51. }
  52. self::$_activeMenu = $activeMenu;
  53. return $activeMenu;
  54. }
  55. public static function menus() {
  56. return array_keys(self::$_items);
  57. }
  58. /**
  59. * _setupOptions
  60. *
  61. * @param array $options
  62. * @return void
  63. */
  64. protected static function _setupOptions(&$options) {
  65. $options = self::_merge(self::$_defaults, $options);
  66. foreach ($options['children'] as &$child) {
  67. self::_setupOptions($child);
  68. }
  69. }
  70. /**
  71. * Add a menu item
  72. *
  73. * @param string $path dot separated path in the array.
  74. * @param array $options menu options array
  75. * @return void
  76. */
  77. public static function add($menu, $path, $options = array()) {
  78. // Juggle argument for backward compatibility
  79. if (is_array($path)) {
  80. $options = $path;
  81. $path = $menu;
  82. $menu = self::activeMenu();
  83. } else {
  84. self::activeMenu($menu);
  85. }
  86. $pathE = explode('.', $path);
  87. $pathE = array_splice($pathE, 0, count($pathE) - 2);
  88. $parent = join('.', $pathE);
  89. if (!empty($parent) && !Hash::check(self::$_items[$menu], $parent)) {
  90. $title = Inflector::humanize(end($pathE));
  91. $o = array('title' => $title);
  92. self::_setupOptions($o);
  93. self::add($parent, $o);
  94. }
  95. self::_setupOptions($options);
  96. $current = Hash::extract(self::$_items[$menu], $path);
  97. if (!empty($current)) {
  98. self::_replace(self::$_items[$menu], $path, $options);
  99. } else {
  100. self::$_items[$menu] = Hash::insert(self::$_items[$menu], $path, $options);
  101. }
  102. }
  103. /**
  104. * Replace a menu element
  105. *
  106. * @param array $target pointer to start of array
  107. * @param string $path path to search for in dot separated format
  108. * @param array $options data to replace with
  109. * @return void
  110. */
  111. protected static function _replace(&$target, $path, $options) {
  112. $pathE = explode('.', $path);
  113. $path = array_shift($pathE);
  114. $fragment = join('.', $pathE);
  115. if (!empty($pathE)) {
  116. self::_replace($target[$path], $fragment, $options);
  117. } else {
  118. $target[$path] = self::_merge($target[$path], $options);
  119. }
  120. }
  121. /**
  122. * Merge $firstArray with $secondArray
  123. *
  124. * Similar to Hash::merge, except duplicates are removed
  125. * @param array $firstArray
  126. * @param array $secondArray
  127. * @return array
  128. */
  129. protected static function _merge($firstArray, $secondArray) {
  130. $merged = Hash::merge($firstArray, $secondArray);
  131. foreach ($merged as $key => $val) {
  132. if (is_array($val) && is_int(key($val))) {
  133. $merged[$key] = array_unique($val);
  134. }
  135. }
  136. return $merged;
  137. }
  138. /**
  139. * Remove a menu item
  140. *
  141. * @param string $path dot separated path in the array.
  142. * @return void
  143. */
  144. public static function remove($path) {
  145. self::$_items = Hash::remove(self::$_items, $path);
  146. }
  147. /**
  148. * Clear all menus
  149. *
  150. * @return void
  151. */
  152. public static function clear($menu = 'sidebar') {
  153. if ($menu) {
  154. if (array_key_exists($menu, self::$_items)) {
  155. self::$_items[$menu] = array();
  156. } else {
  157. throw new UnexpectedValueException('Invalid menu: ' . $menu);
  158. }
  159. } else {
  160. self::$_items = array();
  161. }
  162. }
  163. /**
  164. * Sets or returns menu data in array
  165. *
  166. * @param $items array if empty, the current menu is returned.
  167. * @return array
  168. * @throws UnexpectedValueException
  169. */
  170. public static function items($menu = 'sidebar', $items = null) {
  171. if (!is_string($menu)) {
  172. throw new UnexpectedValueException('Menu id is not a string');
  173. }
  174. if (!empty($items)) {
  175. self::$_items[$menu] = $items;
  176. }
  177. if (!array_key_exists($menu, self::$_items)) {
  178. CakeLog::error('Invalid menu: ' . $menu);
  179. return array();
  180. }
  181. return self::$_items[$menu];
  182. }
  183. /**
  184. * Gets default settings for menu items
  185. * @return array
  186. */
  187. public static function getDefaults() {
  188. return self::$_defaults;
  189. }
  190. }