32 private array $blocks = [];
33 private ?
string $titleStyleClass =
null;
35 private ?
int $siteId =
null;
36 private ?
int $landingId =
null;
37 private string $siteTitle;
38 private string $pageTitle;
39 private string $pageDescription;
40 private string $siteTopic;
41 private string $imageStyle;
42 private ?
string $keywords;
43 private ?
string $previewImagePromptText;
44 private ?
string $previewImageSrc;
55 $this->siteTitle = Loc::getMessage(
'LANDING_COPILOT_DEFAULT_SITE_TITLE');
56 $this->pageTitle = Loc::getMessage(
'LANDING_COPILOT_DEFAULT_PAGE_TITLE');
57 $this->pageDescription = Loc::getMessage(
'LANDING_COPILOT_DEFAULT_PAGE_DESCRIPTION');
58 $this->siteTopic =
'';
59 $this->imageStyle =
'';
61 $this->keywords =
null;
66 $this->wishes =
new Wishes();
68 $this->previewImagePromptText =
null;
69 $this->previewImageSrc =
null;
80 foreach ($this->blocks as $block)
82 $block->setParentSite($this);
84 $this->titleStyleClass = Operator::getTitleStyleClass();
89 foreach ($blockIds as $blockId)
91 $blockDataFromTable = BlockTable::getList([
98 if (!$blockDataFromTable)
103 $landingId = (int)$blockDataFromTable[
'LID'];
104 Landing::setEditMode();
105 $landingInstance = Landing::createInstance($landingId, [
106 'blocks_id' => $blockId
108 $pageTitle = $landingInstance->getTitle();
109 $siteId = $landingInstance->getSiteId();
110 $siteData = \Bitrix\Landing\Site::getList([
118 $siteTitle = $siteData[
'TITLE'];
119 $blockInstance = $landingInstance->getBlockById($blockId);
130 if (isset($siteTitle) && is_string($siteTitle))
134 if (isset($pageTitle) && is_string($pageTitle))
139 $this->initBlockById($blockInstance);
143 private function initBlockById(
Block $blockInstance): void
145 $blockNodeValues = $this->initBlockNodes($blockInstance);
148 $block->setId($blockInstance->
getId());
150 if (!empty($blockNodeValues))
152 foreach ($blockNodeValues as
$key => $nodeData)
154 if ($node =
Node::create(NodeType::from($nodeData[
'type'] ??
''),
$key, $nodeData))
156 $block->addNode($node);
161 $this->addBlock($block);
164 private function initBlockNodes(Block $blockInstance):
array
166 $blockManifest = $blockInstance->getManifest();
167 $blockContent = $blockInstance->getContent();
170 $blockNodeValues = [];
172 foreach ($blockManifest[
'nodes'] as $node)
174 $nodeType = $node[
'type'] ??
'';
175 $nodeCode = $node[
'code'] ??
'';
179 if (!in_array($nodeType, [
'text',
'link',
'icon',
'img'],
true))
187 $nodesData = Text::getNode($blockInstance, $node[
'code']);
188 foreach ($nodesData as $nodeData)
190 $nodeValues[] = $nodeData;
192 $nodeValues = array_merge($nodeValues, $nodesData);
197 foreach ($nodesData as $nodeData)
199 $nodeValues[] = $nodeData[
'text'];
205 foreach ($nodesData as $nodeData)
207 $nodeValues[] = $nodeData[
'classList'][0];
212 $nodesData = Img::getNode($blockInstance, $node[
'code']);
215 foreach ($nodesData as $nodeData)
217 if (isset($nodeData[
'id']) && $nodeData[
'id'] > 0)
219 $fileArray = \CFile::GetFileArray($nodeData[
'id']);
220 $width = $fileArray[
'WIDTH'] > 0 ? $fileArray[
'WIDTH'] : 1;
221 $height = $fileArray[
'HEIGHT'] > 0 ? $fileArray[
'HEIGHT'] : 1;
222 $src = $fileArray[
'SRC'] ??
'';
224 $aspectRatio = Operator::getClosestAspectRatioKey(
$width / $height);
225 $width = Operator::getClosestWidth((
int)
$width, $aspectRatio);
229 $sizeImages = Operator::getSizeDataImagesBySelector($nodeCode, $blockContentFromRepo);
230 $aspectRatio = $sizeImages[$nodeCount][
'aspectRatio'] ?? Operator::getDefaultAspectRatio();
232 if (isset($nodeData[
'src']) && $nodeData[
'src'] !==
'')
234 $src = $nodeData[
'src'];
236 if (isset($nodeData[
'src2x']) && $nodeData[
'src2x'] !==
'')
238 $src2x = $nodeData[
'src2x'];
242 'aspectRatio' => $aspectRatio,
243 'width' =>
$width ?? Operator::getDefaultWidth($aspectRatio),
245 $defaultSrcData = Operator::getDefaultSrc($dataImages);
246 $src = $src ?? $defaultSrcData[
'src'][0];
248 'defaultSrc' => $defaultSrcData[
'src'][0],
249 'defaultSrc2x' => $defaultSrcData[
'src2x'][0],
251 'src2x' => $src2x ?? $src,
258 $trimNodeCode = ltrim($node[
'code'],
'.');
259 $nodeAmountWithSpace = substr_count($blockContent, $trimNodeCode .
' ');
260 $nodeAmountWithQuota = substr_count($blockContent, $trimNodeCode .
'"');
261 $placeholders = array_fill(0, $nodeAmountWithSpace + $nodeAmountWithQuota,
'');
263 $blockNodeValues[$nodeCode] = [
265 'values' => $nodeValues,
266 'placeholders' => $placeholders,
269 if ($node[
'type'] ===
'img')
271 $blockNodeValues[$nodeCode][
'sizeData'] = $dataImages;
275 return $blockNodeValues;
287 $block->setParentSite($this);
288 $this->blocks[] = $block;
302 return $this->blocks;
305 return array_filter($this->blocks,
static function (
Data\
Block $block) use ($ids) {
306 return in_array($block->getId(), $ids,
true);
318 return $this->titleStyleClass;
330 $this->siteTitle =
$title;
344 $this->pageTitle =
$title;
358 $this->pageDescription =
$title;
372 $this->siteTopic = $topic;
386 $this->imageStyle = $style;
400 $this->keywords = $keywords;
412 return $this->siteTitle;
432 return $this->pageDescription;
442 return $this->siteTopic;
452 return $this->imageStyle;
462 return $this->keywords;
474 $this->colors = $colors;
486 return $this->colors;
498 $this->fonts = $fonts;
522 $this->previewImagePromptText = $previewImagePromptText;
534 return $this->previewImagePromptText;
546 $this->previewImageSrc = $previewImageSrc;
558 return $this->previewImageSrc;
569 foreach ($this->blocks as $block)
571 if ($block->isMenu())
603 $this->landingId = $id;
626 return $this->landingId;
636 if (!$this->landingId || $this->landingId <= 0)
641 Landing::setEditMode();
643 return Landing::createInstance($this->landingId);
655 $this->wishes =
$data;
669 $this->titleStyleClass = $titleStyleClass;
681 return $this->wishes;
693 $data[
'wishes'] = $this->wishes->toArray();
696 $data[
'landingId'] = $this->landingId;
698 $data[
'siteTitle'] = $this->siteTitle;
700 $data[
'pageDescription'] = $this->pageDescription;
701 $data[
'siteTopic'] = $this->siteTopic;
702 $data[
'imageStyle'] = $this->imageStyle;
703 $data[
'keywords'] = $this->keywords ??
'';
705 $data[
'blocks'] = [];
706 foreach ($this->blocks as $block)
708 $data[
'blocks'][] = $block->toArray();
711 $data[
'titleStyleClass'] = $this->titleStyleClass;
713 $data[
'colors'] = $this->colors;
714 $data[
'fonts'] = $this->fonts;
716 $data[
'preview'][
'prompt'] = $this->previewImagePromptText ??
'';
717 $data[
'preview'][
'src'] = $this->previewImageSrc ??
'';
731 $siteData =
new Site();
735 if (isset(
$data[
'siteId']))
737 $siteData->setSiteId(
$data[
'siteId']);
739 if (isset(
$data[
'landingId']))
741 $siteData->setLandingId(
$data[
'landingId']);
744 $siteData->setSiteTitle(
$data[
'siteTitle']);
745 $siteData->setPageTitle(
$data[
'pageTitle']);
746 $siteData->setPageDescription(
$data[
'pageDescription']);
747 if (isset(
$data[
'siteTopic']))
749 $siteData->setSiteTopic(
$data[
'siteTopic']);
751 if (isset(
$data[
'imageStyle']))
753 $siteData->setImageStyle(
$data[
'imageStyle']);
755 $siteData->setKeywords(
$data[
'keywords']);
757 if (isset(
$data[
'preview'][
'prompt']))
759 $siteData->setPreviewImagePromptText(
$data[
'preview'][
'prompt']);
761 if (isset(
$data[
'preview'][
'src']))
763 $siteData->setPreviewImageSrc(
$data[
'preview'][
'src']);
766 foreach (
$data[
'blocks'] as $block)
769 $siteData->addBlock($block);
772 $siteData->setTitleStyleClass(
$data[
'titleStyleClass'] ??
'');
774 $siteData->setColors(
$data[
'colors']);
775 $siteData->setFonts(
$data[
'fonts']);
790 self::initTitles($siteData, $json);
791 self::initDescription($siteData, $json);
792 self::initImages($siteData, $json);
793 self::initKeywords($siteData, $json);
794 self::initColors($siteData, $json);
795 self::initFonts($siteData, $json);
806 private static function initTitles(
Site $siteData,
array $json): void
809 isset($json[
'siteData'][
'titles'])
810 && is_array($json[
'siteData'][
'titles'])
813 $titles = $json[
'siteData'][
'titles'];
815 if (isset($titles[
'site']) && is_string($titles[
'site']))
819 if (isset($titles[
'page']) && is_string($titles[
'page']))
834 private static function initDescription(Site $siteData,
array $json): void
837 isset($json[
'siteData'][
'description'])
838 && is_array($json[
'siteData'][
'description'])
841 $descriptions = $json[
'siteData'][
'description'];
843 if (isset($descriptions[
'page']) && is_string($descriptions[
'page']))
845 $siteData->setPageDescription($descriptions[
'page']);
850 private static function initImages(Site $siteData,
array $json): void
853 isset($json[
'siteData'][
'images'])
854 && is_array($json[
'siteData'][
'images'])
857 $images = $json[
'siteData'][
'images'];
859 if (isset($images[
'styleEng']) && is_string($images[
'styleEng']))
861 $siteData->setImageStyle($images[
'styleEng']);
864 if (isset($images[
'siteTopicEng']) && is_string($images[
'siteTopicEng']))
866 $siteData->setSiteTopic($images[
'siteTopicEng']);
869 if (isset($images[
'imgPromptEng']) && is_string($images[
'imgPromptEng']))
871 $siteData->setPreviewImagePromptText($images[
'imgPromptEng']);
884 private static function initKeywords(Site $siteData,
array $json): void
888 isset($json[
'siteData'][
'keywords'])
889 && is_array($json[
'siteData'][
'keywords'])
892 $keywords = $json[
'siteData'][
'keywords'];
894 if (isset($keywords[
'page']) && is_string($keywords[
'page']))
896 $siteData->setKeywords($keywords[
'page']);
912 private static function initColors(Site $siteData,
array $json): void
914 $colorsData = $json[
'siteData'][
'colors'] ??
null;
915 if (!is_array($colorsData))
920 $requiredKeys = [
'theme',
'background'];
921 foreach ($requiredKeys as
$key)
923 if (!isset($colorsData[
$key][
'hex']))
930 [
'headerContrastOnBackground',
'textContrastOnBackground'],
931 [
'headerContrastOnTheme',
'textContrastOnTheme'],
933 foreach ($contrastPairs as [$key1, $key2])
935 if (!isset($colorsData[$key1][
'hex']) && !isset($colorsData[$key2][
'hex']))
939 $colorsData[$key1][
'hex'] = $colorsData[$key1][
'hex'] ?? $colorsData[$key2][
'hex'];
940 $colorsData[$key2][
'hex'] = $colorsData[$key2][
'hex'] ?? $colorsData[$key1][
'hex'];
943 $colors =
new ColorsDto();
945 'theme' => [
'hex' =>
'theme',
'name' =>
'themeName'],
946 'background' => [
'hex' =>
'background',
'name' =>
'backgroundName'],
947 'headerContrastOnBackground' => [
'hex' =>
'headersBg'],
948 'textContrastOnBackground' => [
'hex' =>
'textsBg'],
949 'headerContrastOnTheme' => [
'hex' =>
'headersTheme'],
950 'textContrastOnTheme' => [
'hex' =>
'textsTheme'],
953 foreach ($colorProperties as
$key => $properties)
955 foreach ($properties as $property => $colorField)
957 if (isset($colorsData[
$key][$property]))
959 $colors->$colorField = $colorsData[
$key][$property];
964 $siteData->setColors($colors);
975 private static function initFonts(Site $siteData,
array $json): void
978 isset($json[
'siteData'][
'fonts'])
979 && is_array($json[
'siteData'][
'fonts'])
982 $fonts =
new FontsDto();
983 $fontsData = $json[
'siteData'][
'fonts'];
985 if (isset($fontsData[
'headers'][
'name']))
987 $fonts->headers = $fontsData[
'headers'][
'name'];
989 if (isset($fontsData[
'texts'][
'name']))
991 $fonts->texts = $fontsData[
'texts'][
'name'];
994 $siteData->setFonts($fonts);
1003 foreach ($block->getNodes() as $node)
1005 if ($node->getType() === NodeType::Img)
1008 foreach ($node->getValues() as $value)
1010 $value[
'position'] = $position;
1011 $value[
'nodeCode'] = $node->getCode();
1012 $imageSet[$block->getId()][] = $value;