1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
groupregistry.php
См. документацию.
1<?php
2
3declare(strict_types=1);
4
5namespace Bitrix\Socialnetwork\Internals\Registry;
6
7use Bitrix\Main\ArgumentException;
8use Bitrix\Main\ObjectPropertyException;
9use Bitrix\Main\SystemException;
10use Bitrix\Main\Type\DateTime;
11use Bitrix\Socialnetwork\Collab\Collab;
12use Bitrix\Socialnetwork\Collab\Integration\IM\Dialog;
13use Bitrix\Socialnetwork\Internals\Member\MemberEntityCollection;
14use Bitrix\Socialnetwork\Internals\Registry\Event\GroupLoadedEvent;
15use Bitrix\Socialnetwork\Internals\site\SiteEntityCollection;
16use Bitrix\Socialnetwork\Item\Workgroup;
17use Bitrix\Socialnetwork\Item\Workgroup\Type;
18use Bitrix\Socialnetwork\Space\Member;
19use Bitrix\Socialnetwork\UserToGroupTable;
20use Bitrix\Socialnetwork\WorkgroupTable;
21use Bitrix\Socialnetwork\Integration\Im\Chat;
22
24{
25 protected const UF_ENTITY_ID = 'SONET_GROUP';
26
27 protected static array $storage = [];
28
29 private static ?self $instance = null;
30
31 public static function getInstance(): static
32 {
33 if (static::$instance === null)
34 {
35 static::$instance = new static();
36 }
37
38 return static::$instance;
39 }
40
41 protected function __construct()
42 {
43
44 }
45
51 public function get(int $groupId): ?Workgroup
52 {
53 if ($groupId <= 0)
54 {
55 return null;
56 }
57
58 if (isset(static::$storage[$groupId]))
59 {
60 $this->onObjectAlreadyLoaded(static::$storage[$groupId]);
61
62 return static::$storage[$groupId];
63 }
64
65 $this->load($groupId);
66
67 return static::$storage[$groupId];
68 }
69
70 public function invalidate(int $groupId): static
71 {
72 unset(static::$storage[$groupId]);
73
74 return $this;
75 }
76
82 protected function load(int $groupId): void
83 {
84 $fields = $this->loadData($groupId);
85 if (empty($fields))
86 {
87 static::$storage[$groupId] = null;
88
89 return;
90 }
91
92 $this->fillStorage($fields);
93 }
94
95 protected function loadData(int $groupId): array
96 {
97 $select = [
98 'select' => [
99 '*',
100 'SITES',
101 'MEMBERS',
102 ],
103 ];
104
105 $fields = WorkgroupTable::getByPrimary($groupId, $select)->fetchObject()?->collectValues();
106
107 if (empty($fields))
108 {
109 return [];
110 }
111
112 $this->fillDates($fields);
113 $this->fillUserFields($fields);
114 $this->fillChatId($fields);
115 $this->fillUserMembers($fields);
116 $this->fillSites($fields);
117
119
120 return $fields;
121 }
122
123 protected function onObjectAlreadyLoaded(?Workgroup $group): void
124 {
125
126 }
127
128 protected function onObjectLoaded(Workgroup $group): void
129 {
130 $event = new GroupLoadedEvent($group);
131
132 $event->send();
133 }
134
135 protected function fillStorage(array $fields): void
136 {
137 $groupId = (int)$fields['ID'];
138 $groupType = Type::tryFrom((string)$fields['TYPE']);
139
140 if ($groupType === Type::Collab)
141 {
142 static::$storage[$groupId] = new Collab($fields);
143 }
144 else
145 {
146 static::$storage[$groupId] = new Workgroup($fields);
147 }
148
149 $this->onObjectLoaded(static::$storage[$groupId]);
150 }
151
152 protected function fillDates(array &$fields): void
153 {
154 if ($fields['DATE_CREATE'] instanceof DateTime)
155 {
156 $fields['DATE_CREATE'] = $fields['DATE_CREATE']->toString();
157 }
158 if ($fields['DATE_UPDATE'] instanceof DateTime)
159 {
160 $fields['DATE_UPDATE'] = $fields['DATE_UPDATE']->toString();
161 }
162 if ($fields['DATE_ACTIVITY'] instanceof DateTime)
163 {
164 $fields['DATE_ACTIVITY'] = $fields['DATE_ACTIVITY']->toString();
165 }
166 }
167
168 protected function fillUserFields(array &$fields): void
169 {
170 $id = (int)$fields['ID'];
171
172 global $USER_FIELD_MANAGER;
173 $uf = $USER_FIELD_MANAGER->getUserFields(static::UF_ENTITY_ID, $id, false, 0);
174 if (is_array($uf))
175 {
176 $fields = array_merge($fields, $uf);
177 }
178 }
179
180 protected function fillChatId(array &$fields): void
181 {
182 $id = (int)$fields['ID'];
183
184 $chat = Chat\Workgroup::getChatData([
185 'group_id' => $id,
186 'skipAvailabilityCheck' => true,
187 ]);
188
189 $chatId = (int)($chat[$id] ?? 0);
190
191 $fields['CHAT_ID'] = $chatId;
192
193 $fields['DIALOG_ID'] = Dialog::getDialogId($chatId);
194 }
195
196 protected function fillUserMembers(array &$fields): void
197 {
198 $users = $fields['MEMBERS'];
199
200 if (!$users instanceof MemberEntityCollection)
201 {
202 return;
203 }
204
205 if ($users->isEmpty())
206 {
207 return;
208 }
209
210 $users = array_map(static fn (Member $member): array => $member->collectValues(), iterator_to_array($users));
211
212 $members = array_filter($users, static fn (array $member): bool => in_array($member['ROLE'], UserToGroupTable::getRolesMember(), true));
213 $memberIds = array_column($members, 'USER_ID');
214
215 $fields['MEMBERS'] = $memberIds;
216
217 $ordinaryMembers = array_filter($users, static fn (array $member): bool => $member['ROLE'] === UserToGroupTable::ROLE_USER);
218 $ordinaryMembersIds = array_column($ordinaryMembers, 'USER_ID');
219
220 $fields['ORDINARY_MEMBERS'] = $ordinaryMembersIds;
221
222 $requested = array_filter($users, static fn (array $member): bool => $member['ROLE'] === UserToGroupTable::ROLE_REQUEST);
223 $requestedIds = array_column($requested, 'USER_ID');
224
225 $fields['INVITED_MEMBERS'] = $requestedIds;
226
227 $moderators = array_filter($users, static fn (array $member): bool => $member['ROLE'] === UserToGroupTable::ROLE_MODERATOR);
228 $moderatorsIds = array_column($moderators, 'USER_ID');
229
230 $fields['MODERATOR_MEMBERS'] = $moderatorsIds;
231 }
232
233 protected function fillSites(array &$fields): void
234 {
235 if (!$fields['SITES'] instanceof SiteEntityCollection)
236 {
237 return;
238 }
239
240 $fields['SITE_IDS'] = $fields['SITES']->getSiteIdList();
241 }
242
243 protected function remapBooleanFields(array &$fields): void
244 {
245 $entity = WorkgroupTable::getEntity();
246
247 foreach ($fields as $key => $value)
248 {
249 try
250 {
251 $field = $entity->getField($key);
252 }
253 catch (ArgumentException)
254 {
255 continue;
256 }
257
258 if ($field->getDataType() === 'boolean')
259 {
260 $fields[$key] = $value ? 'Y' : 'N';
261 }
262 }
263 }
264}
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
global $USER_FIELD_MANAGER
Определения attempt.php:6
$entity
$select
Определения iblock_catalog_list.php:194
$event
Определения prolog_after.php:141
if(empty($signedUserToken)) $key
Определения quickway.php:257
$fields
Определения yandex_run.php:501