1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
CategoryProvider.php
См. документацию.
1<?php
2
3namespace Bitrix\Calendar\OpenEvents\Provider;
4
5use Bitrix\Calendar\Core\Common;
6use Bitrix\Calendar\EventCategory\Dto\EventCategoryPermissions;
7use Bitrix\Calendar\EventCategory\EventCategoryAccess;
8use Bitrix\Calendar\Internals\Counter;
9use Bitrix\Calendar\OpenEvents\Internals\Collection\OpenEventCategoryAttendeeCollection;
10use Bitrix\Calendar\OpenEvents\Internals\Collection\OpenEventCategoryCollection;
11use Bitrix\Calendar\OpenEvents\Internals\Entity\OpenEventCategory;
12use Bitrix\Calendar\OpenEvents\Internals\Entity\OpenEventCategoryAttendee;
13use Bitrix\Calendar\OpenEvents\Internals\OpenEventCategoryAttendeeTable;
14use Bitrix\Calendar\OpenEvents\Internals\OpenEventCategoryTable;
15use Bitrix\Calendar\OpenEvents\Item\Category;
16use Bitrix\Calendar\OpenEvents\Provider;
17use Bitrix\Calendar\OpenEvents\Provider\Category\Enum\CategoryOrderEnum;
18use Bitrix\Main\Engine\CurrentUser;
19use Bitrix\Calendar\OpenEvents\Service\DefaultCategoryService;
20use Bitrix\Main\Entity\ReferenceField;
21use Bitrix\Main\Localization\Loc;
22use Bitrix\Main\ORM\Query\Join;
23use Bitrix\Main\ORM\Query\Query;
24use Bitrix\Main\Text\Emoji;
25
27{
28 private int $userId;
29
30 public function __construct(?int $userId = null)
31 {
32 $this->userId = $userId ?? (int)CurrentUser::get()->getId();
33 }
34
36 {
37 $collection = new OpenEventCategoryCollection();
38
39 $categoryResult = OpenEventCategoryTable::query()
40 ->setSelect(['ID', 'CHANNEL_ID', 'NAME'])
41 ->whereIn('ID', $categoryIds)
42 ->exec()
43 ;
44
45 while($category = $categoryResult->fetchObject())
46 {
47 $category->setName($this->prepareCategoryName($category->getName()));
48 $collection->add($category);
49 }
50
51 return $collection;
52 }
53
54 public function getAllCategory(): Category
55 {
56 return new Category(
57 id: 0,
58 closed: false,
59 name: Loc::getMessage('CALENDAR_OPEN_EVENTS_ALL_EVENTS'),
60 description: Loc::getMessage('CALENDAR_OPEN_EVENTS_ALL_EVENTS'),
61 creatorId: 0,
62 eventsCount: 0,
63 permissions: new EventCategoryPermissions(false, false),
64 channelId: -1,
65 isMuted: false,
67 );
68 }
69
70 public function getByChannelId(int $channelId): ?Category
71 {
72 $category = OpenEventCategoryTable::query()
73 ->where('CHANNEL_ID', $channelId)
74 ->fetchObject()
75 ;
76
77 if (!$category)
78 {
79 return null;
80 }
81
82 return new Category(
83 id: $category->getId(),
84 closed: $category->getClosed(),
85 name: $category->getName(),
86 description: $category->getDescription(),
87 creatorId: $category->getCreatorId(),
88 eventsCount: $category->getEventsCount(),
89 permissions: new EventCategoryPermissions(false, false),
90 channelId: $category->getChannelId(),
91 updatedAt: $category->getLastActivity()->getTimestamp(),
92 );
93 }
94
98 public function list(Provider\Category\Query $categoryQuery): array
99 {
100 $query = OpenEventCategoryAttendeeTable::query();
101
102 $this->prepareSelect($query);
103 $this->prepareFilter($query, $categoryQuery);
104 $this->prepareLimit($query, $categoryQuery);
105 $this->prepareOrder($query, $categoryQuery);
106
107 $categoryCollection = $query->fetchCollection();
108
109 return $this->prepareResult($categoryCollection, $categoryQuery);
110 }
111
112 private function prepareSelect(Query $query): void
113 {
114 $query->registerRuntimeField($this->getCategoryReference());
115
116 $query->setSelect([
117 'CATEGORY_ID',
118 'CATEGORY.NAME',
119 'CATEGORY.DESCRIPTION',
120 'CATEGORY.CLOSED',
121 'CATEGORY.CREATOR_ID',
122 'CATEGORY.EVENTS_COUNT',
123 'CATEGORY.CHANNEL_ID',
124 'CATEGORY.LAST_ACTIVITY',
125 ]);
126 }
127
128 private function prepareFilter(Query $query, Provider\Category\Query $categoryQuery): void
129 {
130 $query->whereIn('USER_ID', array_unique([Common::SYSTEM_USER_ID, $this->userId]));
131
132 if (isset($categoryQuery->filter->isBanned))
133 {
134 $bannedCategoryIds = (new CategoryBanProvider($this->userId))->listIds();
135 if (!empty($bannedCategoryIds))
136 {
137 if ($categoryQuery->filter->isBanned)
138 {
139 $query->whereIn('CATEGORY_ID', $bannedCategoryIds);
140 }
141 else
142 {
143 $query->whereNotIn('CATEGORY_ID', $bannedCategoryIds);
144 }
145 }
146 }
147
148 if (!empty($categoryQuery->filter->channelId))
149 {
150 $query->where('CATEGORY.CHANNEL_ID', $categoryQuery->filter->channelId);
151 }
152
153 if (!empty($categoryQuery->filter->query))
154 {
155 $query->whereLike('CATEGORY.NAME', '%' . Emoji::encode($categoryQuery->filter->query) . '%');
156 }
157
158 if (!empty($categoryQuery->filter->categoryId))
159 {
160 $query->where('CATEGORY_ID', $categoryQuery->filter->categoryId);
161 }
162 }
163
164 private function prepareLimit(Query $query, Provider\Category\Query $categoryQuery): void
165 {
166 if (!empty($categoryQuery->limit))
167 {
168 $query->setOffset($categoryQuery->page * $categoryQuery->limit);
169 $query->setLimit($categoryQuery->limit);
170 }
171 }
172
173 private function prepareOrder(Query $query, Provider\Category\Query $categoryQuery): void
174 {
175 if (!empty($categoryQuery->order))
176 {
177 $query->addOrder(...$this->getOrder($categoryQuery->order));
178 }
179 }
180
181 private function getOrder(CategoryOrderEnum $order): array
182 {
183 return match ($order) {
184 CategoryOrderEnum::BY_ACTIVITY => ['CATEGORY.LAST_ACTIVITY', 'DESC'],
185 CategoryOrderEnum::BY_NAME => ['CATEGORY.NAME', 'ASC'],
186 };
187 }
188
192 private function prepareResult(
193 OpenEventCategoryAttendeeCollection $categoryCollection,
194 Provider\Category\Query $categoryQuery,
195 ): array
196 {
197 $categoryIds = $categoryCollection->getCategoryIdList();
198
199 $defaultCategoryMatchesSearchString = !empty($categoryQuery->filter->query)
200 && mb_stripos($this->getDefaultCategoryName(), $categoryQuery->filter->query) !== false;
201
202 if ($categoryQuery->requireDefault || $defaultCategoryMatchesSearchString)
203 {
204 $defaultCategoryId = DefaultCategoryService::getInstance()->getCategoryId();
205
206 if (!in_array($defaultCategoryId, $categoryIds, true))
207 {
208 $categoryCollection->add($this->getDefaultRow());
209
210 $categoryIds[] = $defaultCategoryId;
211 }
212 }
213
214 if ($categoryCollection->isEmpty())
215 {
216 return [];
217 }
218
219 $mutedCategories = (new CategoryMuteProvider($this->userId))->getByCategoryIds($categoryIds);
220
221 if (isset($categoryQuery->filter->isBanned))
222 {
223 $bannedCategoryIds = $categoryQuery->filter->isBanned ? $categoryIds : [];
224 }
225 else
226 {
227 $bannedCategoryIds = (new CategoryBanProvider($this->userId))->listIds();
228 }
229
230 $counter = Counter::getInstance($this->userId);
231
232 $categories = [];
233 foreach ($categoryCollection as $row)
234 {
238 $category = $row->get('CATEGORY');
239
240 if (!$category)
241 {
242 continue;
243 }
244
245 $name = $this->prepareCategoryName($category->getName());
246
247 if (
248 $category->getName() === DefaultCategoryService::DEFAULT_CATEGORY_NAME
249 && !empty($categoryQuery->filter->query)
250 && mb_stripos($name, $categoryQuery->filter->query) === false
251 )
252 {
253 continue;
254 }
255
256 $categories[] = new Category(
257 id: $category->getId(),
258 closed: $category->getClosed(),
259 name: $name,
260 description: $this->prepareCategoryDescription($category->getDescription()),
261 creatorId: $category->getCreatorId(),
262 eventsCount: $category->getEventsCount(),
263 permissions: EventCategoryAccess::getPermissionsForEntity($category, $this->userId),
264 channelId: $category->getChannelId(),
265 isMuted: $mutedCategories[$category->getId()] ?? false,
266 isBanned: in_array($category->getId(), $bannedCategoryIds, true),
267 newCount: $counter->get(Counter\CounterDictionary::COUNTER_OPEN_EVENTS, $category->getId()),
268 updatedAt: $category->getLastActivity()->getTimestamp(),
269 );
270 }
271
272 return $categories;
273 }
274
275 public function prepareCategoryName(string $name): string
276 {
277 if ($name === DefaultCategoryService::DEFAULT_CATEGORY_NAME)
278 {
279 return $this->getDefaultCategoryName();
280 }
281
282 return Emoji::decode($name);
283 }
284
285 protected function getDefaultCategoryName(): string
286 {
287 return Loc::getMessage('CALENDAR_OPEN_EVENTS_DEFAULT_CATEGORY_NAME');
288 }
289
290 public function prepareCategoryDescription(?string $description): string
291 {
292 if ($description === DefaultCategoryService::DEFAULT_CATEGORY_DESCRIPTION)
293 {
294 return Loc::getMessage('CALENDAR_OPEN_EVENTS_DEFAULT_CATEGORY_DESCRIPTION');
295 }
296
297 return Emoji::decode($description ?? '');
298 }
299
300 private function getDefaultRow(): OpenEventCategoryAttendee
301 {
302 $defaultCategory = DefaultCategoryService::getInstance()->getCategory();
303
304 $row = new OpenEventCategoryAttendee();
305 $row->entity->addField($this->getCategoryReference(),'CATEGORY');
306 $row->set('CATEGORY_ID', $defaultCategory->getId());
307 $row->set('CATEGORY', $defaultCategory);
308
309 return $row;
310 }
311
312 private function getCategoryReference(): ReferenceField
313 {
314 return new ReferenceField(
315 'CATEGORY',
316 OpenEventCategoryTable::getEntity(),
317 Join::on('this.CATEGORY_ID', 'ref.ID'),
318 );
319 }
320}
if(empty( $fields)) foreach($fields as $field) $channelId
Определения push.php:23
prepareCategoryDescription(?string $description)
Определения CategoryProvider.php:290
list(Provider\Category\Query $categoryQuery)
Определения CategoryProvider.php:98
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
if(Loader::includeModule( 'bitrix24')) elseif(Loader::includeModule('intranet') &&CIntranetUtils::getPortalZone() !=='ru') $description
Определения .description.php:24
$name
Определения menu_edit.php:35
Определения chain.php:3
$order
Определения payment.php:8
$counter
Определения options.php:5