• Модуль: landing
  • Путь к файлу: ~/bitrix/modules/landing/lib/node/styleimg.php
  • Класс: BitrixLandingNodeStyleImg
  • Вызов: StyleImg::saveNode
static function saveNode(Block $block, $selector, array $data): void
{
	if (!self::isCorrectNodeType($block, $selector))
	{
		Img::saveNode($block, $selector, $data);
	}
	else
	{
		$resultList = NodeStyle::getNodesBySelector($block, $selector);
		$files = null;

		foreach ($data as $pos => $value)
		{
			$id = isset($value['id']) ? (int)$value['id'] : 0;
			$id2x = isset($value['id2x']) ? (int)$value['id2x'] : 0;

			// check permissions to this file ids
			if ($id || $id2x)
			{
				if ($files === null)
				{
					$files = File::getFilesFromBlock($block->getId());
				}
				if (!in_array($id, $files))
				{
					$id = 0;
				}
				if (!in_array($id2x, $files))
				{
					$id2x = 0;
				}
			}

			if (isset($resultList[$pos]))
			{
				// update in content
				if ($resultList[$pos]->getTagName() !== 'IMG')
				{
					$styles = DOMStyleInliner::getStyle($resultList[$pos]);
					if (isset($styles['--bg-url']))
					{
						$styles['background-image'] = '';
					}
					if (!isset($styles['background']))
					{
						if ($id)
						{
							$stylesChanged = false;
							$resultList[$pos]->setAttribute('data-fileid', $id);
							foreach (self::STYLES_VARIABLES_WITH_FILES['1x'] as $var => $pattern)
							{
								if (isset($styles[$var]))
								{
									$fileArray = CFile::GetFileArray($id);
									$styles[$var] = str_replace(
										self::STYLES_URL_MARKER,
										$fileArray['SRC'],
										$pattern
									);
									$stylesChanged = true;
								}
							}

							if ($id2x)
							{
								$resultList[$pos]->setAttribute('data-fileid2x', $id2x);
								foreach (self::STYLES_VARIABLES_WITH_FILES['2x'] as $var => $pattern)
								{
									if (isset($styles[$var]))
									{
										$fileArray = CFile::GetFileArray($id2x);
										$styles[$var] = str_replace(
											self::STYLES_URL_MARKER,
											$fileArray['SRC'],
											$pattern
										);
										$stylesChanged = true;
									}
								}
							}

							if (!$stylesChanged)
							{
								$classList = $resultList[$pos]->getAttribute('class');
								if (!stripos($classList, self::STYLES_NODE_CLASS))
								{
									$classList .= ' ' . self::STYLES_NODE_CLASS;
								}
								$resultList[$pos]->setAttribute('class', $classList);
								$fileArray1x = CFile::GetFileArray($id);
								$src1x = $fileArray1x['SRC'];
								$styles['--bg-url'] = "url('{$src1x}');";
								if ($id2x <= 0 && $id > 0)
								{
									$id2x = $id;
								}
								$fileArray2x = CFile::GetFileArray($id2x);
								$src2x = $fileArray2x['SRC'];
								$styles['--bg-url-2x'] = "url('{$src2x}');";
								$stylesChanged = true;
							}

							if ($stylesChanged)
							{
								DOMStyleInliner::setStyle($resultList[$pos], $styles);
							}
						}
						else
						{
							foreach (['fileid', 'fileid2x'] as $dataCode)
							{
								$attribute = 'data-' . $dataCode;
								$oldId = $resultList[$pos]->getAttribute($attribute);
								if ($oldId > 0)
								{
									File::deleteFromBlock(
										$block->getId(),
										$oldId
									);
								}
							}
						}
					}
				}
			}
		}
	}
}