Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
entity.php
1<?php
3
4use \Bitrix\Landing\Internals\BindingTable;
5use \Bitrix\Landing\Landing;
6use \Bitrix\Landing\Rights;
7use \Bitrix\Landing\Site;
8use \Bitrix\Main;
9
10abstract class Entity
11{
15 const ENTITY_TYPE_SITE = 'S';
16
21
26 protected static $bindingType = null;
27
32 protected $bindingId = null;
33
38 public function __construct($code)
39 {
40 $this->bindingId = $code;
41 }
42
47 private static function clearCache(): void
48 {
49 if (defined('BX_COMP_MANAGED_CACHE'))
50 {
51 \Bitrix\Landing\Manager::getCacheManager()->clearByTag(
52 'intranet_menu_binding'
53 );
54 }
55 }
56
63 private function getBindingId($entityId, $entityType)
64 {
65 $res = BindingTable::getList([
66 'select' => [
67 'ID'
68 ],
69 'filter' => [
70 '=BINDING_TYPE' => static::$bindingType,
71 '=BINDING_ID' => $this->bindingId,
72 '=ENTITY_TYPE' => $entityType,
73 '=ENTITY_ID' => $entityId
74 ]
75 ]);
76 if ($row = $res->fetch())
77 {
78 return $row['ID'];
79 }
80 else
81 {
82 return 0;
83 }
84 }
85
92 private function addBinding($entityId, $entityType)
93 {
94 $res = BindingTable::add([
95 'BINDING_TYPE' => static::$bindingType,
96 'BINDING_ID' => $this->bindingId,
97 'ENTITY_TYPE' => $entityType,
98 'ENTITY_ID' => $entityId
99 ]);
100 if ($res->isSuccess())
101 {
102 return $res->getId();
103 }
104 else
105 {
106 return null;
107 }
108 }
109
115 private function deleteBindingId($bindingId)
116 {
117 $res = BindingTable::delete($bindingId);
118 $res->isSuccess();
119 }
120
127 private function bind($entityId, $entityType)
128 {
129 if (!$this->getBindingId($entityId, $entityType))
130 {
131 $this->clearCache();
132 return $this->addBinding($entityId, $entityType) > 0;
133 }
134 else
135 {
136 return false;
137 }
138 }
139
146 private function unbind($entityId, $entityType)
147 {
148 $bindingId = $this->getBindingId($entityId, $entityType);
149 if ($bindingId)
150 {
151 $this->clearCache();
152 $this->deleteBindingId($bindingId);
153 return true;
154 }
155 else
156 {
157 return false;
158 }
159 }
160
166 private static function getBindings($bindingId)
167 {
168 $items = [];
169
170 // filter
171 $filter = [
172 '=BINDING_TYPE' => static::$bindingType,
173 '=ENTITY_TYPE' => [
174 self::ENTITY_TYPE_SITE,
175 self::ENTITY_TYPE_LANDING
176 ]
177 ];
178 if ($bindingId !== null)
179 {
180 $filter['=BINDING_ID'] = $bindingId;
181 }
182
183 // runtime fields
184 $runtime = [];
185 $runtime[] = new Main\Entity\ReferenceField(
186 'SITE',
187 'Bitrix\Landing\Internals\SiteTable',
188 [
189 '=this.ENTITY_ID' => 'ref.ID',
190 '=this.ENTITY_TYPE' => [
191 '?', self::ENTITY_TYPE_SITE
192 ]
193 ]
194 );
195 $runtime[] = new Main\Entity\ReferenceField(
196 'LANDING',
197 'Bitrix\Landing\Internals\LandingTable',
198 [
199 '=this.ENTITY_ID' => 'ref.ID',
200 '=this.ENTITY_TYPE' => [
201 '?', self::ENTITY_TYPE_LANDING
202 ]
203 ]
204 );
205
206 // selecting
207 $urls = [
208 self::ENTITY_TYPE_SITE => [],
209 self::ENTITY_TYPE_LANDING => []
210 ];
211 $res = BindingTable::getList([
212 'select' => [
213 'ENTITY_ID',
214 'ENTITY_TYPE',
215 'BINDING_ID',
216 'SITE_TITLE' => 'SITE.TITLE',
217 'LANDING_TITLE' => 'LANDING.TITLE',
218 'SITE_DELETED' => 'SITE.DELETED',
219 'LANDING_DELETED' => 'LANDING.DELETED'
220 ],
221 'filter' => $filter,
222 'runtime' => $runtime,
223 'order' => [
224 'ID' => 'desc'
225 ]
226 ]);
227 while ($row = $res->fetch())
228 {
229 if (
230 $row['SITE_DELETED'] == 'Y' ||
231 $row['LANDING_DELETED'] == 'Y'
232 )
233 {
234 continue;
235 }
236 $urls[$row['ENTITY_TYPE']][] = $row['ENTITY_ID'];
237 $title = ($row['ENTITY_TYPE'] == self::ENTITY_TYPE_SITE)
238 ? $row['SITE_TITLE']
239 : $row['LANDING_TITLE'];
240 if (!$title)
241 {
242 continue;
243 }
244 $items[] = [
245 'ENTITY_ID' => $row['ENTITY_ID'],
246 'ENTITY_TYPE' => $row['ENTITY_TYPE'],
247 'BINDING_ID' => $row['BINDING_ID'],
248 'TITLE' => $title,
249 'PUBLIC_URL' => '',
250 ];
251 }
252
253 // get urls
254 if ($urls[self::ENTITY_TYPE_SITE])
255 {
256 $urls[self::ENTITY_TYPE_SITE] = Site::getPublicUrl(
257 $urls[self::ENTITY_TYPE_SITE]
258 );
259 }
260 if ($urls[self::ENTITY_TYPE_LANDING])
261 {
262 $landing = Landing::createInstance(0);
263 $urls[self::ENTITY_TYPE_LANDING] = $landing->getPublicUrl(
264 $urls[self::ENTITY_TYPE_LANDING]
265 );
266 }
267
268 // rebuild for urls
269 foreach ($items as &$item)
270 {
271 if (isset($urls[$item['ENTITY_TYPE']][$item['ENTITY_ID']]))
272 {
273 $item['PUBLIC_URL'] = $urls[$item['ENTITY_TYPE']][$item['ENTITY_ID']];
274 }
275 }
276 unset($item);
277
278 return $items;
279 }
280
286 public static function getList($code = null)
287 {
288 return self::getBindings($code);
289 }
290
296 public function bindSite(int $siteId): bool
297 {
298 $siteId = intval($siteId);
299
300 $success = $this->bind($siteId, $this::ENTITY_TYPE_SITE);
301
302 if ($success && method_exists($this, 'addSiteRights'))
303 {
304 $this->addSiteRights($siteId);
305 }
306
307 return $success;
308 }
309
316 public function rebindSite(int $siteId): bool
317 {
318 // this method only implements parent method currently
319
320 if (method_exists($this, 'updateSiteRights'))
321 {
322 $this->updateSiteRights($siteId);
323 }
324
325 return true;
326 }
327
333 public function unbindSite(int $siteId): bool
334 {
335 $siteId = intval($siteId);
336
337 $success = $this->unbind($siteId, $this::ENTITY_TYPE_SITE);
338
339 if ($success && method_exists($this, 'removeSiteRights'))
340 {
341 $this->removeSiteRights($siteId);
342 }
343
344 return $success;
345 }
346
352 public function bindLanding(int $landingId): bool
353 {
354 $landingId = intval($landingId);
355
356 $success = $this->bind($landingId, $this::ENTITY_TYPE_LANDING);
357
358 if ($success && method_exists($this, 'addLandingRights'))
359 {
360 $this->addLandingRights($landingId);
361 }
362
363 return $success;
364 }
365
371 public function unbindLanding(int $landingId): bool
372 {
373 $landingId = intval($landingId);
374
375 $success = $this->unbind($landingId, $this::ENTITY_TYPE_LANDING);
376
377 if ($success && method_exists($this, 'removeLandingRights'))
378 {
379 $this->removeLandingRights($landingId);
380 }
381
382 return $success;
383 }
384
390 public static function onSiteChange(int $siteId): void
391 {
392 if (defined('BX_COMP_MANAGED_CACHE'))
393 {
394 $res = BindingTable::getList([
395 'select' => [
396 'ID'
397 ],
398 'filter' => [
399 'ENTITY_ID' => $siteId,
400 '=ENTITY_TYPE' => self::ENTITY_TYPE_SITE
401 ],
402 'limit' => 1
403 ]);
404 if ($res->fetch())
405 {
406 self::clearCache();
407 }
408 }
409 }
410
411 public function isForbiddenBindingAction(): bool
412 {
413 return (
414 !Rights::hasAdditionalRight('extension', 'knowledge', false, true)
415 && Site\Type::getCurrentScopeId() !== 'GROUP'
416 );
417 }
418}
static onSiteChange(int $siteId)
Definition entity.php:390
bindLanding(int $landingId)
Definition entity.php:352
static getList($code=null)
Definition entity.php:286
unbindLanding(int $landingId)
Definition entity.php:371