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