PageRenderTime 46ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/libraries/joomla/html/html/category.php

https://bitbucket.org/organicdevelopment/joomla-2.5
PHP | 151 lines | 81 code | 22 blank | 48 comment | 6 complexity | c4fad7cba94a28ed981ce750c7c0f69a MD5 | raw file
Possible License(s): LGPL-3.0, GPL-2.0, MIT, BSD-3-Clause, LGPL-2.1
  1. <?php
  2. /**
  3. * @package Joomla.Platform
  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_PLATFORM') or die;
  10. /**
  11. * Utility class for categories
  12. *
  13. * @package Joomla.Platform
  14. * @subpackage HTML
  15. * @since 11.1
  16. */
  17. abstract class JHtmlCategory
  18. {
  19. /**
  20. * Cached array of the category items.
  21. *
  22. * @var array
  23. * @since 11.1
  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 11.1
  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. $query->order('a.lft');
  64. $db->setQuery($query);
  65. $items = $db->loadObjectList();
  66. // Assemble the list options.
  67. self::$items[$hash] = array();
  68. foreach ($items as &$item)
  69. {
  70. $repeat = ($item->level - 1 >= 0) ? $item->level - 1 : 0;
  71. $item->title = str_repeat('- ', $repeat) . $item->title;
  72. self::$items[$hash][] = JHtml::_('select.option', $item->id, $item->title);
  73. }
  74. }
  75. return self::$items[$hash];
  76. }
  77. /**
  78. * Returns an array of categories for the given extension.
  79. *
  80. * @param string $extension The extension option.
  81. * @param array $config An array of configuration options. By default, only published and unpublished categories are returned.
  82. *
  83. * @return array Categories for the extension
  84. *
  85. * @since 11.1
  86. */
  87. public static function categories($extension, $config = array('filter.published' => array(0, 1)))
  88. {
  89. $hash = md5($extension . '.' . serialize($config));
  90. if (!isset(self::$items[$hash]))
  91. {
  92. $config = (array) $config;
  93. $db = JFactory::getDbo();
  94. $query = $db->getQuery(true);
  95. $query->select('a.id, a.title, a.level, a.parent_id');
  96. $query->from('#__categories AS a');
  97. $query->where('a.parent_id > 0');
  98. // Filter on extension.
  99. $query->where('extension = ' . $db->quote($extension));
  100. // Filter on the published state
  101. if (isset($config['filter.published']))
  102. {
  103. if (is_numeric($config['filter.published']))
  104. {
  105. $query->where('a.published = ' . (int) $config['filter.published']);
  106. }
  107. elseif (is_array($config['filter.published']))
  108. {
  109. JArrayHelper::toInteger($config['filter.published']);
  110. $query->where('a.published IN (' . implode(',', $config['filter.published']) . ')');
  111. }
  112. }
  113. $query->order('a.lft');
  114. $db->setQuery($query);
  115. $items = $db->loadObjectList();
  116. // Assemble the list options.
  117. self::$items[$hash] = array();
  118. foreach ($items as &$item)
  119. {
  120. $repeat = ($item->level - 1 >= 0) ? $item->level - 1 : 0;
  121. $item->title = str_repeat('- ', $repeat) . $item->title;
  122. self::$items[$hash][] = JHtml::_('select.option', $item->id, $item->title);
  123. }
  124. // Special "Add to root" option:
  125. self::$items[$hash][] = JHtml::_('select.option', '1', JText::_('JLIB_HTML_ADD_TO_ROOT'));
  126. }
  127. return self::$items[$hash];
  128. }
  129. }