1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
Json.php
См. документацию.
1<?php
2declare(strict_types=1);
3
5
15use Exception;
16
20class Json
21{
27 public static function getSiteJsonString(Site $siteData, string $requestCode): string
28 {
29 $wishesData = $siteData->getWishes()->toArray();
30 $siteStructureJson = [];
31 switch ($requestCode)
32 {
33 case 'siteData':
34 $siteStructureJson['siteConfig'] = $wishesData;
35 break;
36
37 case 'siteContent':
38 $siteStructureJson['siteConfig'] = $wishesData;
39 $siteStructureJson['blocks'] = self::filterNodesByType(
40 self::prepareBlocks($siteData->getBlocks()),
41 [NodeType::Text->value, NodeType::Link->value, NodeType::Icon->value, NodeType::Img->value],
42 );
43 break;
44 }
45
46 try
47 {
48 $encodedString = Web\Json::encode($siteStructureJson, JSON_UNESCAPED_UNICODE);
49 if (!is_string($encodedString))
50 {
51 $encodedString = '';
52 }
53 }
54 catch (Exception)
55 {
56 $encodedString = '';
57 }
58
59 return $encodedString;
60 }
61
69 public static function getBlockJsonString(array $blocks): string
70 {
71 $siteStructureJson = [];
72 foreach ($blocks as $block)
73 {
74 $siteStructureJson['blocks'][] = self::getBlockArrayWithContent($block);
75 }
76
77 try
78 {
79 $encodedString = Web\Json::encode($siteStructureJson, JSON_UNESCAPED_UNICODE);
80 if (!is_string($encodedString))
81 {
82 $encodedString = '';
83 }
84 }
85 catch (Exception)
86 {
87 $encodedString = '';
88 }
89
90 return $encodedString;
91 }
92
100 protected static function prepareNode(Node $node): ?array
101 {
102 $type = $node->getType();
103 if ($type === null)
104 {
105 return null;
106 }
107
108 $preparedNode = [
109 'code' => $node->getCode(),
110 'type' => $type->value,
111 'numberElements' => count($node->getPlaceholders()),
112 ];
113 if ($node->isAvatarNode())
114 {
115 if ($type === NodeType::Img)
116 {
117 return null;
118 }
119
120 $preparedNode['isName'] = true;
121 }
122 if (method_exists($node, 'isEditInStyle'))
123 {
124 $preparedNode['editInStyle'] = $node->isEditInStyle();
125 }
126 if ($type === NodeType::Img)
127 {
128 $preparedNode['promptTexts'] = $node->getPromptTexts();
129 }
130
131 return $preparedNode;
132 }
133
142 public static function initSiteBlocks(Site $siteData, array $json): void
143 {
144 $isCorrectBlocksAmount = Operator::checkBlocksAmount($siteData, $json);
145 if (!$isCorrectBlocksAmount)
146 {
147 return;
148 }
149
150 $blocksFromResponse = $json['blocks'];
151 $blocksData = $siteData->getBlocks();
152 $count = 0;
153 foreach ($blocksData as $blockData)
154 {
155 if ($blockData->isSeparator() || $blockData->isMenu())
156 {
157 continue;
158 }
159
160 if (isset($blocksFromResponse[$count]))
161 {
162 $blockDataFromResponse = $blocksFromResponse[$count];
163
164 if (isset($blockDataFromResponse['code']))
165 {
166 $codeBlockFromResponse = $blockDataFromResponse['code'];
167 $codeBlockForRequest = $blockData->getCode();
168 if ($codeBlockFromResponse === $codeBlockForRequest)
169 {
170 if (isset($blockDataFromResponse['titleInMenu']))
171 {
172 if (isset($previousBlockData))
173 {
174 if ($previousBlockData->getSection() !== 'title')
175 {
176 $blockData->setMenuTitle($blockDataFromResponse['titleInMenu']);
177 }
178 }
179 else
180 {
181 $blockData->setMenuTitle($blockDataFromResponse['titleInMenu']);
182 }
183 }
184 self::checkNodes($blockDataFromResponse, $blockData);
185 }
186 }
187 }
188
189 $count++;
190 $previousBlockData = $blockData;
191 }
192 }
193
194 public static function initSiteContent(Site $siteData, array $json): void
195 {
196 self::initSiteBlocks($siteData, $json);
197 self::saveImgNodesDataFromResponse($siteData, $json);
198 }
199
208 public static function initSiteBlock(Site $siteData, array $json): void
209 {
210 $blocksFromResponse = $json['blocks'];
211 $blocksData = $siteData->getBlocks();
212 $count = 0;
213 foreach ($blocksData as $blockData)
214 {
215 if (isset($blocksFromResponse[$count]))
216 {
217 $blockDataFromResponse = $blocksFromResponse[$count];
218
219 self::checkNodes($blockDataFromResponse, $blockData);
220
221 if (isset($blockDataFromResponse['styles']))
222 {
223 $blockData->setStyles($blockDataFromResponse['styles']);
224 }
225
226 $count++;
227 }
228 }
229 }
230
239 public static function saveImgNodesDataFromResponse(Site $siteData, array $result): void
240 {
241 if (
242 !is_array($result['blocks'])
243 || empty($result['blocks'])
244 )
245 {
246 return;
247 }
248
249 $blocksFromResponse = $result['blocks'];
250 $count = 0;
251 foreach ($siteData->getBlocks() as $blockData)
252 {
253 if ($blockData->isSeparator() || $blockData->isMenu())
254 {
255 continue;
256 }
257
258 $blockId = $blockData->getId();
259 $blockResponse = null;
260 if ($blockId === 0 && isset($blocksFromResponse[$count]))
261 {
262 $blockResponse = $blocksFromResponse[$count];
263 }
264
265 if ($blockId > 0 )
266 {
267 foreach ($blocksFromResponse as $blockFromResponse)
268 {
269 if ($blockFromResponse['id'] === $blockId)
270 {
271 $blockResponse = $blockFromResponse;
272 break;
273 }
274 }
275 }
276
277 if ($blockResponse === null)
278 {
279 continue;
280 }
281
282 foreach ($blockData->getNodes() as $nodeData)
283 {
284 if ($nodeData->getType() !== NodeType::Img)
285 {
286 continue;
287 }
288
289 if (method_exists($nodeData, 'getGenderData') && !empty($nodeData->getGenderData()))
290 {
291 continue;
292 }
293
294 $promptTexts = [];
295 foreach ($blockResponse['nodes'] as $code => $nodeResponse)
296 {
297 if ($code !== $nodeData->getCode())
298 {
299 continue;
300 }
301
302 $promptTexts = $nodeResponse;
303 }
304
305 if (
306 !empty($promptTexts)
307 && (count($nodeData->getPlaceholders()) === count($promptTexts))
308 )
309 {
310 if (Operator::isNeedCheckDataFromResponse())
311 {
312 $promptTexts = Operator::prepareDataFromResponse($promptTexts);
313 }
314 $promptGenerator = new PromptGenerator(new PromptTemplateProvider(), $siteData, $nodeData);
315 $promptTexts = $promptGenerator->getUpdatedPromptTexts((array)$promptTexts);
316 $nodeData->addPromptTexts($promptTexts);
317 }
318 }
319
320 $count++;
321 }
322 }
323
331 public static function compressJsonString(string $json): string
332 {
333 $replacements = [
334 ".landing-block-node-" => ".lbn-",
335 ".landing-block-card-" => ".lbc-",
336 ".landing-block-" => ".lb-",
337 "-linkcontact-" => "-lc-",
338 ];
339
340 return str_replace(array_keys($replacements), array_values($replacements), $json);
341 }
342
350 public static function expandJsonString(string $json): string
351 {
352 $replacements = [
353 ".lbn-" => ".landing-block-node-",
354 ".lbc-" => ".landing-block-card-",
355 ".lb-" => ".landing-block-",
356 "-lc-" => "-linkcontact-",
357 ];
358
359 return str_replace(array_keys($replacements), array_values($replacements), $json);
360 }
361
367 public static function getSkeletonForSiteDataResponse(): string
368 {
369 $siteData = [
370 "isAllowedRequest" => '',
371 "titles" => [
372 "site" => "",
373 "page" => "",
374 ],
375 "description" => [
376 "page" => "",
377 ],
378 "keywords" => [
379 "page" => "",
380 ],
381 "images" => [
382 "siteTopicEng" => "",
383 "styleEng" => "",
384 "imgPromptEng" => "",
385 ],
386 "fonts" => [
387 "headers" => ["name" => ""],
388 "texts" => ["name" => ""],
389 ],
390 "colors" => [
391 "theme" => ["hex" => "", "name" => ""],
392 "background" => ["hex" => "", "name" => ""],
393 "headerContrastOnBackground" => ["hex" => "", "name" => ""],
394 "textContrastOnBackground" => ["hex" => "", "name" => ""],
395 "headerContrastOnTheme" => ["hex" => "", "name" => ""],
396 "textContrastOnTheme" => ["hex" => "", "name" => ""],
397 ],
398 ];
399
400 $jsonSkeletonForResponse = [
401 'siteData' => $siteData,
402 ];
403
404 try
405 {
406 $encodedJsonSkeletonString = Web\Json::encode($jsonSkeletonForResponse, JSON_UNESCAPED_UNICODE);
407 if (!is_string($encodedJsonSkeletonString))
408 {
409 $encodedJsonSkeletonString = '';
410 }
411 }
412 catch (Exception)
413 {
414 $encodedJsonSkeletonString = '';
415 }
416
417 return $encodedJsonSkeletonString;
418 }
419
427 public static function getSkeletonForBlocksContentResponse(string $jsonDataForRequest): string
428 {
429 try
430 {
431 $jsonDataForRequestDecoded = Web\Json::decode($jsonDataForRequest);
432 }
433 catch (Exception)
434 {
435 return '';
436 }
437
438 $blocksSkeleton = [];
439 $previousBlockSection = null;
440 foreach ($jsonDataForRequestDecoded['blocks'] as $block)
441 {
442 $blockSkeleton = [];
443 $blockSkeleton['code'] = $block['code'];
444 if ($previousBlockSection !== 'title')
445 {
446 $blockSkeleton['titleInMenu'] = '';
447 }
448 $blockNodes = $block['nodes'];
449 foreach ($blockNodes as $blockNode)
450 {
451 $nodeCode = $blockNode['code'];
452 $nodeElements = $blockNode['numberElements'];
453 if (isset($blockNode['isName']) && $blockNode['isName'] === true)
454 {
455 $blockSkeleton['nodes'][$nodeCode] = array_fill(0, $nodeElements, ['name' => "", 'gender' => ""]);
456 }
457 else
458 {
459 $blockSkeleton['nodes'][$nodeCode] = array_fill(0, $nodeElements, "");
460 }
461 }
462 $previousBlockSection = $block['section'];
463 $blocksSkeleton[] = $blockSkeleton;
464 }
465
466 $jsonSkeletonForResponse = [
467 'blocks' => $blocksSkeleton,
468 ];
469
470 try
471 {
472 $encodedJsonSkeletonString = Web\Json::encode($jsonSkeletonForResponse, JSON_UNESCAPED_UNICODE);
473 if (!is_string($encodedJsonSkeletonString))
474 {
475 $encodedJsonSkeletonString = '';
476 }
477 }
478 catch (Exception)
479 {
480 $encodedJsonSkeletonString = '';
481 }
482
483 return $encodedJsonSkeletonString;
484 }
485
486 //todo: add phpdoc
487 public static function getSkeletonForBlockContentResponse(string $jsonDataForRequest): string
488 {
489 try
490 {
491 $jsonDataForRequestDecoded = Web\Json::decode($jsonDataForRequest);
492 }
493 catch (Exception)
494 {
495 return '';
496 }
497
498 $blocksSkeleton = [];
499 foreach ($jsonDataForRequestDecoded['blocks'] as $block)
500 {
501 $blockSkeleton = [];
502 $blockSkeleton['id'] = $block['id'];
503 $blockSkeleton['code'] = $block['code'];
504 $blockNodes = $block['nodes'];
505 foreach ($blockNodes as $blockNode)
506 {
507 $nodeCode = $blockNode['code'];
508 if ($blockNode['type'] === 'img')
509 {
510 $nodeElements = $blockNode['numberElements'];
511 }
512 else
513 {
514 $nodeElements = count($blockNode['values']);
515 }
516 $blockSkeleton['nodes'][$nodeCode] = array_fill(0, $nodeElements, "");
517 }
518 //styles
519 $blockSkeleton['styles'] = [
520 'background' => '',
521 'textsColor' => '',
522 'headersColor' => '',
523 'textsFontName' => '',
524 'headersFontName' => '',
525 ];
526 $blocksSkeleton[] = $blockSkeleton;
527 }
528
529 $jsonSkeletonForResponse = [
530 'blocks' => $blocksSkeleton,
531 "isAllowedRequest" => '',
532 ];
533
534 try
535 {
536 $encodedJsonSkeletonString = Web\Json::encode($jsonSkeletonForResponse, JSON_UNESCAPED_UNICODE);
537 if (!is_string($encodedJsonSkeletonString))
538 {
539 $encodedJsonSkeletonString = '';
540 }
541 }
542 catch (Exception)
543 {
544 $encodedJsonSkeletonString = '';
545 }
546
547 return $encodedJsonSkeletonString;
548 }
549
558 private static function filterNodesByType(array $blocks, array $filterTypes): array
559 {
560 $blocksWithFilteredNodes = [];
561 foreach ($blocks as $block)
562 {
563 $preparedBlockNodes = [];
564 foreach ($block['nodes'] as $node)
565 {
566 if (in_array($node['type'], $filterTypes, true))
567 {
568 $preparedBlockNodes[] = $node;
569 }
570 }
571 $block['nodes'] = $preparedBlockNodes;
572 $blocksWithFilteredNodes[] = $block;
573 }
574
575 return $blocksWithFilteredNodes;
576 }
577
585 private static function prepareBlocks(array $blocksDataArray): array
586 {
587 $filteredBlocks = self::filterBlocks($blocksDataArray);
588
589 return self::processFilteredBlocks($filteredBlocks);
590 }
591
599 private static function filterBlocks(array $blocksDataArray): array
600 {
601 $filteredBlocks = [];
602 foreach ($blocksDataArray as $blockData)
603 {
604 if (!$blockData->isSeparator() && !$blockData->isMenu())
605 {
606 $filteredBlocks[] = [
607 'code' => $blockData->getCode(),
608 'section' => $blockData->getSection(),
609 'bgType' => $blockData->getBgType(),
610 'nodes' => $blockData->getNodes(),
611 'id' => $blockData->getId(),
612 ];
613 }
614 }
615
616 return $filteredBlocks;
617 }
618
626 private static function processFilteredBlocks(array $filteredBlocks): array
627 {
628 $preparedBlocksList = [];
629 foreach ($filteredBlocks as $block)
630 {
631 $preparedBlock = self::prepareBlock($block);
632 $preparedBlocksList[] = $preparedBlock;
633 }
634
635 return $preparedBlocksList;
636 }
637
645 private static function prepareBlock(array $block): array
646 {
647 $preparedBlock = [
648 'id' => $block['id'],
649 'code' => $block['code'],
650 'section' => $block['section'],
651 ];
652
653 $preparedBlock['nodes'] = [];
654 foreach ($block['nodes'] as $node)
655 {
656 $preparedNode = self::prepareNode($node);
657 if ($preparedNode !== null)
658 {
659 $preparedBlock['nodes'][] = $preparedNode;
660 }
661 }
662
663 return $preparedBlock;
664 }
665
674 private static function checkNodes(array $blockDataFromResponse, Block $blockData): void
675 {
676 $avatarImgNodeCode = Collector::getAvatarNodes($blockData->getCode())['imgNodeCode'] ?? null;
677 $nodes = $blockData->getNodes();
678 foreach ($nodes as $node)
679 {
680 if ($node->getType() === NodeType::Img)
681 {
682 continue;
683 }
684 $nodeCode = $node->getCode();
685 $nodeDataFromResponse = $blockDataFromResponse['nodes'][$nodeCode] ?? null;
686
687 if (is_array($nodeDataFromResponse) && Operator::isNeedCheckDataFromResponse())
688 {
689 $nodeDataFromResponse = Operator::prepareDataFromResponse($nodeDataFromResponse);
690 }
691 $values = [];
692 $gendersData = null;
693 foreach ($nodeDataFromResponse as $nodeDataFromResponseItem)
694 {
695 if (is_string($nodeDataFromResponseItem))
696 {
697 $values[] = $nodeDataFromResponseItem;
698 }
699 if (is_array($nodeDataFromResponseItem))
700 {
701 if (isset($nodeDataFromResponseItem['name']))
702 {
703 $values[] = $nodeDataFromResponseItem['name'];
704 }
705 if (isset($nodeDataFromResponseItem['gender']))
706 {
707 $gendersData[] = $nodeDataFromResponseItem['gender'];
708 }
709 }
710 }
711 $node->setData($values);
712 if ($gendersData && method_exists($node, 'setGenderData'))
713 {
714 $node->setGenderData($gendersData);
715 if ($avatarImgNodeCode)
716 {
717 foreach ($nodes as $nodeData)
718 {
719 if (
720 $nodeData->getCode() === $avatarImgNodeCode
721 && $nodeData->isAvatarNode()
722 && method_exists($nodeData, 'setGenderData')
723 && method_exists($nodeData, 'setSrc')
724 && method_exists($nodeData, 'setDefaultSrc')
725 )
726 {
727 $nodeData->setGenderData($gendersData);
728 $nodeData->setDefaultSrc(Operator::getDefaultAvatarLinks($gendersData));
729 $nodeData->setSrc(Operator::getAvatarLinks($gendersData));
730 break;
731 }
732 }
733 }
734 }
735 }
736 }
737
738 //todo: add phpdoc and typing
739 private static function getBlockArrayWithContent($block): array
740 {
741 $preparedBlock = [
742 'id' => $block->getId(),
743 'code' => $block->getCode(),
744 ];
745 $blockNodes = $block->getNodes();
746 $preparedBlock['nodes'] = [];
747 foreach ($blockNodes as $node)
748 {
749 $type = $node->getType()->value;
750 $nodeData = [
751 'type' => $type,
752 'code' => $node->getCode(),
753 ];
754 if ($type === 'text')
755 {
756 $nodeData['values'] = $node->getValues();
757 }
758 if ($type === 'icon')
759 {
760 $classLists = [];
761 $values = $node->getValues();
762 foreach ($values as $value)
763 {
764 $classListArray = [];
765 $classList = $value['classList'];
766 if (is_string($classList))
767 {
768 $classListArray = explode(' ', $classList);
769 $classListArray = array_filter($classListArray, static function($value) {
770 return $value !== null && str_starts_with($value, 'fa');
771 });
772 $classListArray = implode(' ', $classListArray);
773 }
774 $classLists[] = $classListArray;
775 }
776 $nodeData['values'] = $classLists;
777 }
778 if ($type === 'link')
779 {
780 $texts = [];
781 $values = $node->getValues();
782 foreach ($values as $value)
783 {
784 $texts[] = $value['text'];
785 }
786 $nodeData['values'] = $texts;
787 }
788 if ($type === 'img')
789 {
790 $nodeData['numberElements'] = count($node->getPlaceholders());
791 }
792 $preparedBlock['nodes'][] = $nodeData;
793 }
794
795 return $preparedBlock;
796 }
797
804 protected static function filterNodesByEmptyPromptTexts(array $blocksWithImgNodes): array
805 {
806 foreach ($blocksWithImgNodes as $key => $block)
807 {
808 if (isset($block['nodes']))
809 {
810 $filteredNodes = array_filter($block['nodes'], static function($node) {
811 return empty($node['promptTexts']);
812 });
813 if (count($filteredNodes) > 0)
814 {
815 foreach ($filteredNodes as $nodeKey => $filteredNode)
816 {
817 unset($filteredNode['promptTexts']);
818 $filteredNodeWithoutPromptTextKey = $filteredNode;
819 $filteredNodes[$nodeKey] = $filteredNodeWithoutPromptTextKey;
820 }
821 $blocksWithImgNodes[$key]['nodes'] = $filteredNodes;
822 }
823 else
824 {
825 unset($blocksWithImgNodes[$key]);
826 }
827 }
828 }
829
830 return $blocksWithImgNodes;
831 }
832}
$count
Определения admin_tab.php:4
$type
Определения options.php:106
static getSkeletonForBlockContentResponse(string $jsonDataForRequest)
Определения Json.php:487
static getSkeletonForSiteDataResponse()
Определения Json.php:367
static compressJsonString(string $json)
Определения Json.php:331
static initSiteBlock(Site $siteData, array $json)
Определения Json.php:208
static prepareNode(Node $node)
Определения Json.php:100
static saveImgNodesDataFromResponse(Site $siteData, array $result)
Определения Json.php:239
static initSiteContent(Site $siteData, array $json)
Определения Json.php:194
static getSiteJsonString(Site $siteData, string $requestCode)
Определения Json.php:27
static filterNodesByEmptyPromptTexts(array $blocksWithImgNodes)
Определения Json.php:804
static getSkeletonForBlocksContentResponse(string $jsonDataForRequest)
Определения Json.php:427
static expandJsonString(string $json)
Определения Json.php:350
static initSiteBlocks(Site $siteData, array $json)
Определения Json.php:142
static getBlockJsonString(array $blocks)
Определения Json.php:69
getBlocks(?array $ids=null)
Определения Site.php:298
static decode($data)
Определения json.php:50
static encode($data, $options=null)
Определения json.php:22
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$result
Определения get_property_values.php:14
if(!is_null($config))($config as $configItem)(! $configItem->isVisible()) $code
Определения options.php:195
$value
Определения Param.php:39
Определения cookies.php:2
Определения cookie.php:3
if(empty($signedUserToken)) $key
Определения quickway.php:257
</p ></td >< td valign=top style='border-top:none;border-left:none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 2.0pt 0cm 2.0pt;height:9.0pt'>< p class=Normal align=center style='margin:0cm;margin-bottom:.0001pt;text-align:center;line-height:normal'>< a name=ТекстовоеПоле54 ></a ><?=($taxRate > count( $arTaxList) > 0) ? $taxRate."%"
Определения waybill.php:936