1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
UserFactory.php
См. документацию.
1<?php
2
3namespace Bitrix\Im\V2\Entity\User;
4
5use Bitrix\Im\Color;
6use Bitrix\Im\Model\StatusTable;
7use Bitrix\Im\Model\UserTable;
8use Bitrix\Im\V2\Integration\Extranet\CollaberService;
9use Bitrix\Main\Application;
10use Bitrix\Main\Data\Cache;
11use Bitrix\Main\Loader;
12use Bitrix\Main\ORM\Fields\Relations\Reference;
13use Bitrix\Main\ORM\Query\Join;
14use CVoxImplantPhone;
15
17{
18 private const COMMON_SELECT_FIELD = [
19 'ID',
20 'LAST_NAME',
21 'NAME',
22 'EMAIL',
23 'LOGIN',
24 'PERSONAL_PHOTO',
25 'SECOND_NAME',
26 'PERSONAL_BIRTHDAY',
27 'WORK_POSITION',
28 'PERSONAL_GENDER',
29 'EXTERNAL_AUTH_ID',
30 'PERSONAL_WWW',
31 'ACTIVE',
32 'LANGUAGE_ID',
33 'WORK_PHONE',
34 'TIME_ZONE',
35 'PERSONAL_MOBILE',
36 'PERSONAL_PHONE',
37 'COLOR' => 'ST.COLOR',
38 'STATUS' => 'ST.STATUS',
39 ];
40
41 protected static self $instance;
42
43 private function __construct()
44 {
45 }
46
47 public static function getInstance(): self
48 {
49 if (isset(self::$instance))
50 {
51 return self::$instance;
52 }
53
54 self::$instance = new static();
55
56 return self::$instance;
57 }
58
59 public function getUserById(int $id): User
60 {
61 $cache = $this->getCache($id);
62 $cachedUser = $cache->getVars();
63 if ($cachedUser !== false)
64 {
65 return $this->initUser($cachedUser);
66 }
67
68 $userData = $this->getUserFromDb($id);
69
70 if ($userData === null)
71 {
72 return new NullUser();
73 }
74
75 $userData = $this->prepareUserData($userData);
76 $this->saveInCache($cache, $userData);
77
78 return $this->initUser($userData);
79 }
80
81 public function initUser(array $userData): User
82 {
83 $userData = $this->prepareNonCachedUserData($userData);
84
85 if ($userData['IS_BOT'])
86 {
87 return UserBot::initByArray($userData);
88 }
89 if (CollaberService::getInstance()->isCollaber((int)$userData['ID']))
90 {
91 return UserCollaber::initByArray($userData);
92 }
93 if ($userData['IS_EXTRANET'])
94 {
95 return UserExtranet::initByArray($userData);
96 }
97 if ($this->isExternal($userData))
98 {
99 return UserExternal::initByArray($userData);
100 }
101
102 return User::initByArray($userData);
103 }
104
105 protected function prepareUserData(array $userData): array
106 {
107 $avatar = \CIMChat::GetAvatarImage($userData['PERSONAL_PHOTO']) ?: '';
108
109 $preparedUserData = $userData;
110 $preparedUserData['COLOR'] = $this->getColor($userData);
111 $preparedUserData['STATUS'] = $userData['STATUS'] ?? null;
112 $preparedUserData['NAME'] = \Bitrix\Im\User::formatFullNameFromDatabase($userData);
113 $preparedUserData['FIRST_NAME'] = \Bitrix\Im\User::formatNameFromDatabase($userData);
114 $preparedUserData['BIRTHDAY'] =
115 $userData['PERSONAL_BIRTHDAY'] instanceof \Bitrix\Main\Type\Date
116 ? $userData['PERSONAL_BIRTHDAY']->format('d-m')
117 : false
118 ;
119 $preparedUserData['AVATAR'] = $avatar !== '/bitrix/js/im/images/blank.gif' ? $avatar : '';
120 $preparedUserData['AVATAR_HR'] = $avatar;
121 $preparedUserData['AVATAR_ID'] = (int)$userData['PERSONAL_PHOTO'];
122 $preparedUserData['IS_EXTRANET'] = $this->isExtranet($userData);
123 $preparedUserData['IS_NETWORK'] = $this->isNetwork($userData);
124 $preparedUserData['IS_BOT'] = $this->isBot($userData);
125 $preparedUserData['IS_CONNECTOR'] = $this->isConnector($userData);
126 $preparedUserData['LANGUAGE_ID'] = $userData['LANGUAGE_ID'] ?? null;
127
128 if (Loader::includeModule('voximplant'))
129 {
130 $preparedUserData['WORK_PHONE'] = CVoxImplantPhone::Normalize($userData['WORK_PHONE']) ?: null;
131 $preparedUserData['PERSONAL_MOBILE'] = CVoxImplantPhone::Normalize($userData['PERSONAL_MOBILE']) ?: null;
132 $preparedUserData['PERSONAL_PHONE'] = CVoxImplantPhone::Normalize($userData['PERSONAL_PHONE']) ?: null;
133 }
134
135 if (Loader::includeModule('intranet'))
136 {
137 $innerPhone = preg_replace("/[^0-9\#\*]/i", "", $userData['UF_PHONE_INNER'] ?? '');
138 if ($innerPhone)
139 {
140 $preparedUserData['INNER_PHONE'] = $innerPhone;
141 }
142 }
143
144 return $preparedUserData;
145 }
146
147 protected function prepareNonCachedUserData(array $userData): array
148 {
149 $preparedUserData = $userData;
150 $preparedUserData['ABSENT'] = \CIMContactList::formatAbsentResult((int)$userData['ID']) ?: null;
151
152 return $preparedUserData;
153 }
154
155 protected function getUserFromDb(int $id): ?array
156 {
157 $query = UserTable::query()
158 ->setSelect(self::COMMON_SELECT_FIELD)
159 ->setLimit(1)
160 ->where('ID', $id)
161 ->registerRuntimeField(
162 'ST',
163 new Reference(
164 'ST',
165 StatusTable::class,
166 Join::on('this.ID', 'ref.USER_ID'),
167 ['join_type' => Join::TYPE_LEFT]
168 )
169 )
170 ;
171
172 if (Loader::includeModule('intranet'))
173 {
174 $query
175 ->addSelect('UF_DEPARTMENT')
176 ->addSelect('UF_PHONE_INNER')
177 ->addSelect('UF_ZOOM')
178 ->addSelect('UF_SKYPE')
179 ->addSelect('UF_SKYPE_LINK')
180 ;
181 }
182
183 if (Loader::includeModule('voximplant'))
184 {
185 $query->addSelect('UF_VI_PHONE');
186 }
187
188 return $query->fetch() ?: null;
189 }
190
191 protected function isExtranet(array $params): bool
192 {
193 return \CIMContactList::IsExtranet($params);
194 }
195
196 protected function isNetwork(array $params): bool
197 {
198 $bots = \Bitrix\Im\Bot::getListCache();
199 $isNetworkUser = $params['EXTERNAL_AUTH_ID'] === \CIMContactList::NETWORK_AUTH_ID;
200 $isNetworkBot = (
201 $this->isBot($params)
202 && $bots[$params["ID"]]['TYPE'] === \Bitrix\Im\Bot::TYPE_NETWORK
203 );
204
205 return $isNetworkUser || $isNetworkBot;
206 }
207
208 protected function isBot(array $params): bool
209 {
210 return $params['EXTERNAL_AUTH_ID'] === \Bitrix\Im\Bot::EXTERNAL_AUTH_ID;
211 }
212
213 protected function isConnector(array $params): bool
214 {
215 return $params['EXTERNAL_AUTH_ID'] === 'imconnector';
216 }
217
218 protected function getColor(array $userData): string
219 {
220 return $userData['COLOR']
221 ? Color::getColor($userData['COLOR'])
222 : $this->getColorByUserIdAndGender((int)$userData['ID'], $userData['PERSONAL_GENDER'] === 'M'? 'M': 'F');
223 }
224
225 protected function getColorByUserIdAndGender(int $id, string $gender): string
226 {
228 if ($gender === 'M')
229 {
230 $replaceColor = Color::getReplaceColors();
231 if (isset($replaceColor[$code]))
232 {
233 $code = $replaceColor[$code];
234 }
235 }
236
237 return Color::getColor($code);
238 }
239
240 protected function isExternal(array $params): bool
241 {
242 return in_array($params['EXTERNAL_AUTH_ID'], UserTable::filterExternalUserTypes(['bot']), true);
243 }
244
245 //region Cache
246
247 protected function getCache(int $id): Cache
248 {
249 $cache = Application::getInstance()->getCache();
250
251 $cacheTTL = defined("BX_COMP_MANAGED_CACHE") ? 18144000 : 1800;
252 $cacheId = "user_data_{$id}";
253 $cacheDir = $this->getCacheDir($id);
254
255 $cache->initCache($cacheTTL, $cacheId, $cacheDir);
256
257 return $cache;
258 }
259
260 protected function saveInCache(Cache $cache, array $userData): void
261 {
262 $taggedCache = Application::getInstance()->getTaggedCache();
263 $id = (int)$userData['ID'];
264 $cache->startDataCache();
265 $taggedCache->startTagCache($this->getCacheDir($id));
266 $taggedCache->registerTag("USER_NAME_{$id}");
267 $taggedCache->endTagCache();
268 $cache->endDataCache($userData);
269 }
270
271 private function getCacheDir(int $id): string
272 {
273 $cacheSubDir = $id % 100;
274 $cacheSubSubDir = ($id % 10000) / 100;
275
276 return "/bx/imc/userdata_v8/{$cacheSubDir}/{$cacheSubSubDir}/{$id}";
277 }
278
279 public function clearCache(int $id): void
280 {
282 Application::getInstance()->getCache()->cleanDir($this->getCacheDir($id));
283 }
284
285 //endregion
286}
static getColor($code)
Определения color.php:121
static getReplaceColors()
Определения color.php:112
static getCodeByNumber($number)
Определения color.php:166
static formatFullNameFromDatabase($fields)
Определения user.php:1243
static formatNameFromDatabase($fields)
Определения user.php:1222
static clearStaticCache()
Определения user.php:684
static self $instance
Определения UserFactory.php:41
isBot(array $params)
Определения UserFactory.php:208
isNetwork(array $params)
Определения UserFactory.php:196
prepareNonCachedUserData(array $userData)
Определения UserFactory.php:147
getColorByUserIdAndGender(int $id, string $gender)
Определения UserFactory.php:225
isConnector(array $params)
Определения UserFactory.php:213
isExtranet(array $params)
Определения UserFactory.php:191
prepareUserData(array $userData)
Определения UserFactory.php:105
getColor(array $userData)
Определения UserFactory.php:218
initUser(array $userData)
Определения UserFactory.php:81
isExternal(array $params)
Определения UserFactory.php:240
saveInCache(Cache $cache, array $userData)
Определения UserFactory.php:260
static initByArray(array $userData)
Определения User.php:94
static getInstance()
Определения servicelocator.php:33
endDataCache($vars=false)
Определения cache.php:404
startDataCache($TTL=false, $uniqueString=false, $initDir=false, $vars=array(), $baseDir='cache')
Определения cache.php:345
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$query
Определения get_search.php:11
if(!is_null($config))($config as $configItem)(! $configItem->isVisible()) $code
Определения options.php:195
return false
Определения prolog_main_admin.php:185
if($inWords) echo htmlspecialcharsbx(Number2Word_Rus(roundEx($totalVatSum $params['CURRENCY']
Определения template.php:799