PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/libraries/joomla/application/menu.php

https://github.com/dg482/joomla-platform
PHP | 353 lines | 174 code | 34 blank | 145 comment | 21 complexity | 2af4383a787a1a425472290aeb3ee837 MD5 | raw file
  1. <?php
  2. /**
  3. * @package Joomla.Platform
  4. * @subpackage Application
  5. *
  6. * @copyright Copyright (C) 2005 - 2011 Open Source Matters, Inc. All rights reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE
  8. */
  9. defined('JPATH_PLATFORM') or die();
  10. /**
  11. * JMenu class
  12. *
  13. * @package Joomla.Platform
  14. * @subpackage Application
  15. * @since 11.1
  16. */
  17. class JMenu extends JObject
  18. {
  19. /**
  20. * Array to hold the menu items
  21. *
  22. * @var array
  23. * @since 11.1
  24. */
  25. protected $_items = array();
  26. /**
  27. * Identifier of the default menu item
  28. *
  29. * @var integer
  30. * @since 11.1
  31. */
  32. protected $_default = array();
  33. /**
  34. * Identifier of the active menu item
  35. *
  36. * @var integer
  37. * @since 11.1
  38. */
  39. protected $_active = 0;
  40. /**
  41. * Class constructor
  42. *
  43. * @param array $options An array of configuration options.
  44. *
  45. * @return JMenu A JMenu object
  46. *
  47. * @since 11.1
  48. */
  49. public function __construct($options = array())
  50. {
  51. // Load the menu items
  52. $this->load();
  53. foreach ($this->_items as $k => $item)
  54. {
  55. if ($item->home)
  56. {
  57. $this->_default[$item->language] = $item->id;
  58. }
  59. // Decode the item params
  60. $result = new JRegistry;
  61. $result->loadString($item->params);
  62. $item->params = $result;
  63. }
  64. }
  65. /**
  66. * Returns a JMenu object
  67. *
  68. * @param string $client The name of the client
  69. * @param array $options An associative array of options
  70. *
  71. * @return JMenu A menu object.
  72. *
  73. * @since 11.1
  74. */
  75. public static function getInstance($client, $options = array())
  76. {
  77. static $instances;
  78. if (!isset($instances))
  79. {
  80. $instances = array();
  81. }
  82. if (empty($instances[$client]))
  83. {
  84. //Load the router object
  85. $info = JApplicationHelper::getClientInfo($client, true);
  86. $path = $info->path . '/includes/menu.php';
  87. if (file_exists($path))
  88. {
  89. include_once $path;
  90. // Create a JPathway object
  91. $classname = 'JMenu' . ucfirst($client);
  92. $instance = new $classname($options);
  93. }
  94. else
  95. {
  96. //$error = JError::raiseError(500, 'Unable to load menu: '.$client);
  97. //TODO: Solve this
  98. $error = null;
  99. return $error;
  100. }
  101. $instances[$client] = & $instance;
  102. }
  103. return $instances[$client];
  104. }
  105. /**
  106. * Get menu item by id
  107. *
  108. * @param integer $id The item id
  109. *
  110. * @return mixed The item object, or null if not found
  111. *
  112. * @since 11.1
  113. */
  114. public function getItem($id)
  115. {
  116. $result = null;
  117. if (isset($this->_items[$id]))
  118. {
  119. $result = &$this->_items[$id];
  120. }
  121. return $result;
  122. }
  123. /**
  124. * Set the default item by id and language code.
  125. *
  126. * @param integer $id The menu item id.
  127. * @param string $language The language cod (since 1.6).
  128. *
  129. * @return boolean True, if succesful
  130. *
  131. * @since 11.1
  132. */
  133. public function setDefault($id, $language = '')
  134. {
  135. if (isset($this->_items[$id]))
  136. {
  137. $this->_default[$language] = $id;
  138. return true;
  139. }
  140. return false;
  141. }
  142. /**
  143. * Get the default item by language code.
  144. *
  145. * @param string $language The language code, default value of * means all.
  146. *
  147. * @return object The item object
  148. *
  149. * @since 11.1
  150. */
  151. function getDefault($language = '*')
  152. {
  153. if (array_key_exists($language, $this->_default))
  154. {
  155. return $this->_items[$this->_default[$language]];
  156. }
  157. else if (array_key_exists('*', $this->_default))
  158. {
  159. return $this->_items[$this->_default['*']];
  160. }
  161. else
  162. {
  163. return 0;
  164. }
  165. }
  166. /**
  167. * Set the default item by id
  168. *
  169. * @param integer $id The item id
  170. *
  171. * @return mixed If successfull the active item, otherwise null
  172. *
  173. * @since 11.1
  174. */
  175. public function setActive($id)
  176. {
  177. if (isset($this->_items[$id]))
  178. {
  179. $this->_active = $id;
  180. $result = &$this->_items[$id];
  181. return $result;
  182. }
  183. return null;
  184. }
  185. /**
  186. * Get menu item by id.
  187. *
  188. * @return object The item object.
  189. *
  190. * @since 11.1
  191. */
  192. public function getActive()
  193. {
  194. if ($this->_active)
  195. {
  196. $item = &$this->_items[$this->_active];
  197. return $item;
  198. }
  199. return null;
  200. }
  201. /**
  202. * Gets menu items by attribute
  203. *
  204. * @param string $attributes The field name
  205. * @param string $values The value of the field
  206. * @param boolean $firstonly If true, only returns the first item found
  207. *
  208. * @return array
  209. *
  210. * @since 11.1
  211. */
  212. public function getItems($attributes, $values, $firstonly = false)
  213. {
  214. $items = null;
  215. $attributes = (array) $attributes;
  216. $values = (array) $values;
  217. foreach ($this->_items as $item)
  218. {
  219. if (!is_object($item))
  220. {
  221. continue;
  222. }
  223. $test = true;
  224. for ($i = 0, $count = count($attributes); $i < $count; $i++)
  225. {
  226. if (is_array($values[$i]))
  227. {
  228. if (!in_array($item->$attributes[$i], $values[$i]))
  229. {
  230. $test = false;
  231. break;
  232. }
  233. }
  234. else
  235. {
  236. if ($item->$attributes[$i] != $values[$i])
  237. {
  238. $test = false;
  239. break;
  240. }
  241. }
  242. }
  243. if ($test)
  244. {
  245. if ($firstonly)
  246. {
  247. return $item;
  248. }
  249. $items[] = $item;
  250. }
  251. }
  252. return $items;
  253. }
  254. /**
  255. * Gets the parameter object for a certain menu item
  256. *
  257. * @param integer $id The item id
  258. *
  259. * @return JRegistry A JRegistry object
  260. *
  261. * @since 11.1
  262. */
  263. public function getParams($id)
  264. {
  265. if ($menu = $this->getItem($id))
  266. {
  267. return $menu->params;
  268. }
  269. else
  270. {
  271. return new JRegistry;
  272. }
  273. }
  274. /**
  275. * Getter for the menu array
  276. *
  277. * @return array
  278. *
  279. * @since 11.1
  280. */
  281. public function getMenu()
  282. {
  283. return $this->_items;
  284. }
  285. /**
  286. * Method to check JMenu object authorization against an access control
  287. * object and optionally an access extension object
  288. *
  289. * @param integer $id The menu id
  290. *
  291. * @return boolean True if authorised
  292. *
  293. * @since 11.1
  294. */
  295. public function authorise($id)
  296. {
  297. $menu = $this->getItem($id);
  298. $user = JFactory::getUser();
  299. if ($menu)
  300. {
  301. return in_array((int) $menu->access, $user->getAuthorisedViewLevels());
  302. }
  303. else
  304. {
  305. return true;
  306. }
  307. }
  308. /**
  309. * Loads the menu items
  310. *
  311. * @return array
  312. *
  313. * @since 11.1
  314. */
  315. public function load()
  316. {
  317. return array();
  318. }
  319. }