Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
usercontentview.php
1<?php
2
10
12use Bitrix\Main\Entity\ExpressionField;
18
20{
21 public static function getAvailability()
22 {
23 static $result = null;
24 if ($result !== null)
25 {
26 return $result;
27 }
28
29 $result = true;
30/*
31
32 if (!ModuleManager::isModuleInstalled('bitrix24'))
33 {
34 return $result;
35 }
36
37 if (Loader::includeModule('bitrix24'))
38 {
39 $result = (
40 !in_array(\CBitrix24::getLicenseType(), array('project'), true)
41 || \CBitrix24::isNfrLicense()
42 || \CBitrix24::isDemoLicense()
43 );
44 }
45*/
46 return $result;
47
48 }
49
50 public static function getViewData($params = [])
51 {
52 if (!is_array($params))
53 {
54 return false;
55 }
56
57 $contentId = ($params['contentId'] ?? false);
58 if (empty($contentId))
59 {
60 return false;
61 }
62
63 $result = [];
64
65 if (!is_array($contentId))
66 {
67 $contentId = [ $contentId ];
68 }
69
70 $res = UserContentViewTable::getList([
71 'filter' => [
72 '@CONTENT_ID' => $contentId
73 ],
74 'select' => [ 'CNT', 'CONTENT_ID', 'RATING_TYPE_ID', 'RATING_ENTITY_ID' ],
75 'runtime' => [
76 new ExpressionField('CNT', 'COUNT(*)')
77 ],
78 'group' => [ 'CONTENT_ID' ]
79 ]);
80
81 while ($content = $res->fetch())
82 {
83 $result[$content['CONTENT_ID']] = $content;
84 }
85
86 return $result;
87 }
88
89 public static function getUserList(array $params = []): array
90 {
91 global $USER;
92
93 $result = [
94 'items' => [],
95 'hiddenCount' => 0
96 ];
97
98 $contentId = (!empty($params['contentId']) ? $params['contentId'] : false);
99 $pageNum = (!empty($params['page']) ? (int)$params['page'] : 1);
100 $pathToUserProfile = (!empty($params['pathToUserProfile']) ? $params['pathToUserProfile'] : '');
101 $pageSize = 10;
102
103 if (
104 !$contentId
105 && $pageNum <= 0
106 )
107 {
108 return $result;
109 }
110
111 $select = [
112 'USER_ID',
113 'DATE_VIEW',
114 'USER_NAME' => 'USER.NAME',
115 'USER_LAST_NAME' => 'USER.LAST_NAME',
116 'USER_SECOND_NAME' => 'USER.SECOND_NAME',
117 'USER_LOGIN' => 'USER.LOGIN',
118 'USER_PERSONAL_PHOTO' => 'USER.PERSONAL_PHOTO',
119 'USER_PERSONAL_GENDER' => 'USER.PERSONAL_GENDER'
120 ];
121
122 $extranetInstalled = $mailInstalled = false;
123 $extranetIdList = [];
124
125 if (ModuleManager::isModuleInstalled('extranet'))
126 {
127 $extranetInstalled = true;
128 $select['USER_UF_DEPARTMENT'] = "USER.UF_DEPARTMENT";
129 }
130
131 if (IsModuleInstalled('mail'))
132 {
133 $mailInstalled = true;
134 $select['USER_EXTERNAL_AUTH_ID'] = "USER.EXTERNAL_AUTH_ID";
135 }
136
137 $queryParams = [
138 'order' => [
139 'DATE_VIEW' => 'DESC'
140 ],
141 'filter' => [
142 '=CONTENT_ID' => $contentId
143 ],
144 'select' => $select
145 ];
146
147 if (!$extranetInstalled)
148 {
149 $queryParams['limit'] = $pageSize;
150 $queryParams['offset'] = ($pageNum - 1) * $pageSize;
151 }
152
153 $userList = [];
154 $timeZoneOffset = \CTimeZone::getOffset();
155
156 $res = UserContentViewTable::getList($queryParams);
157
158 while ($fields = $res->fetch())
159 {
160 $photoSrc = '';
161
162 if ((int)$fields['USER_PERSONAL_PHOTO'] <= 0)
163 {
164 switch ($fields['USER_PERSONAL_GENDER'])
165 {
166 case "M":
167 $suffix = "male";
168 break;
169 case "F":
170 $suffix = "female";
171 break;
172 default:
173 $suffix = "unknown";
174 }
175 $fields['USER_PERSONAL_PHOTO'] = Option::get('socialnetwork', 'default_user_picture_' . $suffix, false, SITE_ID);
176 }
177
178 if (
179 !empty($fields['USER_PERSONAL_PHOTO'])
180 && (int)$fields['USER_PERSONAL_PHOTO'] > 0
181 )
182 {
183 $file = \CFile::resizeImageGet(
184 $fields["USER_PERSONAL_PHOTO"],
185 [ 'width' => 58, 'height' => 58 ],
186 BX_RESIZE_IMAGE_EXACT,
187 false,
188 false,
189 true
190 );
191 $photoSrc = $file["src"];
192 }
193
194 $userFields = [
195 'NAME' => $fields['USER_NAME'],
196 'LAST_NAME' => $fields['USER_LAST_NAME'],
197 'SECOND_NAME' => $fields['USER_SECOND_NAME'],
198 'LOGIN' => $fields['USER_LOGIN'],
199 ];
200
201 $userType = '';
202 if (
203 $mailInstalled
204 && $fields["USER_EXTERNAL_AUTH_ID"] === "email"
205 )
206 {
207 $userType = "mail";
208 }
209 elseif (
210 $extranetInstalled
211 && (
212 empty($fields["USER_UF_DEPARTMENT"])
213 || (int)$fields["USER_UF_DEPARTMENT"][0] <= 0
214 )
215 )
216 {
217 $userType = "extranet";
218 $extranetIdList[] = $fields["USER_ID"];
219 }
220
221 $dateView = (
222 $fields['DATE_VIEW'] instanceof \Bitrix\Main\Type\DateTime
223 ? $fields['DATE_VIEW']->toString()
224 : ''
225 );
226
227 $userList[$fields['USER_ID']] = [
228 'ID' => $fields['USER_ID'],
229 'TYPE' => $userType,
230 'URL' => \CUtil::jSEscape(\CComponentEngine::makePathFromTemplate($pathToUserProfile, [
231 "UID" => $fields["USER_ID"],
232 "user_id" => $fields["USER_ID"],
233 "USER_ID" => $fields["USER_ID"]
234 ])),
235 'PHOTO_SRC' => $photoSrc,
236 'FULL_NAME' => \CUser::formatName(\CSite::getNameFormat(), $userFields, true, true),
237 'DATE_VIEW' => $dateView,
238 'DATE_VIEW_FORMATTED' => (
239 !empty($dateView)
240 ? \CComponentUtil::getDateTimeFormatted(MakeTimeStamp($dateView), "FULL", $timeZoneOffset)
241 : ''
242 )
243 ];
244 }
245
246 $userIdToCheckList = [];
247
248 if (Loader::includeModule('extranet'))
249 {
250 $userIdToCheckList = (
251 \CExtranet::isIntranetUser(SITE_ID, $USER->getId())
252 ? $extranetIdList
253 : array_keys($userList)
254 );
255 }
256
257 if (!empty($userIdToCheckList))
258 {
259 $myGroupsUserList = \CExtranet::getMyGroupsUsersSimple(\CExtranet::getExtranetSiteID());
260 foreach ($userIdToCheckList as $userIdToCheck)
261 {
262 if (
263 !in_array($userIdToCheck, $myGroupsUserList)
264 && $userIdToCheck != $USER->getId()
265 )
266 {
267 unset($userList[$userIdToCheck]);
268 $result['hiddenCount']++;
269 }
270 }
271 }
272
273 if (!$extranetInstalled)
274 {
275 $result['items'] = $userList;
276 }
277 elseif ($pageNum <= ((count($userList) / $pageSize) + 1))
278 {
279 $res = new \CDBResult();
280 $res->initFromArray($userList);
281 $res->navStart($pageSize, false, $pageNum);
282
283 while($user = $res->fetch())
284 {
285 $result['items'][] = $user;
286 }
287 }
288 else
289 {
290 $result['items'] = [];
291 }
292
293 return $result;
294 }
295
296 public static function deleteNoDemand($userId = 0): bool
297 {
298 $userId = (int)$userId;
299 if ($userId <= 0)
300 {
301 return false;
302 }
303
304 $result = true;
305
306 try
307 {
308 \Bitrix\Main\Application::getConnection()->queryExecute("DELETE FROM ".UserContentViewTable::getTableName()." WHERE USER_ID = ".$userId);
309 }
310 catch (SqlQueryException $exception)
311 {
312 $result = false;
313 }
314
315 return $result;
316 }
317
318 public static function set(array $params = []): void
319 {
320 $xmlIdList = (
321 isset($params["xmlIdList"])
322 && is_array($params["xmlIdList"])
323 ? $params["xmlIdList"]
324 : []
325 );
326
327 $context = ($params['context'] ?? '');
328
329 $userId = (
330 isset($params['userId'])
331 && (int)$params['userId'] > 0
332 ? (int)$params['userId'] :
333 0
334 );
335
336 if (!empty($xmlIdList))
337 {
338 foreach ($xmlIdList as $val)
339 {
340 $xmlId = $val['xmlId'];
341 $save = (
342 !isset($val['save'])
343 || $val['save'] !== 'N'
344 );
345
346 [ $entityType, $entityId ] = self::parseXmlId($xmlId);
347
348 if (
349 !empty($entityType)
350 && $entityId > 0
351 )
352 {
353 $provider = Livefeed\Provider::init([
354 'ENTITY_TYPE' => $entityType,
355 'ENTITY_ID' => $entityId,
356 ]);
357 if ($provider)
358 {
359 $hasPermissions = true;
360 if (
361 isset($val['checkAccess'])
362 && $val['checkAccess'] === true
363 )
364 {
365 $provider->setOption('checkAccess', true);
366 $provider->initSourceFields();
367
368 if (empty($provider->getSourceFields()))
369 {
370 $hasPermissions = false;
371 }
372 }
373
374 if ($hasPermissions)
375 {
376 $provider->setContentView([
377 'save' => $save,
378 ]);
379 }
380
381/*
382TODO: https://bitrix24.team/company/personal/user/15/tasks/task/view/167281/
383 $provider->deleteCounter([
384 'userId' => $this->getCurrentUser()->getId(),
385 'siteId' => SITE_ID
386 ]);
387*/
388 }
389 }
390 }
391
393 'userId' => $userId,
394 'context' => $context
395 ]);
396 }
397 }
398
399 public static function finalize($params = []): bool
400 {
401 $userId = (!empty($params['userId']) ? (int)$params['userId'] : 0);
402 $context = (!empty($params['context']) ? $params['context'] : '');
403
404 if (!$userId)
405 {
406 return false;
407 }
408
409 if (
410 $context !== 'forum.comments/mobile'
411 && ModuleManager::isModuleInstalled('tasks')
412 )
413 {
414 $taskIdList = \Bitrix\Socialnetwork\Integration\Forum\TaskComment::getViewedCommentsTasksList();
415 if (!empty($taskIdList))
416 {
417 $event = new \Bitrix\Main\Event(
418 'socialnetwork', 'onContentFinalizeView',
419 [
420 'userId' => $userId,
421 'commentsTaskIdList' => $taskIdList
422 ]
423 );
424 $event->send();
425 }
426 }
427
428 return true;
429 }
430
431 public static function parseXmlId(string $xmlId = ''): array
432 {
433 $tmp = explode('-', $xmlId, 2);
434 $entityType = trim($tmp[0]);
435 $entityId = (int)$tmp[1];
436
437 return [ $entityType, $entityId ];
438 }
439}