Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
department.php
1<?php
2namespace Bitrix\Im;
3
5{
6 public static function getColleagues($userId = null, $options = array())
7 {
8 $userId = \Bitrix\Im\Common::getUserId($userId);
9 if (!$userId)
10 {
11 return false;
12 }
13
14 $pagination = isset($options['LIST'])? true: false;
15
16 $limit = isset($options['LIST']['LIMIT'])? intval($options['LIST']['LIMIT']): 50;
17 $offset = isset($options['LIST']['OFFSET'])? intval($options['LIST']['OFFSET']): 0;
18
19 $list = Array();
20
21 $departments = \Bitrix\Im\User::getInstance($userId)->getDepartments();
22 $managers = self::getManagers($departments);
23 foreach ($managers as $departmentId => $users)
24 {
25 foreach ($users as $uid)
26 {
27 if ($userId == $uid)
28 continue;
29
30 $list[$uid] = $uid;
31 }
32 }
33
34 $employees = self::getEmployees($departments);
35 foreach ($employees as $departmentId => $users)
36 {
37 foreach ($users as $uid)
38 {
39 if ($userId == $uid)
40 continue;
41
42 $list[$uid] = $uid;
43 }
44 }
45
46 $result = self::getDepartmentYouManage($userId);
47 if (!empty($result))
48 {
49 $managers = self::getManagers(null);
50 foreach ($managers as $departmentId => $users)
51 {
52 foreach ($users as $uid)
53 {
54 if ($userId == $uid)
55 continue;
56
57 $list[$uid] = $uid;
58 }
59 }
60 }
61
62 $count = count($list);
63
64 $list = array_slice($list, $offset, $limit);
65
66 $jsonOption = $options['JSON'] ?? null;
67 $userDataOption = $options['USER_DATA'] ?? null;
68
69 if ($userDataOption === 'Y')
70 {
71 $result = Array();
72
73 $getOptions = Array();
74 if ($jsonOption === 'Y')
75 {
76 $getOptions['JSON'] = 'Y';
77 }
78
79 foreach ($list as $userId)
80 {
81 $result[] = \Bitrix\Im\User::getInstance($userId)->getArray($getOptions);
82 }
83 }
84 else
85 {
86 $result = array_values($list);
87 }
88
89
90 if ($jsonOption === 'Y')
91 {
92 $result = $pagination? Array('total' => $count, 'result' => $result): $result;
93 }
94 else
95 {
96 $result = $pagination? Array('TOTAL' => $count, 'RESULT' => $result): $result;
97 }
98
99 return $result;
100 }
101
102 public static function getDepartmentYouManage($userId = null, $options = array())
103 {
104 $userId = \Bitrix\Im\Common::getUserId($userId);
105 if (!$userId)
106 {
107 return false;
108 }
109
110 $jsonOption = $options['JSON'] ?? null;
111 $userDataOption = $options['USER_DATA'] ?? null;
112
113 $list = \Bitrix\Im\Integration\Intranet\Department::getList();
114
115 $result = Array();
116 foreach ($list as $key => $department)
117 {
118 if ((int)$department['MANAGER_USER_ID'] !== $userId)
119 {
120 continue;
121 }
122 if ($userDataOption === 'Y')
123 {
124 $userData = \Bitrix\Im\User::getInstance($department['MANAGER_USER_ID']);
125 $department['MANAGER_USER_DATA'] = $jsonOption === 'Y'? $userData->getArray(Array('JSON' => 'Y')): $userData;
126 }
127
128 $result[$key] = $jsonOption === 'Y'? array_change_key_case($department, CASE_LOWER): $department;
129 }
130
131 if ($jsonOption === 'Y')
132 {
133 $result = array_values($result);
134 }
135
136 return $result;
137 }
138
139 public static function getStructure($options = array())
140 {
141 $list = \Bitrix\Im\Integration\Intranet\Department::getList();
142
143 if (isset($options['FILTER']['ID']))
144 {
145 foreach ($list as $key => $department)
146 {
147 if (!in_array($department['ID'], $options['FILTER']['ID']))
148 {
149 unset($list[$key]);
150 }
151 }
152 }
153
154 $pagination = isset($options['LIST'])? true: false;
155
156 $limit = isset($options['LIST']['LIMIT'])? intval($options['LIST']['LIMIT']): 50;
157 $offset = isset($options['LIST']['OFFSET'])? intval($options['LIST']['OFFSET']): 0;
158
159 if (isset($options['FILTER']['SEARCH']) && mb_strlen($options['FILTER']['SEARCH']) > 1)
160 {
161 $count = 0;
162 $breakAfterDigit = $offset === 0? $offset: false;
163
164 $options['FILTER']['SEARCH'] = ToLower($options['FILTER']['SEARCH']);
165 foreach ($list as $key => $department)
166 {
167 $checkField = ToLower($department['FULL_NAME']);
168 if (
169 mb_strpos($checkField, $options['FILTER']['SEARCH']) !== 0
170 && mb_strpos($checkField, ' '.$options['FILTER']['SEARCH']) === false
171 )
172 {
173 unset($list[$key]);
174 }
175 if ($breakAfterDigit !== false)
176 {
177 $count++;
178 if ($count === $breakAfterDigit)
179 {
180 break;
181 }
182 }
183 }
184 }
185
186 $count = count($list);
187
188 $list = array_slice($list, $offset, $limit);
189
190 if ($options['JSON'] == 'Y' || $options['USER_DATA'] == 'Y')
191 {
192 if ($options['JSON'] == 'Y')
193 {
194 $list = array_values($list);
195 }
196 foreach ($list as $key => $department)
197 {
198 if ($options['USER_DATA'] == 'Y')
199 {
200 $userData = \Bitrix\Im\User::getInstance($department['MANAGER_USER_ID']);
201 $department['MANAGER_USER_DATA'] = $options['JSON'] == 'Y'? $userData->getArray(Array('JSON' => 'Y')): $userData;
202 }
203
204 $list[$key] = $options['JSON'] == 'Y'? array_change_key_case($department, CASE_LOWER): $department;
205 }
206 }
207
208 if ($options['JSON'] == 'Y')
209 {
210 $list = $pagination? Array('total' => $count, 'result' => $list): $list;
211 }
212 else
213 {
214 $list = $pagination? Array('TOTAL' => $count, 'RESULT' => $list): $list;
215 }
216
217 return $list;
218 }
219
220 public static function getManagers($ids = null, $options = array())
221 {
222 $list = \Bitrix\Im\Integration\Intranet\Department::getList();
223
224 $userOptions = Array();
225 $jsonOption = $options['JSON'] ?? null;
226 $userDataOption = $options['USER_DATA'] ?? null;
227
228 if ($jsonOption)
229 {
230 $userOptions['JSON'] = 'Y';
231 }
232
233 $managers = Array();
234 foreach ($list as $department)
235 {
236 if ($department['MANAGER_USER_ID'] <= 0)
237 continue;
238
239 if (is_array($ids) && !in_array($department['ID'], $ids))
240 continue;
241
242 if ($userDataOption === 'Y')
243 {
244 $managers[$department['ID']][] = \Bitrix\Im\User::getInstance($department['MANAGER_USER_ID'])->getArray($userOptions);
245 }
246 else
247 {
248 $managers[$department['ID']][] = $department['MANAGER_USER_ID'];
249 }
250 }
251
252 return $managers;
253 }
254
255 public static function getEmployeesList($ids = null, $options = array())
256 {
257 if (!\Bitrix\Main\Loader::includeModule('intranet'))
258 {
259 return Array();
260 }
261
262 $structure = \CIntranetUtils::GetStructure();
263 if (!$structure || !isset($structure['DATA']))
264 {
265 return Array();
266 }
267
268 $result = Array();
269 foreach ($structure['DATA'] as $department)
270 {
271 if (is_array($ids) && !in_array($department['ID'], $ids))
272 continue;
273
274 if (!is_array($department['EMPLOYEES']))
275 {
276 $result[$department['ID']] = Array();
277 continue;
278 }
279
280 foreach ($department['EMPLOYEES'] as $key => $value)
281 {
282 $department['EMPLOYEES'][$key] = (int)$value;
283 }
284
285 $result[$department['ID']] = $department['EMPLOYEES'];
286 }
287
288 return $result;
289 }
290
291 public static function getEmployees($ids = null, $options = array())
292 {
293 $list = self::getEmployeesList();
294
295 $userOptions = Array();
296
297 $jsonOption = $options['JSON'] ?? null;
298 $userDataOption = $options['USER_DATA'] ?? null;
299
300 if ($jsonOption)
301 {
302 $userOptions['JSON'] = 'Y';
303 }
304
305 $employees = Array();
306 foreach ($list as $departmentId => $users)
307 {
308 if (is_array($ids) && !in_array($departmentId, $ids))
309 continue;
310
311 foreach ($users as $employeeId)
312 {
313 if ($userDataOption === 'Y')
314 {
315 $employees[$departmentId][] = \Bitrix\Im\User::getInstance($employeeId)->getArray($userOptions);
316 }
317 else
318 {
319 $employees[$departmentId][] = $employeeId;
320 }
321 }
322 }
323
324 return $employees;
325 }
326}
static getColleagues($userId=null, $options=array())
Definition department.php:6
static getStructure($options=array())
static getEmployeesList($ids=null, $options=array())
static getEmployees($ids=null, $options=array())
static getDepartmentYouManage($userId=null, $options=array())
static getManagers($ids=null, $options=array())