/libraries/cms/html/category.php

https://github.com/gpongelli/joomla-cms · PHP · 168 lines · 96 code · 23 blank · 49 comment · 8 complexity · 00cd87fef19f35debc2ccd20959c732d MD5 · raw file

  1. <?php
  2. /**
  3. * @package Joomla.Libraries
  4. * @subpackage HTML
  5. *
  6. * @copyright Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE
  8. */
  9. defined('JPATH_BASE') or die;
  10. /**
  11. * Utility class for categories
  12. *
  13. * @package Joomla.Libraries
  14. * @subpackage HTML
  15. * @since 1.5
  16. */
  17. abstract class JHtmlCategory
  18. {
  19. /**
  20. * Cached array of the category items.
  21. *
  22. * @var array
  23. * @since 1.5
  24. */
  25. protected static $items = array();
  26. /**
  27. * Returns an array of categories for the given extension.
  28. *
  29. * @param string $extension The extension option e.g. com_something.
  30. * @param array $config An array of configuration options. By default, only
  31. * published and unpublished categories are returned.
  32. *
  33. * @return array
  34. *
  35. * @since 1.5
  36. */
  37. public static function options($extension, $config = array('filter.published' => array(0, 1)))
  38. {
  39. $hash = md5($extension . '.' . serialize($config));
  40. if (!isset(self::$items[$hash]))
  41. {
  42. $config = (array) $config;
  43. $db = JFactory::getDbo();
  44. $query = $db->getQuery(true);
  45. $query->select('a.id, a.title, a.level');
  46. $query->from('#__categories AS a');
  47. $query->where('a.parent_id > 0');
  48. // Filter on extension.
  49. $query->where('extension = ' . $db->quote($extension));
  50. // Filter on the published state
  51. if (isset($config['filter.published']))
  52. {
  53. if (is_numeric($config['filter.published']))
  54. {
  55. $query->where('a.published = ' . (int) $config['filter.published']);
  56. }
  57. elseif (is_array($config['filter.published']))
  58. {
  59. JArrayHelper::toInteger($config['filter.published']);
  60. $query->where('a.published IN (' . implode(',', $config['filter.published']) . ')');
  61. }
  62. }
  63. // Filter on the language
  64. if (isset($config['filter.language']))
  65. {
  66. if (is_string($config['filter.language']))
  67. {
  68. $query->where('a.language = ' . $db->quote($config['filter.language']));
  69. }
  70. elseif (is_array($config['filter.language']))
  71. {
  72. foreach ($config['filter.language'] as &$language)
  73. {
  74. $language = $db->quote($language);
  75. }
  76. $query->where('a.language IN (' . implode(',', $config['filter.language']) . ')');
  77. }
  78. }
  79. $query->order('a.lft');
  80. $db->setQuery($query);
  81. $items = $db->loadObjectList();
  82. // Assemble the list options.
  83. self::$items[$hash] = array();
  84. foreach ($items as &$item)
  85. {
  86. $repeat = ($item->level - 1 >= 0) ? $item->level - 1 : 0;
  87. $item->title = str_repeat('- ', $repeat) . $item->title;
  88. self::$items[$hash][] = JHtml::_('select.option', $item->id, $item->title);
  89. }
  90. }
  91. return self::$items[$hash];
  92. }
  93. /**
  94. * Returns an array of categories for the given extension.
  95. *
  96. * @param string $extension The extension option.
  97. * @param array $config An array of configuration options. By default, only published and unpublished categories are returned.
  98. *
  99. * @return array Categories for the extension
  100. *
  101. * @since 1.6
  102. */
  103. public static function categories($extension, $config = array('filter.published' => array(0, 1)))
  104. {
  105. $hash = md5($extension . '.' . serialize($config));
  106. if (!isset(self::$items[$hash]))
  107. {
  108. $config = (array) $config;
  109. $db = JFactory::getDbo();
  110. $query = $db->getQuery(true);
  111. $query->select('a.id, a.title, a.level, a.parent_id');
  112. $query->from('#__categories AS a');
  113. $query->where('a.parent_id > 0');
  114. // Filter on extension.
  115. $query->where('extension = ' . $db->quote($extension));
  116. // Filter on the published state
  117. if (isset($config['filter.published']))
  118. {
  119. if (is_numeric($config['filter.published']))
  120. {
  121. $query->where('a.published = ' . (int) $config['filter.published']);
  122. }
  123. elseif (is_array($config['filter.published']))
  124. {
  125. JArrayHelper::toInteger($config['filter.published']);
  126. $query->where('a.published IN (' . implode(',', $config['filter.published']) . ')');
  127. }
  128. }
  129. $query->order('a.lft');
  130. $db->setQuery($query);
  131. $items = $db->loadObjectList();
  132. // Assemble the list options.
  133. self::$items[$hash] = array();
  134. foreach ($items as &$item)
  135. {
  136. $repeat = ($item->level - 1 >= 0) ? $item->level - 1 : 0;
  137. $item->title = str_repeat('- ', $repeat) . $item->title;
  138. self::$items[$hash][] = JHtml::_('select.option', $item->id, $item->title);
  139. }
  140. // Special "Add to root" option:
  141. self::$items[$hash][] = JHtml::_('select.option', '1', JText::_('JLIB_HTML_ADD_TO_ROOT'));
  142. }
  143. return self::$items[$hash];
  144. }
  145. }