9
9
namespace Xibo\Factory;
13
12
use Xibo\Entity\UserGroup;
14
13
use Xibo\Exception\NotFoundException;
15
use Xibo\Service\LogServiceInterface;
16
use Xibo\Service\SanitizerServiceInterface;
17
use Xibo\Storage\StorageServiceInterface;
15
use Xibo\Helper\Sanitize;
16
use Xibo\Storage\PDOConnect;
20
* Class UserGroupFactory
21
* @package Xibo\Factory
23
18
class UserGroupFactory extends BaseFactory
27
* @param StorageServiceInterface $store
28
* @param LogServiceInterface $log
29
* @param SanitizerServiceInterface $sanitizerService
31
* @param UserFactory $userFactory
33
public function __construct($store, $log, $sanitizerService, $user, $userFactory)
35
$this->setCommonDependencies($store, $log, $sanitizerService);
37
$this->setAclDependencies($user, $userFactory);
41
* Create Empty User Group Object
44
public function createEmpty()
46
return new UserGroup($this->getStore(), $this->getLog(), $this, $this->getUserFactory());
52
* @param $libraryQuota
55
public function create($userGroup, $libraryQuota)
57
$group = $this->createEmpty();
58
$group->group = $userGroup;
59
$group->libraryQuota = $libraryQuota;
66
22
* @param int $groupId
67
23
* @return UserGroup
68
24
* @throws NotFoundException
70
public function getById($groupId)
26
public static function getById($groupId)
72
$groups = $this->query(null, ['disableUserCheck' => 1, 'groupId' => $groupId, 'isUserSpecific' => -1]);
28
$groups = UserGroupFactory::query(null, ['disableUserCheck' => 1, 'groupId' => $groupId, 'isUserSpecific' => -1]);
74
30
if (count($groups) <= 0)
75
31
throw new NotFoundException(__('Group not found'));
113
* Get isSystemNotification Group
114
* @return UserGroup[]
116
public function getSystemNotificationGroups()
118
return $this->query(null, ['disableUserCheck' => 1, 'isSystemNotification' => 1, 'isUserSpecific' => -1]);
122
* Get isDisplayNotification Group
123
* @param int|null $displayGroupId Optionally provide a displayGroupId to restrict to view permissions.
124
* @return UserGroup[]
126
public function getDisplayNotificationGroups($displayGroupId = null)
128
return $this->query(null, [
129
'disableUserCheck' => 1,
130
'isDisplayNotification' => 1,
131
'isUserSpecific' => -1,
132
'displayGroupId' => $displayGroupId
138
69
* @param int $userId
139
70
* @return array[UserGroup]
140
71
* @throws NotFoundException
142
public function getByUserId($userId)
144
return $this->query(null, ['disableUserCheck' => 1, 'userId' => $userId, 'isUserSpecific' => 0]);
148
* Get User Groups assigned to Notifications
149
* @param int $notificationId
150
* @return array[UserGroup]
152
public function getByNotificationId($notificationId)
154
return $this->query(null, ['disableUserCheck' => 1, 'notificationId' => $notificationId, 'isUserSpecific' => -1]);
158
* Get by Display Group
159
* @param int $displayGroupId
160
* @return UserGroup[]
162
public function getByDisplayGroupId($displayGroupId)
164
return $this->query(null, ['disableUserCheck' => 1, 'displayGroupId' => $displayGroupId]);
73
public static function getByUserId($userId)
75
return UserGroupFactory::query(null, ['disableUserCheck' => 1, 'userId' => $userId, 'isUserSpecific' => 0]);
225
119
WHERE `lkusergroup`.userId = :currentUserId
228
$params['currentUserId'] = $this->getUser()->userId;
122
$params['currentUserId'] = self::getUser()->userId;
232
126
// Filter by Group Id
233
if ($this->getSanitizer()->getInt('groupId', $filterBy) !== null) {
127
if (Sanitize::getInt('groupId', $filterBy) !== null) {
234
128
$body .= ' AND `group`.groupId = :groupId ';
235
$params['groupId'] = $this->getSanitizer()->getInt('groupId', $filterBy);
129
$params['groupId'] = Sanitize::getInt('groupId', $filterBy);
238
132
// Filter by Group Name
239
if ($this->getSanitizer()->getString('group', $filterBy) != null) {
240
// Convert into commas
241
foreach (explode(',', $this->getSanitizer()->getString('group', $filterBy)) as $term) {
243
if (empty(trim($term)))
246
// convert into a space delimited array
247
$names = explode(' ', $term);
250
foreach ($names as $searchName) {
252
// Not like, or like?
253
if (substr($searchName, 0, 1) == '-') {
254
$body .= " AND `group`.group NOT RLIKE (:group$i) ";
255
$params['group' . $i] = ltrim(($searchName), '-');
257
$body .= " AND `group`.group RLIKE (:group$i) ";
258
$params['group' . $i] = $searchName;
264
if ($this->getSanitizer()->getString('exactGroup', $filterBy) != null) {
265
$body .= ' AND `group`.group = :exactGroup ';
266
$params['exactGroup'] = $this->getSanitizer()->getString('exactGroup', $filterBy);
133
if (Sanitize::getString('group', $filterBy) != null) {
134
$body .= ' AND `group`.group = :group ';
135
$params['group'] = Sanitize::getString('group', $filterBy);
269
138
// Filter by User Id
270
if ($this->getSanitizer()->getInt('userId', $filterBy) !== null) {
139
if (Sanitize::getInt('userId', $filterBy) !== null) {
271
140
$body .= ' AND `group`.groupId IN (SELECT groupId FROM `lkusergroup` WHERE userId = :userId) ';
272
$params['userId'] = $this->getSanitizer()->getInt('userId', $filterBy);
141
$params['userId'] = Sanitize::getInt('userId', $filterBy);
275
if ($this->getSanitizer()->getInt('isUserSpecific', $filterBy) != -1) {
144
if (Sanitize::getInt('isUserSpecific', $filterBy) != -1) {
276
145
$body .= ' AND isUserSpecific = :isUserSpecific ';
277
$params['isUserSpecific'] = $this->getSanitizer()->getInt('isUserSpecific', 0, $filterBy);
146
$params['isUserSpecific'] = Sanitize::getInt('isUserSpecific', 0, $filterBy);
280
if ($this->getSanitizer()->getInt('isEveryone', $filterBy) != -1) {
149
if (Sanitize::getInt('isEveryone', $filterBy) != -1) {
281
150
$body .= ' AND isEveryone = :isEveryone ';
282
$params['isEveryone'] = $this->getSanitizer()->getInt('isEveryone', 0, $filterBy);
285
if ($this->getSanitizer()->getInt('isSystemNotification', $filterBy) !== null) {
286
$body .= ' AND isSystemNotification = :isSystemNotification ';
287
$params['isSystemNotification'] = $this->getSanitizer()->getInt('isSystemNotification', $filterBy);
290
if (DBVERSION >= 134 && $this->getSanitizer()->getInt('isDisplayNotification', $filterBy) !== null) {
291
$body .= ' AND isDisplayNotification = :isDisplayNotification ';
292
$params['isDisplayNotification'] = $this->getSanitizer()->getInt('isDisplayNotification', $filterBy);
295
if ($this->getSanitizer()->getInt('notificationId', $filterBy) !== null) {
296
$body .= ' AND `group`.groupId IN (SELECT groupId FROM `lknotificationgroup` WHERE notificationId = :notificationId) ';
297
$params['notificationId'] = $this->getSanitizer()->getInt('notificationId', $filterBy);
300
if ($this->getSanitizer()->getInt('displayGroupId', $filterBy) !== null) {
302
AND `group`.groupId IN (
303
SELECT DISTINCT `permission`.groupId
305
INNER JOIN `permissionentity`
306
ON `permissionentity`.entityId = permission.entityId
307
AND `permissionentity`.entity = \'Xibo\\Entity\\DisplayGroup\'
308
WHERE `permission`.objectId = :displayGroupId
309
AND `permission`.view = 1
312
$params['displayGroupId'] = $this->getSanitizer()->getInt('displayGroupId', $filterBy);
151
$params['isEveryone'] = Sanitize::getInt('isEveryone', 0, $filterBy);
322
if ($filterBy !== null && $this->getSanitizer()->getInt('start', $filterBy) !== null && $this->getSanitizer()->getInt('length', $filterBy) !== null) {
323
$limit = ' LIMIT ' . intval($this->getSanitizer()->getInt('start', $filterBy), 0) . ', ' . $this->getSanitizer()->getInt('length', 10, $filterBy);
161
if (Sanitize::getInt('start', $filterBy) !== null && Sanitize::getInt('length', $filterBy) !== null) {
162
$limit = ' LIMIT ' . intval(Sanitize::getInt('start'), 0) . ', ' . Sanitize::getInt('length', 10);
326
165
$sql = $select . $body . $order . $limit;
328
foreach ($this->getStore()->select($sql, $params) as $row) {
329
$entries[] = $this->createEmpty()->hydrate($row);
167
Log::sql($sql, $params);
169
foreach (PDOConnect::select($sql, $params) as $row) {
170
$entries[] = (new UserGroup())->hydrate($row);
333
174
if ($limit != '' && count($entries) > 0) {
334
$results = $this->getStore()->select('SELECT COUNT(*) AS total ' . $body, $params);
335
$this->_countLast = intval($results[0]['total']);
175
$results = PDOConnect::select('SELECT COUNT(*) AS total ' . $body, $params);
176
self::$_countLast = intval($results[0]['total']);
340
181
} catch (\Exception $e) {
342
$this->getLog()->error($e);