Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
site.php
1<?php
3
4use \Bitrix\Landing\Landing as LandingCore;
5use \Bitrix\Landing\Site as SiteCore;
6use \Bitrix\Landing\Hook;
7use \Bitrix\Landing\Template;
8use \Bitrix\Landing\TemplateRef;
9use \Bitrix\Landing\PublicAction;
10use \Bitrix\Landing\Site\Type;
11use \Bitrix\Landing\Transfer\AppConfiguration;
12use \Bitrix\Rest\Marketplace;
13use \Bitrix\Main\Event;
14
15class Site
16{
21
25 const FILENAME_EXPORT_STEP_META = 'page_#site_id#_00';
26
30 const FILENAME_EXPORT_STEP_PAGE = 'page_#site_id#_10_#landing_id#';
31
38 public static function getUrl(string $type, int $siteId): string
39 {
40 if (!\Bitrix\Main\Loader::includeModule('rest'))
41 {
42 return '';
43 }
44 return Marketplace\Url::getConfigurationExportElementUrl(
45 AppConfiguration::PREFIX_CODE . strtolower($type), $siteId
46 );
47 }
48
54 protected static function getRestAppIds(array $appCodes): array
55 {
56 $appIds = [];
57
58 $res = \Bitrix\Rest\AppTable::getList([
59 'select' => [
60 'ID', 'CODE'
61 ],
62 'filter' => [
63 '=CODE' => $appCodes
64 ]
65 ]);
66 while ($row = $res->fetch())
67 {
68 $appIds[$row['CODE']] = $row['ID'];
69 }
70
71 return $appIds;
72 }
73
79 public static function getInitManifest(Event $event): ?array
80 {
81 $code = $event->getParameter('CODE');
82 $siteType = substr($code, strlen(AppConfiguration::PREFIX_CODE));
83 $siteId = (int)$event->getParameter('ITEM_CODE');
84 Type::setScope($siteType);
85
86 // set uses app to main manifest
87 $usedApp = PublicAction::getRestStat(
88 true, false,
89 $siteId ? ['SITE_ID' => $siteId] : []
90 );
91 $usedApp = array_merge(
92 array_keys($usedApp[PublicAction::REST_USAGE_TYPE_BLOCK]),
93 array_keys($usedApp[PublicAction::REST_USAGE_TYPE_PAGE])
94 );
95 $usedApp = array_unique($usedApp);
96 if ($usedApp)
97 {
98 $usedApp = array_values(self::getRestAppIds($usedApp));
99 }
100
101 // get finish id if it is total export
102 if ($siteId)
103 {
104 return [
105 'SETTING' => [
106 'SITE_ID' => $siteId,
107 'SITE_TYPE' => $siteType,
108 'APP_USES_REQUIRED' => $usedApp
109 ],
110 'NEXT' => false
111 ];
112 }
113 $lastSiteId = 0;
114 $res = SiteCore::getList([
115 'select' => [
116 'ID'
117 ],
118 'order' => [
119 'ID' => 'asc'
120 ],
121 'limit' => self::MAX_SITE_FOR_EXPORT
122 ]);
123 while ($row = $res->fetch())
124 {
125 $lastSiteId = $row['ID'];
126 }
127 return [
128 'SETTING' => [
129 'FINISH_ID' => $lastSiteId,
130 'APP_USES_REQUIRED' => $usedApp
131 ],
132 'NEXT' => false
133 ];
134 }
135
141 private static function getFolders(int $siteId): array
142 {
143 $folders = [];
144
145 foreach (SiteCore::getFolders($siteId) as $folder)
146 {
147 $folders[$folder['ID']] = [
148 'PARENT_ID' => $folder['PARENT_ID'],
149 'ACTIVE' => $folder['ACTIVE'],
150 'TITLE' => $folder['TITLE'],
151 'CODE' => $folder['CODE'],
152 'INDEX_ID' => $folder['INDEX_ID']
153 ];
154 }
155
156 return $folders;
157 }
158
164 protected static function exportSiteMeta(int $siteId): ?array
165 {
166 $files = [];
167
168 $site = SiteCore::getList([
169 'filter' => [
170 'ID' => $siteId
171 ]
172 ])->fetch();
173 if (!$site)
174 {
175 return null;
176 }
177
178 $site['DATE_CREATE'] = (string)$site['DATE_CREATE'];
179 $site['DATE_MODIFY'] = (string)$site['DATE_MODIFY'];
180 $site['SYS_PAGES'] = \Bitrix\Landing\Syspage::get($siteId);
181 $site['FOLDERS_NEW'] = self::getFolders($siteId);
182
183 // layout templates
184 $site['TEMPLATES'] = [];
185 $res = Template::getList([
186 'select' => [
187 'ID', 'XML_ID'
188 ]
189 ]);
190 while ($row = $res->fetch())
191 {
192 $site['TEMPLATES'][$row['ID']] = $row['XML_ID'];
193 }
194
195 // site layout template
196 $site['TEMPLATE_REF'] = [];
197 if ($site['TPL_ID'])
198 {
199 $site['TEMPLATE_REF'] = TemplateRef::getForSite($siteId);
200 }
201
202 // additional fields
203 $hookFields = [];
204 foreach (Hook::getForSite($siteId) as $hookCode => $hook)
205 {
206 if ($hookCode == 'SETTINGS')
207 {
208 continue;
209 }
210 foreach ($hook->getFields() as $fCode => $field)
211 {
212 $hookCodeFull = $hookCode . '_' . $fCode;
213 $hookFields[$hookCodeFull] = $field->getValue();
214 if (!$hookFields[$hookCodeFull])
215 {
216 unset($hookFields[$hookCodeFull]);
217 }
218 else if (in_array($hookCodeFull, Hook::HOOKS_CODES_FILES))
219 {
220 if ($hookFields[$hookCodeFull] > 0)
221 {
222 $files[] = ['ID' => $hookFields[$hookCodeFull]];
223 }
224 }
225 }
226 }
227 $site['ADDITIONAL_FIELDS'] = $hookFields;
228
229 return [
230 'FILE_NAME' => str_replace('#site_id#', $siteId, self::FILENAME_EXPORT_STEP_META),
231 'CONTENT' => $site,
232 'FILES' => $files
233 ];
234 }
235
241 public static function nextStep(Event $event): array
242 {
243 $settings = $event->getParameter('SETTING');
244 $manifest = $event->getParameter('MANIFEST');
245 $next = $event->getParameter('NEXT');
246 $itemCode = (int)$event->getParameter('ITEM_CODE');
247 $siteType = substr($manifest['CODE'], strlen(AppConfiguration::PREFIX_CODE));
248
249 Type::setScope($siteType);
251 LandingCore::setEditMode();
252
253 if (!$next || $next === 'false'/*bug fix*/)
254 {
255 $next = [
256 'ID' => 0,
257 'EXPORTED_SITES_META' => []
258 ];
259 }
260 else
261 {
262 $next = unserialize(htmlspecialcharsback($next), ['allowed_classes' => false]);
263 }
264
265 $defaultReturn = [
266 'NEXT' => false
267 ];
268 $filter = [
269 '>ID' => $next['ID']
270 ];
271
272 // limit top border, if sites too much
273 if (isset($settings['FINISH_ID']))
274 {
275 $filter['<=SITE_ID'] = $settings['FINISH_ID'];
276 }
277 // limit current step
278 if ($itemCode)
279 {
280 $filter['SITE_ID'] = $itemCode;
281 }
282
283 // pages export
284 $res = LandingCore::getList([
285 'select' => [
286 'ID', 'SITE_ID'
287 ],
288 'filter' => $filter,
289 'order' => [
290 'ID' => 'asc'
291 ],
292 'limit' => 1
293 ]);
294 if ($row = $res->fetch())
295 {
296 if (!in_array($row['SITE_ID'], $next['EXPORTED_SITES_META']))
297 {
298 $exportSiteMeta = self::exportSiteMeta($row['SITE_ID']);
299 if (!$exportSiteMeta)
300 {
301 return $defaultReturn;
302 }
303 $next['EXPORTED_SITES_META'][] = $row['SITE_ID'];
304 $exportSiteMeta['NEXT'] = serialize($next);
305 // we'll repeat current step
306 return $exportSiteMeta;
307 }
308 $exportLanding = Landing::exportLanding(
309 $row['ID'],
310 self::FILENAME_EXPORT_STEP_PAGE
311 );
312 if (!$exportLanding)
313 {
314 return $defaultReturn;
315 }
316 $next['ID'] = $row['ID'];
317 $exportLanding['NEXT'] = serialize($next);
318 return $exportLanding;
319 }
320
321 return $defaultReturn;
322 }
323
329 public static function onFinish(Event $event): array
330 {
331 return [];
332 }
333}
const HOOKS_CODES_FILES
Definition hook.php:45
static getForSite($id)
Definition hook.php:251
static setEditMode(bool $mode=true)
Definition hook.php:232
static getUrl(string $type, int $siteId)
Definition site.php:38
static nextStep(Event $event)
Definition site.php:241
static getInitManifest(Event $event)
Definition site.php:79
static exportSiteMeta(int $siteId)
Definition site.php:164
static onFinish(Event $event)
Definition site.php:329
static getRestAppIds(array $appCodes)
Definition site.php:54
getParameter($key)
Definition event.php:80
static getList(array $parameters=array())