1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
Site.php
См. документацию.
1<?php
2declare(strict_types=1);
3
4namespace Bitrix\Landing\Copilot\Data;
5
6use Bitrix\Landing\Copilot\Data;
7use Bitrix\Landing\Copilot\Data\Block\Collector;
8use Bitrix\Landing\Copilot\Data\Block\Operator;
9use Bitrix\Landing\Copilot\Data\Node\Node;
10use Bitrix\Landing\Copilot\Data\Type\ColorsDto;
11use Bitrix\Landing\Copilot\Data\Type\FontsDto;
12use Bitrix\Landing\Copilot\Data\Type\NodeType;
13use Bitrix\Landing\Internals\BlockTable;
14use Bitrix\Landing\Landing;
15use Bitrix\Landing\Block;
16use Bitrix\Landing\Node\Icon;
17use Bitrix\Landing\Node\Img;
18use Bitrix\Landing\Node\Link;
19use Bitrix\Landing\Node\Text;
20use Bitrix\Main\Localization\Loc;
21
27class Site
28{
32 private array $blocks = [];
33 private ?string $titleStyleClass = null;
34
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;
45
46 private ?ColorsDto $colors;
47 private ?FontsDto $fonts;
48 private Wishes $wishes;
49
53 public function __construct()
54 {
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 = '';
60
61 $this->keywords = null;
62
63 $this->colors = new ColorsDto();
64 $this->fonts = new FontsDto();
65
66 $this->wishes = new Wishes();
67
68 $this->previewImagePromptText = null;
69 $this->previewImageSrc = null;
70 }
71
77 public function initRandom(): void
78 {
79 $this->blocks = (new Collector())->getBlocks();
80 foreach ($this->blocks as $block)
81 {
82 $block->setParentSite($this);
83 }
84 $this->titleStyleClass = Operator::getTitleStyleClass();
85 }
86
87 public function initByBlockIds(array $blockIds): void
88 {
89 foreach ($blockIds as $blockId)
90 {
91 $blockDataFromTable = BlockTable::getList([
92 'select' => ['LID'],
93 'filter' => [
94 'ID' => $blockId,
95 ],
96 ])->fetch();
97
98 if (!$blockDataFromTable)
99 {
100 continue;
101 }
102
103 $landingId = (int)$blockDataFromTable['LID'];
104 Landing::setEditMode();
105 $landingInstance = Landing::createInstance($landingId, [
106 'blocks_id' => $blockId
107 ]);
108 $pageTitle = $landingInstance->getTitle();
109 $siteId = $landingInstance->getSiteId();
110 $siteData = \Bitrix\Landing\Site::getList([
111 'select' => [
112 'TITLE'
113 ],
114 'filter' => [
115 'ID' => $siteId
116 ]
117 ])->fetch();
118 $siteTitle = $siteData['TITLE'];
119 $blockInstance = $landingInstance->getBlockById($blockId);
120 if (!$blockInstance)
121 {
122 continue;
123 }
124
125 if (isset($siteId))
126 {
127 $this->setSiteId($siteId);
128 }
129 $this->setLandingId($landingId);
130 if (isset($siteTitle) && is_string($siteTitle))
131 {
132 $this->setSiteTitle($siteTitle);
133 }
134 if (isset($pageTitle) && is_string($pageTitle))
135 {
136 $this->setPageTitle($pageTitle);
137 }
138
139 $this->initBlockById($blockInstance);
140 }
141 }
142
143 private function initBlockById(Block $blockInstance): void
144 {
145 $blockNodeValues = $this->initBlockNodes($blockInstance);
146
147 $block = new Data\Block($blockInstance->getCode(), '', '');
148 $block->setId($blockInstance->getId());
149
150 if (!empty($blockNodeValues))
151 {
152 foreach ($blockNodeValues as $key => $nodeData)
153 {
154 if ($node = Node::create(NodeType::from($nodeData['type'] ?? ''), $key, $nodeData))
155 {
156 $block->addNode($node);
157 }
158 }
159 }
160
161 $this->addBlock($block);
162 }
163
164 private function initBlockNodes(Block $blockInstance): array
165 {
166 $blockManifest = $blockInstance->getManifest();
167 $blockContent = $blockInstance->getContent();
168 $blockContentFromRepo = Block::getContentFromRepository($blockInstance->getCode());
169
170 $blockNodeValues = [];
171
172 foreach ($blockManifest['nodes'] as $node)
173 {
174 $nodeType = $node['type'] ?? '';
175 $nodeCode = $node['code'] ?? '';
176 $nodeValues = [];
177 $dataImages = [];
178
179 if (!in_array($nodeType, ['text', 'link', 'icon', 'img'], true))
180 {
181 continue;
182 }
183
184 switch ($nodeType)
185 {
186 case 'text':
187 $nodesData = Text::getNode($blockInstance, $node['code']);
188 foreach ($nodesData as $nodeData)
189 {
190 $nodeValues[] = $nodeData;
191 }
192 $nodeValues = array_merge($nodeValues, $nodesData);
193 break;
194
195 case 'link':
196 $nodesData = Link::getNode($blockInstance, $node['code']);
197 foreach ($nodesData as $nodeData)
198 {
199 $nodeValues[] = $nodeData['text'];
200 }
201 break;
202
203 case 'icon':
204 $nodesData = Icon::getNode($blockInstance, $node['code']);
205 foreach ($nodesData as $nodeData)
206 {
207 $nodeValues[] = $nodeData['classList'][0];
208 }
209 break;
210
211 case 'img':
212 $nodesData = Img::getNode($blockInstance, $node['code']);
213 $nodeCount = 0;
214
215 foreach ($nodesData as $nodeData)
216 {
217 if (isset($nodeData['id']) && $nodeData['id'] > 0)
218 {
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'] ?? '';
223 $src2x = $src;
224 $aspectRatio = Operator::getClosestAspectRatioKey($width / $height);
225 $width = Operator::getClosestWidth((int)$width, $aspectRatio);
226 }
227 else
228 {
229 $sizeImages = Operator::getSizeDataImagesBySelector($nodeCode, $blockContentFromRepo);
230 $aspectRatio = $sizeImages[$nodeCount]['aspectRatio'] ?? Operator::getDefaultAspectRatio();
231 }
232 if (isset($nodeData['src']) && $nodeData['src'] !== '')
233 {
234 $src = $nodeData['src'];
235 }
236 if (isset($nodeData['src2x']) && $nodeData['src2x'] !== '')
237 {
238 $src2x = $nodeData['src2x'];
239 }
240
241 $dataImages[] = [
242 'aspectRatio' => $aspectRatio,
243 'width' => $width ?? Operator::getDefaultWidth($aspectRatio),
244 ];
245 $defaultSrcData = Operator::getDefaultSrc($dataImages);
246 $src = $src ?? $defaultSrcData['src'][0];
247 $nodeValues[] = [
248 'defaultSrc' => $defaultSrcData['src'][0],
249 'defaultSrc2x' => $defaultSrcData['src2x'][0],
250 'src' => $src,
251 'src2x' => $src2x ?? $src,
252 ];
253 $nodeCount++;
254 }
255 break;
256 }
257
258 $trimNodeCode = ltrim($node['code'], '.');
259 $nodeAmountWithSpace = substr_count($blockContent, $trimNodeCode . ' ');
260 $nodeAmountWithQuota = substr_count($blockContent, $trimNodeCode . '"');
261 $placeholders = array_fill(0, $nodeAmountWithSpace + $nodeAmountWithQuota, '');
262
263 $blockNodeValues[$nodeCode] = [
264 'type' => $nodeType,
265 'values' => $nodeValues,
266 'placeholders' => $placeholders,
267 ];
268
269 if ($node['type'] === 'img')
270 {
271 $blockNodeValues[$nodeCode]['sizeData'] = $dataImages;
272 }
273 }
274
275 return $blockNodeValues;
276 }
277
285 public function addBlock(Data\Block $block): self
286 {
287 $block->setParentSite($this);
288 $this->blocks[] = $block;
289
290 return $this;
291 }
292
298 public function getBlocks(?array $ids = null): array
299 {
300 if (empty($ids))
301 {
302 return $this->blocks;
303 }
304
305 return array_filter($this->blocks, static function (Data\Block $block) use ($ids) {
306 return in_array($block->getId(), $ids, true);
307 });
308 }
309
316 public function getTitleStyleClass(): string
317 {
318 return $this->titleStyleClass;
319 }
320
328 public function setSiteTitle(string $title): self
329 {
330 $this->siteTitle = $title;
331
332 return $this;
333 }
334
342 public function setPageTitle(string $title): self
343 {
344 $this->pageTitle = $title;
345
346 return $this;
347 }
348
356 public function setPageDescription(string $title): self
357 {
358 $this->pageDescription = $title;
359
360 return $this;
361 }
362
370 public function setSiteTopic(string $topic): self
371 {
372 $this->siteTopic = $topic;
373
374 return $this;
375 }
376
384 public function setImageStyle(string $style): self
385 {
386 $this->imageStyle = $style;
387
388 return $this;
389 }
390
398 public function setKeywords(string $keywords): self
399 {
400 $this->keywords = $keywords;
401
402 return $this;
403 }
404
410 public function getSiteTitle(): string
411 {
412 return $this->siteTitle;
413 }
414
420 public function getPageTitle(): string
421 {
422 return $this->pageTitle;
423 }
424
430 public function getPageDescription(): string
431 {
432 return $this->pageDescription;
433 }
434
440 public function getSiteTopic(): string
441 {
442 return $this->siteTopic;
443 }
444
450 public function getImageStyle(): string
451 {
452 return $this->imageStyle;
453 }
454
460 public function getKeywords(): ?string
461 {
462 return $this->keywords;
463 }
464
472 public function setColors(ColorsDto $colors): self
473 {
474 $this->colors = $colors;
475
476 return $this;
477 }
478
484 public function getColors(): ColorsDto
485 {
486 return $this->colors;
487 }
488
496 public function setFonts(FontsDto $fonts): self
497 {
498 $this->fonts = $fonts;
499
500 return $this;
501 }
502
508 public function getFonts(): FontsDto
509 {
510 return $this->fonts;
511 }
512
520 public function setPreviewImagePromptText(string $previewImagePromptText): self
521 {
522 $this->previewImagePromptText = $previewImagePromptText;
523
524 return $this;
525 }
526
532 public function getPreviewImagePromptText(): string
533 {
534 return $this->previewImagePromptText;
535 }
536
544 public function setPreviewImageSrc(string $previewImageSrc): self
545 {
546 $this->previewImageSrc = $previewImageSrc;
547
548 return $this;
549 }
550
556 public function getPreviewImageSrc(): ?string
557 {
558 return $this->previewImageSrc;
559 }
560
561
567 public function getMenuBlock(): ?Data\Block
568 {
569 foreach ($this->blocks as $block)
570 {
571 if ($block->isMenu())
572 {
573 return $block;
574 }
575 }
576
577 return null;
578 }
579
587 public function setSiteId(int $id): self
588 {
589 $this->siteId = $id;
590
591 return $this;
592 }
593
601 public function setLandingId(int $id): self
602 {
603 $this->landingId = $id;
604
605 return $this;
606 }
607
613 public function getSiteId(): ?int
614 {
615 // todo: cant return not init param
616 return $this->siteId;
617 }
618
624 public function getLandingId(): ?int
625 {
626 return $this->landingId;
627 }
628
634 public function getLandingInstance(): ?Landing
635 {
636 if (!$this->landingId || $this->landingId <= 0)
637 {
638 return null;
639 }
640
641 Landing::setEditMode();
642
643 return Landing::createInstance($this->landingId);
644 }
645
653 public function setWishes(Wishes $data): self
654 {
655 $this->wishes = $data;
656
657 return $this;
658 }
659
667 public function setTitleStyleClass(string $titleStyleClass): self
668 {
669 $this->titleStyleClass = $titleStyleClass;
670
671 return $this;
672 }
673
679 public function getWishes(): Wishes
680 {
681 return $this->wishes;
682 }
683
689 public function toArray(): array
690 {
691 $data = [];
692
693 $data['wishes'] = $this->wishes->toArray();
694
695 $data['siteId'] = $this->siteId;
696 $data['landingId'] = $this->landingId;
697
698 $data['siteTitle'] = $this->siteTitle;
699 $data['pageTitle'] = $this->pageTitle;
700 $data['pageDescription'] = $this->pageDescription;
701 $data['siteTopic'] = $this->siteTopic;
702 $data['imageStyle'] = $this->imageStyle;
703 $data['keywords'] = $this->keywords ?? '';
704
705 $data['blocks'] = [];
706 foreach ($this->blocks as $block)
707 {
708 $data['blocks'][] = $block->toArray();
709 }
710
711 $data['titleStyleClass'] = $this->titleStyleClass;
712
713 $data['colors'] = $this->colors;
714 $data['fonts'] = $this->fonts;
715
716 $data['preview']['prompt'] = $this->previewImagePromptText ?? '';
717 $data['preview']['src'] = $this->previewImageSrc ?? '';
718
719 return $data;
720 }
721
729 public static function fromArray(array $data): self
730 {
731 $siteData = new Site();
732
733 $siteData->setWishes(Wishes::fromArray($data['wishes'] ?? []));
734
735 if (isset($data['siteId']))
736 {
737 $siteData->setSiteId($data['siteId']);
738 }
739 if (isset($data['landingId']))
740 {
741 $siteData->setLandingId($data['landingId']);
742 }
743
744 $siteData->setSiteTitle($data['siteTitle']);
745 $siteData->setPageTitle($data['pageTitle']);
746 $siteData->setPageDescription($data['pageDescription']);
747 if (isset($data['siteTopic']))
748 {
749 $siteData->setSiteTopic($data['siteTopic']);
750 }
751 if (isset($data['imageStyle']))
752 {
753 $siteData->setImageStyle($data['imageStyle']);
754 }
755 $siteData->setKeywords($data['keywords']);
756
757 if (isset($data['preview']['prompt']))
758 {
759 $siteData->setPreviewImagePromptText($data['preview']['prompt']);
760 }
761 if (isset($data['preview']['src']))
762 {
763 $siteData->setPreviewImageSrc($data['preview']['src']);
764 }
765
766 foreach ($data['blocks'] as $block)
767 {
768 $block = Data\Block::fromArray($block);
769 $siteData->addBlock($block);
770 }
771
772 $siteData->setTitleStyleClass($data['titleStyleClass'] ?? '');
773
774 $siteData->setColors($data['colors']);
775 $siteData->setFonts($data['fonts']);
776
777 return $siteData;
778 }
779
788 public static function initSite(Site $siteData, array $json): void
789 {
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);
796 }
797
806 private static function initTitles(Site $siteData, array $json): void
807 {
808 if (
809 isset($json['siteData']['titles'])
810 && is_array($json['siteData']['titles'])
811 )
812 {
813 $titles = $json['siteData']['titles'];
814
815 if (isset($titles['site']) && is_string($titles['site']))
816 {
817 $siteData->setSiteTitle($titles['site']);
818 }
819 if (isset($titles['page']) && is_string($titles['page']))
820 {
821 $siteData->setPageTitle($titles['page']);
822 }
823 }
824 }
825
834 private static function initDescription(Site $siteData, array $json): void
835 {
836 if (
837 isset($json['siteData']['description'])
838 && is_array($json['siteData']['description'])
839 )
840 {
841 $descriptions = $json['siteData']['description'];
842
843 if (isset($descriptions['page']) && is_string($descriptions['page']))
844 {
845 $siteData->setPageDescription($descriptions['page']);
846 }
847 }
848 }
849
850 private static function initImages(Site $siteData, array $json): void
851 {
852 if (
853 isset($json['siteData']['images'])
854 && is_array($json['siteData']['images'])
855 )
856 {
857 $images = $json['siteData']['images'];
858
859 if (isset($images['styleEng']) && is_string($images['styleEng']))
860 {
861 $siteData->setImageStyle($images['styleEng']);
862 }
863
864 if (isset($images['siteTopicEng']) && is_string($images['siteTopicEng']))
865 {
866 $siteData->setSiteTopic($images['siteTopicEng']);
867 }
868
869 if (isset($images['imgPromptEng']) && is_string($images['imgPromptEng']))
870 {
871 $siteData->setPreviewImagePromptText($images['imgPromptEng']);
872 }
873 }
874 }
875
884 private static function initKeywords(Site $siteData, array $json): void
885 {
886 //todo: why array ? not $keywords['page'] !!!
887 if (
888 isset($json['siteData']['keywords'])
889 && is_array($json['siteData']['keywords'])
890 )
891 {
892 $keywords = $json['siteData']['keywords'];
893
894 if (isset($keywords['page']) && is_string($keywords['page']))
895 {
896 $siteData->setKeywords($keywords['page']);
897 }
898 }
899 }
900
912 private static function initColors(Site $siteData, array $json): void
913 {
914 $colorsData = $json['siteData']['colors'] ?? null;
915 if (!is_array($colorsData))
916 {
917 return;
918 }
919
920 $requiredKeys = ['theme', 'background'];
921 foreach ($requiredKeys as $key)
922 {
923 if (!isset($colorsData[$key]['hex']))
924 {
925 return;
926 }
927 }
928
929 $contrastPairs = [
930 ['headerContrastOnBackground', 'textContrastOnBackground'],
931 ['headerContrastOnTheme', 'textContrastOnTheme'],
932 ];
933 foreach ($contrastPairs as [$key1, $key2])
934 {
935 if (!isset($colorsData[$key1]['hex']) && !isset($colorsData[$key2]['hex']))
936 {
937 return;
938 }
939 $colorsData[$key1]['hex'] = $colorsData[$key1]['hex'] ?? $colorsData[$key2]['hex'];
940 $colorsData[$key2]['hex'] = $colorsData[$key2]['hex'] ?? $colorsData[$key1]['hex'];
941 }
942
943 $colors = new ColorsDto();
944 $colorProperties = [
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'],
951 ];
952
953 foreach ($colorProperties as $key => $properties)
954 {
955 foreach ($properties as $property => $colorField)
956 {
957 if (isset($colorsData[$key][$property]))
958 {
959 $colors->$colorField = $colorsData[$key][$property];
960 }
961 }
962 }
963
964 $siteData->setColors($colors);
965 }
966
975 private static function initFonts(Site $siteData, array $json): void
976 {
977 if (
978 isset($json['siteData']['fonts'])
979 && is_array($json['siteData']['fonts'])
980 )
981 {
982 $fonts = new FontsDto();
983 $fontsData = $json['siteData']['fonts'];
984
985 if (isset($fontsData['headers']['name']))
986 {
987 $fonts->headers = $fontsData['headers']['name'];
988 }
989 if (isset($fontsData['texts']['name']))
990 {
991 $fonts->texts = $fontsData['texts']['name'];
992 }
993
994 $siteData->setFonts($fonts);
995 }
996 }
997
998 public function getImagesSet(): array
999 {
1000 $imageSet = [];
1001 foreach ($this->getBlocks() as $block)
1002 {
1003 foreach ($block->getNodes() as $node)
1004 {
1005 if ($node->getType() === NodeType::Img)
1006 {
1007 $position = 0;
1008 foreach ($node->getValues() as $value)
1009 {
1010 $value['position'] = $position;
1011 $value['nodeCode'] = $node->getCode();
1012 $imageSet[$block->getId()][] = $value;
1013 $position++;
1014 }
1015 }
1016 }
1017 }
1018
1019 return $imageSet;
1020 }
1021}
getId()
Определения block.php:1376
getCode()
Определения block.php:1403
static fromArray(array $data)
Определения Node.php:147
setFonts(FontsDto $fonts)
Определения Site.php:496
static fromArray(array $data)
Определения Site.php:729
setPageDescription(string $title)
Определения Site.php:356
setLandingId(int $id)
Определения Site.php:601
setKeywords(string $keywords)
Определения Site.php:398
getPageDescription()
Определения Site.php:430
setSiteTitle(string $title)
Определения Site.php:328
setPageTitle(string $title)
Определения Site.php:342
static initSite(Site $siteData, array $json)
Определения Site.php:788
initByBlockIds(array $blockIds)
Определения Site.php:87
setImageStyle(string $style)
Определения Site.php:384
setColors(ColorsDto $colors)
Определения Site.php:472
getLandingInstance()
Определения Site.php:634
setWishes(Wishes $data)
Определения Site.php:653
getPreviewImageSrc()
Определения Site.php:556
setSiteId(int $id)
Определения Site.php:587
getTitleStyleClass()
Определения Site.php:316
setSiteTopic(string $topic)
Определения Site.php:370
setTitleStyleClass(string $titleStyleClass)
Определения Site.php:667
setPreviewImagePromptText(string $previewImagePromptText)
Определения Site.php:520
getBlocks(?array $ids=null)
Определения Site.php:298
addBlock(Data\Block $block)
Определения Site.php:285
setPreviewImageSrc(string $previewImageSrc)
Определения Site.php:544
getPreviewImagePromptText()
Определения Site.php:532
static fromArray(array $data)
Определения Wishes.php:120
static getNode(\Bitrix\Landing\Block $block, $selector)
Определения icon.php:81
static getContentFromRepository($code)
Определения block.php:1007
create($actionName)
Определения controller.php:637
$data['IS_AVAILABLE']
Определения .description.php:13
for($fileNumber="";; $fileNumber++) $pageTitle
Определения file_new.php:204
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$siteId
Определения ajax.php:8
Определения cookies.php:2
Определения agent.php:3
Определения aliases.php:105
if(empty($signedUserToken)) $key
Определения quickway.php:257
$width
Определения html.php:68
$title
Определения pdf.php:123