25 $order = [
'ID' =>
'DESC'],
31 $query = UserToGroupTable::query();
33 ->setSelect($this->prepareQuerySelect($select))
36 ->setLimit(($pageNavigation->
getLimit()))
39 $query = $this->processFilter($query, $filter);
41 $res = $query->exec();
42 while ($relation = $res->fetch())
44 $relation[
'FORMATTED_USER_NAME'] = \CUser::FormatName(
45 \CSite::getNameFormat(),
47 'NAME' => $relation[
'USER_NAME'],
48 'LAST_NAME' => $relation[
'USER_LAST_NAME'],
49 'SECOND_NAME' => $relation[
'USER_SECOND_NAME'],
50 'LOGIN' => $relation[
'USER_LOGIN'],
54 $relations[$relation[
'ID']] = $relation;
57 if (!empty($relations))
59 if (in_array(
'USER_PERSONAL_PHOTO', $select,
true))
61 $relations = $this->fillUserAvatars($relations);
63 if (in_array(
'ACTIONS', $select,
true))
65 $relations = $this->fillActions($relations);
68 $relations = $this->convertKeysToCamelCase($relations);
73 private function prepareQuerySelect(array $select): array
75 $userToGroupFields = [
84 'INITIATED_BY_USER_ID',
96 'USER_PERSONAL_PHOTO',
97 'USER_PERSONAL_GENDER',
100 $allowedFields = array_merge($userToGroupFields, $userFields);
101 $prepared = array_intersect($select, $allowedFields);
103 foreach ($prepared as $field)
105 if (in_array($field, $userFields,
true))
107 $prepared[$field] =
'USER.' . str_replace(
'USER_',
'', $field);
108 unset($prepared[array_search($field, $prepared,
true)]);
115 private function processFilter(Query $query, array $filter): Query
117 if (array_key_exists(
'ID', $filter))
119 $ids = (is_array($filter[
'ID']) ? $filter[
'ID'] : [$filter[
'ID']]);
120 $ids = array_map(
'intval', $ids);
121 $ids = array_filter($ids);
126 ? $query->whereIn(
'ID', $ids)
127 : $query->where(
'ID', $ids[0])
132 if (array_key_exists(
'GROUP_ID', $filter))
134 $query->where(
'GROUP_ID', (
int)$filter[
'GROUP_ID']);
137 if (array_key_exists(
'ROLE', $filter))
139 $roles = (is_array($filter[
'ROLE']) ? $filter[
'ROLE'] : [$filter[
'ROLE']]);
140 $roles = array_filter($roles);
144 if (array_key_exists(
'INVITED_BY_ME', $filter) && $filter[
'INVITED_BY_ME'] ===
'Y')
149 ->whereIn(
'ROLE', $roles)
154 ->where(
'INITIATED_BY_USER_ID', $this->getCurrentUser()->getId())
160 $query->whereIn(
'ROLE', $roles);
163 else if (array_key_exists(
'INVITED_BY_ME', $filter) && $filter[
'INVITED_BY_ME'] ===
'Y')
169 ->where(
'INITIATED_BY_USER_ID', $this->getCurrentUser()->getId())
174 if (array_key_exists(
'INITIATED_BY_TYPE', $filter))
176 $query->where(
'INITIATED_BY_TYPE', $filter[
'INITIATED_BY_TYPE']);
179 if (array_key_exists(
'INITIATED_BY_USER_ID', $filter))
181 $query->where(
'INITIATED_BY_USER_ID', $filter[
'INITIATED_BY_USER_ID']);
184 if (array_key_exists(
'SEARCH_INDEX', $filter) && trim($filter[
'SEARCH_INDEX']) !==
'')
187 'USER.INDEX.SEARCH_ADMIN_CONTENT',
188 Filter\Helper::matchAgainstWildcard(Content::prepareStringToken(trim($filter[
'SEARCH_INDEX'])))
195 private function fillUserAvatars(array $relations): array
197 foreach (array_keys($relations) as $id)
199 $relations[$id][
'IMAGE'] =
'';
202 $imageIds = array_filter(
203 array_column($relations,
'USER_PERSONAL_PHOTO',
'ID'),
204 static function ($id) {
209 $avatars = File::getFilesSources($imageIds);
210 $imageIds = array_flip($imageIds);
212 foreach ($imageIds as $imageId => $relationId)
214 $relations[$relationId][
'IMAGE'] = $avatars[$imageId];
220 private function fillActions(array $relations): array
222 $userId = (int)$this->getCurrentUser()->getId();
225 foreach ($relations as $id => $relation)
227 $projectId = (int)$relation[
'GROUP_ID'];
229 if (!array_key_exists($projectId, $permissions))
231 $permissions[$projectId] = Helper\Workgroup::getPermissions([
'groupId' => $projectId]);
234 $projectPermissions = $permissions[$projectId];
235 $canModifyGroup = $projectPermissions[
'UserCanModifyGroup'];
236 $canInitiate = $projectPermissions[
'UserCanInitiate'];
237 $canProcessRequestsIn = $projectPermissions[
'UserCanProcessRequestsIn'];
239 $role = $relation[
'ROLE'];
240 $memberId = (int)$relation[
'USER_ID'];
241 $isAutoMember = ($relation[
'AUTO_MEMBER'] ===
'Y');
242 $initiatedByType = $relation[
'INITIATED_BY_TYPE'];
244 $relations[$id][
'ACTIONS'] = [
247 && $projectPermissions[
'UserIsOwner']
254 'REMOVE_MODERATOR' => (
261 && $memberId !== $userId
274 'ACCEPT_REQUEST' => (
275 $canProcessRequestsIn
280 $canProcessRequestsIn
292 $userId = (int)(isset($params[
'userId']) && (int)$params[
'userId'] > 0 ? $params[
'userId'] : $this->getCurrentUser()->getId());
293 $groupId = (int)(isset($params[
'groupId']) && (int)$params[
'groupId'] > 0 ? $params[
'groupId'] : 0);
297 $this->
addError(
new Error(
'No User Id',
'SONET_CONTROLLER_USERTOGROUP_NO_USER_ID'));
303 $this->
addError(
new Error(
'No Workgroup',
'SONET_CONTROLLER_USERTOGROUP_NO_GROUP'));
307 if (!Loader::includeModule(
'socialnetwork'))
309 $this->
addError(
new Error(
'Cannot include Socialnetwork module',
'SONET_CONTROLLER_USERTOGROUP_NO_SOCIALNETWORK_MODULE'));
314 !\CSocNetUser::isCurrentUserModuleAdmin(SITE_ID,
false)
315 && $userId !== (
int)$this->getCurrentUser()->getId()
318 $this->
addError(
new Error(
'No permissions',
'SONET_CONTROLLER_USERTOGROUP_NO_PERMISSIONS'));
324 $confirmationNeeded = Helper\Workgroup::join([
325 'groupId' => $groupId,
334 if ($confirmationNeeded)
339 EventDictionary::EVENT_WORKGROUP_MEMBER_REQUEST_CONFIRM,
341 'GROUP_ID' => $groupId,
342 'RECEPIENTS' => array_map(
function ($row) {
return $row[
'USER_ID']; }, $moderators),
349 'confirmationNeeded' => $confirmationNeeded,
358 $userId = $this->getCurrentUser()->getId();
359 $groupId = (int)(isset($params[
'groupId']) && (int)$params[
'groupId'] > 0 ? $params[
'groupId'] : 0);
363 $this->
addError(
new Error(
'No User Id',
'SONET_CONTROLLER_USERTOGROUP_NO_USER_ID'));
369 $this->
addError(
new Error(
'No Workgroup',
'SONET_CONTROLLER_USERTOGROUP_NO_GROUP'));
373 if (!Loader::includeModule(
'socialnetwork'))
375 $this->
addError(
new Error(
'Cannot include Socialnetwork module',
'SONET_CONTROLLER_USERTOGROUP_NO_SOCIALNETWORK_MODULE'));
381 Helper\Workgroup::leave([
382 'groupId' => $groupId,
399 return Helper\Workgroup::setOwner([
401 'groupId' => $groupId,
407 return Helper\Workgroup::setModerator([
409 'groupId' => $groupId,
415 return Helper\Workgroup::removeModerator([
417 'groupId' => $groupId,
423 return Helper\Workgroup::setModerators([
424 'userIds' => $userIds,
425 'groupId' => $groupId,
431 return Helper\Workgroup::exclude([
433 'groupId' => $groupId,
439 return \CSocNetUserToGroup::SendRequestToJoinGroup(
440 $this->getCurrentUser()->getId(),
449 return Helper\Workgroup::deleteOutgoingRequest([
451 'groupId' => $groupId,
457 return Helper\Workgroup::deleteIncomingRequest([
459 'groupId' => $groupId,
465 $userId = $this->getCurrentUser()->getId();
469 return Helper\Workgroup::acceptOutgoingRequest([
470 'groupId' => $groupId,
484 $userId = $this->getCurrentUser()->getId();
488 return Helper\Workgroup::rejectOutgoingRequest([
489 'groupId' => $groupId,
503 return \CSocNetUserToGroup::ConfirmRequestToBeMember(
504 $this->getCurrentUser()->getId(),
512 return \CSocNetUserToGroup::RejectRequestToBeMember(
513 $this->getCurrentUser()->getId(),
521 $result = Helper\UserToGroup\RequestPopup::setHideRequestPopup([
522 'groupId' => $groupId,
523 'userId' => (
int)$this->getCurrentUser()->getId(),
528 $this->
addError(
new Error(
'Cannot process operation',
'SONET_CONTROLLER_USERTOGROUP_SET_HIDE_REQUEST_POPUP_ERROR'));