| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168 |
- <?php
- dol_include_once('/documentfollower/class/documentfollowerprocessuser.class.php');
- require_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
- require_once DOL_DOCUMENT_ROOT.'/user/class/usergroup.class.php';
- class DfProcessUserHandler
- {
- public $processId;
- public $db;
- public $level;
- public function __construct(int $processId)
- {
- global $db;
-
- $this->db = $db;
- $this->processId = $processId;
- }
- /**
- *
- */
- public function isUserAssignedToProcess(int $userId): bool
- {
- $assigned = false;
- // assigned as user
- $assignedProcessUser = $this->getUserDataInProcess($userId);
- if ($assignedProcessUser) {
- $assigned = true;
- $this->level = $assignedProcessUser->level;
- }
- // assigned as group member
- if (!$assigned) {
- $rows = (new DocumentFollowerProcessUser($this->db))->fetchAll('ASC', 'level', 0, 0, [
- 'customsql' => 'fk_usergroup IS NOT NULL',
- 'fk_process' => $this->processId
- ]);
- if (is_array($rows) && !empty($rows)) {
- foreach ($rows as $row) {
- $group = new UserGroup($this->db);
- if ($group->fetch($row->fk_usergroup) > 0) {
- $users = $group->listUsersForGroup('', 1);
- if (in_array($userId, $users)) {
- $assigned = true;
- $this->level = $row->level;
- }
- }
- }
- }
- }
- return $assigned;
- }
- /**
- *
- */
- public function getUserDataInProcess(int $userId): ?DocumentFollowerProcessUser
- {
- $dfProcessUser = null;
- $groups = (new UserGroup($this->db))->listGroupsForUser($userId, false);
- $customSql = "fk_user={$userId}";
- if (!empty($groups)) {
- $customSql = "({$customSql} OR fk_usergroup IN (".implode(',', array_keys($groups))."))";
- }
- $condition = [
- 'fk_process' => $this->processId,
- 'customsql' => $customSql
- ];
- $users = (new DocumentFollowerProcessUser($this->db))->fetchAll('ASC', 'level', 1, 0, $condition);
- if (is_array($users)) {
- $dfProcessUser = reset($users);
- }
- return $dfProcessUser;
- }
- /**
- *
- */
- public function getLevelMembers(int $processId, int $level = null, array $excludedUserIds = []): array
- {
- $userIds = [];
- $filter = [
- 'fk_process' => $processId
- ];
- if ($level > 0) {
- $filter['level'] = $level;
- }
- if (!empty($excludedUserIds)) {
- $filter['customsql'] = 'fk_user NOT IN ('.implode(', ', $excludedUserIds).')';
- }
- $users = (new DocumentFollowerProcessUser($this->db))->fetchAll('ASC', 'level', 0, 0, $filter);
- if (is_array($users)) {
- foreach ($users as $user) {
- if ($user->fk_usergroup) {
- $group = new UserGroup($this->db);
- if ($group->fetch($user->fk_usergroup) > 0) {
- if (!empty($group->members)) {
- $memberIds = array_map(function ($member) {
- return intval($member->id);
- },
- $group->members
- );
- $userIds = array_merge($userIds, $memberIds);
- }
- }
- } else {
- $userIds[] = intval($user->fk_user);
- }
- }
- }
- if (!empty($excludedUserIds)) {
- $userIds = array_diff($userIds, $excludedUserIds);
- }
- return $userIds;
- }
- /**
- *
- */
- public function getLastLevel(): int
- {
- $level = 0;
- $users = (new DocumentFollowerProcessUser($this->db))->fetchAll('ASC', 'level', 0, 0, [
- 'fk_process' => $this->processId
- ]);
- if (is_array($users)) {
- $levels = array_map(function ($item) {
- return intval($item->level);
- }, $users);
- $level = max($levels);
- }
- return $level;
- }
- /**
- *
- */
- public function getUserGroupsWithMemberIds(User $user): array
- {
- $result = [];
- $groups = (new UserGroup($this->db))->listGroupsForUser($user->id, true);
- if (is_array($groups)) {
- foreach ($groups as $id => $group) {
- $result[$id] = (empty($group->members)) ? [] : array_keys($group->members);
- if (($key = array_search($user->id, $result[$id])) !== false) {
- unset($result[$id][$key]);
- }
- }
- }
- return $result;
- }
- }
|