Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
UserFabric.php
1<?php
2
4
13use CVoxImplantPhone;
14
16{
17 private const COMMON_SELECT_FIELD = [
18 'ID',
19 'LAST_NAME',
20 'NAME',
21 'EMAIL',
22 'LOGIN',
23 'PERSONAL_PHOTO',
24 'SECOND_NAME',
25 'PERSONAL_BIRTHDAY',
26 'WORK_POSITION',
27 'PERSONAL_GENDER',
28 'EXTERNAL_AUTH_ID',
29 'TIME_ZONE_OFFSET',
30 'PERSONAL_WWW',
31 'ACTIVE',
32 'WORK_PHONE',
33 'PERSONAL_PHONE',
34 'PERSONAL_MOBILE',
35 'COLOR' => 'STATUS.COLOR',
36 ];
37
38 protected static self $instance;
39
40 private function __construct()
41 {
42 }
43
44 public static function getInstance(): self
45 {
46 if (isset(self::$instance))
47 {
48 return self::$instance;
49 }
50
51 self::$instance = new static();
52
53 return self::$instance;
54 }
55
56 public function getUserById(int $id): User
57 {
58 $cache = $this->getCache($id);
59 $cachedUser = $cache->getVars();
60 if ($cachedUser !== false)
61 {
62 $userData = $this->prepareUserData($cachedUser);
63
64 return $this->initUser($userData);
65 }
66
67 $userData = $this->getUserFromDb($id);
68
69 if ($userData === null)
70 {
71 return new NullUser();
72 }
73
74 $this->saveInCache($cache, $userData);
75 $userData = $this->prepareUserData($userData);
76
77 return $this->initUser($userData);
78 }
79
80 public function initUser(array $userData): User
81 {
82 if ($userData['IS_BOT'])
83 {
84 return UserBot::initByArray($userData);
85 }
86 if ($userData['IS_EXTRANET'])
87 {
88 return UserExtranet::initByArray($userData);
89 }
90 if ($this->isExternal($userData))
91 {
92 return UserExternal::initByArray($userData);
93 }
94
95 return User::initByArray($userData);
96 }
97
98 protected function prepareUserData(array $userData): array
99 {
100 $avatar = \CIMChat::GetAvatarImage($userData['PERSONAL_PHOTO']) ?: '';
101
102 $userData['COLOR'] = $this->getColor($userData);
103 $userData['NAME'] = \Bitrix\Im\User::formatFullNameFromDatabase($userData);
104 $userData['FIRST_NAME'] = \Bitrix\Im\User::formatNameFromDatabase($userData);
105 $userData['BIRTHDAY'] =
106 $userData['PERSONAL_BIRTHDAY'] instanceof \Bitrix\Main\Type\Date
107 ? $userData['PERSONAL_BIRTHDAY']->format('d-m')
108 : false
109 ;
110 $userData['AVATAR'] = $avatar !== '/bitrix/js/im/images/blank.gif' ? $avatar : '';
111 $userData['AVATAR_HR'] = $avatar;
112 $userData['AVATAR_ID'] = (int)$userData['PERSONAL_PHOTO'];
113 $userData['IS_EXTRANET'] = $this->isExtranet($userData);
114 $userData['IS_NETWORK'] = $this->isNetwork($userData);
115 $userData['IS_BOT'] = $this->isBot($userData);
116 $userData['IS_CONNECTOR'] = $this->isConnector($userData);
117 $userData['ABSENT'] = \CIMContactList::formatAbsentResult((int)$userData['ID']) ?: null;
118
119 if (Loader::includeModule('voximplant'))
120 {
121 $userData['WORK_PHONE'] = CVoxImplantPhone::Normalize($userData['WORK_PHONE']) ?: null;
122 $userData['PERSONAL_MOBILE'] = CVoxImplantPhone::Normalize($userData['PERSONAL_MOBILE']) ?: null;
123 $userData['PERSONAL_PHONE'] = CVoxImplantPhone::Normalize($userData['PERSONAL_PHONE']) ?: null;
124 }
125
126 return $userData;
127 }
128
129 protected function getUserFromDb(int $id): ?array
130 {
131 $query = UserTable::query()
132 ->setSelect(self::COMMON_SELECT_FIELD)
133 ->setLimit(1)
134 ->where('ID', $id)
135 ->registerRuntimeField(
136 'STATUS',
137 new Reference(
138 'STATUS',
139 StatusTable::class,
140 Join::on('this.ID', 'ref.USER_ID'),
141 ['join_type' => Join::TYPE_LEFT]
142 )
143 )
144 ;
145
146 if (Loader::includeModule('intranet'))
147 {
148 $query->addSelect('UF_DEPARTMENT');
149 }
150
151 return $query->fetch() ?: null;
152 }
153
154 protected function isExtranet(array $params): bool
155 {
156 return \CIMContactList::IsExtranet($params);
157 }
158
159 protected function isNetwork(array $params): bool
160 {
161 $bots = \Bitrix\Im\Bot::getListCache();
162 $isNetworkUser = $params['EXTERNAL_AUTH_ID'] === \CIMContactList::NETWORK_AUTH_ID;
163 $isNetworkBot = (
164 $this->isBot($params)
165 && $bots[$params["ID"]]['TYPE'] === \Bitrix\Im\Bot::TYPE_NETWORK
166 );
167
168 return $isNetworkUser || $isNetworkBot;
169 }
170
171 protected function isBot(array $params): bool
172 {
173 return $params['EXTERNAL_AUTH_ID'] === \Bitrix\Im\Bot::EXTERNAL_AUTH_ID;
174 }
175
176 protected function isConnector(array $params): bool
177 {
178 return $params['EXTERNAL_AUTH_ID'] === 'imconnector';
179 }
180
181 protected function getColor(array $userData): string
182 {
183 return $userData['COLOR']
184 ? Color::getColor($userData['COLOR'])
185 : $this->getColorByUserIdAndGender((int)$userData['ID'], $userData['PERSONAL_GENDER'] === 'M'? 'M': 'F');
186 }
187
188 protected function getColorByUserIdAndGender(int $id, string $gender): string
189 {
190 $code = Color::getCodeByNumber($id);
191 if ($gender === 'M')
192 {
193 $replaceColor = Color::getReplaceColors();
194 if (isset($replaceColor[$code]))
195 {
196 $code = $replaceColor[$code];
197 }
198 }
199
200 return Color::getColor($code);
201 }
202
203 protected function isExternal(array $params): bool
204 {
205 return in_array($params['EXTERNAL_AUTH_ID'], UserTable::filterExternalUserTypes(['bot']), true);
206 }
207
208 //region Cache
209
210 protected function getCache(int $id): Cache
211 {
212 $cache = Application::getInstance()->getCache();
213
214 $cacheTTL = defined("BX_COMP_MANAGED_CACHE") ? 18144000 : 1800;
215 $cacheId = "user_data_{$id}";
216 $cacheDir = $this->getCacheDir($id);
217
218 $cache->initCache($cacheTTL, $cacheId, $cacheDir);
219
220 return $cache;
221 }
222
223 protected function saveInCache(Cache $cache, array $userData): void
224 {
225 $taggedCache = Application::getInstance()->getTaggedCache();
226 $id = (int)$userData['ID'];
227 $cache->startDataCache();
228 $taggedCache->startTagCache($this->getCacheDir($id));
229 $taggedCache->registerTag("USER_NAME_{$id}");
230 $taggedCache->endTagCache();
231 $cache->endDataCache($userData);
232 }
233
234 private function getCacheDir(int $id): string
235 {
236 $cacheSubDir = $id % 100;
237 $cacheSubSubDir = ($id % 10000) / 100;
238
239 return "/bx/imc/userdata_v2/{$cacheSubDir}/{$cacheSubSubDir}/{$id}";
240 }
241
242 //endregion
243}
static getColor($code)
Definition color.php:121
static getReplaceColors()
Definition color.php:112
static getCodeByNumber($number)
Definition color.php:166
getColorByUserIdAndGender(int $id, string $gender)
saveInCache(Cache $cache, array $userData)
static initByArray(array $userData)
Definition User.php:90
endDataCache($vars=false)
Definition cache.php:407
startDataCache($TTL=false, $uniqueString=false, $initDir=false, $vars=array(), $baseDir='cache')
Definition cache.php:348