19 private const RECENT_TAB_FILL_LIMIT = 20;
20 private const SEARCH_ITEMS_LIMIT = self::RECENT_TAB_FILL_LIMIT;
24 parent::__construct();
30 if (!(
$USER instanceof \CUser))
35 return $USER->IsAuthorized() &&
$USER->CanDoOperation(
'view_groups');
40 $recent = $dialog->getRecentItems();
42 $moreToFillCount = self::RECENT_TAB_FILL_LIMIT - $recent->count();
43 if ($moreToFillCount > 0)
45 $addedCount = $this->fillRecentFromGlobalContext($dialog, $moreToFillCount);
46 $moreToFillCount -= $addedCount;
49 if ($moreToFillCount > 0)
51 $this->fillRecentFromDB($dialog, $moreToFillCount);
54 $dialog->addTab(
new Tab([
55 'id' => self::ENTITY_ID,
56 'title' => Loc::getMessage(
'SOCNET_ENTITY_SELECTOR_SITE_GROUPS_TAB_TITLE'),
60 private function fillRecentFromGlobalContext(
Dialog $dialog,
int $maxItemsToAdd): int
62 $recent = $dialog->getRecentItems();
67 foreach ($dialog->getGlobalRecentItems()->getEntityItems(self::ENTITY_ID) as $globalRecentItem)
69 if ($addedCount >= $maxItemsToAdd)
74 $recent->add($globalRecentItem);
81 private function fillRecentFromDB(Dialog $dialog,
int $limit): void
83 $query = GroupTable::query()
85 ->where(
'ANONYMOUS',
'N')
86 ->whereNotNull(
'NAME')
90 $alreadyAddedIds = array_map(fn(RecentItem $item) => $item->getId(), $dialog->getRecentItems()->getAll());
91 ArrayHelper::normalizeArrayValuesByInt($alreadyAddedIds);
92 if (!empty($alreadyAddedIds))
94 $query->whereNotIn(
'ID', $alreadyAddedIds);
97 $ids =
$query->fetchCollection()->getIdList();
100 $dialog->getRecentItems()->add(
new RecentItem([
102 'entityId' => self::ENTITY_ID,
116 $groupItems = GroupTable::query()
117 ->setSelect([
'ID',
'NAME'])
118 ->where(
'ANONYMOUS',
'N')
119 ->whereNotNull(
'NAME')
120 ->whereIn(
'ID', $ids)
124 foreach ($groupItems as $groupItem)
127 'id' => $groupItem[
'ID'],
128 'entityId' => static::ENTITY_ID,
129 'title' => $groupItem[
'NAME'],
132 $item->addTab([
'site-groups']);
142 $items = GroupTable::query()
143 ->setSelect([
'ID',
'NAME'])
144 ->where(
'ANONYMOUS',
'N')
145 ->whereNotNull(
'NAME')
146 ->whereLike(
'NAME',
"%{$searchQuery->getQuery()}%")
147 ->setLimit(self::SEARCH_ITEMS_LIMIT)
153 $dialog->addItem(
new Item([
155 'entityId' => self::ENTITY_ID,
156 'title' => $item[
'NAME'],
160 $isTherePossiblyMoreResultsForThisQuery =
count(
$items) >= self::SEARCH_ITEMS_LIMIT;
161 $searchQuery->
setCacheable(!$isTherePossiblyMoreResultsForThisQuery);