Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
usersubordinate.php
1<?php
10
11
13{
14 public const
21
22 private $userId;
23
24 private static $cache = [];
25
26 private static $structure;
27
28 public static function getDepartmentsByUserId(int $userId): array
29 {
30 $key = 'DEP_'.$userId;
31 if (!array_key_exists($key, static::$cache))
32 {
33 $res = \Bitrix\Main\UserTable::getList(
34 [
35 'filter' => [
36 '=ID' => $userId,
37 ],
38 'select' => ['UF_DEPARTMENT']
39 ]
40 );
41
42 $departments = [];
43 while ($row = $res->fetch())
44 {
45 if (is_array($row['UF_DEPARTMENT']))
46 {
47 $departments = array_merge($departments, $row['UF_DEPARTMENT']);
48 }
49 }
50
51 static::$cache[$key] = $departments;
52 }
53
54 return static::$cache[$key];
55 }
56
57 public function __construct(int $userId)
58 {
59 $this->userId = $userId;
60 }
61
62 public function getSubordinate(int $userId): int
63 {
64 if ($this->userId === $userId)
65 {
67 }
68
69 if (!\CModule::IncludeModule('intranet'))
70 {
72 }
73
74 $key = 'SUB_' . $this->userId .'_'. $userId;
75 if (
76 array_key_exists($key, static::$cache)
77 )
78 {
79 return static::$cache[$key];
80 }
81
82 $managers = self::getAllManagers();
83
84 $selfDepartments = self::getUserDepartments($this->userId);
85 $foreignDepartments = self::getUserDepartments($userId);
86
87 $inDepartment = !empty(array_intersect($selfDepartments, $foreignDepartments));
88
89 $selfManagers = $this->getDepartmentsManagers($selfDepartments);
90 $foreignManagers = $this->getDepartmentsManagers($foreignDepartments, true);
91
92 if (in_array($this->userId, $foreignManagers))
93 {
94 static::$cache[$key] = self::RELATION_SUBORDINATE;
95 }
96 elseif (in_array($userId, $selfManagers))
97 {
98 static::$cache[$key] = self::RELATION_DIRECTOR;
99 }
100 elseif ($inDepartment)
101 {
102 static::$cache[$key] = self::RELATION_DEPARTMENT;
103 }
104 elseif (in_array($userId, $managers))
105 {
106 static::$cache[$key] = self::RELATION_OTHER_DIRECTOR;
107 }
108 else
109 {
110 static::$cache[$key] = self::RELATION_OTHER;
111 }
112
113 return static::$cache[$key];
114 }
115
116 private static function getParentDepartments(int $id)
117 {
118 $structure = self::getStructure();
119 $res = [];
120 foreach ($structure['TREE'] as $parent => $childs)
121 {
122 if (!is_array($childs))
123 {
124 continue;
125 }
126 if ($parent > 0 && in_array($id, $childs))
127 {
128 $res[] = $parent;
129 $res = array_merge($res, self::getParentDepartments($parent));
130 }
131 }
132 return $res;
133 }
134
135 private function getDepartmentsManagers($departments, bool $recursive = false): array
136 {
137 $managersIds = [];
138
139 $deps = [];
140 foreach ($departments as $depId)
141 {
142 $deps[] = $depId;
143 if ($recursive)
144 {
145 $deps = array_merge($deps, self::getParentDepartments($depId));
146 }
147 }
148 $deps = array_unique($deps);
149
150 $structure = self::getStructure();
151 foreach ($structure['DATA'] as $row)
152 {
153 if (in_array($row['ID'], $deps))
154 {
155 $id = (int) $row['UF_HEAD'];
156 $managersIds[$id] = $id;
157 }
158 }
159
160 return $managersIds;
161 }
162
163 private static function getStructure()
164 {
165 if (!static::$structure)
166 {
167 static::$structure = \CIntranetUtils::GetStructure();
168 }
169 return static::$structure;
170 }
171
172 private static function getAllManagers(): array
173 {
174 $managers = [];
175
176 $structure = self::getStructure();
177 foreach ($structure['DATA'] as $row)
178 {
179 $managers[$row['ID']] = $row['UF_HEAD'];
180 }
181
182 return $managers;
183 }
184
185 private static function getUserDepartments(int $userId)
186 {
187 $structure = self::getStructure();
188 $departments = [];
189 foreach ($structure['DATA'] as $row)
190 {
191 if (in_array($userId, $row['EMPLOYEES']))
192 {
193 $departments[$row['ID']] = $row['ID'];
194 }
195 }
196 return $departments;
197 }
198}