• Модуль: crmmobile
  • Путь к файлу: ~/bitrix/modules/crmmobile/lib/ProductGrid/UpdateCatalogProductsCommand.php
  • Класс: BitrixCrmMobileProductGridUpdateCatalogProductsCommand
  • Вызов: UpdateCatalogProductsCommand::updateImages
private function updateImages(BaseSku $sku, array $images): void
{
	$receivedFileIds = [];
	$newImageIds = [];
	$tokens = [];

	foreach ($images as $file)
	{
		if (!empty($file['id']) && is_numeric($file['id']))
		{
			$receivedFileIds[] = (int)$file['id'];
		}
		elseif (!empty($file['token']))
		{
			$tokens[] = $file['token'];
		}
	}

	if (!empty($tokens))
	{
		$pendingFiles = $this->getProductPendingFiles($tokens, $sku->getId());
		$newImageIds = $pendingFiles->getFileIds();
	}

	/**
	 * Remove
	 */
	$imageCollection = $sku->getImageCollection();
	foreach ($imageCollection as $image)
	{
		if (in_array((int)$image->getFields()['ID'], $receivedFileIds, true))
		{
			continue;
		}

		$image->remove();
	}

	/**
	 * Add new
	 */
	$morePhotoProperty = $sku->getPropertyCollection()->findByCode(MorePhotoImage::CODE);
	$hasMorePhoto = $morePhotoProperty && $morePhotoProperty->isActive();

	foreach ($newImageIds as $newImage)
	{
		$fileArray = CFile::MakeFileArray($newImage);

		if ($hasMorePhoto)
		{
			$imageCollection->addValues([$fileArray]);
		}
		else
		{
			$previewImage = $imageCollection->getPreviewImage();
			$previewImageValue = $previewImage->getFileStructure();
			if (empty($previewImageValue))
			{
				$previewImage->setFileStructure($fileArray);
				continue;
			}

			$detailImage = $imageCollection->getDetailImage();
			$detailImageValue = $detailImage->getFileStructure();
			if (empty($detailImageValue))
			{
				$detailImage->setFileStructure($fileArray);
				continue;
			}

			break;
		}
	}
}