Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
userutils.php
1<?php
8namespace Bitrix\Main;
9
11
13{
18 public static function getUserSearchFilter(array $fields)
19 {
20 $result = array();
21
22 if (UserIndexTable::getEntity()->fullTextIndexEnabled('SEARCH_USER_CONTENT'))
23 {
24 $find = '';
25 $findDepartmentOnly = false;
26
27 if (array_key_exists('FIND', $fields))
28 {
29 $find = trim($fields['FIND']);
30
31 if (Search\Content::isIntegerToken($find))
32 {
33 $find = Search\Content::prepareIntegerToken($find);
34 }
35 else
36 {
37 $find = Search\Content::prepareStringToken($find);
38 }
39 }
40 else
41 {
42 $validFields = Array('ID' => 1, 'NAME' => 1, 'LAST_NAME' => 1, 'SECOND_NAME' => 1, 'WORK_POSITION' => 1);
43 foreach ($fields as $key => $value)
44 {
45 if (isset($validFields[$key]) && $validFields[$key])
46 {
47 if (Search\Content::isIntegerToken($value))
48 {
49 $find .= ' '.Search\Content::prepareIntegerToken($value);
50 }
51 else
52 {
53 $find .= ' '.Search\Content::prepareStringToken($value);
54 }
55 $find = trim($find);
56 }
57 }
58
59 if (array_key_exists('UF_DEPARTMENT_NAME', $fields))
60 {
61 if (!$find)
62 {
63 $findDepartmentOnly = true;
64 }
65 if (Search\Content::isIntegerToken($fields['UF_DEPARTMENT_NAME']))
66 {
67 $find .= ' '.Search\Content::prepareIntegerToken($fields['UF_DEPARTMENT_NAME']);
68 }
69 else
70 {
71 $find .= ' '.Search\Content::prepareStringToken($fields['UF_DEPARTMENT_NAME']);
72 }
73 $find = trim($find);
74 }
75 }
76
77 if (Search\Content::canUseFulltextSearch($find, Search\Content::TYPE_MIXED))
78 {
79 $fiendField = $findDepartmentOnly? '*INDEX.SEARCH_DEPARTMENT_CONTENT': '*INDEX.SEARCH_USER_CONTENT';
80 $result[$fiendField] = $find;
81 }
82 }
83 else
84 {
85 $helper = Application::getConnection()->getSqlHelper();
86 if (array_key_exists('FIND', $fields))
87 {
88 $find = trim($fields['FIND']);
89 $find = explode(' ', $find);
90 foreach ($find as $findWord)
91 {
92 if (!$findWord)
93 {
94 continue;
95 }
96
97 $intResult = Array('LOGIC' => 'OR');
98 $validFields = Array('ID', 'NAME', 'LAST_NAME', 'SECOND_NAME', 'WORK_POSITION', 'UF_DEPARTMENT_NAME');
99 foreach ($validFields as $key)
100 {
101 if ($key == 'ID')
102 {
103 $intResult['=ID'] = intval($findWord);
104 }
105 else
106 {
107 $intResult['%=INDEX.'.$key] = $helper->forSql($findWord).'%';
108 }
109 }
110 $result[] = $intResult;
111 }
112 if (!empty($result))
113 {
114 $result['LOGIC'] = 'AND';
115 $result = Array($result);
116 }
117 }
118 else
119 {
120 $validFields = Array('ID' => 1, 'NAME' => 1, 'LAST_NAME' => 1, 'SECOND_NAME' => 1, 'WORK_POSITION' => 1, 'UF_DEPARTMENT_NAME' => 1);
121 foreach ($fields as $key => $value)
122 {
123 if (!$value)
124 {
125 continue;
126 }
127 if (isset($validFields[$key]))
128 {
129 if ($key == 'ID')
130 {
131 $result['=ID'] = intval($value);
132 }
133 else
134 {
135 $result['%=INDEX.'.$key] = $helper->forSql($value).'%';
136 }
137 }
138 }
139 }
140 }
141
142 return $result;
143 }
144
149 public static function getAdminSearchFilter(array $fields)
150 {
151 $result = array();
152
153 if (UserIndexTable::getEntity()->fullTextIndexEnabled('SEARCH_ADMIN_CONTENT'))
154 {
155 $find = '';
156 if (array_key_exists('FIND', $fields))
157 {
158 $find = trim($fields['FIND']);
159
160 if (Search\Content::isIntegerToken($find))
161 {
162 $find = Search\Content::prepareIntegerToken($find);
163 }
164 else
165 {
166 $find = Search\Content::prepareStringToken($find);
167 }
168 }
169 else
170 {
171 $validFields = Array('ID' => 1, 'NAME' => 1, 'LAST_NAME' => 1, 'SECOND_NAME' => 1, 'WORK_POSITION' => 1, 'EMAIL' => 1, 'LOGIN' => 1);
172 foreach ($fields as $key => $value)
173 {
174 if (isset($validFields[$key]) && $validFields[$key])
175 {
176 if (Search\Content::isIntegerToken($value))
177 {
178 $find .= ' '.Search\Content::prepareIntegerToken($value);
179 }
180 else
181 {
182 $find .= ' '.Search\Content::prepareStringToken($value);
183 }
184 $find = trim($find);
185 }
186 }
187 }
188
189 if (Search\Content::canUseFulltextSearch($find, Search\Content::TYPE_MIXED))
190 {
191 $result['*INDEX.SEARCH_ADMIN_CONTENT'] = $find;
192 }
193 }
194 else
195 {
196 $helper = Application::getConnection()->getSqlHelper();
197 if (array_key_exists('FIND', $fields))
198 {
199 $find = trim($fields['FIND']);
200 $find = explode(' ', $find);
201 foreach ($find as $findWord)
202 {
203 if (!$findWord)
204 {
205 continue;
206 }
207
208 $intResult = Array('LOGIC' => 'OR');
209 $validFields = Array('ID', 'NAME', 'LAST_NAME', 'SECOND_NAME', 'WORK_POSITION', 'LOGIN', 'EMAIL');
210 foreach ($validFields as $key)
211 {
212 if ($key == 'ID')
213 {
214 $intResult['=ID'] = intval($findWord);
215 }
216 else if ($key == 'LOGIN' || $key == 'EMAIL')
217 {
218 $intResult['%='.$key] = $helper->forSql($findWord).'%';
219 }
220 else
221 {
222 $intResult['%=INDEX.'.$key] = $helper->forSql($findWord).'%';
223 }
224 }
225 $result[] = $intResult;
226 }
227 if (!empty($result))
228 {
229 $result['LOGIC'] = 'AND';
230 $result = Array($result);
231 }
232 }
233 else
234 {
235 $validFields = Array('ID' => 1, 'NAME' => 1, 'LAST_NAME' => 1, 'SECOND_NAME' => 1, 'WORK_POSITION' => 1, 'LOGIN' => 1, 'EMAIL' => 1);
236 foreach ($fields as $key => $value)
237 {
238 if (!$value)
239 {
240 continue;
241 }
242 if (isset($validFields[$key]))
243 {
244 if ($key == 'ID')
245 {
246 $result['=ID'] = intval($value);
247 }
248 else if ($key == 'LOGIN' || $key == 'EMAIL')
249 {
250 $result['%='.$key] = $helper->forSql($value).'%';
251 }
252 else
253 {
254 $result['%=INDEX.'.$key] = $helper->forSql($value).'%';
255 }
256 }
257 }
258 }
259 }
260
261 return $result;
262 }
263
264 public static function getGroupIds($userId)
265 {
266 return UserTable::getUserGroupIds($userId);
267 }
268
273 public static function getDepartmentName($departmentId)
274 {
275 $result = self::getDepartmentNames(Array($departmentId));
276 return $result[0] ?? false;
277 }
278
283 public static function getDepartmentNames($departmentIds)
284 {
285 $result = Array();
286 if (!ModuleManager::isModuleInstalled('intranet'))
287 {
288 return $result;
289 }
290
291 $cacheTtl = 2592000;
292 $cacheName = 'iblock_structure';
293 $cachePath = '/bx/user/company/structure';
294 $iblockStructureId = Config\Option::get('intranet', 'iblock_structure', 0);
295
296 $taggedCache = Application::getInstance()->getTaggedCache();
297
298 $companyStructure = Array();
299 $cache = Data\Cache::createInstance();
300 if ($cache->initCache($cacheTtl, $cacheName, $cachePath) && false)
301 {
302 $companyStructure = $cache->getVars();
303 }
304 else if ($iblockStructureId <= 0 || !Loader::includeModule('iblock'))
305 {
306 return $result;
307 }
308 else
309 {
310 $orm = \Bitrix\Iblock\SectionTable::getList(Array('select' => Array('ID', 'NAME', 'DEPTH_LEVEL', 'IBLOCK_SECTION_ID'), 'filter' => Array('=IBLOCK_ID' => $iblockStructureId, '=ACTIVE' => 'Y',),));
311 while ($department = $orm->fetch())
312 {
313 $companyStructure[$department['ID']] = $department;
314 }
315
316 $taggedCache->startTagCache($cachePath);
317 $taggedCache->registerTag('iblock_id_'.$iblockStructureId);
318 $taggedCache->endTagCache();
319
320 if ($cache->startDataCache())
321 {
322 $cache->endDataCache($companyStructure);
323 }
324 }
325
326 if (is_array($departmentIds) && !empty($departmentIds))
327 {
328 foreach ($departmentIds as $id)
329 {
330 if (!array_key_exists($id, $companyStructure))
331 {
332 continue;
333 }
334
335 $result[] = $companyStructure[$id];
336 }
337 }
338
339 return $result;
340 }
341
342 public static function getCountryValue(array $params = [])
343 {
344 static $countriesList = null;
345 $result = '';
346
347 $value = (isset($params['VALUE']) ? intval($params['VALUE']) : 0);
348 if ($value <= 0)
349 {
350 return $result;
351 }
352
353 if ($countriesList === null)
354 {
355 $countriesList = [];
356 $countries = getCountryArray();
357 foreach($countries['reference_id'] as $key => $countryId)
358 {
359 $countriesList[$countryId] = $countries['reference'][$key];
360 }
361 }
362
363 if (isset($countriesList[$value]))
364 {
365 $result = $countriesList[$value];
366 }
367
368 return $result;
369 }
370
371 public static function getUFContent($userId)
372 {
373 global $USER_FIELD_MANAGER;
374
375 static $supportedUserFieldTypeIDs = [
376 'address',
377 'string',
378 'integer',
379 'double',
380 'boolean',
381// 'date',
382// 'datetime',
383 'enumeration',
384 'employee',
385 'file',
386 'url',
387 'crm',
388 'crm_status',
389 'iblock_element',
390 'iblock_section'
391 ];
392
393 $ufList = $USER_FIELD_MANAGER->getUserFields(UserTable::getUfId(), $userId, LANGUAGE_ID, false);
394
395 $userTypeMap = array_fill_keys($supportedUserFieldTypeIDs, true);
396 foreach($ufList as $key => $userField)
397 {
398 if(
399 !isset($userTypeMap[$userField['USER_TYPE_ID']])
400 || $userField['EDIT_IN_LIST'] === "N"
401 )
402 {
403 unset($ufList[$key]);
404 }
405 }
406 $ufList = self::postFilterFields($ufList);
407
408 $ufValuesList = [];
409 foreach($ufList as $userField)
410 {
411 $ufValuesList[] = self::getUserFieldValue($userField);
412 }
413
414 return implode(' ', $ufValuesList);
415 }
416
417 private static function postFilterFields(array $fields)
418 {
419 static $ufReserved = [
420 'UF_DEPARTMENT',
421 'UF_USER_CRM_ENTITY',
422 'UF_PUBLIC',
423 'UF_TIMEMAN',
424 'UF_TM_REPORT_REQ',
425 'UF_TM_FREE',
426 'UF_REPORT_PERIOD',
427 'UF_1C',
428 'UF_TM_ALLOWED_DELTA',
429 'UF_SETTING_DATE',
430 'UF_LAST_REPORT_DATE',
431 'UF_DELAY_TIME',
432 'UF_TM_REPORT_DATE',
433 'UF_TM_DAY',
434 'UF_TM_TIME',
435 'UF_TM_REPORT_TPL',
436 'UF_TM_MIN_DURATION',
437 'UF_TM_MIN_FINISH',
438 'UF_TM_MAX_START',
439 'UF_CONNECTOR_MD5',
440 'UF_WORK_BINDING',
441 'UF_IM_SEARCH',
442 'UF_BXDAVEX_CALSYNC',
443 'UF_BXDAVEX_MLSYNC',
444 'UF_UNREAD_MAIL_COUNT',
445 'UF_BXDAVEX_CNTSYNC',
446 'UF_BXDAVEX_MAILBOX',
447 'UF_VI_PASSWORD',
448 'UF_VI_BACKPHONE',
449 'UF_VI_PHONE',
450 'UF_VI_PHONE_PASSWORD'
451 ];
452
453 foreach ($ufReserved as $ufId)
454 {
455 if (isset($fields[$ufId]))
456 {
457 unset($fields[$ufId]);
458 }
459 }
460
461 return $fields;
462 }
463
464 private static function getUserFieldValue(array $userField)
465 {
466 global $USER_FIELD_MANAGER;
467
468 $userTypeID = $userField['USER_TYPE_ID'] ?? '';
469 if($userTypeID === 'boolean')
470 {
471 $values = [];
472 if(isset($userField['VALUE']) && (bool)$userField['VALUE'] && isset($userField['EDIT_FORM_LABEL']))
473 {
474 $values[] = $userField['EDIT_FORM_LABEL'];
475 }
476 }
477 else
478 {
479 $values = explode(',', $USER_FIELD_MANAGER->getPublicText($userField));
480 }
481
482 return implode(' ', $values);
483 }
484
485 public static function getTagsContent($userId)
486 {
487 $result = '';
488
489 if (Loader::includeModule('socialnetwork'))
490 {
491 $tagsList = [];
492
493 $res = UserTagTable::getList([
494 'filter' => [
495 'USER_ID' => $userId
496 ],
497 'select' => [ 'NAME' ]
498 ]);
499 while($tagFields = $res->fetch())
500 {
501 $tagsList[] = $tagFields['NAME'];
502 }
503
504 $result = implode(' ', $tagsList);
505 }
506
507 return $result;
508 }
509}
static getConnection($name="")
static includeModule($moduleName)
Definition loader.php:69
static isModuleInstalled($moduleName)
static getUfId()
Definition user.php:52
static getUserGroupIds($userId)
Definition user.php:240
static getTagsContent($userId)
static getUFContent($userId)
static getGroupIds($userId)
static getDepartmentName($departmentId)
static getUserSearchFilter(array $fields)
Definition userutils.php:18
static getAdminSearchFilter(array $fields)
static getDepartmentNames($departmentIds)
static getCountryValue(array $params=[])