Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
source.php
1<?php
3
4use \Bitrix\Landing\Manager;
5use \Bitrix\Landing\Rights;
6use \Bitrix\Landing\Site;
7use \Bitrix\Landing\Landing;
8use \Bitrix\Landing\File;
9
10class Source
11{
15 const AVAILABLE_SOURCES = ['blog', 'blogcomment', 'taskcomment', 'livefeed'];
16
20 const BLOCKS = [
21 'header' => [
22 'code' => '27.2.one_col_full_title',
23 'selector' => '.landing-block-node-title'
24 ],
25 'text' => [
26 'code' => '27.6.one_col_fix_text_with_headings',
27 'selector' => '.landing-block-node-text'
28 ],
29 'quote' => [
30 'code' => '27.5.one_col_fix_text_with_border',
31 'selector' => '.landing-block-node-text'
32 ],
33 'code' => [
34 'code' => '27.7.one_col_fix_text_on_bg',
35 'selector' => '.landing-block-node-text'
36 ],
37 'img' => [
38 'code' => '32.2.img_one_big',
39 'selector' => '.landing-block-node-img'
40 ],
41 'table' => [
42 'code' => '27.6.one_col_fix_text_with_headings',
43 'selector' => '.landing-block-node-text'
44 ],
45 'video' => [
46 'code' => '49.1.video_just_video',
47 'selector' => '.landing-block-node-embed'
48 ]
49 ];
50
56 protected static function getBlockMetaByType(string $type): ?array
57 {
58 return self::BLOCKS[$type] ?? null;
59 }
60
65 protected static function getLastCreatedSite(): int
66 {
67 $res = Site::getList([
68 'select' => [
69 'ID'
70 ],
71 'filter' => [
72 'CREATED_BY_ID' => Manager::getUserId()
73 ],
74 'order' => [
75 'ID' => 'desc'
76 ]
77 ]);
78 while ($row = $res->fetch())
79 {
81 $row['ID'],
83 );
84 if ($check)
85 {
86 return $row['ID'];
87 }
88 }
89
90 return 0;
91 }
92
100 public static function createFromSource(int $kbId, string $sourceType, int $sourceId): ?array
101 {
102 if (in_array($sourceType, self::AVAILABLE_SOURCES))
103 {
104 $class = __NAMESPACE__ . '\\Source\\' . $sourceType;
105 $data = $class::getData($sourceId);
106
107 if ($data)
108 {
110 if (!$kbId)
111 {
112 $kbId = self::getLastCreatedSite();
113 }
114 $res = Landing::add([
115 'TITLE' => $data['TITLE'],
116 'SITE_ID' => $kbId,
117 'ACTIVE' => 'N',
118 'PUBLIC' => 'N'
119 ]);
120 if ($res->isSuccess())
121 {
122 Target::rememberLastSite($res->getId());
123 $landing = Landing::createInstance($res->getId());
124 Site::addLandingToMenu($landing->getSiteId(), [
125 'ID' => $landing->getId(),
126 'TITLE' => $landing->getTitle()
127 ]);
128 // add block to the landing
129 foreach ($data['BLOCKS'] as $blockData)
130 {
131 $blockMeta = self::getBlockMetaByType($blockData['type']);
132 if ($blockMeta)
133 {
134 $blockId = $landing->addBlock($blockMeta['code'], [
135 'PUBLIC' => 'N'
136 ]);
137 if ($blockId)
138 {
139 $block = $landing->getBlockById($blockId);
140 if ($blockData['type'] == 'img')
141 {
142 // for correct saving we should set file to the block
143 File::addToBlock($blockId, $blockData['content']['id']);
144 }
145 $block->updateNodes([
146 $blockMeta['selector'] => [$blockData['content']]
147 ]);
148 $block->save();
149 }
150 }
151 }
152 return [
153 'ID' => $landing->getId(),
154 'TITLE' => $landing->getTitle(),
155 'PUBLIC_URL' => $landing->getPublicUrl()
156 ];
157 }
158 }
159 }
160
161 return null;
162 }
163}
static addToBlock(int $blockId, $fileId, bool $temp=false)
Definition file.php:305
static setEditMode($mode=true)
Definition landing.php:423
static createInstance($id, array $params=array())
Definition landing.php:534
static createFromSource(int $kbId, string $sourceType, int $sourceId)
Definition source.php:100
static getBlockMetaByType(string $type)
Definition source.php:56
static rememberLastSite(int $landingId)
Definition target.php:21
static hasAccessForSite($siteId, $accessType, $deleted=false)
Definition rights.php:544
static addLandingToMenu(int $siteId, array $data)
Definition site.php:1547