Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
projecttagprovider.php
1<?php
3
4use Bitrix\Main\Entity\Query;
5use Bitrix\Main\Entity\Query\Join;
6use Bitrix\Main\Entity\ReferenceField;
8use Bitrix\SocialNetwork\EO_WorkgroupTag ;
9use Bitrix\SocialNetwork\EO_WorkgroupTag_Collection ;
19
26{
27 private static $entityId = 'project-tag';
28 private static $maxCount = 100;
29
30 public function __construct(array $options = [])
31 {
32 parent::__construct();
33
34 $this->options['groupId'] = $options['groupId'];
35 }
36
37 public function isAvailable(): bool
38 {
39 return $GLOBALS['USER']->isAuthorized();
40 }
41
42 public function getItems(array $ids): array
43 {
44 return [];
45 }
46
47 public function getSelectedItems(array $ids): array
48 {
49 return [];
50 }
51
52 public function getTagItems(array $options = []): array
53 {
54 return $this->makeTagItems($this->getTagCollection($options), $options);
55 }
56
57 public function getTagCollection(array $options = []): EO_WorkgroupTag_Collection
58 {
59 $options = array_merge($this->getOptions(), $options);
60
61 return self::getTags($options);
62 }
63
64 public static function getTags(array $options = []): EO_WorkgroupTag_Collection
65 {
66 $query = WorkgroupTagTable::query();
67 $query->setSelect(['NAME', 'GROUP_ID']);
68
69 if (($options['selected'] ?? null) && $options['groupId'])
70 {
71 $query->where('GROUP_ID', $options['groupId']);
72 }
73
74 if (!empty($options['searchQuery'] ?? null) && is_string($options['searchQuery']))
75 {
76 $query
77 ->setDistinct(true)
78 ->registerRuntimeField(
79 'G',
80 new ReferenceField(
81 'G',
82 WorkgroupTable::getEntity(),
83 Join::on('this.GROUP_ID', 'ref.ID'),
84 ['join_type' => 'left']
85 )
86 )
87 ->registerRuntimeField(
88 'UG',
89 new ReferenceField(
90 'UG',
91 UserToGroupTable::getEntity(),
92 Join::on('this.GROUP_ID', 'ref.GROUP_ID'),
93 ['join_type' => 'left']
94 )
95 )
96 ->where(
97 Query::filter()
98 ->logic('or')
99 ->where('G.VISIBLE', 'Y')
100 ->where(
101 Query::filter()
102 ->whereNotNull('UG.ID')
103 ->whereIn('UG.ROLE', UserToGroupTable::getRolesMember())
104 )
105 )
106 ->whereLike('NAME', "{$options['searchQuery']}%")
107 ;
108 }
109
110 return $query->exec()->fetchCollection();
111 }
112
113 public function makeTagItems(EO_WorkgroupTag_Collection $tags, array $options = []): array
114 {
115 return self::makeItems($tags, array_merge($this->getOptions(), $options));
116 }
117
123 public static function makeItems(EO_WorkgroupTag_Collection $tags, array $options = []): array
124 {
125 $result = [];
126 foreach ($tags as $tag)
127 {
128 $result[] = self::makeItem($tag, $options);
129 }
130
131 return $result;
132 }
133
140 public static function makeItem(EO_WorkgroupTag $tag, array $options = []): Item
141 {
142 return new Item([
143 'id' => $tag->getName(),
144 'entityId' => self::$entityId,
145 'title' => $tag->getName(),
146 'selected' => (isset($options['selected']) && $options['selected']),
147 'tabs' => ['all'],
148 ]);
149 }
150
151 public function doSearch(SearchQuery $searchQuery, Dialog $dialog): void
152 {
153 $dialog->addItems(
154 $this->getTagItems(['searchQuery' => $searchQuery->getQuery()])
155 );
156 }
157
158 public function fillDialog(Dialog $dialog): void
159 {
160 $dialog->addTab(
161 new Tab([
162 'id' => 'all',
163 'title' => Loc::getMessage('SOCNET_ENTITY_SELECTOR_PROJECT_TAG_TAB_TITLE'),
164 'stub' => true,
165 ])
166 );
167
168 $options = $this->getOptions();
169 if ($options['groupId'])
170 {
171 $dialog->addItems(
172 $this->getTagItems(['selected' => true])
173 );
174 }
175
176 if ($dialog->getItemCollection()->count() < self::$maxCount)
177 {
178 $this->fillWithRecentTags($dialog);
179 }
180 }
181
182 private function fillWithRecentTags(Dialog $dialog): void
183 {
184 $recentItems = $dialog->getRecentItems()->getAll();
185 foreach ($recentItems as $item)
186 {
188 if ($dialog->getItemCollection()->get(self::$entityId, $item->getId()))
189 {
190 continue;
191 }
192
193 $name = (string)$item->getId();
194 $dialog->addItem(
195 new Item([
196 'id' => $name,
197 'entityId' => self::$entityId,
198 'title' => $name,
199 'selected' => false,
200 'tabs' => ['all'],
201 ])
202 );
203
204 if ($dialog->getItemCollection()->count() >= self::$maxCount)
205 {
206 break;
207 }
208 }
209 }
210}
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
makeTagItems(EO_WorkgroupTag_Collection $tags, array $options=[])
static makeItems(EO_WorkgroupTag_Collection $tags, array $options=[])