PageRenderTime 26ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/bizproc/lib/service/user.php

https://gitlab.com/alexprowars/bitrix
PHP | 150 lines | 125 code | 25 blank | 0 comment | 7 complexity | 9642363e7b6797f04b207b1843d7d406 MD5 | raw file
  1. <?php
  2. namespace Bitrix\Bizproc\Service;
  3. use Bitrix\Main;
  4. class User extends \CBPRuntimeService
  5. {
  6. protected const DEPARTMENT_MODULE_ID = 'intranet';
  7. protected const DEPARTMENT_OPTION_NAME = 'iblock_structure';
  8. public function getUserDepartments(int $userId): array
  9. {
  10. $departments = [];
  11. $result = \CUser::getList(
  12. 'id', 'asc',
  13. ['ID_EQUAL_EXACT' => $userId],
  14. ['FIELDS' => ['ID'], 'SELECT' => ['UF_DEPARTMENT']]
  15. );
  16. if ($user = $result->fetch())
  17. {
  18. if (isset($user['UF_DEPARTMENT']))
  19. {
  20. $user['UF_DEPARTMENT'] = (array) $user['UF_DEPARTMENT'];
  21. foreach ($user['UF_DEPARTMENT'] as $dpt)
  22. {
  23. $departments[] = (int) $dpt;
  24. }
  25. }
  26. }
  27. return $departments;
  28. }
  29. public function getUserInfo(int $userId): ?array
  30. {
  31. $dbUsers = \CUser::GetList(
  32. 'id', 'asc',
  33. ['ID_EQUAL_EXACT' => $userId],
  34. [
  35. 'FIELDS' => ['ID', 'EMAIL'],
  36. 'SELECT' => [
  37. 'EMAIL',
  38. 'UF_SKYPE',
  39. 'UF_TWITTER',
  40. 'UF_FACEBOOK',
  41. 'UF_LINKEDIN',
  42. 'UF_XING',
  43. 'UF_WEB_SITES',
  44. 'UF_PHONE_INNER',
  45. ]
  46. ]
  47. );
  48. $info = is_object($dbUsers) ? $dbUsers->fetch() : null;
  49. return is_array($info) ? $info : null;
  50. }
  51. public function getUserDepartmentChains(int $userId): array
  52. {
  53. $chains = [];
  54. foreach ($this->getUserDepartments($userId) as $departmentId)
  55. {
  56. $chains[] = $this->getDepartmentChain($departmentId);
  57. }
  58. return $chains;
  59. }
  60. public function getDepartmentChain(int $departmentId): array
  61. {
  62. $chain = [];
  63. if (!$this->canUseIblockApi())
  64. {
  65. return $chain;
  66. }
  67. $departmentIblockId = $this->getDepartmentIblockId();
  68. $pathResult = \CIBlockSection::getNavChain($departmentIblockId, $departmentId);
  69. while ($path = $pathResult->fetch())
  70. {
  71. $chain[] = (int) $path['ID'];
  72. }
  73. return array_reverse($chain);
  74. }
  75. public function getUserHeads(int $userId): array
  76. {
  77. $heads = [];
  78. $userDepartments = $this->getUserDepartmentChains($userId);
  79. foreach ($userDepartments as $chain)
  80. {
  81. foreach ($chain as $deptId)
  82. {
  83. $departmentHead = $this->getDepartmentHead($deptId);
  84. if (!$departmentHead || $departmentHead === $userId)
  85. {
  86. continue;
  87. }
  88. $heads[] = $departmentHead;
  89. break;
  90. }
  91. }
  92. return array_unique($heads);
  93. }
  94. public function getDepartmentHead(int $departmentId): ?int
  95. {
  96. if (!$this->canUseIblockApi())
  97. {
  98. return null;
  99. }
  100. $departmentIblockId = $this->getDepartmentIblockId();
  101. $sectionResult = \CIBlockSection::GetList(
  102. [],
  103. ['IBLOCK_ID' => $departmentIblockId, 'ID' => $departmentId],
  104. false,
  105. ['ID', 'UF_HEAD']
  106. );
  107. $section = $sectionResult->fetch();
  108. return $section ? (int) $section['UF_HEAD'] : null;
  109. }
  110. public function getUserSchedule(int $userId): Sub\UserSchedule
  111. {
  112. return new Sub\UserSchedule($userId);
  113. }
  114. protected function getDepartmentIblockId(): int
  115. {
  116. return (int) Main\Config\Option::get(
  117. static::DEPARTMENT_MODULE_ID,
  118. static::DEPARTMENT_OPTION_NAME
  119. );
  120. }
  121. private function canUseIblockApi()
  122. {
  123. return Main\Loader::includeModule('iblock');
  124. }
  125. }