Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
landing.php
1<?php
3
4use \Bitrix\Landing\Landing as LandingCore;
5use \Bitrix\Landing\Hook;
6use \Bitrix\Landing\Repo;
7use \Bitrix\Landing\File;
8use \Bitrix\Landing\Block;
9use \Bitrix\Landing\TemplateRef;
10
12{
19 public static function exportLanding(int $landingId, string $fileName): ?array
20 {
21 // base fields
22 $landing = LandingCore::getList([
23 'filter' => [
24 'ID' => $landingId
25 ]
26 ])->fetch();
27 if (!$landing)
28 {
29 return null;
30 }
31 $landing['DATE_CREATE'] = (string)$landing['DATE_CREATE'];
32 $landing['DATE_MODIFY'] = (string)$landing['DATE_MODIFY'];
33 $landing['DATE_PUBLIC'] = (string)$landing['DATE_PUBLIC'];
34 $files = [];
35
36 // additional fields
37 $hookFields = [];
38 foreach (Hook::getForLanding($landingId) as $hookCode => $hook)
39 {
40 if ($hookCode == 'SETTINGS')
41 {
42 continue;
43 }
44 foreach ($hook->getFields() as $fCode => $field)
45 {
46 $hookCodeFull = $hookCode . '_' . $fCode;
47 $hookFields[$hookCodeFull] = $field->getValue();
48 if (!$hookFields[$hookCodeFull])
49 {
50 unset($hookFields[$hookCodeFull]);
51 }
52 else if (in_array($hookCodeFull, Hook::HOOKS_CODES_FILES))
53 {
54 if ($hookFields[$hookCodeFull] > 0)
55 {
56 $files[] = ['ID' => $hookFields[$hookCodeFull]];
57 }
58 }
59 }
60 }
61 $landing['ADDITIONAL_FIELDS'] = $hookFields;
62
63 // site layout template
64 $landing['TEMPLATE_REF'] = [];
65 if ($landing['TPL_ID'])
66 {
67 $landing['TEMPLATE_REF'] = TemplateRef::getForLanding($landingId);
68 }
69
70 $landingInstance = LandingCore::createInstance($landingId);
71
72 // saved block (favorites)
73 if ($landing['TPL_CODE'])
74 {
75 $tplCode = $landing['TPL_CODE'];
76 if (strpos($tplCode, '@'))
77 {
78 [, $tplCode] = explode('@', $tplCode);
79 }
80 foreach (Block::getFavorites($tplCode) as $row)
81 {
82 $landingInstance->addBlockToCollection(new Block($row['ID']));
83 }
84 }
85
86 // blocks
87 $landing['BLOCKS'] = [];
88 foreach ($landingInstance->getBlocks() as $block)
89 {
90 if (!$block->isActive())
91 {
92 continue;
93 }
94
95 // repo blocks
96 $repoBlock = [];
97 if ($block->getRepoId())
98 {
99 $repoBlock = Repo::getBlock(
100 $block->getRepoId()
101 );
102 if ($repoBlock)
103 {
104 $repoBlock = [
105 'app_code' => $repoBlock['block']['app_code'],
106 'xml_id' => $repoBlock['block']['xml_id']
107 ];
108 }
109 }
110
111 $repoInfo = [];
112 if ($block->getRepoId())
113 {
114 $repoInfo = Repo::getById($block->getRepoId())->fetch();
115 if ($repoInfo)
116 {
117 $repoInfo = [
118 'NAME' => $repoInfo['NAME'],
119 'DESCRIPTION' => $repoInfo['DESCRIPTION'],
120 'SECTIONS' => $repoInfo['SECTIONS'],
121 'PREVIEW' => $repoInfo['PREVIEW'],
122 'MANIFEST' => $repoInfo['MANIFEST'],
123 'CONTENT' => $repoInfo['CONTENT']
124 ];
125 }
126 else
127 {
128 $repoInfo = [];
129 }
130 }
131
132 $metaBlock = $block->getMeta();
133 $exportBlock = $block->export();
134 $exportItem = array(
135 'code' => $block->getCode(),
136 'old_id' => $block->getId(),
137 'access' => $block->getAccess(),
138 'anchor' => $block->getLocalAnchor(),
139 'designed' => $block->isDesigned(),
140 'meta' => $metaBlock,
141 'full_content' => $block->isDesigned() ? $block->getContent() : null,
142 'repo_block' => $repoBlock,
143 'repo_info' => $repoInfo,
144 'cards' => $exportBlock['cards'],
145 'nodes' => $exportBlock['nodes'],
146 'menu' => $exportBlock['menu'],
147 'style' => $exportBlock['style'],
148 'attrs' => $exportBlock['attrs'],
149 'dynamic' => $exportBlock['dynamic']
150 );
151
152 foreach ($exportItem as $key => $item)
153 {
154 if (!$item)
155 {
156 unset($exportItem[$key]);
157 }
158 }
159 $landing['BLOCKS'][$block->getId()] = $exportItem;
160
161 $blockFiles = File::getFilesFromBlockContent(
162 $block->getId(),
163 $block->getContent()
164 );
165 foreach ($blockFiles as $fileId)
166 {
167 $files[] = ['ID' => $fileId];
168 }
169 // favorite cover
170 if (intval($metaBlock['FAVORITE_META']['preview'] ?? 0) > 0)
171 {
172 $files[] = ['ID' => intval($metaBlock['FAVORITE_META']['preview'])];
173 }
174 }
175
176 return [
177 'FILE_NAME' => str_replace(
178 ['#site_id#', '#landing_id#'],
179 [$landing['SITE_ID'], $landing['ID']],
180 $fileName
181 ),
182 'CONTENT' => $landing,
183 'FILES' => $files
184 ];
185 }
186}
static getFilesFromBlockContent($blockId, $content)
Definition file.php:374
static getForLanding($id)
Definition hook.php:275
const HOOKS_CODES_FILES
Definition hook.php:45
static getById($id)
Definition repo.php:193
static getBlock($id)
Definition repo.php:144
static exportLanding(int $landingId, string $fileName)
Definition landing.php:19
static getList(array $parameters=array())