PageRenderTime 26ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/home/libraries/cms/menu/site.php

https://bitbucket.org/rubbystar/carimod
PHP | 195 lines | 99 code | 24 blank | 72 comment | 12 complexity | 416ccd86c5709d05e9f56ded090c4a63 MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0, GPL-3.0
  1. <?php
  2. /**
  3. * @package Joomla.Libraries
  4. * @subpackage Menu
  5. *
  6. * @copyright Copyright (C) 2005 - 2016 Open Source Matters, Inc. All rights reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE.txt
  8. */
  9. defined('JPATH_PLATFORM') or die;
  10. /**
  11. * JMenu class
  12. *
  13. * @since 1.5
  14. */
  15. class JMenuSite extends JMenu
  16. {
  17. /**
  18. * Application object
  19. *
  20. * @var JApplicationCms
  21. * @since 3.5
  22. */
  23. protected $app;
  24. /**
  25. * Database driver
  26. *
  27. * @var JDatabaseDriver
  28. * @since 3.5
  29. */
  30. protected $db;
  31. /**
  32. * Language object
  33. *
  34. * @var JLanguage
  35. * @since 3.5
  36. */
  37. protected $language;
  38. /**
  39. * Class constructor
  40. *
  41. * @param array $options An array of configuration options.
  42. *
  43. * @since 1.5
  44. */
  45. public function __construct($options = array())
  46. {
  47. // Extract the internal dependencies before calling the parent constructor since it calls $this->load()
  48. $this->app = isset($options['app']) && $options['app'] instanceof JApplicationCms ? $options['app'] : JFactory::getApplication();
  49. $this->db = isset($options['db']) && $options['db'] instanceof JDatabaseDriver ? $options['db'] : JFactory::getDbo();
  50. $this->language = isset($options['language']) && $options['language'] instanceof JLanguage ? $options['language'] : JFactory::getLanguage();
  51. parent::__construct($options);
  52. }
  53. /**
  54. * Loads the entire menu table into memory.
  55. *
  56. * @return boolean True on success, false on failure
  57. *
  58. * @since 1.5
  59. */
  60. public function load()
  61. {
  62. $db = $this->db;
  63. $query = $db->getQuery(true)
  64. ->select('m.id, m.menutype, m.title, m.alias, m.note, m.path AS route, m.link, m.type, m.level, m.language')
  65. ->select($db->quoteName('m.browserNav') . ', m.access, m.params, m.home, m.img, m.template_style_id, m.component_id, m.parent_id')
  66. ->select('e.element as component')
  67. ->from('#__menu AS m')
  68. ->join('LEFT', '#__extensions AS e ON m.component_id = e.extension_id')
  69. ->where('m.published = 1')
  70. ->where('m.parent_id > 0')
  71. ->where('m.client_id = 0')
  72. ->order('m.lft');
  73. // Set the query
  74. $db->setQuery($query);
  75. try
  76. {
  77. $this->_items = $db->loadObjectList('id');
  78. }
  79. catch (RuntimeException $e)
  80. {
  81. JError::raiseWarning(500, JText::sprintf('JERROR_LOADING_MENUS', $e->getMessage()));
  82. return false;
  83. }
  84. foreach ($this->_items as &$item)
  85. {
  86. // Get parent information.
  87. $parent_tree = array();
  88. if (isset($this->_items[$item->parent_id]))
  89. {
  90. $parent_tree = $this->_items[$item->parent_id]->tree;
  91. }
  92. // Create tree.
  93. $parent_tree[] = $item->id;
  94. $item->tree = $parent_tree;
  95. // Create the query array.
  96. $url = str_replace('index.php?', '', $item->link);
  97. $url = str_replace('&amp;', '&', $url);
  98. parse_str($url, $item->query);
  99. }
  100. return true;
  101. }
  102. /**
  103. * Gets menu items by attribute
  104. *
  105. * @param string $attributes The field name
  106. * @param string $values The value of the field
  107. * @param boolean $firstonly If true, only returns the first item found
  108. *
  109. * @return array
  110. *
  111. * @since 1.6
  112. */
  113. public function getItems($attributes, $values, $firstonly = false)
  114. {
  115. $attributes = (array) $attributes;
  116. $values = (array) $values;
  117. if ($this->app->isSite())
  118. {
  119. // Filter by language if not set
  120. if (($key = array_search('language', $attributes)) === false)
  121. {
  122. if (JLanguageMultilang::isEnabled())
  123. {
  124. $attributes[] = 'language';
  125. $values[] = array(JFactory::getLanguage()->getTag(), '*');
  126. }
  127. }
  128. elseif ($values[$key] === null)
  129. {
  130. unset($attributes[$key]);
  131. unset($values[$key]);
  132. }
  133. // Filter by access level if not set
  134. if (($key = array_search('access', $attributes)) === false)
  135. {
  136. $attributes[] = 'access';
  137. $values[] = $this->user->getAuthorisedViewLevels();
  138. }
  139. elseif ($values[$key] === null)
  140. {
  141. unset($attributes[$key]);
  142. unset($values[$key]);
  143. }
  144. }
  145. // Reset arrays or we get a notice if some values were unset
  146. $attributes = array_values($attributes);
  147. $values = array_values($values);
  148. return parent::getItems($attributes, $values, $firstonly);
  149. }
  150. /**
  151. * Get menu item by id
  152. *
  153. * @param string $language The language code.
  154. *
  155. * @return mixed The item object or null when not found for given language
  156. *
  157. * @since 1.6
  158. */
  159. public function getDefault($language = '*')
  160. {
  161. if (array_key_exists($language, $this->_default) && $this->app->isSite() && $this->app->getLanguageFilter())
  162. {
  163. return $this->_items[$this->_default[$language]];
  164. }
  165. if (array_key_exists('*', $this->_default))
  166. {
  167. return $this->_items[$this->_default['*']];
  168. }
  169. return;
  170. }
  171. }