dfprocessuserhandler.class.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. dol_include_once('/documentfollower/class/documentfollowerprocessuser.class.php');
  3. require_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
  4. require_once DOL_DOCUMENT_ROOT.'/user/class/usergroup.class.php';
  5. class DfProcessUserHandler
  6. {
  7. public $processId;
  8. public $db;
  9. public $level;
  10. public function __construct(int $processId)
  11. {
  12. global $db;
  13. $this->db = $db;
  14. $this->processId = $processId;
  15. }
  16. /**
  17. *
  18. */
  19. public function isUserAssignedToProcess(int $userId): bool
  20. {
  21. $assigned = false;
  22. // assigned as user
  23. $assignedProcessUser = $this->getUserDataInProcess($userId);
  24. if ($assignedProcessUser) {
  25. $assigned = true;
  26. $this->level = $assignedProcessUser->level;
  27. }
  28. // assigned as group member
  29. if (!$assigned) {
  30. $rows = (new DocumentFollowerProcessUser($this->db))->fetchAll('ASC', 'level', 0, 0, [
  31. 'customsql' => 'fk_usergroup IS NOT NULL',
  32. 'fk_process' => $this->processId
  33. ]);
  34. if (is_array($rows) && !empty($rows)) {
  35. foreach ($rows as $row) {
  36. $group = new UserGroup($this->db);
  37. if ($group->fetch($row->fk_usergroup) > 0) {
  38. $users = $group->listUsersForGroup('', 1);
  39. if (in_array($userId, $users)) {
  40. $assigned = true;
  41. $this->level = $row->level;
  42. }
  43. }
  44. }
  45. }
  46. }
  47. return $assigned;
  48. }
  49. /**
  50. *
  51. */
  52. public function getUserDataInProcess(int $userId): ?DocumentFollowerProcessUser
  53. {
  54. $dfProcessUser = null;
  55. $groups = (new UserGroup($this->db))->listGroupsForUser($userId, false);
  56. $customSql = "fk_user={$userId}";
  57. if (!empty($groups)) {
  58. $customSql = "({$customSql} OR fk_usergroup IN (".implode(',', array_keys($groups))."))";
  59. }
  60. $condition = [
  61. 'fk_process' => $this->processId,
  62. 'customsql' => $customSql
  63. ];
  64. $users = (new DocumentFollowerProcessUser($this->db))->fetchAll('ASC', 'level', 1, 0, $condition);
  65. if (is_array($users)) {
  66. $dfProcessUser = reset($users);
  67. }
  68. return $dfProcessUser;
  69. }
  70. /**
  71. *
  72. */
  73. public function getLevelMembers(int $processId, int $level = null, array $excludedUserIds = []): array
  74. {
  75. $userIds = [];
  76. $filter = [
  77. 'fk_process' => $processId
  78. ];
  79. if ($level > 0) {
  80. $filter['level'] = $level;
  81. }
  82. if (!empty($excludedUserIds)) {
  83. $filter['customsql'] = 'fk_user NOT IN ('.implode(', ', $excludedUserIds).')';
  84. }
  85. $users = (new DocumentFollowerProcessUser($this->db))->fetchAll('ASC', 'level', 0, 0, $filter);
  86. if (is_array($users)) {
  87. foreach ($users as $user) {
  88. if ($user->fk_usergroup) {
  89. $group = new UserGroup($this->db);
  90. if ($group->fetch($user->fk_usergroup) > 0) {
  91. if (!empty($group->members)) {
  92. $memberIds = array_map(function ($member) {
  93. return intval($member->id);
  94. },
  95. $group->members
  96. );
  97. $userIds = array_merge($userIds, $memberIds);
  98. }
  99. }
  100. } else {
  101. $userIds[] = intval($user->fk_user);
  102. }
  103. }
  104. }
  105. if (!empty($excludedUserIds)) {
  106. $userIds = array_diff($userIds, $excludedUserIds);
  107. }
  108. return $userIds;
  109. }
  110. /**
  111. *
  112. */
  113. public function getLastLevel(): int
  114. {
  115. $level = 0;
  116. $users = (new DocumentFollowerProcessUser($this->db))->fetchAll('ASC', 'level', 0, 0, [
  117. 'fk_process' => $this->processId
  118. ]);
  119. if (is_array($users)) {
  120. $levels = array_map(function ($item) {
  121. return intval($item->level);
  122. }, $users);
  123. $level = max($levels);
  124. }
  125. return $level;
  126. }
  127. /**
  128. *
  129. */
  130. public function getUserGroupsWithMemberIds(User $user): array
  131. {
  132. $result = [];
  133. $groups = (new UserGroup($this->db))->listGroupsForUser($user->id, true);
  134. if (is_array($groups)) {
  135. foreach ($groups as $id => $group) {
  136. $result[$id] = (empty($group->members)) ? [] : array_keys($group->members);
  137. if (($key = array_search($user->id, $result[$id])) !== false) {
  138. unset($result[$id][$key]);
  139. }
  140. }
  141. }
  142. return $result;
  143. }
  144. }