1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
Manager.php
См. документацию.
1<?php
2
3namespace Bitrix\Landing\Mainpage;
4
5use Bitrix\AI\Integration;
6use Bitrix\Bitrix24\Feature;
7use Bitrix\Intranet\MainPage\Publisher;
8use Bitrix\Landing;
9use Bitrix\Landing\Site;
10use Bitrix\Landing\Rights;
11use Bitrix\Landing\Site\Type;
12use Bitrix\Main\Config\Option;
13use Bitrix\Main\EventManager;
14use Bitrix\Main\Loader;
15use Bitrix\Main\Localization\Loc;
16use Bitrix\Pull;
17
22{
23 private const SITE_ID_OPTION_CODE = 'mainpage_site_id';
24 private const CREATION_OPTION_CODE = 'mainpage_creating';
25 private const CREATION_OPTION_PROCESS = 'process';
26 private const CREATION_OPTION_FULL = 'fully';
27 private const USE_DEMO_OPTION_CODE = 'use_demo_data_in_block_widgets';
28 private const FREE_MODE_OPTION_CODE = 'enable_at_free_tariff';
29
33 private ?int $siteId = null;
34 private ?int $landingId = null;
35 private ?string $previewImg = null;
36 private ?string $pageTitle = null;
37 private ?string $scopeBefore = null;
38 private bool $rightsBefore;
39
44 public static function isAvailable(): bool
45 {
46 // not in SMN
47 return Loader::includeModule('intranet');
48 }
49
50 public static function isFeatureEnable(): bool
51 {
52 if (Loader::includeModule('bitrix24'))
53 {
54 return Feature::isFeatureEnabled('main_page');
55 }
56
57 return true;
58 }
59
63 public function __construct()
64 {
65 $this->onBeforeOperation();
66
67 $this->detectConnectedSite();
68 $this->detectConnectedPage();
69
70 $this->onAfterOperation();
71 }
72
77 private function detectConnectedSite(): void
78 {
79 if ($this->siteId)
80 {
81 return;
82 }
83
85
86 $optionSiteId = (int)Landing\Manager::getOption(self::SITE_ID_OPTION_CODE);
87
88 // check that exists
89 if ($optionSiteId > 0)
90 {
91 $connectedSite = (Landing\Site::getList([
92 'select' => ['LANDING_ID_INDEX'],
93 'filter' => [
94 '=ID' => $optionSiteId,
95 '=ACTIVE' => 'Y',
96 'TYPE' => Type::SCOPE_CODE_MAINPAGE,
97 '=SPECIAL' => 'Y',
98 'CHECK_PERMISSIONS' => 'N',
99 ],
100 'cache' => ['ttl' => 86400],
101 ]))->fetch();
102 if ($connectedSite)
103 {
104 $this->siteId = $optionSiteId;
105 if (!$this->landingId && $connectedSite['LANDING_ID_INDEX'] > 0)
106 {
107 $this->landingId = (int)$connectedSite['LANDING_ID_INDEX'];
108 }
110
111 return;
112 }
113 }
114
115 // try find
116 $exists = (Landing\Site::getList([
117 'select' => ['ID', 'TYPE', 'ACTIVE', 'LANDING_ID_INDEX'],
118 'filter' => [
119 '=ACTIVE' => 'Y',
120 'TYPE' => Type::SCOPE_CODE_MAINPAGE,
121 '=SPECIAL' => 'Y',
122 'CHECK_PERMISSIONS' => 'N',
123 ],
124 ]))->fetch();
125 if ($exists && (int)$exists['ID'])
126 {
127 $this->siteId = (int)$exists['ID'];
128 if (!$this->landingId && $exists['LANDING_ID_INDEX'] > 0)
129 {
130 $this->landingId = (int)$exists['LANDING_ID_INDEX'];
131 }
132 }
133 // create
134 else
135 {
136 $newId = $this->createDefaultSite();
137 if ($newId)
138 {
139 $this->siteId = $newId;
140 $this->landingId = null;
141 }
142 }
143
144 if ($this->siteId && $this->siteId !== $optionSiteId)
145 {
146 Landing\Manager::setOption(self::SITE_ID_OPTION_CODE, $this->siteId);
147 }
148
150 }
151
152 private function createDefaultSite(): ?int
153 {
154 $new = Landing\Site::add([
155 'TITLE' => Loc::getMessage('LANDING_MAINPAGE_SITE_NAME'),
156 'CODE' => strtolower(Type::SCOPE_CODE_MAINPAGE),
157 'TYPE' => Type::SCOPE_CODE_MAINPAGE,
158 'SPECIAL' => 'Y',
159 ]);
160
161 $defaultSiteId = null;
162 if ($new->isSuccess())
163 {
164 $defaultSiteId = (int)$new->getId();
165 }
166
167 return $defaultSiteId;
168 }
169
174 private function detectConnectedPage(): void
175 {
176 if (!$this->getConnectedSiteId())
177 {
178 $this->landingId = null;
179
180 return;
181 }
182
183 // check that exists
184 if ($this->landingId > 0)
185 {
186 $exists = (Landing\Landing::getList([
187 'select' => ['ID'],
188 'filter' => [
189 '=SITE_ID' => $this->getConnectedSiteId(),
190 '=ID' => $this->landingId,
191 ],
192 'order' => [
193 'ID' => 'asc',
194 ],
195 'cache' => ['ttl' => 86400],
196 ]))->fetch();
197
198 if ($exists && (int)$exists['ID'])
199 {
200 $this->landingId = (int)$exists['ID'];
201
202 $this->detectPreviewImg();
203 $this->detectPageTitle();
204 }
205 else
206 {
207 $this->landingId = null;
208 }
209 }
210 }
211
216 private function detectPreviewImg(): void
217 {
218 $this->previewImg = $this->getConnectedPageId()
219 ? Landing\Manager::getUrlFromFile(Site::getPreview($this->getConnectedSiteId(), true))
220 : null;
221 }
222
227 private function detectPageTitle(): void
228 {
229 $this->pageTitle = $this->getConnectedPageId()
230 ? Landing\Landing::createInstance($this->getConnectedPageId())->getTitle()
231 : null;
232 }
233
235 {
236 if (!Loader::includeModule('socialnetwork'))
237 {
238 return false;
239 }
240
241 $storedGroupId = (int)Landing\Manager::getOption('mainpage_id_publication_group', 0);
242 if ($storedGroupId > 0)
243 {
244 return true;
245 }
246
247 $firstSubject = \CSocNetGroupSubject::GetList(
248 ["SORT" => "ASC", "NAME" => "ASC"],
249 ["SITE_ID" => SITE_ID],
250 false,
251 false,
252 ["ID", "NAME"]
253 )->Fetch();
254
255 $fields = array(
256 "SITE_ID" => SITE_ID,
257 "NAME" => Loc::getMessage('LANDING_MAINPAGE_SOCIAL_GROUP_FOR_PUBLICATION_NAME'),
258 "VISIBLE" => 'Y',
259 "OPENED" => 'Y',
260 "CLOSED" => 'N',
261 "LANDING" => 'Y',
262 "SUBJECT_ID" => $firstSubject['ID'] ?? 0,
263 "INITIATE_PERMS" => 'E',
264 "SPAM_PERMS" => 'E',
265 );
266 $newGroupId = (int)\CSocNetGroup::createGroup(Landing\Manager::getUserId(), $fields);
267 if ($newGroupId && $newGroupId > 0)
268 {
269 Option::set('landing', 'mainpage_id_publication_group', $newGroupId);
270
271 return true;
272 }
273
274 return false;
275 }
276
277 public function createPageByTemplate(?Templates $code = null, bool $publication = false): bool
278 {
279 if (!self::isAvailable())
280 {
281 return false;
282 }
283
284 $siteId = $this->getConnectedSiteId();
285 if (!$siteId)
286 {
287 return false;
288 }
289
290 $this->onBeforeOperation();
291 $this->onStartPageCreation();
292 if ($publication)
293 {
294 (new Publisher())->publish();
295 }
296
297 $installer = new Installer($siteId);
298
299 if ($code === null)
300 {
301 $newPageId = $installer->createDemoPage();
302 }
303 else
304 {
305 $newPageId = $installer->createPageByTemplate($code);
306 $this->onTemplateCreation();
307 }
308
309 if (!$newPageId)
310 {
311 return false;
312 }
313
314 $this->landingId = $newPageId;
315 $this->onFinishPageCreation();
316 $this->onAfterOperation();
317
318 return true;
319 }
320
325 public function createDemoPage(): bool
326 {
327 return $this->createPageByTemplate();
328 }
329
330 // todo: private
331 private function onTemplateCreation(): void
332 {
333 EventManager::getInstance()->registerEventHandler(
334 'intranet',
335 'onLicenseHasChanged',
336 'bitrix24',
337 EventHandler::class,
338 'onLicenseHasChanged'
339 );
340 self::setFreeTariffMode();
341 }
342
348 public static function setFreeTariffMode(bool $flag = true): void
349 {
350 Landing\Manager::setOption(self::FREE_MODE_OPTION_CODE, $flag ? 'Y' : 'N');
351 }
352
357 public static function isFreeTariffMode(): bool
358 {
359 return Landing\Manager::getOption(self::FREE_MODE_OPTION_CODE, 'N') === 'Y';
360 }
361
366 public function isProcessing(): bool
367 {
368 return Landing\Manager::getOption(self::CREATION_OPTION_CODE) === self::CREATION_OPTION_PROCESS;
369 }
370
375 public function isReady(): bool
376 {
377 return $this->getConnectedPageId() && $this->isFullyCreated();
378 }
379
380 protected function isFullyCreated(): bool
381 {
382 $creating = Landing\Manager::getOption(self::CREATION_OPTION_CODE, self::CREATION_OPTION_FULL);
383
384 return $creating === self::CREATION_OPTION_FULL;
385 }
386
387
393 public function onStartPageCreation(): void
394 {
395 Landing\Manager::setOption(self::CREATION_OPTION_CODE, self::CREATION_OPTION_PROCESS);
396 }
397
403 private function onBeforeOperation(): void
404 {
405 // getList filter by TYPE don't work in wrong scope
406 $this->scopeBefore = Type::getCurrentScopeId();
407 Type::setScope(Type::SCOPE_CODE_MAINPAGE);
408
409 $this->rightsBefore = Rights::isOn();
412 }
413
419 public function onFinishPageCreation(): void
420 {
421 $connectedSiteId = $this->getConnectedSiteId();
422 $connectedPageId = $this->getConnectedPageId();
423 if (isset($connectedSiteId, $connectedPageId))
424 {
425 Landing\Manager::setOption(self::CREATION_OPTION_CODE, self::CREATION_OPTION_FULL);
426
428
429 Landing\Site::update($connectedSiteId, [
430 'LANDING_ID_INDEX' => $connectedPageId,
431 ]);
432
433 if (Loader::includeModule('pull'))
434 {
437 [
438 'module_id' => 'landing',
439 'command' => 'Vibe:onCreate',
440 'params' => [
441 // todo: add vibe entity type
442 ],
443 ]
444 );
445 }
446 }
447 }
448
453 private function onAfterOperation(): void
454 {
455 if ($this->scopeBefore === null)
456 {
457 Type::clearScope();
458 }
459 elseif (
460 is_string($this->scopeBefore)
461 && $this->scopeBefore !== Type::SCOPE_CODE_MAINPAGE
462 )
463 {
464 Type::setScope($this->scopeBefore);
465 }
466
467 if ($this->rightsBefore)
468 {
471 }
472 }
473
478 public function getConnectedSiteId(): ?int
479 {
480 return (int)$this->siteId > 0 ? (int)$this->siteId : null;
481 }
482
487 public function getConnectedPageId(): ?int
488 {
489 return
490 $this->getConnectedSiteId() && (int)$this->landingId > 0
491 ? (int)$this->landingId
492 : null;
493 }
494
499 public function getPreviewImg(): ?string
500 {
501 return $this->previewImg;
502 }
503
508 public function getPageTitle(): ?string
509 {
510 return $this->pageTitle;
511 }
512
517 public static function isUseDemoData(): bool
518 {
519 return Landing\Manager::getOption(self::USE_DEMO_OPTION_CODE, 'N') === 'Y';
520 }
521}
createPageByTemplate(?Templates $code=null, bool $publication=false)
Определения Manager.php:277
createSonetGroupForPublicationOnce()
Определения Manager.php:234
onStartPageCreation()
Определения Manager.php:393
static setFreeTariffMode(bool $flag=true)
Определения Manager.php:348
onFinishPageCreation()
Определения Manager.php:419
static isFeatureEnable()
Определения Manager.php:50
static isFreeTariffMode()
Определения Manager.php:357
static isAvailable()
Определения Manager.php:44
static isUseDemoData()
Определения Manager.php:517
static getOption($code, $default=null)
Определения manager.php:160
static setOption($code, $value)
Определения manager.php:171
static getUserId()
Определения manager.php:107
static isOn()
Определения rights.php:125
static setGlobalOn()
Определения rights.php:116
static setOff()
Определения rights.php:89
static setOn()
Определения rights.php:98
static setGlobalOff()
Определения rights.php:107
setScope($scope)
Определения controller.php:373
static add($recipient, array $parameters, $channelType=\CPullChannel::TYPE_PRIVATE)
Определения event.php:22
$new
Определения file_edit.php:48
for($fileNumber="";; $fileNumber++) $pageTitle
Определения file_new.php:204
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
if(!is_null($config))($config as $configItem)(! $configItem->isVisible()) $code
Определения options.php:195
Templates
Определения Templates.php:8
Определения agent.php:3
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения prolog_main_admin.php:393
const SITE_ID
Определения sonet_set_content_view.php:12
$fields
Определения yandex_run.php:501