PageRenderTime 38ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/joomla/libraries/gantry5/classes/Gantry/Component/Menu/AbstractMenu.php

https://gitlab.com/ricardosanchez/prueba
PHP | 403 lines | 239 code | 63 blank | 101 comment | 36 complexity | 2e313d38e51275b28674679706b95be3 MD5 | raw file
  1. <?php
  2. /**
  3. * @package Gantry5
  4. * @author RocketTheme http://www.rockettheme.com
  5. * @copyright Copyright (C) 2007 - 2016 RocketTheme, LLC
  6. * @license Dual License: MIT or GNU/GPLv2 and later
  7. *
  8. * http://opensource.org/licenses/MIT
  9. * http://www.gnu.org/licenses/gpl-2.0.html
  10. *
  11. * Gantry Framework code that extends GPL code is considered GNU/GPLv2 and later
  12. */
  13. namespace Gantry\Component\Menu;
  14. use Gantry\Component\Config\Config;
  15. use Gantry\Component\File\CompiledYamlFile;
  16. use Gantry\Component\Gantry\GantryTrait;
  17. use RocketTheme\Toolbox\ArrayTraits\ArrayAccessWithGetters;
  18. use RocketTheme\Toolbox\ArrayTraits\Countable;
  19. use RocketTheme\Toolbox\ArrayTraits\Export;
  20. use RocketTheme\Toolbox\ArrayTraits\Iterator;
  21. use RocketTheme\Toolbox\ResourceLocator\UniformResourceLocator;
  22. abstract class AbstractMenu implements \ArrayAccess, \Iterator, \Countable
  23. {
  24. use GantryTrait, ArrayAccessWithGetters, Iterator, Export, Countable;
  25. protected $default;
  26. protected $base;
  27. protected $active;
  28. protected $params;
  29. protected $override = false;
  30. protected $config;
  31. /**
  32. * @var array|Item[]
  33. */
  34. protected $items;
  35. protected $defaults = [
  36. 'menu' => '',
  37. 'base' => '/',
  38. 'startLevel' => 1,
  39. 'maxLevels' => 0,
  40. 'showAllChildren' => true,
  41. 'highlightAlias' => true,
  42. 'highlightParentAlias' => true
  43. ];
  44. abstract public function __construct();
  45. /**
  46. * Return list of menus.
  47. *
  48. * @return array
  49. */
  50. abstract public function getMenus();
  51. /**
  52. * Return default menu.
  53. *
  54. * @return string
  55. */
  56. public function getDefaultMenuName()
  57. {
  58. return null;
  59. }
  60. /**
  61. * Returns true if the platform implements a Default menu.
  62. *
  63. * @return boolean
  64. */
  65. public function hasDefaultMenu()
  66. {
  67. return false;
  68. }
  69. /**
  70. * Return active menu.
  71. *
  72. * @return string
  73. */
  74. public function getActiveMenuName()
  75. {
  76. return null;
  77. }
  78. /**
  79. * Returns true if the platform implements an Active menu.
  80. *
  81. * @return boolean
  82. */
  83. public function hasActiveMenu()
  84. {
  85. return false;
  86. }
  87. /**
  88. * @param array $params
  89. * @param Config $menu
  90. * @return AbstractMenu
  91. */
  92. public function instance(array $params = [], Config $menu = null)
  93. {
  94. $params = $params + $this->defaults;
  95. $menus = $this->getMenus();
  96. if (!$menus) {
  97. throw new \RuntimeException('Site does not have menus', 404);
  98. }
  99. if (empty($params['menu'])) {
  100. $params['menu'] = $this->getDefaultMenuName();
  101. if (!$params['menu'] && !empty($params['admin'])) {
  102. // In admin just select the first menu if there isn't default menu to be selected.
  103. $params['menu'] = reset($menus);
  104. };
  105. } elseif ($params['menu'] == '-active-') {
  106. $params['menu'] = $this->getActiveMenuName();
  107. }
  108. if (!$params['menu']) {
  109. throw new \RuntimeException('No menu selected', 404);
  110. }
  111. if (!in_array($params['menu'], $menus)) {
  112. throw new \RuntimeException('Menu not found', 404);
  113. }
  114. $instance = clone $this;
  115. $instance->params = $params;
  116. if ($menu) {
  117. $instance->override = true;
  118. $instance->config = $menu;
  119. } else {
  120. $instance->config = null;
  121. }
  122. $config = $instance->config();
  123. $items = isset($config['items']) ? $config['items'] : [];
  124. // Create menu structure.
  125. $instance->init($params);
  126. // Get menu items from the system (if not specified otherwise).
  127. if ($config->get('settings.type') !== 'custom') {
  128. $instance->getList($params, $items);
  129. }
  130. // Add custom menu items.
  131. $instance->addCustom($params, $items);
  132. // Sort menu items.
  133. $instance->sortAll();
  134. return $instance;
  135. }
  136. /**
  137. * Get menu configuration.
  138. *
  139. * @return Config
  140. */
  141. public function config()
  142. {
  143. if (!$this->config) {
  144. $gantry = static::gantry();
  145. /** @var UniformResourceLocator $locator */
  146. $locator = $gantry['locator'];
  147. $menu = $this->params['menu'];
  148. $file = CompiledYamlFile::instance($locator("gantry-config://menu/{$menu}.yaml"));
  149. $this->config = new Config($file->content());
  150. $this->config->def('settings.title', ucfirst($menu));
  151. $file->free();
  152. }
  153. return $this->config;
  154. }
  155. public function name()
  156. {
  157. return $this->params['menu'];
  158. }
  159. public function root()
  160. {
  161. return $this->offsetGet('');
  162. }
  163. public function ordering()
  164. {
  165. $list = [];
  166. foreach ($this->items as $name => $item) {
  167. $groups = $item->groups();
  168. if (count($groups) == 1 && empty($groups[0])) {
  169. continue;
  170. }
  171. $list[$name] = [];
  172. foreach ($groups as $col => $children) {
  173. $list[$name][$col] = [];
  174. foreach ($children as $child) {
  175. $list[$name][$col][] = $child->path;
  176. }
  177. }
  178. }
  179. return $list;
  180. }
  181. public function items($withdefaults = true)
  182. {
  183. $list = [];
  184. foreach ($this->items as $key => $item) {
  185. if ($key !== '') {
  186. $list[$item->path] = $item->toArray($withdefaults);
  187. }
  188. }
  189. return $list;
  190. }
  191. public function settings()
  192. {
  193. return (array) $this->config()->get('settings');
  194. }
  195. /**
  196. * @return object
  197. */
  198. public function getBase()
  199. {
  200. return $this->offsetGet($this->base);
  201. }
  202. /**
  203. * @return object
  204. */
  205. public function getDefault()
  206. {
  207. return $this->offsetGet($this->default);
  208. }
  209. /**
  210. * @return object
  211. */
  212. public function getActive()
  213. {
  214. return $this->offsetGet($this->active);
  215. }
  216. public function isActive($item)
  217. {
  218. if ($item && $item->path && strpos($this->base, $item->path) === 0) {
  219. return true;
  220. }
  221. return false;
  222. }
  223. public function isCurrent($item)
  224. {
  225. $active = $this->getActive();
  226. return $item && $active && $item->path == $active->path;
  227. }
  228. public function init(&$params)
  229. {
  230. $this->items = ['' => new Item($this, '', ['layout' => 'horizontal'])];
  231. }
  232. public function add(Item $item)
  233. {
  234. $this->items[$item->path] = $item;
  235. // If parent exists, assign menu item to its parent; otherwise ignore menu item.
  236. if (isset($this->items[$item->parent_id])) {
  237. $this->items[$item->parent_id]->addChild($item);
  238. } elseif (!$this->items['']->count()) {
  239. $this->items[$item->parent_id] = $this->items[''];
  240. $this->items[$item->parent_id]->addChild($item);
  241. }
  242. return $this;
  243. }
  244. /**
  245. * Get menu items from the platform.
  246. *
  247. * @param int $levels
  248. * @return array
  249. */
  250. abstract protected function getItemsFromPlatform($levels);
  251. /**
  252. * Get base menu item.
  253. *
  254. * If itemid is not specified or does not exist, return active menu item.
  255. * If there is no active menu item, fall back to home page for the current language.
  256. * If there is no home page, return null.
  257. *
  258. * @param string $path
  259. *
  260. * @return string
  261. */
  262. abstract protected function calcBase($path);
  263. /**
  264. * Get a list of the menu items.
  265. *
  266. * @param array $params
  267. * @param array $items
  268. */
  269. abstract public function getList(array $params, array $items);
  270. /**
  271. * Add custom menu items.
  272. *
  273. * @param array $params
  274. * @param array $items
  275. */
  276. public function addCustom(array $params, array $items)
  277. {
  278. $start = $params['startLevel'];
  279. $max = $params['maxLevels'];
  280. $end = $max ? $start + $max - 1 : 0;
  281. $config = $this->config();
  282. $type = $config->get('settings.type');
  283. // Add custom menu elements.
  284. foreach ($items as $route => $item) {
  285. if ($type !== 'custom' && (!isset($item['type']) || $item['type'] !== 'particle')) {
  286. continue;
  287. }
  288. $tree = explode('/', $route);
  289. $parentTree = $tree;
  290. array_pop($parentTree);
  291. $item['level'] = $level = count($tree);
  292. $item['parent_id'] = implode('/', $parentTree);
  293. if (($start && $start > $level)
  294. || ($end && $level > $end)
  295. // TODO: Improve. In the mean time Item::add() handles this part.
  296. // || ($start > 1 && !in_array($tree[$start - 2], $tree))
  297. ) {
  298. continue;
  299. }
  300. $item = new Item($this, $route, $item);
  301. $this->add($item);
  302. }
  303. }
  304. /**
  305. * @param array $ordering
  306. * @param string $path
  307. */
  308. public function sortAll(array &$ordering = null, $path = '')
  309. {
  310. if (!$ordering) {
  311. $config = $this->config();
  312. $ordering = $config['ordering'] ? $config['ordering'] : [];
  313. }
  314. if (!isset($this->items[$path]) || !$this->items[$path]->hasChildren()) {
  315. return;
  316. }
  317. $item = $this->items[$path];
  318. if ($this->isAssoc($ordering)) {
  319. $item->sortChildren($ordering);
  320. foreach ($ordering as $key => &$value) {
  321. if (is_array($value)) {
  322. $this->sortAll($value, $path ? $path . '/' . $key : $key);
  323. }
  324. }
  325. } else {
  326. $item->groupChildren($ordering);
  327. foreach ($ordering as &$group) {
  328. foreach ($group as $key => &$value) {
  329. if (is_array($value)) {
  330. $this->sortAll($value, $path ? $path . '/' . $key : $key);
  331. }
  332. }
  333. }
  334. }
  335. }
  336. protected function isAssoc(array $array)
  337. {
  338. return (array_values($array) !== $array);
  339. }
  340. }