1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
RelationProvider.php
См. документацию.
1<?php
2
3declare(strict_types=1);
4
5namespace Bitrix\Im\V2\Relation\Provider;
6
7use Bitrix\Im\Model\RelationTable;
8use Bitrix\Im\V2\Chat;
9use Bitrix\Im\V2\Common\ContextCustomer;
10use Bitrix\Im\V2\Relation;
11use Bitrix\Im\V2\RelationCollection;
12use Bitrix\Main\ORM\Fields\ExpressionField;
13use Bitrix\Main\ORM\Fields\IntegerField;
14use Bitrix\Main\ORM\Query\Query;
15
17{
18 use ContextCustomer;
19
20 public const ROLE_PRIORITY_MAP = [
21 Chat::ROLE_OWNER => 1,
22 Chat::ROLE_MANAGER => 2,
23 Chat::ROLE_MEMBER => 3,
24 ];
25
26 protected Chat $chat;
27
28 public function __construct(int $chatId)
29 {
30 $this->chat = Chat::getInstance($chatId);
31 }
32
33 public function getMembers(?int $limit = 50, ?RelationCursor $cursor = null): RelationCollection
34 {
35 $query = RelationTable::query()
37 ->where('CHAT_ID', (int)$this->chat->getId())
38 ->where('IS_HIDDEN', false)
39 ->where('USER.ACTIVE', true)
40 ->setOrder(['ROLE_PRIORITY', 'ID'])
41 ->setLimit($limit)
42 ;
43
44 $this->prepareQuery($query, $cursor);
45
46 return new RelationCollection($query->fetchAll());
47 }
48
49 public function getAllMemberIds(): array
50 {
51 return $this
52 ->chat
53 ->getRelations()
54 ->filter(static fn (Relation $relation) => !$relation->isHidden())
55 ->getUserIds()
56 ;
57 }
58
59 protected function prepareQuery(Query $query, ?RelationCursor $cursor): void
60 {
61 $this->defineRoles($query);
62 if ($cursor)
63 {
64 $this->applyCursor($query, $cursor);
65 }
66 }
67
68 protected function defineRoles(Query $query): void
69 {
70 $chatAuthorId = (int)$this->chat->getAuthorId();
71 $ownerRole = Chat::ROLE_OWNER;
72 $managerRole = Chat::ROLE_MANAGER;
73 $memberRole = Chat::ROLE_MEMBER;
74 $rolePriorityMap = self::ROLE_PRIORITY_MAP;
75
76 $roleField = new ExpressionField(
77 'ROLE',
78 "CASE
79 WHEN %s = {$chatAuthorId} THEN '{$ownerRole}'
80 WHEN %s = 'Y' THEN '{$managerRole}'
81 ELSE '{$memberRole}'
82 END",
83 ['USER_ID', 'MANAGER']
84 );
85 $query->registerRuntimeField('ROLE', $roleField);
86
87 $rolePriorityField = new ExpressionField(
88 'ROLE_PRIORITY',
89 "CASE
90 WHEN %s = '{$ownerRole}' THEN {$rolePriorityMap[$ownerRole]}
91 WHEN %s = '{$managerRole}' THEN {$rolePriorityMap[$managerRole]}
92 ELSE {$rolePriorityMap[$memberRole]}
93 END",
94 ['ROLE', 'ROLE']
95 );
96 $rolePriorityField->configureValueType(IntegerField::class);
97 $query->registerRuntimeField($rolePriorityField);
98 }
99
100 protected function applyCursor(Query $query, RelationCursor $cursor): void
101 {
102 $rolePriority = self::ROLE_PRIORITY_MAP[$cursor->role];
103 $filter = Query::filter()
104 ->logic('or')
105 ->where('ROLE_PRIORITY', '>', $rolePriority)
106 ->where(
107 Query::filter()
108 ->where('ROLE_PRIORITY', $rolePriority)
109 ->where('ID', '>', $cursor->relationId)
110 )
111 ;
112 $query->where($filter);
113 }
114}
applyCursor(Query $query, RelationCursor $cursor)
Определения RelationProvider.php:100
getMembers(?int $limit=50, ?RelationCursor $cursor=null)
Определения RelationProvider.php:33
prepareQuery(Query $query, ?RelationCursor $cursor)
Определения RelationProvider.php:59
static getInstance()
Определения application.php:98
</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
$filter
Определения iblock_catalog_list.php:54
Определения chain.php:3