Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
user.php
1<?php
2namespace Bitrix\Im;
3
6
7Loc::loadMessages(__FILE__);
8
9class User
10{
11 private static $instance = Array();
12 private $userId = 0;
13 private $userData = null;
14
15 static $formatNameTemplate = null;
16
17 const FILTER_LIMIT = 50;
18
19 const PHONE_ANY = 'phone_any';
20 const PHONE_WORK = 'work_phone';
21 const PHONE_PERSONAL = 'personal_phone';
22 const PHONE_MOBILE = 'personal_mobile';
23 const PHONE_INNER = 'uf_phone_inner';
24
25 const SERVICE_ANY = 'service_any';
26 const SERVICE_ZOOM = 'zoom';
27 const SERVICE_SKYPE = 'skype';
28
29 protected function __construct($userId = null)
30 {
31 global $USER;
32
33 $this->userId = (int)$userId;
34 if ($this->userId <= 0 && is_object($USER) && $USER->GetID() > 0)
35 {
36 $this->userId = (int)$USER->GetID();
37 }
38 }
39
44 public static function getInstance($userId = null): self
45 {
46 global $USER;
47
48 $userId = (int)$userId;
49 if ($userId <= 0 && is_object($USER) && $USER->GetID() > 0)
50 {
51 $userId = (int)$USER->GetID();
52 }
53
54 if (!isset(self::$instance[$userId]))
55 {
56 self::$instance[$userId] = new self($userId);
57 }
58
59 return self::$instance[$userId];
60 }
61
65 public function getId()
66 {
67 return $this->userId;
68 }
69
73 public function getFullName($safe = true)
74 {
75 $fields = $this->getFields();
76 if (!$fields)
77 return '';
78
79 return $safe? $fields['name']: htmlspecialcharsback($fields['name']);
80 }
81
85 public function getName($safe = true)
86 {
87 $fields = $this->getFields();
88 if (!$fields)
89 return '';
90
91 return $safe? $fields['first_name']: htmlspecialcharsback($fields['first_name']);
92 }
93
97 public function getLastName($safe = true)
98 {
99 $fields = $this->getFields();
100 if (!$fields)
101 return '';
102
103 return $safe? $fields['last_name']: htmlspecialcharsback($fields['last_name']);
104 }
105
109 public function getAvatar()
110 {
111 $fields = $this->getFields();
112
113 return $fields && $fields['avatar'] != '/bitrix/js/im/images/blank.gif'? $fields['avatar']: '';
114 }
115
119 public function getAvatarHr()
120 {
121 $fields = $this->getFields();
122 if (!$fields)
123 {
124 return '';
125 }
126
127 return $fields['avatar'];
128 }
129
133 public function getProfile()
134 {
135 $fields = $this->getFields();
136 if (!$fields)
137 {
138 return '';
139 }
140
141 return $fields['profile'];
142 }
143
147 public function getStatus()
148 {
149 $fields = $this->getFields();
150 if (!$fields)
151 {
152 return 'offline';
153 }
154
155 //return $fields['status']?: 'online';
156 return 'online';
157 }
158
162 public function getIdle()
163 {
164 //return $this->getOnlineFields()['idle'];
165 return false;
166 }
167
171 public function getLastActivityDate()
172 {
173 return $this->getOnlineFields()['last_activity_date'];
174 }
175
179 public function getMobileLastDate()
180 {
181 //return $this->getOnlineFields()['mobile_last_date'];
182 return false;
183 }
184
188 public function getBirthday()
189 {
190 $fields = $this->getFields();
191
192 return $fields? $fields['birthday']: '';
193 }
194
198 public function getAvatarId()
199 {
200 $fields = $this->getFields();
201
202 return $fields? $fields['avatar_id']: 0;
203 }
204
208 public function getWorkPosition($safe = false)
209 {
210 $fields = $this->getFields();
211
212 if ($fields)
213 {
214 return $safe? $fields['work_position']: htmlspecialcharsback($fields['work_position']);
215 }
216 else
217 {
218 return false;
219 }
220 }
221
225 public function getGender()
226 {
227 $fields = $this->getFields();
228
229 return $fields? $fields['gender']: '';
230 }
231
235 public function getExternalAuthId()
236 {
237 $fields = $this->getFields();
238
239 return $fields? $fields['external_auth_id']: '';
240 }
241
245 public function getWebsite()
246 {
247 $fields = $this->getFields();
248
249 return $fields? $fields['website']: '';
250 }
251
255 public function getEmail()
256 {
257 $fields = $this->getFields();
258
259 return $fields? $fields['email']: '';
260 }
261
266 public function getPhone($type = self::PHONE_ANY)
267 {
268 $fields = $this->getPhones();
269
270 $result = '';
271 if ($type == self::PHONE_ANY)
272 {
273 if (isset($fields[self::PHONE_MOBILE]))
274 {
275 $result = $fields[self::PHONE_MOBILE];
276 }
277 else if (isset($fields[self::PHONE_PERSONAL]))
278 {
279 $result = $fields[self::PHONE_PERSONAL];
280 }
281 else if (isset($fields[self::PHONE_WORK]))
282 {
283 $result = $fields[self::PHONE_WORK];
284 }
285 }
286 else if (isset($fields[$type]))
287 {
288 $result = $fields[$type];
289 }
290
291 return $result;
292 }
293
298 public function getService($type = self::PHONE_ANY)
299 {
300 $fields = $this->getServices();
301
302 $result = '';
303 if ($type == self::SERVICE_ANY)
304 {
305 if (isset($fields[self::SERVICE_SKYPE]))
306 {
307 $result = $fields[self::SERVICE_SKYPE];
308 }
309 else if (isset($fields[self::SERVICE_ZOOM]))
310 {
311 $result = $fields[self::SERVICE_ZOOM];
312 }
313 }
314 else if (isset($fields[$type]))
315 {
316 $result = $fields[$type];
317 }
318
319 return $result;
320 }
321
325 public function getColor()
326 {
327 $fields = $this->getFields();
328
329 return $fields? $fields['color']: '';
330 }
331
335 public function getTzOffset()
336 {
337 $fields = $this->getFields();
338
339 return $fields? $fields['tz_offset']: '';
340 }
341
345 public function isOnline()
346 {
347 $fields = $this->getFields();
348
349 return $fields? $fields['status'] != 'offline': false;
350 }
354 public function isExtranet()
355 {
356 $fields = $this->getFields();
357
358 return $fields? (bool)$fields['extranet']: null;
359 }
360
364 public function isActive()
365 {
366 $fields = $this->getFields();
367
368 return $fields? (bool)$fields['active']: null;
369 }
370
374 public function isAbsent()
375 {
376 return \CIMContactList::formatAbsentResult($this->getId());
377 }
378
382 public function isNetwork()
383 {
384 $fields = $this->getFields();
385
386 return $fields? (bool)$fields['network']: null;
387 }
388
392 public function isBot()
393 {
394 $fields = $this->getFields();
395
396 return $fields? (bool)$fields['bot']: null;
397 }
398
402 public function isConnector()
403 {
404 $fields = $this->getFields();
405
406 return $fields? (bool)$fields['connector']: null;
407 }
408
412 public function isExists()
413 {
414 $fields = $this->getFields();
415
416 return $fields? true: false;
417 }
418
422 public function getFields()
423 {
424 $params = $this->getParams();
425
426 return $params? $params['user']: null;
427 }
428
432 public function getPhones()
433 {
434 $params = $this->getFields();
435
436 return $params? $params['phones']: null;
437 }
438
442 public function getServices()
443 {
444 $params = $this->getFields();
445
446 return $params? $params['services']: null;
447 }
448
452 public function getDepartments()
453 {
454 $params = $this->getFields();
455
456 return $params? $params['departments']: Array();
457 }
458
469 public function getArray($options = [])
470 {
471 if (!$this->isExists())
472 {
473 return null;
474 }
475
476 $hrPhotoOption = $options['HR_PHOTO'] ?? null;
477 $livechatOption = $options['LIVECHAT'] ?? null;
478 $jsonOption = $options['JSON'] ?? null;
479 $skipOnlineOption = $options['SKIP_ONLINE'] ?? null;
480
481 $result = [
482 'ID' => $this->getId(),
483 'ACTIVE' => $this->isActive(),
484 'NAME' => $this->getFullName(false),
485 'FIRST_NAME' => $this->getName(false),
486 'LAST_NAME' => $this->getLastName(false),
487 'WORK_POSITION' => $this->getWorkPosition(false),
488 'COLOR' => $this->getColor(),
489 'AVATAR' => $this->getAvatar(),
490 'GENDER' => $this->getGender(),
491 'BIRTHDAY' => (string)$this->getBirthday(),
492 'EXTRANET' => $this->isExtranet(),
493 'NETWORK' => $this->isNetwork(),
494 'BOT' => $this->isBot(),
495 'CONNECTOR' => $this->isConnector(),
496 'EXTERNAL_AUTH_ID' => $this->getExternalAuthId(),
497 'STATUS' => $this->getStatus(),
498 'IDLE' => $skipOnlineOption === 'Y' ? false: $this->getIdle(),
499 'LAST_ACTIVITY_DATE' => $skipOnlineOption === 'Y' ? false: $this->getLastActivityDate(),
500 'MOBILE_LAST_DATE' => $skipOnlineOption === 'Y' ? false: $this->getMobileLastDate(),
501 'ABSENT' => $this->isAbsent(),
502 'DEPARTMENTS' => $this->getDepartments(),
503 'PHONES' => $this->getPhones(),
504 ];
505 if ($hrPhotoOption)
506 {
507 $result['AVATAR_HR'] = $this->getAvatarHr();
508 }
509
510 //TODO: Live chat, open lines
511 //Just one call, here: \Bitrix\ImOpenLines\Connector::onStartWriting and \Bitrix\Im\Chat::getMessages
512 if ($livechatOption && !$this->isConnector())
513 {
514 $lineId = \Bitrix\ImOpenLines\Queue::getActualLineId(['LINE_ID' => $livechatOption, 'USER_CODE' => $options['USER_CODE']]);
515
516 $imolUserData = \Bitrix\ImOpenLines\Queue::getUserData($lineId, $this->getId());
517 if ($imolUserData)
518 {
519 $result = array_merge($result, $imolUserData);
520 $result['AVATAR_HR'] = $result['AVATAR'];
521 }
522 }
523 //TODO: END: Live chat, open lines
524
525 if ($jsonOption)
526 {
527 foreach ($result as $key => $value)
528 {
529 if ($value instanceof \Bitrix\Main\Type\DateTime)
530 {
531 $result[$key] = date('c', $value->getTimestamp());
532 }
533 else if (is_string($value) && is_string($key) && in_array($key, ['AVATAR', 'AVATAR_HR']) && is_string($value) && $value && mb_strpos($value, 'http') !== 0)
534 {
535 $result[$key] = \Bitrix\Im\Common::getPublicDomain().$value;
536 }
537 }
538 $result = array_change_key_case($result, CASE_LOWER);
539 }
540
541 return $result;
542 }
543
548 public static function getArrayWithOnline(array $users, array $options = ['JSON' => 'Y', 'SKIP_ONLINE' => 'Y']): array
549 {
550 $result = [];
551
552 foreach ($users as $user)
553 {
554 $result[$user->getId()] = $user->getArray($options);
555 }
556
557 $ids = array_keys($result);
558
559 if (empty($ids))
560 {
561 return [];
562 }
563
564 $statuses = StatusTable::query()
565 ->setSelect(['USER_ID', 'IDLE', 'MOBILE_LAST_DATE', 'DESKTOP_LAST_DATE', 'LAST_ACTIVITY_DATE' => 'USER.LAST_ACTIVITY_DATE'])
566 ->whereIn('USER_ID', $ids)
567 ->fetchAll()
568 ;
569
570 foreach ($statuses as $status)
571 {
572 $id = (int)$status['USER_ID'];
573 $result[$id]['last_activity_date'] = $status['LAST_ACTIVITY_DATE'] ? $status['LAST_ACTIVITY_DATE']->format('c') : false;
574 $result[$id]['desktop_last_date'] = $status['DESKTOP_LAST_DATE'] ? $status['DESKTOP_LAST_DATE']->format('c') : false;
575 $result[$id]['mobile_last_date'] = $status['MOBILE_LAST_DATE'] ? $status['MOBILE_LAST_DATE']->format('c') : false;
576 $result[$id]['idle'] = $status['IDLE'] ? $status['IDLE']->format('c') : false;
577 }
578
579 return array_values($result);
580 }
581
585 private function getParams()
586 {
587 if (is_null($this->userData))
588 {
589 $userData = \CIMContactList::GetUserData(Array(
590 'ID' => $this->getId(),
591 'PHONES' => 'Y',
592 'EXTRA_FIELDS' => 'Y',
593 'DATE_ATOM' => 'N',
594 'SHOW_ONLINE' => 'N',
595 ));
596 if (isset($userData['users'][$this->getId()]))
597 {
598 $this->userData['user'] = $userData['users'][$this->getId()];
599 }
600 }
601 return $this->userData;
602 }
603
610 public static function uploadAvatar($avatarUrl = '', $hash = '')
611 {
612 if (!$ar = parse_url($avatarUrl))
613 {
614 return 0;
615 }
616
617 if (!preg_match('#\.(png|jpg|jpeg|gif|webp)$#i', $ar['path'], $matches))
618 {
619 return 0;
620 }
621
622 $hash = md5($hash. $avatarUrl);
623
624 $orm = \Bitrix\Im\Model\ExternalAvatarTable::getList([
625 'select' => ['*', 'FILE_EXISTS' => 'FILE.ID'],
626 'filter' => ['=LINK_MD5' => $hash]
627 ]);
628 if ($cache = $orm->fetch())
629 {
630 if ((int)$cache['FILE_EXISTS'] > 0)
631 {
632 return (int)$cache['AVATAR_ID'];
633 }
634 else
635 {
636 \Bitrix\Im\Model\ExternalAvatarTable::delete((int)$cache['ID']);
637 }
638 }
639
640 try
641 {
642 $tempPath = \CFile::GetTempName('', $hash.'.'.$matches[1]);
643
644 $http = new \Bitrix\Main\Web\HttpClient();
645 $http
646 ->setTimeout(10)
647 ->setStreamTimeout(10);
648
649 if (!defined('BOT_CLIENT_URL'))
650 {
651 $http->setPrivateIp(false);
652 }
653 if ($http->download($avatarUrl, $tempPath))
654 {
655 $recordFile = \CFile::MakeFileArray($tempPath);
656 }
657 else
658 {
659 return 0;
660 }
661 }
662 catch (\Bitrix\Main\IO\IoException $exception)
663 {
664 return 0;
665 }
666
667 if (!\CFile::IsImage($recordFile['name'], $recordFile['type']))
668 {
669 return 0;
670 }
671
672 if (is_array($recordFile) && $recordFile['size'] && $recordFile['size'] > 0 && $recordFile['size'] < 1000000)
673 {
674 $recordFile = array_merge($recordFile, ['MODULE_ID' => 'imbot']);
675 }
676 else
677 {
678 $recordFile = 0;
679 }
680
681 if ($recordFile)
682 {
683 $recordFile = \CFile::SaveFile($recordFile, 'botcontroller', true);
684 }
685
686 if ((int)$recordFile > 0)
687 {
688 \Bitrix\Im\Model\ExternalAvatarTable::add([
689 'LINK_MD5' => $hash,
690 'AVATAR_ID' => (int)$recordFile
691 ]);
692 }
693
694 return $recordFile;
695 }
696
700 public static function clearStaticCache()
701 {
702 self::$instance = Array();
703 return true;
704 }
705
706 public static function isOpenlinesOperator($userId = null)
707 {
708 $userId = \Bitrix\Im\Common::getUserId($userId);
709 if (!$userId)
710 {
711 return false;
712 }
713
714 return \Bitrix\ImOpenLines\Config::isOperator($userId);
715 }
716
717 private function getOnlineFields()
718 {
719 $online = \CIMStatus::GetList(Array('ID' => $this->getId()));
720 if (!$online || !isset($online['users'][$this->getId()]))
721 {
722 return null;
723 }
724
725 $online = $online['users'][$this->getId()];
726
727 return [
728 'id' => $this->getId(),
729 'color' => $online['color']?: '',
730 'idle' => $online['idle']?: false,
731 'last_activity_date' => $online['last_activity_date']?: false,
732 'mobile_last_date' => $online['mobile_last_date']?: false,
733 ];
734 }
735
736 public static function getList($params)
737 {
738 $params = is_array($params)? $params: Array();
739
740 if (!isset($params['CURRENT_USER']) && is_object($GLOBALS['USER']))
741 {
742 $params['CURRENT_USER'] = $GLOBALS['USER']->GetID();
743 }
744
745 $params['CURRENT_USER'] = intval($params['CURRENT_USER']);
746
747 $userId = $params['CURRENT_USER'];
748 if ($userId <= 0)
749 {
750 return false;
751 }
752
753 $enableLimit = false;
754 if (isset($params['OFFSET']))
755 {
756 $filterLimit = intval($params['LIMIT']);
757 $filterLimit = $filterLimit <= 0? self::FILTER_LIMIT: $filterLimit;
758
759 $filterOffset = intval($params['OFFSET']);
760
761 $enableLimit = true;
762 }
763 else
764 {
765 $filterLimit = false;
766 $filterOffset = false;
767 }
768
769 $ormParams = self::getListParams($params);
770 if (is_null($ormParams))
771 {
772 return false;
773 }
774
775 $filter = $ormParams['filter'];
776 $filter['ACTIVE'] = 'Y';
777
778 $intranetInstalled = \Bitrix\Main\Loader::includeModule('intranet');
779 $voximplantInstalled = \Bitrix\Main\Loader::includeModule('voximplant');
780
781 $select = array(
782 "ID", "LAST_NAME", "NAME", "LOGIN", "PERSONAL_PHOTO", "SECOND_NAME", "PERSONAL_BIRTHDAY", "WORK_POSITION", "PERSONAL_GENDER", "EXTERNAL_AUTH_ID", "WORK_PHONE", "PERSONAL_PHONE", "PERSONAL_MOBILE", "TIME_ZONE_OFFSET", "ACTIVE", "LAST_ACTIVITY_DATE",
783 "COLOR" => "ST.COLOR", "STATUS" => "ST.STATUS", "IDLE" => "ST.IDLE", "MOBILE_LAST_DATE" => "ST.MOBILE_LAST_DATE",
784 );
785 if($intranetInstalled)
786 {
787 $select[] = 'UF_PHONE_INNER';
788 $select[] = 'UF_DEPARTMENT';
789 }
790 if ($voximplantInstalled)
791 {
792 $select[] = 'UF_VI_PHONE';
793 }
794
795 $ormParams = Array(
796 'select' => $select,
797 'filter' => $filter,
798 'runtime' => Array(
799 new \Bitrix\Main\Entity\ReferenceField(
800 'ST',
801 '\Bitrix\Im\Model\StatusTable',
802 array(
803 "=ref.USER_ID" => "this.ID",
804 ),
805 array("join_type"=>"LEFT")
806 )
807 ),
808 );
809 if ($enableLimit)
810 {
811 $ormParams['offset'] = $filterOffset;
812 $ormParams['limit'] = $filterLimit;
813 }
814
815 $orm = \Bitrix\Main\UserTable::getList($ormParams);
816 $bots = \Bitrix\Im\Bot::getListCache();
817
818 $users = array();
819 while ($user = $orm->fetch())
820 {
821 if (\CIMContactList::IsExtranet($user))
822 {
823 continue;
824 }
825
826 $color = false;
827 if (isset($user['COLOR']) && $user['COLOR'] <> '')
828 {
829 $color = \Bitrix\Im\Color::getColor($user['COLOR']);
830 }
831 if (!$color)
832 {
833 $color = \CIMContactList::GetUserColor($user["ID"], $user['PERSONAL_GENDER'] == 'M'? 'M': 'F');
834 }
835
836 $users[$user["ID"]] = Array(
837 'ID' => (int)$user["ID"],
838 'NAME' => \Bitrix\Im\User::formatFullNameFromDatabase($user),
839 'FIRST_NAME' => \Bitrix\Im\User::formatNameFromDatabase($user),
840 'LAST_NAME' => $user['LAST_NAME'],
841 'WORK_POSITION' => $user['WORK_POSITION'],
842 'COLOR' => $color,
843 'AVATAR' => \CIMChat::GetAvatarImage($user["PERSONAL_PHOTO"], 200, false),
844 'GENDER' => $user['PERSONAL_GENDER'] == 'F'? 'F': 'M',
845 'BIRTHDAY' => $user['PERSONAL_BIRTHDAY'] instanceof \Bitrix\Main\Type\Date? $user['PERSONAL_BIRTHDAY']->format('d-m'): false,
846 'EXTRANET' => \CIMContactList::IsExtranet($user),
847 'NETWORK' => $user['EXTERNAL_AUTH_ID'] == \CIMContactList::NETWORK_AUTH_ID || $user['EXTERNAL_AUTH_ID'] == \Bitrix\Im\Bot::EXTERNAL_AUTH_ID && $bots[$user["ID"]]['TYPE'] == \Bitrix\Im\Bot::TYPE_NETWORK,
848 'BOT' => $user['EXTERNAL_AUTH_ID'] == \Bitrix\Im\Bot::EXTERNAL_AUTH_ID,
849 'CONNECTOR' => $user['EXTERNAL_AUTH_ID'] == "imconnector",
850 'EXTERNAL_AUTH_ID' => $user['EXTERNAL_AUTH_ID']? $user['EXTERNAL_AUTH_ID']: 'default',
851 'STATUS' => $user['STATUS'],
852 'IDLE' => $user['IDLE'] instanceof \Bitrix\Main\Type\DateTime? $user['IDLE']: false,
853 'LAST_ACTIVITY_DATE' => $user['MOBILE_LAST_DATE'] instanceof \Bitrix\Main\Type\DateTime? $user['MOBILE_LAST_DATE']: false,
854 'MOBILE_LAST_DATE' => $user['LAST_ACTIVITY_DATE'] instanceof \Bitrix\Main\Type\DateTime? $user['LAST_ACTIVITY_DATE']: false,
855 'DEPARTMENTS' => is_array($user['UF_DEPARTMENT']) && !empty($user['UF_DEPARTMENT'])? $user['UF_DEPARTMENT']: [],
856 'ABSENT' => \CIMContactList::formatAbsentResult($user["ID"]),
857 );
858 if ($params['HR_PHOTO'])
859 {
860 $users[$user["ID"]]['AVATAR_HR'] = $users[$user["ID"]]['avatar'];
861 }
862
863 if ($voximplantInstalled)
864 {
865 $user["WORK_PHONE"] = \CVoxImplantPhone::Normalize($user["WORK_PHONE"]);
866 if ($user["WORK_PHONE"])
867 {
868 $users[$user["ID"]]['PHONES']['WORK_PHONE'] = $user['WORK_PHONE'];
869 }
870 $user["PERSONAL_MOBILE"] = \CVoxImplantPhone::Normalize($user["PERSONAL_MOBILE"]);
871 if ($user["PERSONAL_MOBILE"])
872 {
873 $users[$user["ID"]]['PHONES']['PERSONAL_MOBILE'] = $user['PERSONAL_MOBILE'];
874 }
875 $user["PERSONAL_PHONE"] = \CVoxImplantPhone::Normalize($user["PERSONAL_PHONE"]);
876 if ($user["PERSONAL_PHONE"])
877 {
878 $users[$user["ID"]]['PHONES']['PERSONAL_PHONE'] = $user['PERSONAL_PHONE'];
879 }
880 $user["UF_PHONE_INNER"] = preg_replace("/[^0-9\#\*]/i", "", $user["UF_PHONE_INNER"]);
881 if ($user["UF_PHONE_INNER"])
882 {
883 $users[$user["ID"]]['PHONES']['INNER_PHONE'] = $user["UF_PHONE_INNER"];
884 }
885 }
886 else
887 {
888 $users[$user["ID"]]['PHONES']['WORK_PHONE'] = $user['WORK_PHONE'];
889 $users[$user["ID"]]['PHONES']['PERSONAL_MOBILE'] = $user['PERSONAL_MOBILE'];
890 $users[$user["ID"]]['PHONES']['PERSONAL_PHONE'] = $user['PERSONAL_PHONE'];
891 $users[$user["ID"]]['PHONES']['INNER_PHONE'] = $user["UF_PHONE_INNER"];
892 }
893 }
894
895 if ($params['JSON'])
896 {
897 foreach ($users as $key => $userData)
898 {
899 foreach ($userData as $field => $value)
900 {
901 if ($value instanceof \Bitrix\Main\Type\DateTime)
902 {
903 $users[$key][$field] = date('c', $value->getTimestamp());
904 }
905 else if (is_string($value) && $value && is_string($field) && in_array($field, Array('AVATAR', 'AVATAR_HR')) && mb_strpos($value, 'http') !== 0)
906 {
907 $users[$key][$field] = \Bitrix\Im\Common::getPublicDomain().$value;
908 }
909 else if (is_array($value))
910 {
911 $users[$key][$field] = array_change_key_case($value, CASE_LOWER);
912 }
913 }
914 $users[$key] = array_change_key_case($users[$key], CASE_LOWER);;
915 }
916 }
917
918 return $users;
919 }
920
921 public static function getListParams($params)
922 {
923 if (isset($params['FILTER']['SEARCH']))
924 {
925 $filter = \Bitrix\Main\UserUtils::getUserSearchFilter(Array('FIND' => $params['FILTER']['SEARCH']));
926 if (empty($filter))
927 {
928 return null;
929 }
930 }
931 else
932 {
933 $filter = Array();
934 }
935
936 $filter['=ACTIVE'] = 'Y';
937 $filter['=CONFIRM_CODE'] = false;
938 $filter['!=EXTERNAL_AUTH_ID'] = \Bitrix\Im\Model\UserTable::filterExternalUserTypes([\Bitrix\Im\Bot::EXTERNAL_AUTH_ID]);
939
940 $filterByUsers = [];
941
942 if (User::getInstance($params['CURRENT_USER'])->isExtranet())
943 {
944 $groups = \Bitrix\Im\Integration\Socialnetwork\Extranet::getGroup(Array(), $params['CURRENT_USER']);
945 if (is_array($groups))
946 {
947 foreach ($groups as $group)
948 {
949 foreach ($group['USERS'] as $userId)
950 {
951 $filterByUsers[$userId] = $userId;
952 }
953 }
954 $filterByUsers[$params['CURRENT_USER']] = $params['CURRENT_USER'];
955 }
956 }
957
958 if (
959 $params['FILTER']['BUSINESS'] == 'Y'
960 && \Bitrix\Main\Loader::includeModule('bitrix24')
961 && !\CBitrix24BusinessTools::isLicenseUnlimited()
962 )
963 {
964 $businessUsers = \CBitrix24BusinessTools::getUnlimUsers();
965
966 if (User::getInstance($params['CURRENT_USER'])->isExtranet())
967 {
968 $extranetBusinessResult = [];
969 foreach ($filterByUsers as $userId)
970 {
971 if (in_array($userId, $businessUsers))
972 {
973 $extranetBusinessResult[$userId] = $userId;
974 }
975 }
976 $filterByUsers = $extranetBusinessResult;
977 }
978 else
979 {
980 foreach ($businessUsers as $userId)
981 {
982 $filterByUsers[$userId] = $userId;
983 }
984 }
985 }
986
987 if ($filterByUsers)
988 {
989 $filter['=ID'] = array_keys($filterByUsers);
990 }
991
992 return ['filter' => $filter];
993 }
994
995 public static function getBusiness($userId = null, $options = array())
996 {
997 $userId = \Bitrix\Im\Common::getUserId($userId);
998 if (!$userId)
999 {
1000 return false;
1001 }
1002
1003 $pagination = isset($options['LIST'])? true: false;
1004
1005 $limit = isset($options['LIST']['LIMIT'])? intval($options['LIST']['LIMIT']): 50;
1006 $offset = isset($options['LIST']['OFFSET'])? intval($options['LIST']['OFFSET']): 0;
1007
1008 $list = Array();
1009
1010 $businessUsersAvailable = false;
1011 if (\Bitrix\Main\Loader::includeModule('bitrix24') && !\CBitrix24BusinessTools::isLicenseUnlimited())
1012 {
1013 $businessUsers = \CBitrix24BusinessTools::getUnlimUsers();
1014
1015 if (User::getInstance($userId)->isExtranet())
1016 {
1017 $extranetBusinessResult = [];
1018 $groups = \Bitrix\Im\Integration\Socialnetwork\Extranet::getGroup(Array(), $userId);
1019 if (is_array($groups))
1020 {
1021 foreach ($groups as $group)
1022 {
1023 foreach ($group['USERS'] as $uid)
1024 {
1025 if (in_array($uid, $businessUsers))
1026 {
1027 $extranetUserList[$uid] = $uid;
1028 }
1029 }
1030 }
1031 }
1032 $list = $extranetBusinessResult;
1033 }
1034 else
1035 {
1036 foreach ($businessUsers as $userId)
1037 {
1038 $list[$userId] = $userId;
1039 }
1040 }
1041
1042 $businessUsersAvailable = true;
1043 }
1044
1045 $count = count($list);
1046
1047 $list = array_slice($list, $offset, $limit);
1048
1049 if ($options['USER_DATA'] == 'Y')
1050 {
1051 $result = Array();
1052
1053 $getOptions = Array();
1054 if ($options['JSON'] == 'Y')
1055 {
1056 $getOptions['JSON'] = 'Y';
1057 }
1058
1059 foreach ($list as $userId)
1060 {
1061 $result[] = \Bitrix\Im\User::getInstance($userId)->getArray($getOptions);
1062 }
1063 }
1064 else
1065 {
1066 $result = array_values($list);
1067 }
1068
1069 if ($pagination)
1070 {
1071 $result = Array('TOTAL' => $count, 'RESULT' => $result, 'AVAILABLE' => $businessUsersAvailable);
1072
1073 if ($options['JSON'] == 'Y')
1074 {
1075 $result = array_change_key_case($result, CASE_LOWER);
1076 }
1077 }
1078 else
1079 {
1080 if (!$businessUsersAvailable)
1081 {
1082 $result = false;
1083 }
1084 }
1085
1086 return $result;
1087 }
1088
1089 public static function getMessages($userId = null, $options = Array())
1090 {
1091 $userId = \Bitrix\Im\Common::getUserId($userId);
1092 if (!$userId)
1093 {
1094 return false;
1095 }
1096
1097
1098 $filter = Array(
1099 '=AUTHOR_ID' => $userId
1100 );
1101
1102 if (isset($options['FIRST_ID']))
1103 {
1104 $order = array();
1105
1106 if (intval($options['FIRST_ID']) > 0)
1107 {
1108 $filter['>ID'] = $options['FIRST_ID'];
1109 }
1110 }
1111 else
1112 {
1113 $order = Array('ID' => 'DESC');
1114
1115 if (isset($options['LAST_ID']) && intval($options['LAST_ID']) > 0)
1116 {
1117 $filter['<ID'] = intval($options['LAST_ID']);
1118 }
1119 }
1120
1121 if (isset($options['LIMIT']))
1122 {
1123 $options['LIMIT'] = intval($options['LIMIT']);
1124 $limit = $options['LIMIT'] >= 500? 500: $options['LIMIT'];
1125 }
1126 else
1127 {
1128 $limit = 50;
1129 }
1130
1131 $skipMessage = isset($options['SKIP_MESSAGE']) && $options['SKIP_MESSAGE'] == 'Y';
1132
1133 $select = Array(
1134 'ID', 'CHAT_ID', 'DATE_CREATE',
1135 'CHAT_TITLE' => 'CHAT.TITLE'
1136 );
1137 if (!$skipMessage)
1138 {
1139 $select[] = 'MESSAGE';
1140 }
1141
1142 $orm = \Bitrix\Im\Model\MessageTable::getList(array(
1143 'filter' => $filter,
1144 'select' => $select,
1145 'order' => $order,
1146 'limit' => $limit
1147 ));
1148
1149 $messages = Array();
1150 $messagesChat = Array();
1151 while($message = $orm->fetch())
1152 {
1153 $messages[$message['ID']] = Array(
1154 'ID' => (int)$message['ID'],
1155 'DATE' => $message['DATE_CREATE'],
1156 'TEXT' => (string)$message['MESSAGE'],
1157 );
1158
1159 if ($skipMessage)
1160 {
1161 unset($messages[$message['ID']]['TEXT']);
1162 }
1163
1164 $messagesChat[$message['ID']] = Array(
1165 'ID' => (int)$message['ID'],
1166 'CHAT_ID' => (int)$message['CHAT_ID']
1167 );
1168 }
1169
1170 $params = \CIMMessageParam::Get(array_keys($messages));
1171
1172 $fileIds = Array();
1173 foreach ($params as $messageId => $param)
1174 {
1175 $messages[$messageId]['params'] = empty($param)? null: $param;
1176
1177 if (isset($param['FILE_ID']))
1178 {
1179 foreach ($param['FILE_ID'] as $fileId)
1180 {
1181 $fileIds[$messagesChat[$messageId]['CHAT_ID']][$fileId] = $fileId;
1182 }
1183 }
1184 }
1185
1186 $messages = \CIMMessageLink::prepareShow($messages, $params);
1187
1188 $files = array();
1189 foreach ($fileIds as $chatId => $fileId)
1190 {
1191 if ($result = \CIMDisk::GetFiles($chatId, $fileId))
1192 {
1193 $files = array_merge($files, $result);
1194 }
1195 }
1196
1197 $result = Array(
1198 'MESSAGES' => $messages,
1199 'FILES' => $files,
1200 );
1201
1202 if ($options['JSON'])
1203 {
1204 foreach ($result['MESSAGES'] as $key => $value)
1205 {
1206 if ($value['DATE'] instanceof \Bitrix\Main\Type\DateTime)
1207 {
1208 $result['MESSAGES'][$key]['DATE'] = date('c', $value['DATE']->getTimestamp());
1209 }
1210
1211 $result['MESSAGES'][$key] = array_change_key_case($result['MESSAGES'][$key], CASE_LOWER);
1212 }
1213 $result['MESSAGES'] = array_values($result['MESSAGES']);
1214
1215 foreach ($result['FILES'] as $key => $value)
1216 {
1217 if ($value['date'] instanceof \Bitrix\Main\Type\DateTime)
1218 {
1219 $result['FILES'][$key]['date'] = date('c', $value['date']->getTimestamp());
1220 }
1221
1222 foreach (['urlPreview', 'urlShow', 'urlDownload'] as $field)
1223 {
1224 $url = $result['FILES'][$key][$field];
1225 if (is_string($url) && $url && mb_strpos($url, 'http') !== 0)
1226 {
1227 $result['FILES'][$key][$field] = \Bitrix\Im\Common::getPublicDomain().$url;
1228 }
1229 }
1230 }
1231
1232 $result = array_change_key_case($result, CASE_LOWER);
1233 }
1234
1235 return $result;
1236 }
1237
1238 public static function formatNameFromDatabase($fields)
1239 {
1240 if (empty($fields['NAME']) && empty($fields['LAST_NAME']))
1241 {
1242 if (in_array($fields['EXTERNAL_AUTH_ID'], \Bitrix\Main\UserTable::getExternalUserTypes()))
1243 {
1244 return Loc::getMessage('IM_USER_GUEST_NAME');
1245 }
1246 else if (!empty($fields['LOGIN']))
1247 {
1248 return $fields['LOGIN'];
1249 }
1250 else
1251 {
1252 return Loc::getMessage('IM_USER_ANONYM_NAME');
1253 }
1254 }
1255
1256 return $fields['NAME'];
1257 }
1258
1259 public static function formatFullNameFromDatabase($fields)
1260 {
1261 if (is_null(self::$formatNameTemplate))
1262 {
1263 self::$formatNameTemplate = \CSite::GetNameFormat(false);
1264 }
1265
1266 if (empty($fields['NAME']) && empty($fields['LAST_NAME']))
1267 {
1268 if (in_array($fields['EXTERNAL_AUTH_ID'], \Bitrix\Main\UserTable::getExternalUserTypes()))
1269 {
1270 return Loc::getMessage('IM_USER_GUEST_NAME');
1271 }
1272 else if (!empty($fields['LOGIN']))
1273 {
1274 return $fields['LOGIN'];
1275 }
1276 else
1277 {
1278 return Loc::getMessage('IM_USER_ANONYM_NAME');
1279 }
1280 }
1281
1282 return \CUser::FormatName(self::$formatNameTemplate, $fields, true, false);
1283 }
1284
1285 public static function setInstance($userId, $instance)
1286 {
1287 $c = __CLASS__;
1288 if ($instance instanceof $c)
1289 {
1290 self::$instance[$userId] = $instance;
1291 }
1292 }
1293}
getWorkPosition($safe=false)
Definition user.php:208
static getMessages($userId=null, $options=Array())
Definition user.php:1089
static formatFullNameFromDatabase($fields)
Definition user.php:1259
const SERVICE_SKYPE
Definition user.php:27
getFullName($safe=true)
Definition user.php:73
static getList($params)
Definition user.php:736
static isOpenlinesOperator($userId=null)
Definition user.php:706
static formatNameFromDatabase($fields)
Definition user.php:1238
const PHONE_WORK
Definition user.php:20
getName($safe=true)
Definition user.php:85
static uploadAvatar($avatarUrl='', $hash='')
Definition user.php:610
const PHONE_PERSONAL
Definition user.php:21
const FILTER_LIMIT
Definition user.php:17
const SERVICE_ZOOM
Definition user.php:26
getLastName($safe=true)
Definition user.php:97
const PHONE_MOBILE
Definition user.php:22
getLastActivityDate()
Definition user.php:171
static getInstance($userId=null)
Definition user.php:44
const PHONE_ANY
Definition user.php:19
static clearStaticCache()
Definition user.php:700
static getListParams($params)
Definition user.php:921
const PHONE_INNER
Definition user.php:23
const SERVICE_ANY
Definition user.php:25
static $formatNameTemplate
Definition user.php:15
getPhone($type=self::PHONE_ANY)
Definition user.php:266
static getBusiness($userId=null, $options=array())
Definition user.php:995
__construct($userId=null)
Definition user.php:29
getMobileLastDate()
Definition user.php:179
static setInstance($userId, $instance)
Definition user.php:1285
static getArrayWithOnline(array $users, array $options=['JSON'=> 'Y', 'SKIP_ONLINE'=> 'Y'])
Definition user.php:548
getArray($options=[])
Definition user.php:469
getService($type=self::PHONE_ANY)
Definition user.php:298
getExternalAuthId()
Definition user.php:235
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
$GLOBALS['____1444769544']
Definition license.php:1