UserGroupsHelper.php
5.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
<?php
/**
* Joomla! Content Management System
*
* @copyright Copyright (C) 2005 - 2019 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
namespace Joomla\CMS\Helper;
defined('JPATH_PLATFORM') or die;
/**
* Helper to deal with user groups.
*
* @since 3.6.3
*/
final class UserGroupsHelper
{
/**
* Indicates the current helper instance is the singleton instance.
*
* @var integer
* @since 3.6.3
*/
const MODE_SINGLETON = 1;
/**
* Indicates the current helper instance is a standalone class instance.
*
* @var integer
* @since 3.6.3
*/
const MODE_INSTANCE = 2;
/**
* Singleton instance.
*
* @var array
* @since 3.6.3
*/
private static $instance;
/**
* Available user groups
*
* @var array
* @since 3.6.3
*/
private $groups = array();
/**
* Mode this class is working: singleton or std instance
*
* @var integer
* @since 3.6.3
*/
private $mode;
/**
* Total available groups
*
* @var integer
* @since 3.6.3
*/
private $total;
/**
* Constructor
*
* @param array $groups Array of groups
* @param integer $mode Working mode for this class
*
* @since 3.6.3
*/
public function __construct(array $groups = array(), $mode = self::MODE_INSTANCE)
{
$this->mode = (int) $mode;
if ($groups)
{
$this->setGroups($groups);
}
}
/**
* Count loaded user groups.
*
* @return integer
*
* @since 3.6.3
*/
public function count()
{
return count($this->groups);
}
/**
* Get the helper instance.
*
* @return self
*
* @since 3.6.3
*/
public static function getInstance()
{
if (static::$instance === null)
{
// Only here to avoid code style issues...
$groups = array();
static::$instance = new static($groups, static::MODE_SINGLETON);
}
return static::$instance;
}
/**
* Get a user group by its id.
*
* @param integer $id Group identifier
*
* @return mixed stdClass on success. False otherwise
*
* @since 3.6.3
*/
public function get($id)
{
if ($this->has($id))
{
return $this->groups[$id];
}
// Singleton will load groups as they are requested
if ($this->isSingleton())
{
$this->groups[$id] = $this->load($id);
return $this->groups[$id];
}
return false;
}
/**
* Get the list of existing user groups.
*
* @return array
*
* @since 3.6.3
*/
public function getAll()
{
if ($this->isSingleton() && $this->total() !== $this->count())
{
$this->loadAll();
}
return $this->groups;
}
/**
* Check if a group is in the list.
*
* @param integer $id Group identifier
*
* @return boolean
*
* @since 3.6.3
*/
public function has($id)
{
return (array_key_exists($id, $this->groups) && $this->groups[$id] !== false);
}
/**
* Check if this instance is a singleton.
*
* @return boolean
*
* @since 3.6.3
*/
private function isSingleton()
{
return $this->mode === static::MODE_SINGLETON;
}
/**
* Get total available user groups in database.
*
* @return integer
*
* @since 3.6.3
*/
public function total()
{
if ($this->total === null)
{
$db = \JFactory::getDbo();
$query = $db->getQuery(true)
->select('count(id)')
->from('#__usergroups');
$db->setQuery($query);
$this->total = (int) $db->loadResult();
}
return $this->total;
}
/**
* Load a group from database.
*
* @param integer $id Group identifier
*
* @return mixed
*
* @since 3.6.3
*/
public function load($id)
{
$db = \JFactory::getDbo();
$query = $db->getQuery(true)
->select('*')
->from('#__usergroups')
->where('id = ' . (int) $id);
$db->setQuery($query);
$group = $db->loadObject();
if (!$group)
{
return false;
}
return $this->populateGroupData($group);
}
/**
* Load all user groups from the database.
*
* @return self
*
* @since 3.6.3
*/
public function loadAll()
{
$this->groups = array();
$db = \JFactory::getDbo();
$query = $db->getQuery(true)
->select('*')
->from('#__usergroups')
->order('lft ASC');
$db->setQuery($query);
$groups = $db->loadObjectList('id');
$this->groups = $groups ?: array();
$this->populateGroupsData();
return $this;
}
/**
* Populates extra information for groups.
*
* @return array
*
* @since 3.6.3
*/
private function populateGroupsData()
{
foreach ($this->groups as $group)
{
$this->populateGroupData($group);
}
return $this->groups;
}
/**
* Populate data for a specific user group.
*
* @param \stdClass $group Group
*
* @return \stdClass
*
* @since 3.6.3
*/
public function populateGroupData($group)
{
if (!$group || property_exists($group, 'path'))
{
return $group;
}
$parentId = (int) $group->parent_id;
if ($parentId === 0)
{
$group->path = array($group->id);
$group->level = 0;
return $group;
}
$parentGroup = $this->has($parentId) ? $this->get($parentId) : $this->load($parentId);
if (!property_exists($parentGroup, 'path'))
{
$parentGroup = $this->populateGroupData($parentGroup);
}
$group->path = array_merge($parentGroup->path, array($group->id));
$group->level = count($group->path) - 1;
return $group;
}
/**
* Set the groups to be used as source.
*
* @param array $groups Array of user groups.
*
* @return self
*
* @since 3.6.3
*/
public function setGroups(array $groups)
{
$this->groups = $groups;
$this->populateGroupsData();
$this->total = count($groups);
return $this;
}
}