1 <?php
2 /**
3 * @package Joomla.Libraries
4 * @subpackage Form
5 *
6 * @copyright Copyright (C) 2005 - 2017 Open Source Matters, Inc. All rights reserved.
7 * @license GNU General Public License version 2 or later; see LICENSE.txt
8 */
9
10 defined('JPATH_PLATFORM') or die;
11
12 JFormHelper::loadFieldClass('list');
13
14 /**
15 * Field to load a dropdown list of available user groups
16 *
17 * @since 3.2
18 */
19 class JFormFieldUserGroupList extends JFormFieldList
20 {
21 /**
22 * The form field type.
23 *
24 * @var string
25 * @since 3.2
26 */
27 protected $type = 'UserGroupList';
28
29 /**
30 * Cached array of the category items.
31 *
32 * @var array
33 * @since 3.2
34 */
35 protected static $options = array();
36
37 /**
38 * Method to attach a JForm object to the field.
39 *
40 * @param SimpleXMLElement $element The SimpleXMLElement object representing the `<field>` tag for the form field object.
41 * @param mixed $value The form field value to validate.
42 * @param string $group The field name group control value. This acts as an array container for the field.
43 * For example if the field has name="foo" and the group value is set to "bar" then the
44 * full field name would end up being "bar[foo]".
45 *
46 * @return boolean True on success.
47 *
48 * @since 11.1
49 */
50 public function setup(SimpleXMLElement $element, $value, $group = null)
51 {
52 if (is_string($value) && strpos($value, ',') !== false)
53 {
54 $value = explode(',', $value);
55 }
56
57 return parent::setup($element, $value, $group);
58 }
59
60 /**
61 * Method to get the options to populate list
62 *
63 * @return array The field option objects.
64 *
65 * @since 3.2
66 */
67 protected function getOptions()
68 {
69 // Hash for caching
70 $hash = md5($this->element);
71
72 if (!isset(static::$options[$hash]))
73 {
74 static::$options[$hash] = parent::getOptions();
75
76 $groups = JHelperUsergroups::getInstance()->getAll();
77 $checkSuperUser = (int) $this->getAttribute('checksuperusergroup', 0);
78 $isSuperUser = JFactory::getUser()->authorise('core.admin');
79 $options = array();
80
81 foreach ($groups as $group)
82 {
83 // Don't show super user groups to non super users.
84 if ($checkSuperUser && !$isSuperUser && JAccess::checkGroup($group->id, 'core.admin'))
85 {
86 continue;
87 }
88
89 $options[] = (object) array(
90 'text' => str_repeat('- ', $group->level) . $group->title,
91 'value' => $group->id,
92 'level' => $group->level
93 );
94 }
95
96 static::$options[$hash] = array_merge(static::$options[$hash], $options);
97 }
98
99 return static::$options[$hash];
100 }
101 }
102