/libraries/fof/form/field/usergroup.php

https://github.com/pjwiseman/joomla-cms · PHP · 143 lines · 73 code · 24 blank · 46 comment · 4 complexity · 2599869befed781fea91e7ba00d9fe03 MD5 · raw file

  1. <?php
  2. /**
  3. * @package FrameworkOnFramework
  4. * @subpackage form
  5. * @subpackage form
  6. * @copyright Copyright (C) 2010 - 2015 Nicholas K. Dionysopoulos / Akeeba Ltd. All rights reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE.txt
  8. */
  9. // Protect from unauthorized access
  10. defined('_JEXEC') or die;
  11. JFormHelper::loadFieldClass('usergroup');
  12. /**
  13. * Form Field class for FOF
  14. * Joomla! user groups
  15. *
  16. * @package FrameworkOnFramework
  17. * @since 2.0
  18. */
  19. class FOFFormFieldUsergroup extends JFormFieldUsergroup implements FOFFormField
  20. {
  21. protected $static;
  22. protected $repeatable;
  23. /** @var int A monotonically increasing number, denoting the row number in a repeatable view */
  24. public $rowid;
  25. /** @var FOFTable The item being rendered in a repeatable form field */
  26. public $item;
  27. /**
  28. * Method to get certain otherwise inaccessible properties from the form field object.
  29. *
  30. * @param string $name The property name for which to the the value.
  31. *
  32. * @return mixed The property value or null.
  33. *
  34. * @since 2.0
  35. */
  36. public function __get($name)
  37. {
  38. switch ($name)
  39. {
  40. case 'static':
  41. if (empty($this->static))
  42. {
  43. $this->static = $this->getStatic();
  44. }
  45. return $this->static;
  46. break;
  47. case 'repeatable':
  48. if (empty($this->repeatable))
  49. {
  50. $this->repeatable = $this->getRepeatable();
  51. }
  52. return $this->repeatable;
  53. break;
  54. default:
  55. return parent::__get($name);
  56. }
  57. }
  58. /**
  59. * Get the rendering of this field type for static display, e.g. in a single
  60. * item view (typically a "read" task).
  61. *
  62. * @since 2.0
  63. *
  64. * @return string The field HTML
  65. */
  66. public function getStatic()
  67. {
  68. $class = $this->element['class'] ? ' class="' . (string) $this->element['class'] . '"' : '';
  69. $params = $this->getOptions();
  70. $db = JFactory::getDbo();
  71. $query = $db->getQuery(true);
  72. $query->select('a.id AS value, a.title AS text');
  73. $query->from('#__usergroups AS a');
  74. $query->group('a.id, a.title');
  75. $query->order('a.id ASC');
  76. $query->order($query->qn('title') . ' ASC');
  77. // Get the options.
  78. $db->setQuery($query);
  79. $options = $db->loadObjectList();
  80. // If params is an array, push these options to the array
  81. if (is_array($params))
  82. {
  83. $options = array_merge($params, $options);
  84. }
  85. // If all levels is allowed, push it into the array.
  86. elseif ($params)
  87. {
  88. array_unshift($options, JHtml::_('select.option', '', JText::_('JOPTION_ACCESS_SHOW_ALL_LEVELS')));
  89. }
  90. return '<span id="' . $this->id . '" ' . $class . '>' .
  91. htmlspecialchars(FOFFormFieldList::getOptionName($options, $this->value), ENT_COMPAT, 'UTF-8') .
  92. '</span>';
  93. }
  94. /**
  95. * Get the rendering of this field type for a repeatable (grid) display,
  96. * e.g. in a view listing many item (typically a "browse" task)
  97. *
  98. * @since 2.0
  99. *
  100. * @return string The field HTML
  101. */
  102. public function getRepeatable()
  103. {
  104. $class = $this->element['class'] ? (string) $this->element['class'] : '';
  105. $db = JFactory::getDbo();
  106. $query = $db->getQuery(true);
  107. $query->select('a.id AS value, a.title AS text');
  108. $query->from('#__usergroups AS a');
  109. $query->group('a.id, a.title');
  110. $query->order('a.id ASC');
  111. $query->order($query->qn('title') . ' ASC');
  112. // Get the options.
  113. $db->setQuery($query);
  114. $options = $db->loadObjectList();
  115. return '<span class="' . $this->id . ' ' . $class . '">' .
  116. htmlspecialchars(FOFFormFieldList::getOptionName($options, $this->value), ENT_COMPAT, 'UTF-8') .
  117. '</span>';
  118. }
  119. }