28 if (!Loader::includeModule(self::$moduleId))
38 if (empty($queueOption))
44 $executiveUserId = ($queueOption[
"executiveUserId"] ?: 0);
46 $siteId = ($queueOption[
'siteId'] ?: 0);
47 $copiedSiteId = ($queueOption[
'copiedSiteId'] ?: 0);
49 Landing\Rights::setContextUserId($executiveUserId);
53 $copiedPageIds = $this->getPageIdsBySiteId($copiedSiteId);
54 $offset = count($copiedPageIds);
56 $pageIdsToCopy = $this->getPageIdsBySiteId($siteId);
57 $count = count($pageIdsToCopy);
58 $pageIdsToCopy = array_slice($pageIdsToCopy, $offset, self::LIMIT);
62 $pageCopier = $this->getPageCopier($copiedSiteId);
65 foreach ($pageIdsToCopy as $pageId)
68 $containerCollection[] = $container;
70 $result = $pageCopier->copy($containerCollection);
71 if (!$result->isSuccess())
77 $this->saveCopiedMapIds($pageCopier, $queueOption, $result);
79 $option[
'count'] = $count;
80 $option[
'steps'] = $offset;
84 $this->onAfterCopy($queueOption);
91 catch (\Exception $exception)
93 $this->clearContextUserId();
100 private function getPageIdsBySiteId(
int $siteId): array
103 $queryObject = Landing\Landing::getList([
105 'filter' => [
'SITE_ID' => $siteId],
107 while ($page = $queryObject->fetch())
109 $pageIds[] = $page[
'ID'];
114 private function getPageCopier(
int $copiedSiteId): EntityCopier
117 $pageCopyImplementer->setTargetSiteId($copiedSiteId);
119 return new EntityCopier($pageCopyImplementer);
122 private function saveCopiedMapIds(EntityCopier $pageCopier, array $queueOption, Result $result): void
124 $pageMapIds = ($queueOption[
'pageMapIds'] ?: []);
125 $blockMapIds = ($queueOption[
'blockMapIds'] ?: []);
127 $pageMapIds = $pageCopier->getMapIdsByImplementer(
130 $queueOption[
'pageMapIds'] = $pageMapIds;
132 $blockMapIds = $pageCopier->getMapIdsByImplementer(
133 'LandingBlocks', $result->getData()
135 $queueOption[
'blockMapIds'] = $blockMapIds;
140 private function onAfterCopy(array $queueOption)
142 $siteId = ($queueOption[
'siteId'] ?: 0);
143 $copiedSiteId = ($queueOption[
'copiedSiteId'] ?: 0);
144 $pageMapIds = array_filter(($queueOption[
'pageMapIds'] ?: []));
145 $folderMapIds = array_filter(($queueOption[
'folderMapIds'] ?: []));
146 $folderIndexIds = array_filter(($queueOption[
'folderIndexIds'] ?: []));
147 $blockMapIds = array_filter(($queueOption[
'blockMapIds'] ?: []));
153 $this->updateFolderIds($copiedSiteId, $folderMapIds);
154 $this->updateFolderIndexes($folderMapIds, $folderIndexIds, $pageMapIds);
156 $this->updateBlockIds($pageMapIds, $blockMapIds);
157 $this->updateCopiedSite($siteId, $copiedSiteId, $pageMapIds);
160 $this->clearContextUserId();
163 private function clearContextUserId()
165 Landing\Rights::clearContextUserId();
168 private function updateFolderIds(
int $siteId, array $folderMapIds): void
170 \Bitrix\Landing\Landing::disableCheckUniqueAddress();
172 $res = Landing\Landing::getList([
177 'SITE_ID' => $siteId,
178 'FOLDER_ID' => array_keys($folderMapIds)
181 while ($row = $res->fetch())
183 if (isset($folderMapIds[$row[
'FOLDER_ID']]))
185 Landing\Landing::update($row[
'ID'], [
186 'FOLDER_ID' => $folderMapIds[$row[
'FOLDER_ID']]
191 \Bitrix\Landing\Landing::enableCheckUniqueAddress();
194 private function updateFolderIndexes(array $folderMapIds, array $folderIndexIds, array $pageMapIds): void
196 foreach ($folderMapIds as $oldFolderId => $newFolderId)
198 Folder::update($newFolderId, [
199 'INDEX_ID' => $pageMapIds[ $folderIndexIds[$oldFolderId] ] ??
null
204 private function updateBlockIds(array $pageMapIds, array $blockMapIds): void
209 Landing\Landing::setEditMode();
211 foreach ($pageMapIds as $pageId => $copiedPageId)
213 $pageMapIds[
'#landing'.$pageId] =
'#landing'.$copiedPageId;
214 unset($pageMapIds[$pageId]);
216 foreach ($blockMapIds as $blockId => $copiedBlockId)
218 $blockMapIds[
'#block'.$blockId] =
'#block'.$copiedBlockId;
219 unset($blockMapIds[$blockId]);
222 foreach ($pageMapIds as $pageId => $copiedPageId)
224 $copiedLandingInstance = Landing\Landing::createInstance(mb_substr($copiedPageId, 8));
225 foreach ($copiedLandingInstance->getBlocks() as $copiedBlock)
227 $content = $copiedBlock->getContent();
228 $content = str_replace(
229 array_keys($pageMapIds),
230 array_values($pageMapIds),
233 $content = str_replace(
234 array_keys($blockMapIds),
235 array_values($blockMapIds),
238 $copiedBlock->saveContent($content);
239 $copiedBlock->save();
244 private function updateCopiedSite(
int $siteId,
int $copiedSiteId, array $pageMapIds): void
246 $siteData = $this->getSiteData($siteId);
247 $siteDataForUpdate = $this->getSiteDataForUpdateCopiedSite($siteData, $pageMapIds);
248 if ($siteData[
'TPL_ID'])
250 $siteDataForUpdate[
'TPL_ID'] = $siteData[
'TPL_ID'];
251 $this->copyTemplate($siteId, $copiedSiteId, $pageMapIds);
253 if ($siteDataForUpdate)
255 Landing\Site::update($copiedSiteId, $siteDataForUpdate);
259 private function getSiteData(
int $siteId): array
261 $queryObject = Landing\Site::getList([
'filter' => [
'ID' => $siteId]]);
262 return (($siteData = $queryObject->fetch()) ? $siteData : []);
265 private function getSiteDataForUpdateCopiedSite(array $siteData, array $pageMapIds): array
267 $copiedSiteData = [];
268 $codes = [
'LANDING_ID_INDEX',
'LANDING_ID_404',
'LANDING_ID_503'];
269 foreach ($codes as $code)
271 if ($siteData[$code] && isset($pageMapIds[$siteData[$code]]))
273 $copiedSiteData[$code] = $pageMapIds[$siteData[$code]];
276 return $copiedSiteData;
279 private function copyTemplate(
int $siteId,
int $copiedSiteId, array $pageMapIds): void
283 foreach ($refs as $areaId => $oldId)
285 if (isset($pageMapIds[$oldId]))
287 $refs[$areaId] = $pageMapIds[$oldId];
291 unset($refs[$areaId]);
296 Landing\TemplateRef::setForSite($copiedSiteId, $refs);
308 $queueId = (string) current($queue);
309 $this->checkerName = (mb_strpos($this->checkerName, $queueId) ===
false ?
311 $this->baseName = (mb_strpos($this->baseName, $queueId) ===
false ?
313 $this->errorName = (mb_strpos($this->errorName, $queueId) ===
false ?
324 Option::set(static::$moduleId, $this->baseName, serialize($data));
332 Option::delete(static::$moduleId, [
"name" => $this->checkerName]);
333 Option::delete(static::$moduleId, [
"name" => $this->baseName]);
338 $queueId = current($queue);
339 $currentPos = array_search($queueId, $queue);
340 if ($currentPos !==
false)
342 unset($queue[$currentPos]);
343 Option::set(static::$moduleId, $this->queueName, serialize($queue));
350 return empty($queue);
355 $option = Option::get(static::$moduleId, $optionName);
356 $option = ($option !==
"" ? unserialize($option, [
'allowed_classes' =>
false]) : []);
357 return (is_array($option) ? $option : []);
362 Option::delete(static::$moduleId, [
"name" => $optionName]);